KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > api > java > source > gen > GeneratorTestMDRCompat


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 package org.netbeans.api.java.source.gen;
20
21 import java.io.BufferedReader JavaDoc;
22 import java.io.File JavaDoc;
23 import java.io.FileNotFoundException JavaDoc;
24 import java.io.FileReader JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.io.PrintStream JavaDoc;
27 import java.net.MalformedURLException JavaDoc;
28 import java.net.URL JavaDoc;
29 import java.util.ArrayList JavaDoc;
30 import java.util.List JavaDoc;
31 import java.util.StringTokenizer JavaDoc;
32 import javax.swing.JEditorPane JavaDoc;
33 import junit.framework.Assert;
34 import org.netbeans.api.java.classpath.ClassPath;
35 import org.netbeans.api.java.source.*;
36 import org.netbeans.api.java.source.JavaSource.Phase;
37 import org.netbeans.api.java.source.transform.Transformer;
38 import org.netbeans.junit.NbTestCase;
39 import org.netbeans.modules.java.JavaDataLoader;
40 import org.netbeans.modules.java.source.usages.IndexUtil;
41 import org.netbeans.spi.java.classpath.ClassPathProvider;
42 import org.netbeans.spi.java.classpath.support.ClassPathSupport;
43 import org.openide.filesystems.FileObject;
44 import org.openide.filesystems.FileStateInvalidException;
45 import org.openide.filesystems.FileUtil;
46 import org.openide.filesystems.Repository;
47 import org.openide.filesystems.XMLFileSystem;
48 import org.openide.util.Lookup;
49 import org.openide.util.SharedClassObject;
50
51 /**
52  *
53  * @author Pavel Flaska
54  */

55 public abstract class GeneratorTestMDRCompat extends NbTestCase {
56
57     File JavaDoc testFile = null;
58     
59     public GeneratorTestMDRCompat(String JavaDoc aName) {
60         super(aName);
61     }
62     
63     protected void setUp() throws Exception JavaDoc {
64         XMLFileSystem system = new XMLFileSystem();
65         system.setXmlUrls(new URL JavaDoc[] {
66             GeneratorTestMDRCompat.class.getResource("/org/netbeans/modules/java/source/resources/layer.xml")
67         });
68         Repository repository = new Repository(system);
69         ClassPathProvider cpp = new ClassPathProvider() {
70             public ClassPath findClassPath(FileObject file, String JavaDoc type) {
71                 if (type == ClassPath.SOURCE)
72                     return ClassPathSupport.createClassPath(new FileObject[] {FileUtil.toFileObject(getDataDir())});
73                     if (type == ClassPath.COMPILE)
74                         return ClassPathSupport.createClassPath(new FileObject[0]);
75                     if (type == ClassPath.BOOT)
76                         return createClassPath(System.getProperty("sun.boot.class.path"));
77                     return null;
78             }
79         };
80         SharedClassObject loader = JavaDataLoader.findObject(JavaDataLoader.class, true);
81         SourceUtilsTestUtil.prepareTest(new String JavaDoc[0], new Object JavaDoc[] {repository, loader, cpp});
82         JEditorPane.registerEditorKitForContentType("text/x-java", "org.netbeans.modules.editor.java.JavaKit");
83         File JavaDoc cacheFolder = new File JavaDoc(getWorkDir(), "var/cache/index");
84         cacheFolder.mkdirs();
85         IndexUtil.setCacheFolder(cacheFolder);
86     }
87     
88     public <R, P> void process(final Transformer<R, P> transformer) throws IOException JavaDoc {
89         assertNotNull(testFile);
90         FileObject testSourceFO = FileUtil.toFileObject(testFile);
91         assertNotNull(testSourceFO);
92         JavaSource js = JavaSource.forFileObject(testSourceFO);
93         js.runModificationTask(new CancellableTask<WorkingCopy>() {
94             public void cancel() {
95             }
96             public void run(WorkingCopy wc) throws IOException JavaDoc {
97                 wc.toPhase(Phase.RESOLVED);
98                 SourceUtilsTestUtil2.run(wc, transformer);
99             }
100         }).commit();
101         printFile();
102     }
103     
104     private static ClassPath createClassPath(String JavaDoc classpath) {
105         StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(classpath, File.pathSeparator);
106         List JavaDoc/*<PathResourceImplementation>*/ list = new ArrayList JavaDoc();
107         while (tokenizer.hasMoreTokens()) {
108             String JavaDoc item = tokenizer.nextToken();
109             File JavaDoc f = FileUtil.normalizeFile(new File JavaDoc(item));
110             URL JavaDoc url = getRootURL(f);
111             if (url!=null) {
112                 list.add(ClassPathSupport.createResource(url));
113             }
114         }
115         return ClassPathSupport.createClassPath(list);
116     }
117     
118     // XXX this method could probably be removed... use standard FileUtil stuff
119
private static URL JavaDoc getRootURL (File JavaDoc f) {
120         URL JavaDoc url = null;
121         try {
122             if (isArchiveFile(f)) {
123                 url = FileUtil.getArchiveRoot(f.toURI().toURL());
124             } else {
125                 url = f.toURI().toURL();
126                 String JavaDoc surl = url.toExternalForm();
127                 if (!surl.endsWith("/")) {
128                     url = new URL JavaDoc(surl+"/");
129                 }
130             }
131         } catch (MalformedURLException JavaDoc e) {
132             throw new AssertionError JavaDoc(e);
133         }
134         return url;
135     }
136     
137     private static boolean isArchiveFile(File JavaDoc f) {
138         // the f might not exist and so you cannot use e.g. f.isFile() here
139
String JavaDoc fileName = f.getName().toLowerCase();
140         return fileName.endsWith(".jar") || fileName.endsWith(".zip"); //NOI18N
141
}
142     
143     String JavaDoc getGoldenDir() {
144         return getDataDir() + "/goldenfiles";
145     }
146     
147     String JavaDoc getSourceDir() {
148         return getDataDir().getAbsolutePath();
149     }
150     
151     public static File JavaDoc getFile(String JavaDoc aDataDir, String JavaDoc aFileName) throws FileStateInvalidException {
152         String JavaDoc result = new File JavaDoc(aDataDir).getAbsolutePath() + '/' + aFileName;
153         return new File JavaDoc(result);
154     }
155     
156     static JavaSource getJavaSource(File JavaDoc aFile) throws IOException JavaDoc {
157         FileObject testSourceFO = FileUtil.toFileObject(aFile);
158         assertNotNull(testSourceFO);
159         return JavaSource.forFileObject(testSourceFO);
160     }
161
162     File JavaDoc getTestFile() {
163         return testFile;
164     }
165     
166     void assertFiles(final String JavaDoc aGoldenFile) throws IOException JavaDoc, FileStateInvalidException {
167         assertFile("File is not correctly generated.",
168             getTestFile(),
169             getFile(getGoldenDir(), getGoldenPckg() + aGoldenFile),
170             getWorkDir()
171         );
172     }
173     
174     void printFile() throws FileNotFoundException JavaDoc, IOException JavaDoc {
175         PrintStream JavaDoc log = getLog();
176         BufferedReader JavaDoc in = new BufferedReader JavaDoc(new FileReader JavaDoc(getTestFile()));
177         String JavaDoc str;
178         while ((str = in.readLine()) != null) {
179             log.println(str);
180         }
181         in.close();
182     }
183
184     abstract String JavaDoc getGoldenPckg();
185
186     abstract String JavaDoc getSourcePckg();
187
188 }
189
Popular Tags