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..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
@@ -28,11 +28,25 @@
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
+ *
-Thomas Mueller
-
-
-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:
+This is an alpha release. The package names, API, and features may change in a future release.
+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 # before compiling.
*
* @author Thomas Mueller
*/
@@ -56,8 +70,11 @@ public class Compiler {
private Class> javacSun;
- // private static final JavaCompiler javaCompiler;
+ /*# Java 6 #
+ private JavaCompiler javaCompiler;
+ /*/
private Object javaCompiler;
+ //*/
private String compileDir = System.getProperty("java.io.tmpdir", ".");
@@ -66,17 +83,19 @@ 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();
+ /*/
+ 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 +229,27 @@ private void javaxToolsJavac(File javaFile) throws Exception {
"-d", compileDir,
"-encoding", "UTF-8");
- // JavaCompiler compiler = (JavaCompiler) javaCompiler;
+ /*# Java 6 #
+ JavaCompiler compiler = (JavaCompiler) javaCompiler;
+ StandardJavaFileManager fileManager = compiler.
+ getStandardFileManager(null, null, Charset.forName("UTF-8"));
+ Iterable extends JavaFileObject> compilationUnits =
+ fileManager.getJavaFileObjects(javaFile);
+ CompilationTask task = compiler.getTask(
+ writer, fileManager, null, options, null, compilationUnits);
+ task.call();
+ fileManager.close();
+ /*/
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 extends JavaFileObject> 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");
+ //*/
String err = writer.toString();
throwSyntaxError(err);
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
-
+
+assertThrows(emptyList).get(0);
+
Contents
+
@@ -172,7 +196,7 @@ Contents
Calling a Final Method, and Forgetting to Call a Method
that no method was called on the proxy:
-List@@ -234,6 +258,49 @@list = new ArrayList (); +List<String> list = new ArrayList<String>(); assertThrows(list); assertThrows(list).get(0);
ThreadLocal
variable internally.
+
+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); + }}; + } +} ++
+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> ++
This project does not required additional libraries. However, if you want to @@ -256,6 +323,11 @@
+This project uses the Apache License Version 2.0. +
+There are classpath problems when using Maven 2.x. together with the the compiling proxy factory.