001 /*
002 * Created on Jan 26, 2011
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
005 * the License. You may obtain a copy of the License at
006 *
007 * http://www.apache.org/licenses/LICENSE-2.0
008 *
009 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
010 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
011 * specific language governing permissions and limitations under the License.
012 *
013 * Copyright @2011 the original author or authors.
014 */
015 package org.fest.assertions.internal;
016
017 import static org.fest.assertions.error.ShouldBeAbsolutePath.shouldBeAbsolutePath;
018 import static org.fest.assertions.error.ShouldBeDirectory.shouldBeDirectory;
019 import static org.fest.assertions.error.ShouldBeFile.shouldBeFile;
020 import static org.fest.assertions.error.ShouldBeRelativePath.shouldBeRelativePath;
021 import static org.fest.assertions.error.ShouldExist.shouldExist;
022 import static org.fest.assertions.error.ShouldHaveEqualContent.shouldHaveEqualContent;
023 import static org.fest.assertions.error.ShouldNotExist.shouldNotExist;
024
025 import java.io.*;
026 import java.util.List;
027
028 import org.fest.assertions.core.AssertionInfo;
029 import org.fest.util.*;
030
031 /**
032 * Reusable assertions for <code>{@link File}</code>s.
033 *
034 * @author David DIDIER
035 * @author Yvonne Wang
036 * @author Alex Ruiz
037 */
038 public class Files {
039
040 private static final Files INSTANCE = new Files();
041
042 /**
043 * Returns the singleton instance of this class.
044 * @return the singleton instance of this class.
045 */
046 public static Files instance() {
047 return INSTANCE;
048 }
049
050 @VisibleForTesting Diff diff = new Diff();
051 @VisibleForTesting Failures failures = Failures.instance();
052
053 @VisibleForTesting Files() {}
054
055 /**
056 * Asserts that the given files have equal content. Adapted from
057 * <a href="http://junit-addons.sourceforge.net/junitx/framework/FileAssert.html" target="_blank">FileAssert</a> (from
058 * <a href="http://sourceforge.net/projects/junit-addons">JUnit-addons</a>.)
059 * @param info contains information about the assertion.
060 * @param actual the "actual" file.
061 * @param expected the "expected" file.
062 * @throws NullPointerException if {@code expected} is {@code null}.
063 * @throws IllegalArgumentException if {@code expected} is not an existing file.
064 * @throws AssertionError if {@code actual} is {@code null}.
065 * @throws AssertionError if {@code actual} is not an existing file.
066 * @throws FilesException if an I/O error occurs.
067 * @throws AssertionError if the given files do not have equal content.
068 */
069 public void assertEqualContent(AssertionInfo info, File actual, File expected) {
070 verifyIsFile(expected);
071 assertIsFile(info, actual);
072 try {
073 List<String> diffs = diff.diff(actual, expected);
074 if (diffs.isEmpty()) return;
075 throw failures.failure(info, shouldHaveEqualContent(actual, expected, diffs));
076 } catch (IOException e) {
077 String msg = String.format("Unable to compare contents of files:<%s> and:<%s>", actual, expected);
078 throw new FilesException(msg, e);
079 }
080 }
081
082 private void verifyIsFile(File expected) {
083 if (expected == null) throw new NullPointerException("The file to compare to should not be null");
084 if (expected.isFile()) return;
085 throw new IllegalArgumentException(String.format("Expected file:<'%s'> should be an existing file", expected));
086 }
087
088 /**
089 * Asserts that the given file is an existing file.
090 * @param info contains information about the assertion.
091 * @param actual the given file.
092 * @throws AssertionError if the given file is {@code null}.
093 * @throws AssertionError if the given file is not an existing file.
094 */
095 public void assertIsFile(AssertionInfo info, File actual) {
096 assertNotNull(info, actual);
097 if (actual.isFile()) return;
098 throw failures.failure(info, shouldBeFile(actual));
099 }
100
101 /**
102 * Asserts that the given file is an existing directory.
103 * @param info contains information about the assertion.
104 * @param actual the given file.
105 * @throws AssertionError if the given file is {@code null}.
106 * @throws AssertionError if the given file is not an existing directory.
107 */
108 public void assertIsDirectory(AssertionInfo info, File actual) {
109 assertNotNull(info, actual);
110 if (actual.isDirectory()) return;
111 throw failures.failure(info, shouldBeDirectory(actual));
112 }
113
114 /**
115 * Asserts that the given file is an absolute path.
116 * @param info contains information about the assertion.
117 * @param actual the given file.
118 * @throws AssertionError if the given file is {@code null}.
119 * @throws AssertionError if the given file is not an absolute path.
120 */
121 public void assertIsAbsolute(AssertionInfo info, File actual) {
122 if (isAbsolutePath(info, actual)) return;
123 throw failures.failure(info, shouldBeAbsolutePath(actual));
124 }
125
126 /**
127 * Asserts that the given file is a relative path.
128 * @param info contains information about the assertion.
129 * @param actual the given file.
130 * @throws AssertionError if the given file is {@code null}.
131 * @throws AssertionError if the given file is not a relative path.
132 */
133 public void assertIsRelative(AssertionInfo info, File actual) {
134 if (!isAbsolutePath(info, actual)) return;
135 throw failures.failure(info, shouldBeRelativePath(actual));
136 }
137
138 private boolean isAbsolutePath(AssertionInfo info, File actual) {
139 assertNotNull(info, actual);
140 return actual.isAbsolute();
141 }
142
143 /**
144 * Asserts that the given file exists, regardless it's a file or directory.
145 * @param info contains information about the assertion.
146 * @param actual the given file.
147 * @throws AssertionError if the given file is {@code null}.
148 * @throws AssertionError if the given file does not exist.
149 */
150 public void assertExists(AssertionInfo info, File actual) {
151 assertNotNull(info, actual);
152 if (actual.exists()) return;
153 throw failures.failure(info, shouldExist(actual));
154 }
155
156 /**
157 * Asserts that the given file does not exist.
158 * @param info contains information about the assertion.
159 * @param actual the given file.
160 * @throws AssertionError if the given file is {@code null}.
161 * @throws AssertionError if the given file exists.
162 */
163 public void assertDoesNotExist(AssertionInfo info, File actual) {
164 assertNotNull(info, actual);
165 if (!actual.exists()) return;
166 throw failures.failure(info, shouldNotExist(actual));
167 }
168
169 private static void assertNotNull(AssertionInfo info, File actual) {
170 Objects.instance().assertNotNull(info, actual);
171 }
172 }