1 19 20 package org.netbeans.modules.apisupport.jackpotrules; 21 22 import java.io.ByteArrayInputStream ; 23 import java.io.File ; 24 import java.io.FileInputStream ; 25 import java.io.FileOutputStream ; 26 import java.io.InputStream ; 27 import java.io.OutputStream ; 28 import java.io.OutputStreamWriter ; 29 import java.io.Writer ; 30 import java.net.URL ; 31 import java.util.zip.GZIPInputStream ; 32 33 import junit.framework.Assert; 34 35 import org.netbeans.jackpot.engine.*; 36 import org.netbeans.jackpot.transform.Transformer; 37 38 import org.openide.filesystems.FileObject; 39 import org.openide.filesystems.FileUtil; 40 import org.openide.filesystems.Repository; 41 42 46 public final class JackpotUtils { 47 48 49 private JackpotUtils() { 50 } 51 52 public static void apply(File dir, URL rules) throws Exception { 53 DefaultApplicationContext context = new DefaultApplicationContext(); 54 JackpotEngine eng = EngineFactory.createEngine(context); 55 56 File rulesFile = extractResource(rules); 57 58 int errors = eng.initialize(dir.getPath(), System.getProperty("java.class.path"), "1.4"); 59 Assert.assertEquals("No errors during compilation", 0, errors); 60 eng.runScript("q", "t", rulesFile.getPath()); 61 62 Assert.assertTrue("There is something to commit", eng.needsCommit()); 63 Assert.assertTrue("commit ok", eng.commit()); 64 } 65 66 final static String readFile (java.io.File f, boolean gzip) throws java.io.IOException { 67 if (!gzip) { 68 int s = (int)f.length (); 69 byte[] data = new byte[s]; 70 Assert.assertEquals ("Read all data", s, new FileInputStream (f).read (data)); 71 72 return new String (data); 73 } else { 74 GZIPInputStream is = new GZIPInputStream (new FileInputStream (f)); 75 byte[] arr = new byte[256 * 256]; 76 int first = 0; 77 for(;;) { 78 int len = is.read(arr, first, arr.length - first); 79 if (first + len < arr.length) { 80 return new String (arr, 0, first + len); 81 } 82 } 83 } 84 } 85 86 final static File extractString(String res) throws Exception { 87 File f = File.createTempFile("res", ".xml"); 88 f.deleteOnExit (); 89 return extractString(f, res); 90 } 91 92 final static File extractString (File f, String res) throws Exception { 93 FileOutputStream os = new FileOutputStream (f); 94 InputStream is = new ByteArrayInputStream (res.getBytes("UTF-8")); 95 for (;;) { 96 int ch = is.read (); 97 if (ch == -1) break; 98 os.write (ch); 99 } 100 os.close (); 101 102 return f; 103 } 104 105 final static File extractResource(String res) throws Exception { 106 URL u = JackpotUtils.class.getResource(res); 107 Assert.assertNotNull ("Resource should be found " + res, u); 108 return extractResource(u); 109 } 110 111 final static File extractResource(URL u) throws Exception { 112 Assert.assertNotNull ("Resource should be found " + u, u); 113 114 File f = File.createTempFile("res", ".xml"); 115 f.deleteOnExit (); 116 117 FileOutputStream os = new FileOutputStream (f); 118 InputStream is = u.openStream(); 119 for (;;) { 120 int ch = is.read (); 121 if (ch == -1) break; 122 os.write (ch); 123 } 124 os.close (); 125 126 return f; 127 } 128 129 } 130 | Popular Tags |