KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > filesystems > FileSystemFactoryHid


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.openide.filesystems;
21
22 import junit.framework.*;
23 import org.netbeans.junit.*;
24
25 import java.io.*;
26 import java.util.*;
27
28 import org.openide.filesystems.*;
29
30
31 /**
32  *
33  * @author vs124454, rm111737
34  * @version
35  */

36 public abstract class FileSystemFactoryHid extends NbTestSetup {
37     private static Map<Test, List<FileSystemFactoryHid>> map =
38             new HashMap<Test, List<FileSystemFactoryHid>> ();
39     private static String JavaDoc className;
40
41
42     /** Creates new FileSystemFactory
43      * @param test */

44     public FileSystemFactoryHid(Test test) {
45         super(test);
46         /**Adds */
47         registerMap (test);
48     }
49
50     /**
51      * Intended to allow prepare tested environment for each individual test.
52      * Although this method is static, all subclasses must modify its behaviour by means of
53      * createTestedFS.
54      * @param testName name of test
55      * @return array of FileSystems that should be tested in test named: "testName"*/

56     final static FileSystem[] createFileSystem (String JavaDoc testName,String JavaDoc[] resources, Test test) throws IOException {
57          return getInstance (test,true).createFileSystem(testName, resources);
58     }
59       
60     final static void destroyFileSystem (String JavaDoc testName, Test test) throws IOException {
61         getInstance (test,false).destroyFileSystem(testName);
62     }
63     
64     final static String JavaDoc getResourcePrefix (String JavaDoc testName, Test test, String JavaDoc[] resources) {
65         return getInstance (test,false).getResourcePrefix(testName, resources);
66     }
67
68     
69     
70     private final static FileSystemFactoryHid getInstance (Test test, boolean delete) {
71             FileSystemFactoryHid factory = getFromMap (test,delete);
72             if (factory != null)
73                 className = factory.getClass().getName();
74         return factory;
75     }
76     
77     final static String JavaDoc getTestClassName () {
78         return (className != null) ? className : "Unknown TestSetup";
79     }
80     
81     /**
82      * @param resources that are required to run given test
83      * @return array of FileSystems that should be tested in test named: "testName"*/

84     protected abstract FileSystem[] createFileSystem(String JavaDoc testName, String JavaDoc[] resources) throws IOException;
85     
86     protected abstract void destroyFileSystem(String JavaDoc testName) throws IOException;
87
88     protected String JavaDoc getResourcePrefix (String JavaDoc testName, String JavaDoc[] resources) {
89         return "";
90     }
91     
92
93     private void registerMap (Test test) {
94         if (test instanceof TestSuite) {
95             Enumeration en = ((TestSuite)test).tests ();
96             while (en.hasMoreElements()) {
97                 Test tst = (Test)en.nextElement();
98                 if (tst instanceof TestSuite)
99                     registerMap (tst);
100                 else {
101                     addToMap (tst);
102                 }
103             }
104         } else {
105             addToMap (test);
106         }
107     }
108     
109     private void addToMap (Test test) {
110         List<FileSystemFactoryHid> s = map.get (test);
111         if (s == null) {
112             s = new LinkedList<FileSystemFactoryHid>();
113         }
114         s.add(this);
115         map.put(test ,s );
116         
117     }
118         
119
120     private static FileSystemFactoryHid getFromMap (Test test, boolean delete) {
121         LinkedList s = (LinkedList)map.get (test);
122         FileSystemFactoryHid retVal;
123         try {
124             retVal = (FileSystemFactoryHid)s.getLast();
125         } catch (NoSuchElementException x ) {
126             System.out.println("exc: "+ test + " : " );
127             throw x;
128         }
129         if (delete) {
130             s.remove(retVal);
131         }
132         return retVal;
133     }
134 }
135
Popular Tags