KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > vfs > test > AbstractTestSuite


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.vfs.test;
17
18 import junit.extensions.TestSetup;
19 import junit.framework.Test;
20 import junit.framework.TestSuite;
21 import org.apache.commons.AbstractVfsTestCase;
22 import org.apache.commons.vfs.FileName;
23 import org.apache.commons.vfs.FileObject;
24 import org.apache.commons.vfs.impl.DefaultFileReplicator;
25 import org.apache.commons.vfs.impl.DefaultFileSystemManager;
26 import org.apache.commons.vfs.impl.PrivilegedFileReplicator;
27 import org.apache.commons.vfs.provider.local.DefaultLocalFileProvider;
28
29 import java.io.File JavaDoc;
30 import java.lang.reflect.Field JavaDoc;
31 import java.lang.reflect.Method JavaDoc;
32 import java.lang.reflect.Modifier JavaDoc;
33 import java.util.ArrayList JavaDoc;
34 import java.util.Enumeration JavaDoc;
35 import java.util.List JavaDoc;
36
37 /**
38  * The suite of tests for a file system.
39  *
40  * @author <a HREF="mailto:adammurdoch@apache.org">Adam Murdoch</a>
41  * @author Gary D. Gregory
42  */

43 public class AbstractTestSuite
44     extends TestSetup
45 {
46     private final ProviderTestConfig providerConfig;
47     private final String JavaDoc prefix;
48     private TestSuite testSuite;
49
50     private FileObject baseFolder;
51     private FileObject readFolder;
52     private FileObject writeFolder;
53     private DefaultFileSystemManager manager;
54     private File JavaDoc tempDir;
55
56     private Thread JavaDoc[] startThreadSnapshot;
57     private Thread JavaDoc[] endThreadSnapshot;
58
59     /**
60      * Adds the tests for a file system to this suite.
61      */

62     public AbstractTestSuite(final ProviderTestConfig providerConfig) throws Exception JavaDoc
63     {
64         this(providerConfig, "", false);
65     }
66
67     protected AbstractTestSuite(final ProviderTestConfig providerConfig,
68                                 final String JavaDoc prefix,
69                                 final boolean nested)
70         throws Exception JavaDoc
71     {
72         super(new TestSuite());
73         testSuite = (TestSuite) fTest;
74         this.providerConfig = providerConfig;
75         this.prefix = prefix;
76         addBaseTests();
77         if (!nested)
78         {
79             // Add nested tests
80
// TODO - move nested jar and zip tests here
81
// TODO - enable this again
82
//testSuite.addTest( new ProviderTestSuite( new JunctionProviderConfig( providerConfig ), "junction.", true ));
83
}
84     }
85
86     /**
87      * Adds base tests - excludes the nested test cases.
88      */

89     protected void addBaseTests() throws Exception JavaDoc
90     {
91     }
92
93     /**
94      * Adds the tests from a class to this suite. The supplied class must be
95      * a subclass of {@link AbstractProviderTestCase} and have a public a
96      * no-args constructor. This method creates an instance of the supplied
97      * class for each public 'testNnnn' method provided by the class.
98      */

99     public void addTests(final Class JavaDoc testClass) throws Exception JavaDoc
100     {
101         // Verify the class
102
if (!AbstractProviderTestCase.class.isAssignableFrom(testClass))
103         {
104             throw new Exception JavaDoc("Test class " + testClass.getName() + " is not assignable to " + AbstractProviderTestCase.class.getName());
105         }
106
107         // Locate the test methods
108
final Method JavaDoc[] methods = testClass.getMethods();
109         for (int i = 0; i < methods.length; i++)
110         {
111             final Method JavaDoc method = methods[i];
112             if (!method.getName().startsWith("test")
113                 || Modifier.isStatic(method.getModifiers())
114                 || method.getReturnType() != Void.TYPE
115                 || method.getParameterTypes().length != 0)
116             {
117                 continue;
118             }
119
120             // Create instance
121
final AbstractProviderTestCase testCase = (AbstractProviderTestCase) testClass.newInstance();
122             testCase.setMethod(method);
123             testCase.setName(prefix + method.getName());
124             testSuite.addTest(testCase);
125         }
126     }
127
128     protected void setUp() throws Exception JavaDoc
129     {
130         startThreadSnapshot = createThreadSnapshot();
131
132         // Locate the temp directory, and clean it up
133
tempDir = AbstractVfsTestCase.getTestDirectory("temp");
134         checkTempDir("Temp dir not empty before test");
135
136         // Create the file system manager
137
manager = new DefaultFileSystemManager();
138         manager.setFilesCache(providerConfig.getFilesCache());
139
140         final DefaultFileReplicator replicator = new DefaultFileReplicator(tempDir);
141         manager.setReplicator(new PrivilegedFileReplicator(replicator));
142         manager.setTemporaryFileStore(replicator);
143
144         providerConfig.prepare(manager);
145
146         if (!manager.hasProvider("file"))
147         {
148             manager.addProvider("file", new DefaultLocalFileProvider());
149         }
150
151         manager.init();
152
153         // Locate the base folders
154
baseFolder = providerConfig.getBaseTestFolder(manager);
155         readFolder = baseFolder.resolveFile("read-tests");
156         writeFolder = baseFolder.resolveFile("write-tests");
157
158         // Make some assumptions about the read folder
159
assertTrue("Folder does not exist: " + readFolder, readFolder.exists());
160         assertFalse(readFolder.getName().getPath().equals(FileName.ROOT_PATH));
161
162         // Configure the tests
163
final Enumeration JavaDoc tests = testSuite.tests();
164         while (tests.hasMoreElements())
165         {
166             final Test test = (Test) tests.nextElement();
167             if (test instanceof AbstractProviderTestCase)
168             {
169                 final AbstractProviderTestCase providerTestCase = (AbstractProviderTestCase) test;
170                 providerTestCase.setConfig(manager, baseFolder, readFolder, writeFolder);
171             }
172         }
173     }
174
175     protected void tearDown() throws Exception JavaDoc
176     {
177         readFolder.close();
178         writeFolder.close();
179         baseFolder.close();
180
181         readFolder = null;
182         writeFolder = null;
183         baseFolder = null;
184         testSuite = null;
185         fTest = null;
186
187         // force the SoftRefFilesChache to free all files
188
System.err.println(".");
189         System.gc();
190         Thread.sleep(1000);
191         System.err.println(".");
192         System.gc();
193         Thread.sleep(1000);
194         System.err.println(".");
195         System.gc();
196         Thread.sleep(1000);
197         System.err.println(".");
198         System.gc();
199         Thread.sleep(1000);
200
201         manager.freeUnusedResources();
202         endThreadSnapshot = createThreadSnapshot();
203
204         Thread JavaDoc[] diffThreadSnapshot = diffThreadSnapshot(startThreadSnapshot, endThreadSnapshot);
205         if (diffThreadSnapshot.length > 0)
206         {
207             String JavaDoc message = dumpThreadSnapshot(diffThreadSnapshot);
208             /*
209             if (providerConfig.checkCleanThreadState())
210             {
211                 // close the manager to do a "not thread safe" release of all resources
212                 // and allow the vm to shutdown
213                 manager.close();
214                 fail(message);
215             }
216             else
217             {
218             */

219             System.out.println(message);
220             // }
221
}
222         // System.in.read();
223

224         manager.close();
225
226         // Make sure temp directory is empty or gone
227
checkTempDir("Temp dir not empty after test");
228     }
229
230     /**
231      * Asserts that the temp dir is empty or gone.
232      */

233     private void checkTempDir(final String JavaDoc assertMsg)
234     {
235         if (tempDir.exists())
236         {
237             assertTrue(assertMsg + " (" + tempDir.getAbsolutePath() + ")", tempDir.isDirectory() && tempDir.list().length == 0);
238         }
239     }
240
241     private String JavaDoc dumpThreadSnapshot(Thread JavaDoc[] threadSnapshot)
242     {
243         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(256);
244         sb.append("created threads still running:\n");
245
246         Field JavaDoc threadTargetField = null;
247         try
248         {
249             threadTargetField = Thread JavaDoc.class.getDeclaredField("target");
250             threadTargetField.setAccessible(true);
251         }
252         catch (NoSuchFieldException JavaDoc e)
253         {
254             ;
255         }
256
257         for (int iter = 0; iter < threadSnapshot.length; iter++)
258         {
259             Thread JavaDoc thread = threadSnapshot[iter];
260             if (thread == null)
261             {
262                 continue;
263             }
264
265             sb.append("#");
266             sb.append(iter + 1);
267             sb.append(": ");
268             sb.append(thread.getThreadGroup().getName());
269             sb.append("\t");
270             sb.append(thread.getName());
271             sb.append("\t");
272             if (thread.isDaemon())
273             {
274                 sb.append("daemon");
275             }
276             else
277             {
278                 sb.append("not_a_daemon");
279             }
280
281             if (threadTargetField != null)
282             {
283                 sb.append("\t");
284                 try
285                 {
286                     Object JavaDoc threadTarget = threadTargetField.get(thread);
287                     if (threadTarget != null)
288                     {
289                         sb.append(threadTarget.getClass());
290                     }
291                     else
292                     {
293                         sb.append("null");
294                     }
295                 }
296                 catch (IllegalAccessException JavaDoc e)
297                 {
298                     sb.append("unknown class");
299                 }
300             }
301
302             sb.append("\n");
303         }
304
305         return sb.toString();
306     }
307
308     private Thread JavaDoc[] diffThreadSnapshot(Thread JavaDoc[] startThreadSnapshot, Thread JavaDoc[] endThreadSnapshot)
309     {
310         List JavaDoc diff = new ArrayList JavaDoc(10);
311
312         nextEnd: for (int iterEnd = 0; iterEnd < endThreadSnapshot.length; iterEnd++)
313         {
314             nextStart: for (int iterStart = 0; iterStart < startThreadSnapshot.length; iterStart++)
315             {
316                 if (startThreadSnapshot[iterStart] == endThreadSnapshot[iterEnd])
317                 {
318                     continue nextEnd;
319                 }
320             }
321
322             diff.add(endThreadSnapshot[iterEnd]);
323         }
324
325         Thread JavaDoc ret[] = new Thread JavaDoc[diff.size()];
326         diff.toArray(ret);
327         return ret;
328     }
329
330     private Thread JavaDoc[] createThreadSnapshot()
331     {
332         ThreadGroup JavaDoc tg = Thread.currentThread().getThreadGroup();
333         while (tg.getParent() != null)
334         {
335             tg = tg.getParent();
336         }
337
338         Thread JavaDoc snapshot[] = new Thread JavaDoc[200];
339         tg.enumerate(snapshot, true);
340
341         return snapshot;
342     }
343 }
344
Popular Tags