From 1d4d6057141cb5e648c0ddc001c6a66b8660fd35 Mon Sep 17 00:00:00 2001
From: Thomas Mueller
+ * 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
if
* available, and com.sun.tools.javac.Main
otherwise.
+ * 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 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");
+ //*/
+
+ /*# 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();
+ //*/
String err = writer.toString();
throwSyntaxError(err);
From 73d01a7a3f813559a74ba793d12965e8b3e13759 Mon Sep 17 00:00:00 2001
From: Thomas Mueller /*#
* 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
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 @@ Getting the Last Throwable
it uses a 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.