KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > source > parsing > FileManagerTest


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.netbeans.modules.java.source.parsing;
21
22 import java.io.IOException JavaDoc;
23 import java.util.Arrays JavaDoc;
24 import java.util.jar.JarFile JavaDoc;
25 import javax.tools.JavaCompiler;
26 import javax.tools.JavaFileManager;
27 import static javax.tools.JavaFileObject.Kind.*;
28 import javax.tools.StandardJavaFileManager;
29 import javax.tools.ToolProvider;
30 import junit.extensions.TestSetup;
31 import junit.framework.*;
32 import java.io.File JavaDoc;
33 import java.util.zip.ZipFile JavaDoc;
34 import javax.tools.StandardLocation;
35 import org.netbeans.modules.java.source.TestUtil;
36
37 /** Base class for testing file managers. This class basically tests itself.
38  *
39  * @author Petr Hrebejk
40  */

41 public class FileManagerTest extends TestCase {
42     
43     protected static Setup setup;
44     private CachingArchiveProvider archiveProvider;
45         
46     public FileManagerTest(String JavaDoc testName) {
47         super(testName);
48     }
49
50     protected void setUp() throws Exception JavaDoc {
51         archiveProvider = new CachingArchiveProvider();
52     }
53
54     protected void tearDown() throws Exception JavaDoc {
55     }
56
57     public static Test suite() {
58         setup = new Setup( new TestSuite( FileManagerTest.class ) );
59         return setup;
60     }
61     
62     protected JavaFileManagerDescripton[] getDescriptions() throws IOException JavaDoc {
63     
64     JavaFileManager tfm = createGoldenJFM( new File JavaDoc[] { setup.rtFolder },
65                            new File JavaDoc[] { setup.srcFolder} );
66     JavaFileManager gfm = createGoldenJFM( new File JavaDoc[] { setup.rtFile },
67                            new File JavaDoc[] { setup.srcFile } );
68     return new JavaFileManagerDescripton[] {
69         new JavaFileManagerDescripton( tfm, gfm, setup.srcZipArchive ),
70     };
71     }
72     
73     public static class JavaFileManagerDescripton {
74     
75         
76     public Archive archive;
77     public JavaFileManager testJFM;
78     public JavaFileManager goldenJFM;
79     
80     public JavaFileManagerDescripton( JavaFileManager testJFM,
81                       JavaFileManager goldenJFM,
82                       Archive archive ) {
83         this.testJFM = testJFM;
84         this.goldenJFM = goldenJFM;
85         this.archive = archive;
86     }
87     
88     }
89     
90     
91     // Test methods ------------------------------------------------------------
92
//TODO: Fix me
93
// public void testList() throws Exception {
94
//
95
// JavaFileManagerDescripton[] jfmds = getDescriptions();
96
// for ( JavaFileManagerDescripton jfmd : jfmds ) {
97
// try {
98
// JavaFileManager tfm = jfmd.testJFM;
99
// JavaFileManager gfm = jfmd.goldenJFM;
100
// Archive archive = jfmd.archive;
101
//
102
// // Test all packages in the archive
103
// for( String folder : Iterators.toIterable( archive.getFolders() ) ) {
104
// String pkg = FileObjects.convertFolder2Package( folder);
105
//
106
// for( JavaFileObject jfo : tfm.list(StandardLocation.CLASS_PATH, pkg, EnumSet.of( CLASS ), false ) ) {
107
// // Test that all of the JFOs are classes
108
// assertTrue( "Must be a class " + jfo.toUri(), jfo.getKind() == CLASS );
109
// }
110
//
111
// for( JavaFileObject jfo : tfm.list(StandardLocation.SOURCE_PATH, pkg, EnumSet.of( SOURCE ), false ) ) {
112
// // Test that all of the JFOs are sources
113
// assertTrue( "Must be a source " + jfo.toUri(), jfo.getKind() == SOURCE );
114
// }
115
//
116
// }
117
// }finally {
118
// jfmd.goldenJFM.close();
119
// jfmd.testJFM.close();
120
// }
121
// }
122
// }
123
//
124
// public void testGetFileForInput() {
125
// JavaFileManagerDescripton[] jfmds = getDescriptions();
126
//
127
// for ( JavaFileManagerDescripton jfmd : jfmds ) {
128
// }
129
// }
130
//
131
// public void testGetFileForOutput() throws Exception {
132
// JavaFileManagerDescripton[] jfmds = getDescriptions();
133
//
134
// for ( JavaFileManagerDescripton jfmd : jfmds ) {
135
// }
136
// }
137
//
138
// public void testGetInputFile() {
139
// JavaFileManagerDescripton[] jfmds = getDescriptions();
140
//
141
// for ( JavaFileManagerDescripton jfmd : jfmds ) {
142
// }
143
// }
144
//
145
//
146
// NOT SURE WHAT TO TEST HERE
147
//
148
// public void testSetLocation() {
149
// fail("The test case is empty.");
150
// }
151
//
152
// public void testFlush() throws Exception {
153
// fail("The test case is empty.");
154
// }
155
//
156
// public void testClose() throws Exception {
157
// fail("The test case is empty.");
158
// }
159

160     // Other usefull methods ---------------------------------------------------
161

162     /** Crates the default javac file managare tro have something to comare
163      * our file managers against
164      */

165     public static JavaFileManager createGoldenJFM( File JavaDoc[] classpath, File JavaDoc[] sourcpath ) throws IOException JavaDoc {
166     
167     JavaCompiler jc = ToolProvider.getSystemJavaCompiler();
168         StandardJavaFileManager fm = jc.getStandardFileManager (null, null, null);
169     
170     if ( classpath != null ) {
171             fm.setLocation(StandardLocation.CLASS_PATH,Arrays.asList(classpath));
172     }
173     
174     if ( sourcpath != null ) {
175         fm.setLocation(StandardLocation.SOURCE_PATH,Arrays.asList(sourcpath));
176     }
177     
178     return fm;
179         
180     }
181     
182             
183     // Innerclasses ------------------------------------------------------------
184

185      private static class Setup extends TestSetup {
186         
187         public File JavaDoc workDir;
188     public File JavaDoc rtFile, srcFile;
189         public File JavaDoc rtFolder, srcFolder;
190         public CachingArchiveProvider archiveProvider;
191         
192     public Archive rtJarArchive;
193     public Archive rtFolderArchive;
194     public Archive srcZipArchive;
195     public Archive srcFolderArchive;
196     
197         public Setup( Test test ) {
198             super( test );
199         }
200         
201         protected void tearDown() throws Exception JavaDoc {
202         TestUtil.removeWorkFolder( workDir );
203             super.tearDown();
204         }
205         
206         protected void setUp() throws Exception JavaDoc {
207             super.setUp();
208         
209             workDir = TestUtil.createWorkFolder();
210         System.out.println("Workdir " + workDir );
211             TestUtil.copyFiles( TestUtil.getJdkDir(), workDir, TestUtil.RT_JAR );
212             TestUtil.copyFiles( TestUtil.getJdkDir(), workDir, TestUtil.SRC_ZIP );
213         
214         rtFile = new File JavaDoc( workDir, TestUtil.RT_JAR );
215             JarFile JavaDoc rtJar = new JarFile JavaDoc( rtFile );
216             srcFile = new File JavaDoc( workDir, TestUtil.SRC_ZIP );
217             ZipFile JavaDoc srcZip = new ZipFile JavaDoc( srcFile );
218                             
219             rtFolder = new File JavaDoc( workDir, "rtFolder" );
220             TestUtil.unzip( rtJar, rtFolder );
221             
222             srcFolder = new File JavaDoc( workDir, "src" );
223             TestUtil.unzip( srcZip, srcFolder );
224             
225         // Create archive provider
226
archiveProvider = CachingArchiveProvider.getDefault();
227         
228         rtJarArchive = archiveProvider.getArchive( rtFile.toURI().toURL(), true );
229         rtFolderArchive = archiveProvider.getArchive( rtFolder.toURI().toURL(), true );
230         srcZipArchive = archiveProvider.getArchive( srcFile.toURI().toURL(), true );
231         srcFolderArchive = archiveProvider.getArchive( srcFolder.toURI().toURL(), true );
232                         
233         }
234      }
235            
236 }
237
Popular Tags