1 19 package org.netbeans.api.java.source.gen; 20 21 import java.io.BufferedReader ; 22 import java.io.File ; 23 import java.io.FileNotFoundException ; 24 import java.io.FileReader ; 25 import java.io.IOException ; 26 import java.io.PrintStream ; 27 import java.net.MalformedURLException ; 28 import java.net.URL ; 29 import java.util.ArrayList ; 30 import java.util.List ; 31 import java.util.StringTokenizer ; 32 import javax.swing.JEditorPane ; 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 55 public abstract class GeneratorTest extends NbTestCase { 56 57 private FileObject dataDir; 58 59 File testFile = null; 60 61 public GeneratorTest(String aName) { 62 super(aName); 63 } 64 65 private void deepCopy(FileObject source, FileObject targetDirectory) throws IOException { 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 { 78 SourceUtilsTestUtil.prepareTest(new String [0], new Object [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 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 [0], new Object [] {loader, cpp}); 98 JEditorPane.registerEditorKitForContentType("text/x-java", "org.netbeans.modules.editor.java.JavaKit"); 99 File cacheFolder = new File (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 { 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 { 113 wc.toPhase(Phase.RESOLVED); 114 SourceUtilsTestUtil2.run(wc, transformer); 115 } 116 }).commit(); 117 printFile(); 118 } 119 120 private static ClassPath createClassPath(String classpath) { 121 StringTokenizer tokenizer = new StringTokenizer (classpath, File.pathSeparator); 122 List list = new ArrayList (); 123 while (tokenizer.hasMoreTokens()) { 124 String item = tokenizer.nextToken(); 125 File f = FileUtil.normalizeFile(new File (item)); 126 URL url = getRootURL(f); 127 if (url!=null) { 128 list.add(ClassPathSupport.createResource(url)); 129 } 130 } 131 return ClassPathSupport.createClassPath(list); 132 } 133 134 private static URL getRootURL (File f) { 136 URL url = null; 137 try { 138 if (isArchiveFile(f)) { 139 url = FileUtil.getArchiveRoot(f.toURI().toURL()); 140 } else { 141 url = f.toURI().toURL(); 142 String surl = url.toExternalForm(); 143 if (!surl.endsWith("/")) { 144 url = new URL (surl+"/"); 145 } 146 } 147 } catch (MalformedURLException e) { 148 throw new AssertionError (e); 149 } 150 return url; 151 } 152 153 private static boolean isArchiveFile(File f) { 154 String fileName = f.getName().toLowerCase(); 156 return fileName.endsWith(".jar") || fileName.endsWith(".zip"); } 158 159 String getGoldenDir() { 160 return getDataDir() + "/goldenfiles"; 161 } 162 163 String getSourceDir() { 164 return FileUtil.toFile(dataDir).getAbsolutePath(); 165 } 166 167 public static File getFile(String aDataDir, String aFileName) throws FileStateInvalidException { 168 String result = new File (aDataDir).getAbsolutePath() + '/' + aFileName; 169 return new File (result); 170 } 171 172 static JavaSource getJavaSource(File aFile) throws IOException { 173 FileObject testSourceFO = FileUtil.toFileObject(aFile); 174 assertNotNull(testSourceFO); 175 return JavaSource.forFileObject(testSourceFO); 176 } 177 178 File getTestFile() { 179 return testFile; 180 } 181 182 void assertFiles(final String aGoldenFile) throws IOException , 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 , IOException { 191 PrintStream log = getLog(); 192 BufferedReader in = new BufferedReader (new FileReader (getTestFile())); 193 String str; 194 while ((str = in.readLine()) != null) { 195 log.println(str); 196 } 197 in.close(); 198 } 199 200 abstract String getGoldenPckg(); 201 202 abstract String getSourcePckg(); 203 204 } 205 | Popular Tags |