1 package org.testng;
2 
3 import java.io.File;
4 import java.io.IOException;
5 
6 /**
7  * Assertion tool for File centric assertions.
8  * Conceptually this is an extension of {@link Assert}
9  * Presents assertion methods with a more natural parameter order.
10  * The order is always <B>actualValue</B>, <B>expectedValue</B> [, message].
11  *
12  * @author <a href='mailto:pmendelson@trueoutcomes.com'>Paul Mendelon</a>
13  * @since 5.6
14  * @version $Revision: 650 $, $Date: 2009-01-05 03:51:54 -0800 (Mon, 05 Jan 2009) $
15  */
16 public class FileAssert {
17 
18   /**
19    * Protect constructor since it is a static only class
20    */
FileAssert()21   private FileAssert() {
22   	// hide constructor
23   }
24 
25   /**
26    * Asserts that a {@code tstvalue} is a proper directory. If it isn't,
27    * an AssertionError, with the given message, is thrown.
28    * @param tstvalue the file to evaluate
29    * @param message the assertion error message
30    */
assertDirectory(File tstvalue, String message)31   static public void assertDirectory(File tstvalue, String message) {
32   	boolean condition=false;
33   	try {
34   	condition=tstvalue.isDirectory();
35   	} catch(SecurityException e) {
36   	failSecurity(e,tstvalue,fileType(tstvalue),"Directory", message);
37   	}
38   	if(!condition) {
39   	failFile(tstvalue,fileType(tstvalue),"Directory", message);
40   	}
41   }
42 
assertDirectory(File tstvalue)43   static public void assertDirectory(File tstvalue) {
44   	assertDirectory(tstvalue, null);
45   }
46 
47   /**
48    * Asserts that a {@code tstvalue} is a proper file. If it isn't,
49    * an AssertionError, with the given message, is thrown.
50    * @param tstvalue the file to evaluate
51    * @param message the assertion error message
52    */
assertFile(File tstvalue, String message)53   static public void assertFile(File tstvalue, String message) {
54   	boolean condition=false;
55   	try {
56   	condition=tstvalue.isFile();
57   	} catch(SecurityException e) {
58   	failSecurity(e,tstvalue,fileType(tstvalue),"File", message);
59   	}
60   	if(!condition) {
61   	failFile(tstvalue,fileType(tstvalue),"File", message);
62   	}
63   }
64 
65   /**
66    * @see #assertFile(File, String)
67    */
assertFile(File tstvalue)68   static public void assertFile(File tstvalue) {
69   	assertFile(tstvalue, null);
70   }
71 
72   /**
73    * Asserts that a {@code tstvalue} is a file of exactly {@code expected} characters
74    * or a directory of exactly {@code expected} entries. If it isn't,
75    * an AssertionError, with the given message, is thrown.
76    * @param tstvalue the file to evaluate
77    * @param message the assertion error message
78    */
assertLength(File tstvalue, long expected, String message)79   static public void assertLength(File tstvalue, long expected, String message) {
80   	long actual=-1L;
81   	try {
82   	actual=tstvalue.isDirectory()?tstvalue.list().length:tstvalue.length();
83   	} catch(SecurityException e) {
84   	failSecurity(e,tstvalue,String.valueOf(actual),String.valueOf(expected), message);
85   	}
86   	if(actual!=expected) {
87   	failFile(tstvalue,String.valueOf(actual),String.valueOf(expected), message);
88   	}
89   }
90 
91   /**
92    * @see #assertLength(File, long, String)
93    */
assertLength(File tstvalue, long expected)94   static public void assertLength(File tstvalue, long expected) {
95   	assertLength(tstvalue, expected, null);
96   }
97 
98   /**
99    * Asserts that a {@code tstvalue} is a file of at least {@code expected} characters
100    * or a directory of at least {@code expected} entries. If it isn't,
101    * an AssertionError, with the given message, is thrown.
102    * @param tstvalue the file to evaluate
103    * @param message the assertion error message
104    */
assertMinLength(File tstvalue, long expected, String message)105   static public void assertMinLength(File tstvalue, long expected, String message) {
106   	long actual=-1L;
107   	try {
108   	actual=tstvalue.isDirectory()?tstvalue.list().length:tstvalue.length();
109   	} catch(SecurityException e) {
110   	failSecurity(e,tstvalue,String.valueOf(actual),"at least "+String.valueOf(expected), message);
111   	}
112   	if(actual<expected) {
113   	failFile(tstvalue,String.valueOf(actual),"at least "+String.valueOf(expected), message);
114   	}
115   }
116 
117   /**
118    * @see #assertMinLength(File, long, String)
119    */
assertMinLength(File tstvalue, long expected)120   static public void assertMinLength(File tstvalue, long expected) {
121   	assertMinLength(tstvalue, expected, null);
122   }
123 
124   /**
125    * Asserts that a {@code tstvalue} is a file of at most {@code expected} characters
126    * or a directory of at most {@code expected} entries. If it isn't,
127    * an AssertionError, with the given message, is thrown.
128    * @param tstvalue the file to evaluate
129    * @param message the assertion error message
130    */
assertMaxLength(File tstvalue, long expected, String message)131   static public void assertMaxLength(File tstvalue, long expected, String message) {
132   	long actual=-1L;
133   	try {
134   	actual=tstvalue.isDirectory()?tstvalue.list().length:tstvalue.length();
135   	} catch(SecurityException e) {
136   	failSecurity(e,tstvalue,String.valueOf(actual),"at most "+String.valueOf(expected), message);
137   	}
138   	if(actual>expected) {
139   	failFile(tstvalue,String.valueOf(actual),"at most "+String.valueOf(expected), message);
140   	}
141   }
142 
143   /**
144    * @see #assertMaxLength(File, long, String)
145    */
assertMaxLength(File tstvalue, long expected)146   static public void assertMaxLength(File tstvalue, long expected) {
147   	assertMaxLength(tstvalue, expected, null);
148   }
149 
150   /**
151    * Asserts that a {@code tstvalue} is readable. If it isn't,
152    * an AssertionError, with the given message, is thrown.
153    * @param tstvalue the file to evaluate
154    * @param message the assertion error message
155    */
assertReadable(File tstvalue, String message)156   static public void assertReadable(File tstvalue, String message) {
157   	boolean condition=false;
158   	try {
159   	condition=tstvalue.canRead();
160   	} catch(SecurityException e) {
161   	failSecurity(e,tstvalue,fileAccess(tstvalue),"Read Access", message);
162   	}
163   	if(!condition) {
164   	failFile(tstvalue,fileAccess(tstvalue),"Read Access", message);
165   	}
166   }
167 
168   /**
169    * @see #assertReadable(File, String)
170    */
assertReadable(File tstvalue)171   static public void assertReadable(File tstvalue) {
172   	assertReadable(tstvalue, null);
173   }
174 
175   /**
176    * Asserts that a {@code tstvalue} is writeable. If it isn't,
177    * an AssertionError, with the given message, is thrown.
178    * @param tstvalue the file to evaluate
179    * @param message the assertion error message
180    */
assertWriteable(File tstvalue, String message)181   static public void assertWriteable(File tstvalue, String message) {
182   	boolean condition=false;
183   	try {
184   	condition=tstvalue.canWrite();
185   	} catch(SecurityException e) {
186   	failSecurity(e,tstvalue,fileAccess(tstvalue),"Write Access", message);
187   	}
188   	if(!condition) {
189   	failFile(tstvalue,fileAccess(tstvalue),"Write Access", message);
190   	}
191   }
192 
193   /**
194    * @see #assertWriteable(File, String)
195    */
assertWriteable(File tstvalue)196   static public void assertWriteable(File tstvalue) {
197   	assertReadable(tstvalue, null);
198   }
199 
200   /**
201    * Asserts that a {@code tstvalue} is readable and writeable. If it isn't,
202    * an AssertionError, with the given message, is thrown.
203    * @param tstvalue the file to evaluate
204    * @param message the assertion error message
205    */
assertReadWrite(File tstvalue, String message)206   static public void assertReadWrite(File tstvalue, String message) {
207   	boolean condition=false;
208   	try {
209   	condition=tstvalue.canRead() && tstvalue.canWrite();
210   	} catch(SecurityException e) {
211   	failSecurity(e,tstvalue,fileAccess(tstvalue),"Read/Write Access", message);
212   	}
213   	if(!condition) {
214   	failFile(tstvalue,fileAccess(tstvalue),"Read/Write Access", message);
215   	}
216   }
217 
218   /**
219    * @see #assertReadWrite(File, String)
220    */
assertReadWrite(File tstvalue)221   static public void assertReadWrite(File tstvalue) {
222   	assertReadWrite(tstvalue, null);
223   }
224 
225   /**
226    * Fails a test with the given message and wrapping the original exception.
227    *
228    * @param message the assertion error message
229    * @param realCause the original exception
230    */
fail(String message, Throwable realCause)231   static public void fail(String message, Throwable realCause) {
232   	AssertionError ae = new AssertionError(message);
233   	ae.initCause(realCause);
234 
235   	throw ae;
236   }
237 
238   /**
239    * Fails a test with the given message.
240    * @param message the assertion error message
241    */
fail(String message)242   static public void fail(String message) {
243   	throw new AssertionError(message);
244   }
245 
246   /**
247    * Fails a test with no message.
248    */
fail()249   static public void fail() {
250   	fail(null);
251   }
252 
253   /**
254    * Formats failure for file assertions
255    */
failFile(File path, String actual, String expected, String message)256   private static void failFile(File path, String actual, String expected, String message) {
257   	String formatted = "";
258   	if(message != null) {
259   	formatted = message + " ";
260   	}
261   	fail(formatted + "expected <" + expected +"> but was <" + toString(path) + ">"
262   		+(expected!=null?"<" + expected +">":""));
263   }
264 
265   /**
266    * @param tstvalue
267    * @param string
268    * @param string2
269    * @param message
270    */
failSecurity(Exception e, File path, String actual, String expected, String message)271   private static void failSecurity(Exception e, File path, String actual, String expected, String message) {
272   	String formatted = "";
273   	if(message != null) {
274   	formatted = message + " ";
275   	}
276   	fail(formatted + "expected <" + expected +"> but was <" + toString(path) + ">"
277   		+"<"
278   		+ (e!=null && e.getMessage()!=null && e.getMessage().length()>0
279   			?e.getMessage()
280   			:"not authorized by JVM")
281   		+ ">");
282   }
283 
284   /**
285    * String representation of what sort of file {@code path} is.
286    */
fileType(File path)287   private static String fileType(File path) {
288   	try {
289   	if(!path.exists()) {
290       return "Non existant";
291     } else if (path.isDirectory()) {
292       return "Directory";
293     } else if (path.isFile()) {
294       return "File";
295     } else {
296       return "Special File";
297     }
298   	} catch (SecurityException e) {
299   	return "Unauthorized";
300   	}
301   }
302 
303   /**
304    * String representation of what sort of file {@code path} is.
305    */
fileAccess(File path)306   private static String fileAccess(File path) {
307   	try {
308   	if(!path.exists()) {
309       return "Non existant";
310     } else if (path.canWrite() && path.canRead()) {
311       return "Read/Write Access";
312     } else if (path.canRead()) {
313       return "Read only Access";
314     } else if (path.canWrite()) {
315       return "Write only Access";
316     } else {
317       return "No Access";
318     }
319   	} catch (SecurityException e) {
320   	return "Unauthorized";
321   	}
322   }
323 
toString(File path)324   private static String toString(File path) {
325   	try {
326   	return path.getCanonicalPath();
327   	} catch(IOException e) {
328   	return path.getAbsolutePath();
329   	}
330   }
331 }
332