001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.java.source.parsing;
043:
044: import java.io.IOException;
045: import java.util.Arrays;
046: import java.util.jar.JarFile;
047: import javax.tools.JavaCompiler;
048: import javax.tools.JavaFileManager;
049: import static javax.tools.JavaFileObject.Kind.*;
050: import javax.tools.StandardJavaFileManager;
051: import javax.tools.ToolProvider;
052: import junit.extensions.TestSetup;
053: import junit.framework.*;
054: import java.io.File;
055: import java.util.zip.ZipFile;
056: import javax.tools.StandardLocation;
057: import org.netbeans.modules.java.source.TestUtil;
058:
059: /** Base class for testing file managers. This class basically tests itself.
060: *
061: * @author Petr Hrebejk
062: */
063: public class FileManagerTest extends TestCase {
064:
065: protected static Setup setup;
066: private CachingArchiveProvider archiveProvider;
067:
068: public FileManagerTest(String testName) {
069: super (testName);
070: }
071:
072: protected void setUp() throws Exception {
073: archiveProvider = new CachingArchiveProvider();
074: }
075:
076: protected void tearDown() throws Exception {
077: }
078:
079: public static Test suite() {
080: setup = new Setup(new TestSuite(FileManagerTest.class));
081: return setup;
082: }
083:
084: protected JavaFileManagerDescripton[] getDescriptions()
085: throws IOException {
086:
087: JavaFileManager tfm = createGoldenJFM(
088: new File[] { setup.rtFolder },
089: new File[] { setup.srcFolder });
090: JavaFileManager gfm = createGoldenJFM(
091: new File[] { setup.rtFile },
092: new File[] { setup.srcFile });
093: return new JavaFileManagerDescripton[] { new JavaFileManagerDescripton(
094: tfm, gfm, setup.srcZipArchive), };
095: }
096:
097: public static class JavaFileManagerDescripton {
098:
099: public Archive archive;
100: public JavaFileManager testJFM;
101: public JavaFileManager goldenJFM;
102:
103: public JavaFileManagerDescripton(JavaFileManager testJFM,
104: JavaFileManager goldenJFM, Archive archive) {
105: this .testJFM = testJFM;
106: this .goldenJFM = goldenJFM;
107: this .archive = archive;
108: }
109:
110: }
111:
112: // Test methods ------------------------------------------------------------
113: //TODO: Fix me
114: // public void testList() throws Exception {
115: //
116: // JavaFileManagerDescripton[] jfmds = getDescriptions();
117: // for ( JavaFileManagerDescripton jfmd : jfmds ) {
118: // try {
119: // JavaFileManager tfm = jfmd.testJFM;
120: // JavaFileManager gfm = jfmd.goldenJFM;
121: // Archive archive = jfmd.archive;
122: //
123: // // Test all packages in the archive
124: // for( String folder : Iterators.toIterable( archive.getFolders() ) ) {
125: // String pkg = FileObjects.convertFolder2Package( folder);
126: //
127: // for( JavaFileObject jfo : tfm.list(StandardLocation.CLASS_PATH, pkg, EnumSet.of( CLASS ), false ) ) {
128: // // Test that all of the JFOs are classes
129: // assertTrue( "Must be a class " + jfo.toUri(), jfo.getKind() == CLASS );
130: // }
131: //
132: // for( JavaFileObject jfo : tfm.list(StandardLocation.SOURCE_PATH, pkg, EnumSet.of( SOURCE ), false ) ) {
133: // // Test that all of the JFOs are sources
134: // assertTrue( "Must be a source " + jfo.toUri(), jfo.getKind() == SOURCE );
135: // }
136: //
137: // }
138: // }finally {
139: // jfmd.goldenJFM.close();
140: // jfmd.testJFM.close();
141: // }
142: // }
143: // }
144: //
145: // public void testGetFileForInput() {
146: // JavaFileManagerDescripton[] jfmds = getDescriptions();
147: //
148: // for ( JavaFileManagerDescripton jfmd : jfmds ) {
149: // }
150: // }
151: //
152: // public void testGetFileForOutput() throws Exception {
153: // JavaFileManagerDescripton[] jfmds = getDescriptions();
154: //
155: // for ( JavaFileManagerDescripton jfmd : jfmds ) {
156: // }
157: // }
158: //
159: // public void testGetInputFile() {
160: // JavaFileManagerDescripton[] jfmds = getDescriptions();
161: //
162: // for ( JavaFileManagerDescripton jfmd : jfmds ) {
163: // }
164: // }
165: //
166: //
167: // NOT SURE WHAT TO TEST HERE
168: //
169: // public void testSetLocation() {
170: // fail("The test case is empty.");
171: // }
172: //
173: // public void testFlush() throws Exception {
174: // fail("The test case is empty.");
175: // }
176: //
177: // public void testClose() throws Exception {
178: // fail("The test case is empty.");
179: // }
180:
181: // Other usefull methods ---------------------------------------------------
182:
183: /** Crates the default javac file managare tro have something to comare
184: * our file managers against
185: */
186: public static JavaFileManager createGoldenJFM(File[] classpath,
187: File[] sourcpath) throws IOException {
188:
189: JavaCompiler jc = ToolProvider.getSystemJavaCompiler();
190: StandardJavaFileManager fm = jc.getStandardFileManager(null,
191: null, null);
192:
193: if (classpath != null) {
194: fm.setLocation(StandardLocation.CLASS_PATH, Arrays
195: .asList(classpath));
196: }
197:
198: if (sourcpath != null) {
199: fm.setLocation(StandardLocation.SOURCE_PATH, Arrays
200: .asList(sourcpath));
201: }
202:
203: return fm;
204:
205: }
206:
207: // Innerclasses ------------------------------------------------------------
208:
209: private static class Setup extends TestSetup {
210:
211: public File workDir;
212: public File rtFile, srcFile;
213: public File rtFolder, srcFolder;
214: public CachingArchiveProvider archiveProvider;
215:
216: public Archive rtJarArchive;
217: public Archive rtFolderArchive;
218: public Archive srcZipArchive;
219: public Archive srcFolderArchive;
220:
221: public Setup(Test test) {
222: super (test);
223: }
224:
225: protected void tearDown() throws Exception {
226: TestUtil.removeWorkFolder(workDir);
227: super .tearDown();
228: }
229:
230: protected void setUp() throws Exception {
231: super .setUp();
232:
233: workDir = TestUtil.createWorkFolder();
234: System.out.println("Workdir " + workDir);
235: TestUtil.copyFiles(TestUtil.getJdkDir(), workDir,
236: TestUtil.RT_JAR);
237: TestUtil.copyFiles(TestUtil.getJdkDir(), workDir,
238: TestUtil.SRC_ZIP);
239:
240: rtFile = new File(workDir, TestUtil.RT_JAR);
241: JarFile rtJar = new JarFile(rtFile);
242: srcFile = new File(workDir, TestUtil.SRC_ZIP);
243: ZipFile srcZip = new ZipFile(srcFile);
244:
245: rtFolder = new File(workDir, "rtFolder");
246: TestUtil.unzip(rtJar, rtFolder);
247:
248: srcFolder = new File(workDir, "src");
249: TestUtil.unzip(srcZip, srcFolder);
250:
251: // Create archive provider
252: archiveProvider = CachingArchiveProvider.getDefault();
253:
254: rtJarArchive = archiveProvider.getArchive(rtFile.toURI()
255: .toURL(), true);
256: rtFolderArchive = archiveProvider.getArchive(rtFolder
257: .toURI().toURL(), true);
258: srcZipArchive = archiveProvider.getArchive(srcFile.toURI()
259: .toURL(), true);
260: srcFolderArchive = archiveProvider.getArchive(srcFolder
261: .toURI().toURL(), true);
262:
263: }
264: }
265:
266: }
|