void test(String[] opts, String className) throws Exception { count++; System.err.println("Test " + count + " " + Arrays.asList(opts) + " " + className); Path testSrcDir = Paths.get(System.getProperty("test.src")); Path testClassesDir = Paths.get(System.getProperty("test.classes")); Path classes = Paths.get("classes." + count); classes.createDirectory(); Context ctx = new Context(); PathFileManager fm = new JavacPathFileManager(ctx, true, null); JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); List<String> options = new ArrayList<String>(); options.addAll(Arrays.asList(opts)); options.addAll(Arrays.asList("-verbose", "-XDverboseCompilePolicy", "-d", classes.toString())); Iterable<? extends JavaFileObject> compilationUnits = fm.getJavaFileObjects(testSrcDir.resolve(className + ".java")); StringWriter sw = new StringWriter(); PrintWriter out = new PrintWriter(sw); JavaCompiler.CompilationTask t = compiler.getTask(out, fm, null, options, null, compilationUnits); boolean ok = t.call(); System.err.println(sw.toString()); if (!ok) { throw new Exception("compilation failed"); } File expect = new File("classes." + count + "/" + className + ".class"); if (!expect.exists()) throw new Exception("expected file not found: " + expect); long expectedSize = new File(testClassesDir.toString(), className + ".class").length(); long actualSize = expect.length(); if (expectedSize != actualSize) throw new Exception("wrong size found: " + actualSize + "; expected: " + expectedSize); }
// returns the stream of final pig script to be passed to Grunt private static BufferedReader runParamPreprocessor( BufferedReader origPigScript, ArrayList<String> params, ArrayList<String> paramFiles, String scriptFile, boolean createFile) throws org.apache.pig.tools.parameters.ParseException, IOException { ParameterSubstitutionPreprocessor psp = new ParameterSubstitutionPreprocessor(50); String[] type1 = new String[1]; String[] type2 = new String[1]; if (createFile) { BufferedWriter fw = new BufferedWriter(new FileWriter(scriptFile)); psp.genSubstitutedFile( origPigScript, fw, params.size() > 0 ? params.toArray(type1) : null, paramFiles.size() > 0 ? paramFiles.toArray(type2) : null); return new BufferedReader(new FileReader(scriptFile)); } else { StringWriter writer = new StringWriter(); psp.genSubstitutedFile( origPigScript, writer, params.size() > 0 ? params.toArray(type1) : null, paramFiles.size() > 0 ? paramFiles.toArray(type2) : null); return new BufferedReader(new StringReader(writer.toString())); } }
private String getFileText(File file) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8")); StringWriter sw = new StringWriter(); int n; char[] cbuf = new char[1024]; while ((n = br.read(cbuf, 0, cbuf.length)) != -1) sw.write(cbuf, 0, n); br.close(); return sw.toString(); }
/** * Writes the index file out to <code>jos</code>. <code>oldEntries</code> gives the names of the * files that were removed, <code>movedMap</code> maps from the new name to the old name. */ private static void createIndex(JarOutputStream jos, List oldEntries, Map movedMap) throws IOException { StringWriter writer = new StringWriter(); writer.write(VERSION_HEADER); writer.write("\r\n"); // Write out entries that have been removed for (int counter = 0; counter < oldEntries.size(); counter++) { String name = (String) oldEntries.get(counter); writer.write(REMOVE_COMMAND); writer.write(" "); writeEscapedString(writer, name); writer.write("\r\n"); } // And those that have moved Iterator names = movedMap.keySet().iterator(); if (names != null) { while (names.hasNext()) { String newName = (String) names.next(); String oldName = (String) movedMap.get(newName); writer.write(MOVE_COMMAND); writer.write(" "); writeEscapedString(writer, oldName); writer.write(" "); writeEscapedString(writer, newName); writer.write("\r\n"); } } JarEntry je = new JarEntry(INDEX_NAME); byte[] bytes = writer.toString().getBytes("UTF-8"); writer.close(); jos.putNextEntry(je); jos.write(bytes, 0, bytes.length); }
public void run() throws Exception { // Selection of files to be compiled File absJar = createJar(new File("abs.jar").getAbsoluteFile(), "j.A"); File relJar = createJar(new File("rel.jar"), "j.R"); File absDir = createDir(new File("abs.dir").getAbsoluteFile(), "d.A"); File relDir = createDir(new File("rel.dir"), "d.R"); File absTestFile = writeFile(new File("AbsTest.java").getAbsoluteFile(), "class AbsTest { class Inner { } }"); File relTestFile = writeFile(new File("RelTest.java"), "class RelTest { class Inner { } }"); File relTest2File = writeFile(new File("p/RelTest2.java"), "package p; class RelTest2 { class Inner { } }"); // This next class references other classes that will be found on the source path // and which will therefore need to be compiled as well. File mainFile = writeFile(new File("Main.java"), "class Main { j.A ja; j.R jr; d.A da; d.R dr; }" + ""); String sourcePath = createPath(absJar, relJar, absDir, relDir); File outDir = new File("classes"); outDir.mkdirs(); String[] args = { "-sourcepath", sourcePath, "-d", outDir.getPath(), absTestFile.getPath(), relTestFile.getPath(), relTest2File.getPath(), mainFile.getPath(), }; System.err.println("compile: " + Arrays.asList(args)); StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); int rc = com.sun.tools.javac.Main.compile(args, pw); pw.close(); if (rc != 0) { System.err.println(sw.toString()); throw new Exception("unexpected exit from javac: " + rc); } Set<File> expect = getFiles( outDir, "d/A.class", "d/A$Inner.class", "d/R.class", "d/R$Inner.class", "j/A.class", "j/A$Inner.class", "j/R.class", "j/R$Inner.class", "AbsTest.class", "AbsTest$Inner.class", "RelTest.class", "RelTest$Inner.class", "p/RelTest2.class", "p/RelTest2$Inner.class", "Main.class"); Set<File> found = findFiles(outDir); if (!found.equals(expect)) { if (found.containsAll(expect)) throw new Exception("unexpected files found: " + diff(found, expect)); else if (expect.containsAll(found)) throw new Exception("expected files not found: " + diff(expect, found)); } for (File f : found) verifySourceFileAttribute(f); if (errors > 0) throw new Exception(errors + " errors occurred"); }