Index: jdktools/modules/jdktools/src/test/java/org/apache/harmony/tests/tools/javac/binary/JavacBinTest.java =================================================================== --- jdktools/modules/jdktools/src/test/java/org/apache/harmony/tests/tools/javac/binary/JavacBinTest.java (revision 0) +++ jdktools/modules/jdktools/src/test/java/org/apache/harmony/tests/tools/javac/binary/JavacBinTest.java (revision 0) @@ -0,0 +1,212 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.harmony.tests.tools.javac.binary; + +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.io.PrintStream; + +import junit.framework.TestCase; + +public class JavacBinTest extends TestCase{ + + +private static final String RESOURCES = "resources/"; + + /** + * Method that takes in a non-existent file and checks the output for the appropriate Error message + * + */ + public void test_nonExists() throws IOException, InterruptedException + { + + final String testStr = "no_this_test.java"; + + final Object[] output = runExe( new String[]{"javac", testStr}, null); + final StringBuilder errOutput = (StringBuilder) output[1]; + final String stdOut = getProcessOutput(output, false); + assertTrue("The output should have " + testStr + " Error - " + errOutput.toString(), errOutput.toString().contains(testStr) && errOutput.toString().contains("not found") ); + } + + /** + * Method that takes a valid (A pgm without any errors) file and tests for the proper return code + */ + public void test_exists() throws IOException, InterruptedException + { + final String srcFile = RESOURCES + "Simple.java"; + final File f = new File(srcFile); + final String testStr = f.getAbsolutePath(); + + final Object[] output = runExe( new String[]{"javac", testStr}, null); + final StringBuilder errOutput = (StringBuilder) output[1]; + final String stdOut = getProcessOutput(output, false); + + final String classFile = f.getParent() + "\\Simple.class"; + final File clsFile = new File(classFile); + + assertTrue("The program " + testStr + " should cleanly compile. Error - " + errOutput.toString(), errOutput.toString().trim().equals("") && clsFile.exists()); + } + + /** + * Method that takes a valid (A program without any errors) file but with unresolved dependencies and tests for the proper return code + */ + public void test_existsWithUnresolvedDep() throws IOException, InterruptedException + { + + final String srcFile = RESOURCES + "Sample.java"; + final File f = new File(srcFile); + final String testStr = f.getAbsolutePath(); + + final Object[] output = runExe( new String[]{"javac", testStr}, null); + final StringBuilder errOutput = (StringBuilder) output[1]; + final String stdOut = getProcessOutput(output, false); + + assertTrue("The program " + testStr + " shouldn't compile due to unresolved dependencies. Error - " + errOutput.toString(), errOutput.toString().contains("error")); + } + + /** + * Method that takes a valid (A program without any errors) file with Resolved dependencies and tests for the proper return code + */ + public void test_existsWithResolvedDep() throws IOException, InterruptedException + { + final String srcFile = RESOURCES + "Sample.java"; + final File f = new File(srcFile); + final String testStr = f.getAbsolutePath(); + + final String option1 = "-classpath" ; + + final String jarFile = RESOURCES + "Dependency.jar"; + final File f1 = new File(jarFile); + final String option2 = f1.getAbsolutePath(); + + final Object[] output = runExe( new String[]{"javac", testStr, option1, option2}, null); + final StringBuilder errOutput = (StringBuilder) output[1]; + final String stdOut = getProcessOutput(output, false); + + final String classFile = f.getParent() + "\\Sample.class"; + final File clsFile = new File(classFile); + + assertTrue("The program " + testStr + " should compile as dependency " + option2 + " is resolved. Error - " + errOutput.toString(), + errOutput.toString().trim().equals("") && clsFile.exists()); + } + + /** + * Method to extract information from a Process's Output + * @param arr An array which has reference to the Process and its Error output + * @param displayOutput A flag which can be set to true if we want to see outputs for debug + * @return The stream content returned as a String + * @throws IOException IOException thrown during a stream read + * @throws InterruptedException InterruptedException during a Thread interrupt + */ + private static String getProcessOutput(Object[] arr, boolean displayOutput) throws IOException, InterruptedException + { + Process proc = (Process) arr[0]; + StringBuilder output = new StringBuilder(); + InputStream in = proc.getInputStream(); + int result; + byte[] bytes = new byte[1024]; + + while ((result = in.read(bytes)) != -1) { + output.append(new String(bytes, 0, result)); + + if (displayOutput) { + System.out.write(bytes, 0, result); + } + } + + in.close(); + proc.waitFor(); + proc.destroy(); + + return output.toString(); + } + + /** + * Method to execute a process + * @param args String array of arguments to run the executable + * @param envp Environment variables required to run the process + * @return An array holding the process reference and the Error output + * @throws IOException IOException thrown during a stream read + * @throws InterruptedException InterruptedException during a Thread interrupt + */ + private static Object[] runExe(String[] args, String[] envp) throws IOException, InterruptedException + { + + final Process proc = Runtime.getRuntime().exec( + args, envp); + final StringBuilder errBuf = new StringBuilder(); + Thread errThread = new Thread(new Runnable() { + public void run() { + synchronized (errBuf) { + InputStream err; + int result; + byte[] bytes = new byte[1024]; + + synchronized (proc) { + proc.notifyAll(); + } + + err = proc.getErrorStream(); + try { + while ((result = err.read(bytes)) != -1) { + //System.err.write(bytes, 0, result); + errBuf.append(new String(bytes)); + } + err.close(); + } catch (IOException e) { + ByteArrayOutputStream out = new ByteArrayOutputStream(); + PrintStream printer = new PrintStream(out); + + e.printStackTrace(); + e.printStackTrace(printer); + printer.close(); + errBuf.append(new String(out.toByteArray())); + } + } + } + }); + + synchronized (proc) { + errThread.start(); + // wait for errThread to start + int count = 0; + boolean isFinished = false; + while(!isFinished) { + try { + proc.wait(); + isFinished = true; + } catch (InterruptedException e) { + if(++count == 2) { + throw e; + } + } + } + if(count > 0) { + Thread.currentThread().interrupt(); + } + } + + return new Object[] { proc, errBuf }; + + } + + + +} Index: jdktools/modules/jdktools/src/test/java/org/apache/harmony/tests/tools/javaw/binary/JavawBinTest.java =================================================================== --- jdktools/modules/jdktools/src/test/java/org/apache/harmony/tests/tools/javaw/binary/JavawBinTest.java (revision 0) +++ jdktools/modules/jdktools/src/test/java/org/apache/harmony/tests/tools/javaw/binary/JavawBinTest.java (revision 0) @@ -0,0 +1,226 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.harmony.tests.tools.javaw.binary; + +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.io.PrintStream; + +import junit.framework.TestCase; + +public class JavawBinTest extends TestCase{ + + private static final String RESOURCES = "resources/"; + + /** + * Method that attempts to launch the javaw executable with a wrong option + * + * @throws IOException IOException thrown during a stream read + * @throws InterruptedException InterruptedException during a Thread interrupt + */ + public void testLaunch() throws IOException, InterruptedException + { + final Object[] execOutput = runExe( new String[]{"javaw", "-dummy"}, null); + final StringBuilder errExecOutput = (StringBuilder) execOutput[1]; + final String stdExecOut = getProcessOutput(execOutput, false); + + assertTrue("VM should not get launched. Error - " + errExecOutput.toString(), errExecOutput.toString().contains("-dummy")); + } + + /** + * Method that attempts a execution of a class file using javaw + * + * @throws IOException IOException thrown during a stream read + * @throws InterruptedException InterruptedException during a Thread interrupt + */ + public void testExecution() throws IOException, InterruptedException + { + //Compile a file + final String srcFile = RESOURCES + "Simple.java"; + final File f = new File(srcFile); + final String testStr = f.getAbsolutePath(); + + final Object[] compileOutput = runExe( new String[]{"javac", testStr}, null); + final StringBuilder errCompileOutput = (StringBuilder) compileOutput[1]; + final String stdCompileOut = getProcessOutput(compileOutput, false); + + final String classFile = f.getParent() + "/Simple.class"; + final File clsFile = new File(classFile); + + assertTrue("The program " + testStr + " should cleanly compile", errCompileOutput.toString().trim().equals("") && clsFile.exists()); + + final Object[] execOutput = runExe( new String[]{"javaw", "resources.Simple"}, null); + final StringBuilder errExecOutput = (StringBuilder) execOutput[1]; + final String stdExecOut = getProcessOutput(execOutput, false); + + assertTrue("Class file " + classFile + " wasn't executed. Error - " + errExecOutput.toString(), errExecOutput.toString().trim().equals("") && stdExecOut.contains("Hello World")); + + } + + /** + * Method to execute a Jar file + * @throws IOException IOException thrown during a stream read + * @throws InterruptedException InterruptedException during a Thread interrupt + */ + public void testJarExecution() throws IOException, InterruptedException + { + final String jarFile = RESOURCES + "Simple.jar"; + + final Object[] jarOutput = runExe( new String[]{"javaw", "-jar", jarFile}, null); + final StringBuilder errJarOutput = (StringBuilder) jarOutput[1]; + final String stdJarOut = getProcessOutput(jarOutput, false); + + assertTrue("Jar file " + jarFile + " wasn't executed. Error - " + errJarOutput.toString(), errJarOutput.toString().trim().equals("") && stdJarOut.contains("Hello World")); + + } + + /** + * Method to execute a jar which depends on another class and the class is not resolved in the classpath + * @throws IOException IOException thrown during a stream read + * @throws InterruptedException InterruptedException during a Thread interrupt + */ + public void testJarExecWithUnresolvedCP() throws IOException, InterruptedException + { + final String jarFile = RESOURCES + "Sample_unresolved.jar"; + + final Object[] jarOutput = runExe( new String[]{"javaw", "-jar", jarFile}, null); + final StringBuilder errJarOutput = (StringBuilder) jarOutput[1]; + final String stdJarOut = getProcessOutput(jarOutput, false); + + assertTrue("Jar file " + jarFile + " wasn't executed. Error - " + errJarOutput.toString(), errJarOutput.toString().contains("ClassNotFoundException")); + } + + /** + * Method to execute a jar which depends on another class and the class is resolved in the classpath + * @throws IOException + * @throws InterruptedException + */ + public void testJarExecWithResolvedCP() throws IOException, InterruptedException + { + final String jarFile = RESOURCES + "Sample_resolved.jar"; + + final Object[] jarOutput = runExe( new String[]{"javaw", "-jar", jarFile}, null); + final StringBuilder errJarOutput = (StringBuilder) jarOutput[1]; + final String stdJarOut = getProcessOutput(jarOutput, false); + + assertTrue("Jar file " + jarFile + " wasn't executed. Error - " + errJarOutput.toString(), errJarOutput.toString().trim().equals("") && stdJarOut.contains("Dependency")); + } + + /** + * Method to extract information from a Process's Output + * @param arr An array which has reference to the Process and its Error output + * @param displayOutput A flag which can be set to true if we want to see outputs for debug + * @return The stream content returned as a String + * @throws IOException IOException thrown during a stream read + * @throws InterruptedException InterruptedException during a Thread interrupt + */ + private static String getProcessOutput(Object[] arr, boolean displayOutput) throws IOException, InterruptedException + { + Process proc = (Process) arr[0]; + StringBuilder output = new StringBuilder(); + InputStream in = proc.getInputStream(); + int result; + byte[] bytes = new byte[1024]; + + while ((result = in.read(bytes)) != -1) { + output.append(new String(bytes, 0, result)); + + if (displayOutput) { + System.out.write(bytes, 0, result); + } + } + + in.close(); + proc.waitFor(); + proc.destroy(); + + return output.toString(); + } + + /** + * Method to execute a process + * @param args String array of arguments to run the executable + * @param envp Environment variables required to run the process + * @return An array holding the process reference and the Error output + * @throws IOException IOException thrown during a stream read + * @throws InterruptedException InterruptedException during a Thread interrupt + */ + private static Object[] runExe(String[] args, String[] envp) throws IOException, InterruptedException + { + + final Process proc = Runtime.getRuntime().exec( + args, envp); + final StringBuilder errBuf = new StringBuilder(); + Thread errThread = new Thread(new Runnable() { + public void run() { + synchronized (errBuf) { + InputStream err; + int result; + byte[] bytes = new byte[1024]; + + synchronized (proc) { + proc.notifyAll(); + } + + err = proc.getErrorStream(); + try { + while ((result = err.read(bytes)) != -1) { + //System.err.write(bytes, 0, result); + errBuf.append(new String(bytes)); + } + err.close(); + } catch (IOException e) { + ByteArrayOutputStream out = new ByteArrayOutputStream(); + PrintStream printer = new PrintStream(out); + + e.printStackTrace(); + e.printStackTrace(printer); + printer.close(); + errBuf.append(new String(out.toByteArray())); + } + } + } + }); + + synchronized (proc) { + errThread.start(); + // wait for errThread to start + int count = 0; + boolean isFinished = false; + while(!isFinished) { + try { + proc.wait(); + isFinished = true; + } catch (InterruptedException e) { + if(++count == 2) { + throw e; + } + } + } + if(count > 0) { + Thread.currentThread().interrupt(); + } + } + + return new Object[] { proc, errBuf }; + + } + +} Index: jdktools/modules/jdktools/src/test/resources/Dependency.jar =================================================================== Cannot display: file marked as a binary type. svn:mime-type = application/octet-stream Index: jdktools/modules/jdktools/src/test/resources/Sample.java =================================================================== --- jdktools/modules/jdktools/src/test/resources/Sample.java (revision 984706) +++ jdktools/modules/jdktools/src/test/resources/Sample.java (working copy) @@ -20,12 +20,13 @@ public class Sample { - /** + /**Command line arguments * @param args */ public static void main(String[] args) { - // TODO Auto-generated method stub + Dependency d = new Dependency(10,20); + System.out.println(d.toString()); } } Index: jdktools/modules/jdktools/src/test/resources/Sample_resolved.jar =================================================================== Cannot display: file marked as a binary type. svn:mime-type = application/octet-stream Property changes on: jdktools\modules\jdktools\src\test\resources\Sample_resolved.jar ___________________________________________________________________ Added: svn:mime-type + application/octet-stream Index: jdktools/modules/jdktools/src/test/resources/Sample_unresolved.jar =================================================================== Cannot display: file marked as a binary type. svn:mime-type = application/octet-stream Property changes on: jdktools\modules\jdktools\src\test\resources\Sample_unresolved.jar ___________________________________________________________________ Added: svn:mime-type + application/octet-stream Index: jdktools/modules/jdktools/src/test/resources/Simple.jar =================================================================== Cannot display: file marked as a binary type. svn:mime-type = application/octet-stream Property changes on: jdktools\modules\jdktools\src\test\resources\Simple.jar ___________________________________________________________________ Added: svn:mime-type + application/octet-stream