1 19 20 package org.netbeans.jackpot.test; 21 22 import org.netbeans.modules.java.source.engine.BuildErrorsException; 23 import java.io.ByteArrayInputStream ; 24 import java.io.EOFException ; 25 import java.io.File ; 26 import java.io.FileInputStream ; 27 import java.io.FileOutputStream ; 28 import java.io.IOException ; 29 import java.io.InputStream ; 30 import java.io.OutputStream ; 31 import java.io.OutputStreamWriter ; 32 import java.io.Writer ; 33 import java.net.URL ; 34 import java.util.zip.GZIPInputStream ; 35 import org.netbeans.api.java.source.query.ResultTableModel; 36 import org.netbeans.api.java.source.transform.Transformer; 37 import org.netbeans.modules.java.source.engine.DefaultApplicationContext; 38 import org.netbeans.modules.java.source.engine.EngineFactory; 39 import org.netbeans.modules.java.source.engine.JackpotEngine; 40 41 import org.openide.filesystems.FileObject; 42 import org.openide.filesystems.FileUtil; 43 import org.openide.filesystems.Repository; 44 45 51 public final class TestUtilities { 52 53 private TestUtilities() {} 55 56 64 public static void assertTransform(String from, String result, String rules) throws TransformationException, Exception { 65 File src = copyStringToFile(getClassName(from), from); 66 File rulesFile = copyStringToFile("test.rules", rules); 67 apply(tempDirectory, rulesFile.getPath(), true, true, false); 68 69 String txt = copyFileToString(src); 70 if (!txt.equals(result)) 71 throw new TransformationException("expected: \"" + result + "\" got: \"" + txt + "\""); 72 } 73 74 82 public static void assertTransform(String from, String result, Class clazz) throws TransformationException, Exception { 83 File src = copyStringToFile(getClassName(from), from); 84 String className = clazz.getName(); 85 apply(tempDirectory, className, false, true, false); 86 87 String txt = copyFileToString(src); 88 if (!txt.equals(result)) 89 throw new TransformationException("expected: \"" + result + "\" got: \"" + txt + "\""); 90 } 91 92 private static String getClassName(String src) { 93 return null; } 95 96 105 public static int applyRules(File dir, URL rules) 106 throws BuildErrorsException, Exception { 107 return applyRules(dir, rules, false); 108 } 109 110 120 public static int applyRules(File dir, URL rules, boolean allowErrors) 121 throws BuildErrorsException, Exception { 122 File rulesFile = copyResourceToFile(rules); 123 return apply(dir, rulesFile.getPath(), true, true, allowErrors).getResultCount(); 124 } 125 126 138 public static ResultTableModel applyQuery(File dir, String queryName) 139 throws BuildErrorsException, Exception { 140 return apply(dir, queryName, false, true, false); 141 } 142 143 152 public static int applyTransformer(File dir, String transformerName) 153 throws BuildErrorsException, Exception { 154 return apply(dir, transformerName, false, true, false).getResultCount(); 155 } 156 157 private static ResultTableModel apply(File dir, String cmd, boolean isScript, boolean makesChanges, boolean allowErrors) 158 throws BuildErrorsException, Exception { 159 DefaultApplicationContext context = new DefaultApplicationContext(); 160 JackpotEngine eng = EngineFactory.createEngine(context); 161 162 int errors = eng.initialize(dir.getPath(), System.getProperty("java.class.path"), "1.5"); 163 if (errors > 0 && !allowErrors) 164 throw new BuildErrorsException(Integer.toString(errors) + " build errors"); 166 ResultTableModel result = isScript ? 167 eng.runScript("q", "t", cmd) : eng.runCommand("q", "t", cmd); 168 if (makesChanges && !eng.commit()) 169 throw new AssertionError ("commit canceled"); 171 return result; 172 } 173 174 180 public final static String copyFileToString (java.io.File f) throws java.io.IOException { 181 int s = (int)f.length (); 182 byte[] data = new byte[s]; 183 int len = new FileInputStream (f).read (data); 184 if (len != s) 185 throw new EOFException ("truncated file"); 186 return new String (data); 187 } 188 189 195 public final static String copyGZipFileToString (java.io.File f) throws java.io.IOException { 196 GZIPInputStream is = new GZIPInputStream (new FileInputStream (f)); 197 byte[] arr = new byte[256 * 256]; 198 int first = 0; 199 for(;;) { 200 int len = is.read(arr, first, arr.length - first); 201 if (first + len < arr.length) { 202 return new String (arr, 0, first + len); 203 } 204 } 205 } 206 207 214 public final static File copyStringToFile (File f, String content) throws Exception { 215 FileOutputStream os = new FileOutputStream (f); 216 InputStream is = new ByteArrayInputStream (content.getBytes("UTF-8")); 217 FileUtil.copy(is, os); 218 os.close (); 219 220 return f; 221 } 222 223 private final static File copyStringToFile(String filename, String res) throws Exception { 224 File f = new File (tempDirectory, filename); 225 f.deleteOnExit (); 226 return copyStringToFile(f, res); 227 } 228 229 private final static File copyResourceToFile(URL u) throws Exception { 230 File f = File.createTempFile("res", ".xml"); 231 f.deleteOnExit (); 232 233 FileOutputStream os = new FileOutputStream (f); 234 InputStream is = u.openStream(); 235 FileUtil.copy(is, os); 236 os.close (); 237 238 return f; 239 } 240 241 private static File tempDirectory; 242 { 243 try { 244 File f = File.createTempFile("foo", "bar"); 245 tempDirectory = f.getParentFile(); 246 } catch (IOException e) { 247 tempDirectory = new File ("/tmp"); 248 } 249 } 250 } | Popular Tags |