From 1d4d6057141cb5e648c0ddc001c6a66b8660fd35 Mon Sep 17 00:00:00 2001 From: Thomas Mueller Date: Fri, 5 Aug 2011 11:47:07 +0200 Subject: [PATCH 1/3] Support compiling with Java 6 (after switching the source code). --- .../contrib/assertthrows/proxy/Compiler.java | 61 +++++++++++++------ 1 file changed, 42 insertions(+), 19 deletions(-) diff --git a/assertthrows/src/main/java/org/junit/contrib/assertthrows/proxy/Compiler.java b/assertthrows/src/main/java/org/junit/contrib/assertthrows/proxy/Compiler.java index 9be466f..4581734 100644 --- a/assertthrows/src/main/java/org/junit/contrib/assertthrows/proxy/Compiler.java +++ b/assertthrows/src/main/java/org/junit/contrib/assertthrows/proxy/Compiler.java @@ -28,11 +28,27 @@ import java.nio.charset.Charset; import java.util.Arrays; import java.util.HashMap; +/*# Java 6 # +import javax.tools.JavaCompiler; +import javax.tools.JavaFileObject; +import javax.tools.StandardJavaFileManager; +import javax.tools.ToolProvider; +import javax.tools.JavaCompiler.CompilationTask; +//*/ /** * This class allows to convert source code to a class. It uses one class loader * per class, and internally uses javax.tools.JavaCompiler if * available, and com.sun.tools.javac.Main otherwise. + *

+ * Implementation note: this class is uses reflection to try to load a + * javax.tools.JavaCompiler, which also runs on Java 5, and can be + * compiled with Java 5. The source code can be switched to Java 6 however. To + * do that, replace /*# + * Java 6 # with //# + * Java 6 #, and //# + * Java 5 # with /*# + * Java 5 # before compiling. * * @author Thomas Mueller */ @@ -56,8 +72,12 @@ public class Compiler { private Class javacSun; - // private static final JavaCompiler javaCompiler; + /*# Java 6 # + private JavaCompiler javaCompiler; + //*/ + //# Java 5 # private Object javaCompiler; + //*/ private String compileDir = System.getProperty("java.io.tmpdir", "."); @@ -66,17 +86,20 @@ public void setUseSystemJavaCompiler(boolean useSystemJavaCompiler) { } private void initCompiler() { - Object comp = null; + javaCompiler = null; if (useSystemJavaCompiler) { try { - // comp = ToolProvider.getSystemJavaCompiler(); - comp = ReflectionUtils.callStaticMethod( + /*# Java 6 # + javaCompiler = ToolProvider.getSystemJavaCompiler(); + //*/ + //# Java 5 # + javaCompiler = ReflectionUtils.callStaticMethod( "javax.tools.ToolProvider.getSystemJavaCompiler"); + //*/ } catch (Exception e) { // ignore } } - javaCompiler = comp; Class javac; try { javac = Class.forName("com.sun.tools.javac.Main"); @@ -210,29 +233,29 @@ private void javaxToolsJavac(File javaFile) throws Exception { "-d", compileDir, "-encoding", "UTF-8"); - // JavaCompiler compiler = (JavaCompiler) javaCompiler; + //# Java 5 # Object compiler = javaCompiler; - - // StandardJavaFileManager fileManager = compiler. - // getStandardFileManager(null, null, Charset.forName("UTF-8")); Object fileManager = ReflectionUtils.callMethod(compiler, "getStandardFileManager", null, null, Charset.forName("UTF-8")); - - // Iterable compilationUnits = - // fileManager.getJavaFileObjects(javaFile); Object compilationUnits = ReflectionUtils.callMethod(fileManager, "getJavaFileObjects", (Object) new File[] { javaFile }); - - // Task task = compiler.getTask( - // writer, fileManager, null, options, null, compilationUnits); Object task = ReflectionUtils.callMethod(compiler, "getTask", writer, fileManager, null, options, null, compilationUnits); - - // task.call(); ReflectionUtils.callMethod(task, "call"); - - // fileManager.close(); ReflectionUtils.callMethod(fileManager, "close"); + //*/ + + /*# Java 6 # + JavaCompiler compiler = (JavaCompiler) javaCompiler; + StandardJavaFileManager fileManager = compiler. + getStandardFileManager(null, null, Charset.forName("UTF-8")); + Iterable compilationUnits = + fileManager.getJavaFileObjects(javaFile); + CompilationTask task = compiler.getTask( + writer, fileManager, null, options, null, compilationUnits); + task.call(); + fileManager.close(); + //*/ String err = writer.toString(); throwSyntaxError(err); From 73d01a7a3f813559a74ba793d12965e8b3e13759 Mon Sep 17 00:00:00 2001 From: Thomas Mueller Date: Fri, 5 Aug 2011 12:53:57 +0200 Subject: [PATCH 2/3] Simplify switching the source code to Java 6. --- .../contrib/assertthrows/proxy/Compiler.java | 32 ++++++++----------- 1 file changed, 13 insertions(+), 19 deletions(-) diff --git a/assertthrows/src/main/java/org/junit/contrib/assertthrows/proxy/Compiler.java b/assertthrows/src/main/java/org/junit/contrib/assertthrows/proxy/Compiler.java index 4581734..7d84e05 100644 --- a/assertthrows/src/main/java/org/junit/contrib/assertthrows/proxy/Compiler.java +++ b/assertthrows/src/main/java/org/junit/contrib/assertthrows/proxy/Compiler.java @@ -46,9 +46,7 @@ * compiled with Java 5. The source code can be switched to Java 6 however. To * do that, replace /*# * Java 6 # with //# - * Java 6 #, and //# - * Java 5 # with /*# - * Java 5 # before compiling. + * Java 6 # before compiling. * * @author Thomas Mueller */ @@ -74,8 +72,7 @@ public class Compiler { /*# Java 6 # private JavaCompiler javaCompiler; - //*/ - //# Java 5 # + /*/ private Object javaCompiler; //*/ @@ -91,8 +88,7 @@ private void initCompiler() { try { /*# Java 6 # javaCompiler = ToolProvider.getSystemJavaCompiler(); - //*/ - //# Java 5 # + /*/ javaCompiler = ReflectionUtils.callStaticMethod( "javax.tools.ToolProvider.getSystemJavaCompiler"); //*/ @@ -233,18 +229,6 @@ private void javaxToolsJavac(File javaFile) throws Exception { "-d", compileDir, "-encoding", "UTF-8"); - //# Java 5 # - Object compiler = javaCompiler; - Object fileManager = ReflectionUtils.callMethod(compiler, "getStandardFileManager", - null, null, Charset.forName("UTF-8")); - Object compilationUnits = ReflectionUtils.callMethod(fileManager, "getJavaFileObjects", - (Object) new File[] { javaFile }); - Object task = ReflectionUtils.callMethod(compiler, "getTask", - writer, fileManager, null, options, null, compilationUnits); - ReflectionUtils.callMethod(task, "call"); - ReflectionUtils.callMethod(fileManager, "close"); - //*/ - /*# Java 6 # JavaCompiler compiler = (JavaCompiler) javaCompiler; StandardJavaFileManager fileManager = compiler. @@ -255,6 +239,16 @@ private void javaxToolsJavac(File javaFile) throws Exception { writer, fileManager, null, options, null, compilationUnits); task.call(); fileManager.close(); + /*/ + Object compiler = javaCompiler; + Object fileManager = ReflectionUtils.callMethod(compiler, "getStandardFileManager", + null, null, Charset.forName("UTF-8")); + Object compilationUnits = ReflectionUtils.callMethod(fileManager, "getJavaFileObjects", + (Object) new File[] { javaFile }); + Object task = ReflectionUtils.callMethod(compiler, "getTask", + writer, fileManager, null, options, null, compilationUnits); + ReflectionUtils.callMethod(task, "call"); + ReflectionUtils.callMethod(fileManager, "close"); //*/ String err = writer.toString(); From ee966ab956266c3c5b0ca72e8a1f1d908b61c5d4 Mon Sep 17 00:00:00 2001 From: Thomas Mueller Date: Mon, 8 Aug 2011 13:00:37 +0200 Subject: [PATCH 3/3] Slightly improved documentation. --- assertthrows/src/site/resources/index.html | 100 ++++++++++++++++++--- 1 file changed, 86 insertions(+), 14 deletions(-) diff --git a/assertthrows/src/site/resources/index.html b/assertthrows/src/site/resources/index.html index ffa33bd..550a4a0 100644 --- a/assertthrows/src/site/resources/index.html +++ b/assertthrows/src/site/resources/index.html @@ -1,13 +1,17 @@ - -JUnit Contrib - AssertThrows - - + +

JUnit Contrib - AssertThrows

-

-Thomas Mueller -

- -
+
Testing for Expected Exceptions

- -This is an alpha release. The package names, API, and features may change in a future release. - +This JUnit extension allows you to test for exceptions as you would test for other conditions, often as:

+
+assertThrows(emptyList).get(0);
+

Contents

+

+This is an alpha release. The package names, API, and features may change in a future release. +

@@ -172,7 +196,7 @@

Calling a Final Method, and Forgetting to Call a Method

that no method was called on the proxy:

-List list = new ArrayList();
+List<String> list = new ArrayList<String>();
 assertThrows(list);
 assertThrows(list).get(0);
 
@@ -234,6 +258,49 @@

Getting the Last Throwable

it uses a ThreadLocal variable internally.

+

Usage

+

+To use this tool, download add the .jar file to your project. +A minimal test case is: +

+
+import java.util.ArrayList;
+import java.util.List;
+import org.junit.Test;
+import org.junit.contrib.assertthrows.AssertThrows;
+import static org.junit.contrib.assertthrows.AssertThrows.*;
+public class HelloAssertThrows {
+    @Test
+    public void test() {
+        final List<String> emptyList = new ArrayList<String>();
+        assertThrows(emptyList).get(0);
+        new AssertThrows() { public void test() {
+            emptyList.get(0);
+        }};
+    }
+}
+
+

Using Maven

+

+The jar file is not yet in a Maven central repository, but you can build and deploy it locally if you wish. +To do that, first download the .zip or .tar.gz file, and build it using: +

+
+cd assertthrows
+mvn clean install
+
+

+Then use the following dependency: +

+
+<dependency>
+    <groupId>org.junit.contrib</groupId>
+    <artifactId>junit-assertthrows</artifactId>
+    <version>0.1-SNAPSHOT</version>
+    <scope>test</scope>
+</dependency>
+
+

Dependencies

This project does not required additional libraries. However, if you want to @@ -256,6 +323,11 @@

Dependencies

</dependency> +

License

+

+This project uses the Apache License Version 2.0. +

+

Known Problems

There are classpath problems when using Maven 2.x. together with the the compiling proxy factory.