KickJava   Java API By Example, From Geeks To Geeks.

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


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