KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > AbstractVfsTestCase


1 /*
2  * Copyright 2002-2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.commons;
17
18 import junit.framework.TestCase;
19 import org.apache.commons.vfs.FileSystemException;
20 import org.apache.commons.vfs.util.Messages;
21
22 import java.io.File JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.lang.reflect.Method JavaDoc;
25
26 /**
27  * A base class for VFS tests. Provides utility methods for locating
28  * test resources.
29  *
30  * @author <a HREF="mailto:adammurdoch@apache.org">Adam Murdoch</a>
31  */

32 public abstract class AbstractVfsTestCase
33     extends TestCase
34 {
35     private static File JavaDoc baseDir;
36
37     /**
38      * Returns the name of the package containing a class.
39      *
40      * @return The . delimited package name, or an empty string if the class
41      * is in the default package.
42      */

43     public static String JavaDoc getPackageName(final Class JavaDoc clazz)
44     {
45         final Package JavaDoc pkg = clazz.getPackage();
46         if (null != pkg)
47         {
48             return pkg.getName();
49         }
50
51         final String JavaDoc name = clazz.getName();
52         if (-1 == name.lastIndexOf("."))
53         {
54             return "";
55         }
56         else
57         {
58             return name.substring(0, name.lastIndexOf("."));
59         }
60     }
61
62     /**
63      * Locates a test resource, and asserts that the resource exists
64      *
65      * @param name path of the resource, relative to this test's base directory.
66      */

67     public static File JavaDoc getTestResource(final String JavaDoc name)
68     {
69         return getTestResource(name, true);
70     }
71
72     /**
73      * Locates a test resource.
74      *
75      * @param name path of the resource, relative to this test's base directory.
76      */

77     public static File JavaDoc getTestResource(final String JavaDoc name, final boolean mustExist)
78     {
79         File JavaDoc file = new File JavaDoc(getTestDirectoryFile(), name);
80         file = getCanonicalFile(file);
81         if (mustExist)
82         {
83             assertTrue("Test file \"" + file + "\" does not exist.", file.exists());
84         }
85         else
86         {
87             assertTrue("Test file \"" + file + "\" should not exist.", !file.exists());
88         }
89
90         return file;
91     }
92
93     /**
94      * Locates the base directory for this test.
95      */

96     public static File JavaDoc getTestDirectoryFile()
97     {
98         if (baseDir == null)
99         {
100             // final String baseDirProp = System.getProperty("test.basedir");
101
final String JavaDoc baseDirProp = getTestDirectory();
102             baseDir = getCanonicalFile(new File JavaDoc(baseDirProp));
103         }
104         return baseDir;
105     }
106
107     public static String JavaDoc getTestDirectory()
108     {
109         return System.getProperty("test.basedir");
110         /*
111         if (baseDir == null)
112         {
113             final String baseDirProp = System.getProperty("test.basedir");
114             baseDir = getCanonicalFile(new File(baseDirProp));
115         }
116         return baseDir;
117         */

118     }
119
120     /**
121      * Locates a test directory, creating it if it does not exist.
122      *
123      * @param name path of the directory, relative to this test's base directory.
124      */

125     public static File JavaDoc getTestDirectory(final String JavaDoc name)
126     {
127         File JavaDoc file = new File JavaDoc(getTestDirectoryFile(), name);
128         file = getCanonicalFile(file);
129         assertTrue("Test directory \"" + file + "\" does not exist or is not a directory.",
130             file.isDirectory() || file.mkdirs());
131         return file;
132     }
133
134     /**
135      * Makes a file canonical
136      */

137     public static File JavaDoc getCanonicalFile(final File JavaDoc file)
138     {
139         try
140         {
141             return file.getCanonicalFile();
142         }
143         catch (IOException JavaDoc e)
144         {
145             return file.getAbsoluteFile();
146         }
147     }
148
149     /**
150      * Asserts that an exception chain contains the expected messages.
151      *
152      * @param messages The messages, in order. A null entry in this array
153      * indicates that the message should be ignored.
154      */

155     public static void assertSameMessage(final String JavaDoc[] messages, final Throwable JavaDoc throwable)
156     {
157         Throwable JavaDoc current = throwable;
158         for (int i = 0; i < messages.length; i++)
159         {
160             String JavaDoc message = messages[i];
161             assertNotNull(current);
162             if (message != null)
163             {
164                 assertEquals(message, current.getMessage());
165             }
166
167             // Get the next exception in the chain
168
current = getCause(current);
169         }
170     }
171
172     /**
173      * Returns the cause of an exception.
174      */

175     public static Throwable JavaDoc getCause(Throwable JavaDoc throwable)
176     {
177         try
178         {
179             Method JavaDoc method = throwable.getClass().getMethod("getCause", null);
180             return (Throwable JavaDoc) method.invoke(throwable, null);
181         }
182         catch (Exception JavaDoc e)
183         {
184             return null;
185         }
186     }
187
188     /**
189      * Asserts that an exception contains the expected message.
190      */

191     public static void assertSameMessage(final String JavaDoc code,
192                                          final Throwable JavaDoc throwable)
193     {
194         assertSameMessage(code, new Object JavaDoc[0], throwable);
195     }
196
197     /**
198      * Asserts that an exception contains the expected message.
199      */

200     public static void assertSameMessage(final String JavaDoc code,
201                                          final Object JavaDoc[] params,
202                                          final Throwable JavaDoc throwable)
203     {
204         if (throwable instanceof FileSystemException)
205         {
206             final FileSystemException fse = (FileSystemException) throwable;
207
208             // Compare message code and params
209
assertEquals(code, fse.getCode());
210             assertEquals(params.length, fse.getInfo().length);
211             for (int i = 0; i < params.length; i++)
212             {
213                 final Object JavaDoc param = params[i];
214                 assertEquals(String.valueOf(param), fse.getInfo()[i]);
215             }
216         }
217
218         // Compare formatted message
219
final String JavaDoc message = Messages.getString(code, params);
220         assertEquals(message, throwable.getMessage());
221     }
222
223     /**
224      * Asserts that an exception contains the expected message.
225      */

226     public static void assertSameMessage(final String JavaDoc code,
227                                          final Object JavaDoc param,
228                                          final Throwable JavaDoc throwable)
229     {
230         assertSameMessage(code, new Object JavaDoc[]{param}, throwable);
231     }
232
233     /**
234      * Compares 2 objects for equality, nulls are equal. Used by the test
235      * classes' equals() methods.
236      */

237     public static boolean equals(final Object JavaDoc o1, final Object JavaDoc o2)
238     {
239         if (o1 == null && o2 == null)
240         {
241             return true;
242         }
243         if (o1 == null || o2 == null)
244         {
245             return false;
246         }
247         return o1.equals(o2);
248     }
249 }
250
Popular Tags