1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons;
18
19 import junit.framework.TestCase;
20 import org.apache.commons.vfs.FileSystemException;
21 import org.apache.commons.vfs.util.Messages;
22
23 import java.io.File;
24 import java.io.IOException;
25 import java.lang.reflect.Method;
26
27 /***
28 * A base class for VFS tests. Provides utility methods for locating
29 * test resources.
30 *
31 * @author <a href="mailto:adammurdoch@apache.org">Adam Murdoch</a>
32 * @version $Revision: 485757 $ $Date: 2006-12-11 18:14:29 +0100 (Mo, 11 Dez 2006) $
33 */
34 public abstract class AbstractVfsTestCase
35 extends TestCase
36 {
37 private static File baseDir;
38
39 /***
40 * Returns the name of the package containing a class.
41 *
42 * @return The . delimited package name, or an empty string if the class
43 * is in the default package.
44 */
45 public static String getPackageName(final Class clazz)
46 {
47 final Package pkg = clazz.getPackage();
48 if (null != pkg)
49 {
50 return pkg.getName();
51 }
52
53 final String name = clazz.getName();
54 if (-1 == name.lastIndexOf("."))
55 {
56 return "";
57 }
58 else
59 {
60 return name.substring(0, name.lastIndexOf("."));
61 }
62 }
63
64 /***
65 * Locates a test resource, and asserts that the resource exists
66 *
67 * @param name path of the resource, relative to this test's base directory.
68 */
69 public static File getTestResource(final String name)
70 {
71 return getTestResource(name, true);
72 }
73
74 /***
75 * Locates a test resource.
76 *
77 * @param name path of the resource, relative to this test's base directory.
78 */
79 public static File getTestResource(final String name, final boolean mustExist)
80 {
81 File file = new File(getTestDirectoryFile(), name);
82 file = getCanonicalFile(file);
83 if (mustExist)
84 {
85 assertTrue("Test file \"" + file + "\" does not exist.", file.exists());
86 }
87 else
88 {
89 assertTrue("Test file \"" + file + "\" should not exist.", !file.exists());
90 }
91
92 return file;
93 }
94
95 /***
96 * Locates the base directory for this test.
97 */
98 public static File getTestDirectoryFile()
99 {
100 if (baseDir == null)
101 {
102
103 final String baseDirProp = getTestDirectory();
104 baseDir = getCanonicalFile(new File(baseDirProp));
105 }
106 return baseDir;
107 }
108
109 public static String getTestDirectory()
110 {
111 return System.getProperty("test.basedir");
112 }
113
114 public static String getResourceTestDirectory()
115 {
116 return System.getProperty("test.basedir.res");
117 }
118
119 /***
120 * Locates a test directory, creating it if it does not exist.
121 *
122 * @param name path of the directory, relative to this test's base directory.
123 */
124 public static File getTestDirectory(final String name)
125 {
126 File file = new File(getTestDirectoryFile(), name);
127 file = getCanonicalFile(file);
128 assertTrue("Test directory \"" + file + "\" does not exist or is not a directory.",
129 file.isDirectory() || file.mkdirs());
130 return file;
131 }
132
133 /***
134 * Makes a file canonical
135 */
136 public static File getCanonicalFile(final File file)
137 {
138 try
139 {
140 return file.getCanonicalFile();
141 }
142 catch (IOException e)
143 {
144 return file.getAbsoluteFile();
145 }
146 }
147
148 /***
149 * Asserts that an exception chain contains the expected messages.
150 *
151 * @param messages The messages, in order. A null entry in this array
152 * indicates that the message should be ignored.
153 */
154 public static void assertSameMessage(final String[] messages, final Throwable throwable)
155 {
156 Throwable current = throwable;
157 for (int i = 0; i < messages.length; i++)
158 {
159 String message = messages[i];
160 assertNotNull(current);
161 if (message != null)
162 {
163 assertEquals(message, current.getMessage());
164 }
165
166
167 current = getCause(current);
168 }
169 }
170
171 /***
172 * Returns the cause of an exception.
173 */
174 public static Throwable getCause(Throwable throwable)
175 {
176 try
177 {
178 Method method = throwable.getClass().getMethod("getCause", null);
179 return (Throwable) method.invoke(throwable, null);
180 }
181 catch (Exception e)
182 {
183 return null;
184 }
185 }
186
187 /***
188 * Asserts that an exception contains the expected message.
189 */
190 public static void assertSameMessage(final String code,
191 final Throwable throwable)
192 {
193 assertSameMessage(code, new Object[0], throwable);
194 }
195
196 /***
197 * Asserts that an exception contains the expected message.
198 */
199 public static void assertSameMessage(final String code,
200 final Object[] params,
201 final Throwable throwable)
202 {
203 if (throwable instanceof FileSystemException)
204 {
205 final FileSystemException fse = (FileSystemException) throwable;
206
207
208 assertEquals(code, fse.getCode());
209 assertEquals(params.length, fse.getInfo().length);
210 for (int i = 0; i < params.length; i++)
211 {
212 final Object param = params[i];
213 assertEquals(String.valueOf(param), fse.getInfo()[i]);
214 }
215 }
216
217
218 final String message = Messages.getString(code, params);
219 assertEquals(message, throwable.getMessage());
220 }
221
222 /***
223 * Asserts that an exception contains the expected message.
224 */
225 public static void assertSameMessage(final String code,
226 final Object param,
227 final Throwable throwable)
228 {
229 assertSameMessage(code, new Object[]{param}, throwable);
230 }
231
232 /***
233 * Compares 2 objects for equality, nulls are equal. Used by the test
234 * classes' equals() methods.
235 */
236 public static boolean equals(final Object o1, final Object o2)
237 {
238 if (o1 == null && o2 == null)
239 {
240 return true;
241 }
242 if (o1 == null || o2 == null)
243 {
244 return false;
245 }
246 return o1.equals(o2);
247 }
248 }