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 GeneratorTestMDRCompat extends NbTestCase { 56 57 File testFile = null; 58 59 public GeneratorTestMDRCompat(String aName) { 60 super(aName); 61 } 62 63 protected void setUp() throws Exception { 64 XMLFileSystem system = new XMLFileSystem(); 65 system.setXmlUrls(new URL [] { 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 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 [0], new Object [] {repository, loader, cpp}); 82 JEditorPane.registerEditorKitForContentType("text/x-java", "org.netbeans.modules.editor.java.JavaKit"); 83 File cacheFolder = new File (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 { 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 { 97 wc.toPhase(Phase.RESOLVED); 98 SourceUtilsTestUtil2.run(wc, transformer); 99 } 100 }).commit(); 101 printFile(); 102 } 103 104 private static ClassPath createClassPath(String classpath) { 105 StringTokenizer tokenizer = new StringTokenizer (classpath, File.pathSeparator); 106 List list = new ArrayList (); 107 while (tokenizer.hasMoreTokens()) { 108 String item = tokenizer.nextToken(); 109 File f = FileUtil.normalizeFile(new File (item)); 110 URL url = getRootURL(f); 111 if (url!=null) { 112 list.add(ClassPathSupport.createResource(url)); 113 } 114 } 115 return ClassPathSupport.createClassPath(list); 116 } 117 118 private static URL getRootURL (File f) { 120 URL url = null; 121 try { 122 if (isArchiveFile(f)) { 123 url = FileUtil.getArchiveRoot(f.toURI().toURL()); 124 } else { 125 url = f.toURI().toURL(); 126 String surl = url.toExternalForm(); 127 if (!surl.endsWith("/")) { 128 url = new URL (surl+"/"); 129 } 130 } 131 } catch (MalformedURLException e) { 132 throw new AssertionError (e); 133 } 134 return url; 135 } 136 137 private static boolean isArchiveFile(File f) { 138 String fileName = f.getName().toLowerCase(); 140 return fileName.endsWith(".jar") || fileName.endsWith(".zip"); } 142 143 String getGoldenDir() { 144 return getDataDir() + "/goldenfiles"; 145 } 146 147 String getSourceDir() { 148 return getDataDir().getAbsolutePath(); 149 } 150 151 public static File getFile(String aDataDir, String aFileName) throws FileStateInvalidException { 152 String result = new File (aDataDir).getAbsolutePath() + '/' + aFileName; 153 return new File (result); 154 } 155 156 static JavaSource getJavaSource(File aFile) throws IOException { 157 FileObject testSourceFO = FileUtil.toFileObject(aFile); 158 assertNotNull(testSourceFO); 159 return JavaSource.forFileObject(testSourceFO); 160 } 161 162 File getTestFile() { 163 return testFile; 164 } 165 166 void assertFiles(final String aGoldenFile) throws IOException , 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 , IOException { 175 PrintStream log = getLog(); 176 BufferedReader in = new BufferedReader (new FileReader (getTestFile())); 177 String str; 178 while ((str = in.readLine()) != null) { 179 log.println(str); 180 } 181 in.close(); 182 } 183 184 abstract String getGoldenPckg(); 185 186 abstract String getSourcePckg(); 187 188 } 189 | Popular Tags |