Skip to content

Commit

Permalink
Merge pull request dsaff#6 from thomasmueller/master
Browse files Browse the repository at this point in the history
Documentation
  • Loading branch information
dsaff committed Aug 8, 2011
2 parents 2fdd123 + ee966ab commit ed6586f
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 33 deletions.
Expand Up @@ -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 <code>javax.tools.JavaCompiler</code> if
* available, and <code>com.sun.tools.javac.Main</code> otherwise.
* <p>
* Implementation note: this class is uses reflection to try to load a
* <code>javax.tools.JavaCompiler</code>, 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 <code>/*#
* Java 6 #</code> with <code>//#
* Java 6 # before compiling.
*
* @author Thomas Mueller
*/
Expand All @@ -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", ".");

Expand All @@ -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");
Expand Down Expand Up @@ -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);
Expand Down
100 changes: 86 additions & 14 deletions assertthrows/src/site/resources/index.html
@@ -1,13 +1,17 @@
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head><meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<title>
JUnit Contrib - AssertThrows
</title>
<style>
body {
<title>JUnit Contrib - AssertThrows</title>
<style type="text/css">
body {
margin: 0 auto;
max-width: 640px;
margin-top: 1.0em;
font: 13px/1.4 Arial, sans-serif;
}
h1 {
margin-bottom: 0px;
}
a {
text-decoration: none;
}
Expand All @@ -18,33 +22,53 @@
background-color: #eee;
padding: 6px;
}
.button {
color: #fff;
font-size: 12px;
border: solid 1px;
border-color: #0a0;
background-color: #080;
background: -moz-linear-gradient(top, #0c0, #080);
background: -webkit-gradient(linear, left top, left bottom, from(#0c0), to(#080));
padding: 5px;
border-radius: 8px;
-moz-border-radius: 8px;
-webkit-border-radius: 8px;
}
.button a {
color: #fff;
}
</style>
</head>

<body>

<h1>
<span style="color: #33ff33">J</span><span style="color: #cc0000">U</span>nit Contrib -
AssertThrows</h1>
<p>
Thomas Mueller
</p>

<hr/>
<div><i>Testing for Expected Exceptions</i></div>

<p>
<span style="border: solid 1px; padding: 4px;">
This is an alpha release. The package names, API, and features may change in a future release.
</span>
This JUnit extension allows you to test for exceptions as you would test for other conditions, often as:
</p>
<pre>
assertThrows(emptyList).get(0);
</pre>

<h2>Contents</h2>
<p style="border: solid 1px; padding: 4px;">
This is an alpha release. The package names, API, and features may change in a future release.
</p>

<ul><li><a href="#TheProblem">The Problem</a>
</li><li><a href="#Annotation">Annotation</a>
</li><li><a href="#TryFailCatch">Try-Fail-Catch</a>
</li><li><a href="#Proxy">Proxy</a>
</li><li><a href="#AnonymousClass">Anonymous Class</a>
</li><li><a href="#LastThrown">Getting the Last Throwable</a>
</li><li><a href="#Usage">Usage</a>
</li><li><a href="#Dependencies">Dependencies</a>
</li><li><a href="#License">License</a>
</li><li><a href="#KnownProblems">Known Problems</a>
</li></ul>

Expand Down Expand Up @@ -172,7 +196,7 @@ <h3>Calling a Final Method, and Forgetting to Call a Method</h3>
that no method was called on the proxy:
</p>
<pre>
List<String> list = new ArrayList<String>();
List&lt;String&gt; list = new ArrayList&lt;String&gt;();
assertThrows(list);
assertThrows(list).get(0);
</pre>
Expand Down Expand Up @@ -234,6 +258,49 @@ <h2 id="LastThrown">Getting the Last Throwable</h2>
it uses a <code>ThreadLocal</code> variable internally.
</p>

<h2 id="Usage">Usage</h2>
<p>
To use this tool, download add the <code>.jar</code> file to your project.
A minimal test case is:
</p>
<pre>
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&lt;String&gt; emptyList = new ArrayList&lt;String&gt;();
assertThrows(emptyList).get(0);
new AssertThrows() { public void test() {
emptyList.get(0);
}};
}
}
</pre>
<h3>Using Maven</h3>
<p>
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 <code>.zip</code> or <code>.tar.gz</code> file, and build it using:
</p>
<pre>
cd assertthrows
mvn clean install
</pre>
<p>
Then use the following dependency:
</p>
<pre>
&lt;dependency&gt;
&lt;groupId&gt;org.junit.contrib&lt;/groupId&gt;
&lt;artifactId&gt;junit-assertthrows&lt;/artifactId&gt;
&lt;version&gt;0.1-SNAPSHOT&lt;/version&gt;
&lt;scope&gt;test&lt;/scope&gt;
&lt;/dependency&gt;
</pre>

<h2 id="Dependencies">Dependencies</h2>
<p>
This project does not required additional libraries. However, if you want to
Expand All @@ -256,6 +323,11 @@ <h2 id="Dependencies">Dependencies</h2>
&lt;/dependency&gt;
</pre>

<h2 id="License">License</h2>
<p>
This project uses the <a href="http://www.apache.org/licenses/LICENSE-2.0.html">Apache License Version 2.0</a>.
</p>

<h2 id="KnownProblems">Known Problems</h2>
<p>
There are classpath problems when using Maven 2.x. together with the the compiling proxy factory.
Expand Down

0 comments on commit ed6586f

Please sign in to comment.