summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBob Lee <crazybob@crazybob.org>2009-09-28 19:11:16 +0000
committerBob Lee <crazybob@crazybob.org>2009-09-28 19:11:16 +0000
commit38d5c62e329d696d118a06f47d64c4da79309543 (patch)
tree2b42cd214c97dcc6555d7b2cdc8ded5a689e8622
parentfc7281e855bf2bd77b49c765a81e2ea2f45251ba (diff)
downloadplatform_external_jsr330-38d5c62e329d696d118a06f47d64c4da79309543.tar.gz
platform_external_jsr330-38d5c62e329d696d118a06f47d64c4da79309543.tar.bz2
platform_external_jsr330-38d5c62e329d696d118a06f47d64c4da79309543.zip
Modified Tck to use JUnit.
git-svn-id: https://atinject.googlecode.com/svn/trunk@33 3bc8319c-20ab-11de-9edc-3f40a397ab60
-rw-r--r--lib/junit-src.jarbin0 -> 55217 bytes
-rw-r--r--tck/com/googlecode/atinject/Candidate.java48
-rw-r--r--tck/com/googlecode/atinject/Tck.java126
-rw-r--r--tck/com/googlecode/atinject/auto/Convertible.java16
-rw-r--r--tck/tck.iml4
5 files changed, 111 insertions, 83 deletions
diff --git a/lib/junit-src.jar b/lib/junit-src.jar
new file mode 100644
index 0000000..4cde8b7
--- /dev/null
+++ b/lib/junit-src.jar
Binary files differ
diff --git a/tck/com/googlecode/atinject/Candidate.java b/tck/com/googlecode/atinject/Candidate.java
deleted file mode 100644
index 6a707c2..0000000
--- a/tck/com/googlecode/atinject/Candidate.java
+++ /dev/null
@@ -1,48 +0,0 @@
-/*
- * Copyright (C) 2009 The JSR-330 Expert Group
- *
- * Licensed 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 com.googlecode.atinject;
-
-import com.googlecode.atinject.auto.Car;
-
-/**
- * An injector with the following configuration:
- *
- * <ul>
- * <li>{@link com.googlecode.atinject.auto.Car} is implemented by
- * {@link com.googlecode.atinject.auto.Convertible Convertible}.
- * <li>{@link com.googlecode.atinject.auto.Drivers @Drivers} {@link com.googlecode.atinject.auto.Seat Seat} is
- * implemented by {@link com.googlecode.atinject.auto.DriversSeat DriversSeat}.
- * <li>{@link com.googlecode.atinject.auto.Engine Engine} is implemented by
- * {@link com.googlecode.atinject.auto.V8Engine V8Engine}.
- * <li>{@link javax.inject.Named @Named("spare")} {@link com.googlecode.atinject.auto.Tire Tire} is implemented by
- * {@link com.googlecode.atinject.auto.accessories.SpareTire SpareTire}.
- * <li>The following concrete classes may also be injected: {@link com.googlecode.atinject.auto.accessories.Cupholder
- * Cupholder}, {@link com.googlecode.atinject.auto.Tire Tire} and {@link com.googlecode.atinject.auto.FuelTank
- * FuelTank}.
- * </ul>
- *
- * <p>The static members of the following types shall also be injected: {@link com.googlecode.atinject.auto.Convertible
- * Convertible}, {@link com.googlecode.atinject.auto.Tire Tire}, and {@link
- * com.googlecode.atinject.auto.accessories.SpareTire SpareTire}.
- */
-public interface Candidate {
-
- /**
- * Returns an injected car instance.
- */
- public Car getCar();
-}
diff --git a/tck/com/googlecode/atinject/Tck.java b/tck/com/googlecode/atinject/Tck.java
index 7392481..1bbb31e 100644
--- a/tck/com/googlecode/atinject/Tck.java
+++ b/tck/com/googlecode/atinject/Tck.java
@@ -17,55 +17,113 @@
package com.googlecode.atinject;
import com.googlecode.atinject.auto.Car;
+import com.googlecode.atinject.auto.Convertible;
-public class Tck {
+import junit.framework.Test;
+import junit.framework.TestResult;
+import junit.framework.TestSuite;
- private final Candidate candidate;
-
- public Tck(Candidate candidate) {
- this.candidate = candidate;
- }
+/**
+ * Extend this class, implement {@link #getCar()}, and declare a static
+ * {@code suite} method (a JUnit convention):
+ *
+ * <pre>
+ * public class MyTck extends Tck {
+ * protected Car getCar() {
+ * return new MyInjector().getInstance(Car.class);
+ * }
+ * public static Test suite() {
+ * return new MyTck();
+ * }
+ * }
+ * </pre>
+ *
+ * <p>Then, run the tests using JUnit. For example:
+ *
+ * <pre>
+ * java junit.textui.TestRunner MyTck
+ * </pre>
+ */
+public abstract class Tck implements Test {
- private boolean testCompatibility() {
- Tester tester = new Tester();
+ private final Test delegate;
+ protected Tck() {
Car car;
try {
- car = candidate.getCar();
- } catch (Throwable e) {
- System.out.println("FAIL!");
- e.printStackTrace();
- return false;
+ car = getCar();
+ } catch (Throwable t) {
+ delegate = new Failure("getCar() threw an exception", t);
+ return;
}
if (car == null) {
- tester.addProblem("Null returned from Candidate.getCar()");
- } else {
- car.check(tester);
+ delegate = new Failure("getCar() returned null",
+ new NullPointerException());
+ return;
}
- if (!tester.hasProblems()) {
- System.out.println("SUCCESS!");
- return true;
+ if (!(car instanceof Convertible)) {
+ delegate = new Failure(
+ "getCar() did not return an instance of Convertible",
+ new ClassCastException("Expected: Convertible, got: "
+ + car.getClass().getName()));
+ return;
}
- System.out.println("FAIL!");
- for (String problem : tester.problems()) {
- System.out.println(problem);
- }
- return false;
+ Convertible.Test.car = (Convertible) car;
+ delegate = new TestSuite(Convertible.Test.class);
}
- public static void main(String[] args)
- throws ClassNotFoundException, IllegalAccessException, InstantiationException {
- if (args.length != 1) {
- System.out.println("Usage: Tck <candidate>");
- return;
- }
+ public int countTestCases() {
+ return delegate.countTestCases();
+ }
+
+ public void run(TestResult result) {
+ delegate.run(result);
+ }
- Class<?> c = Class.forName(args[0]);
- Candidate candidate = (Candidate) c.newInstance();
- boolean success = new Tck(candidate).testCompatibility();
- System.exit(success ? 0 : 1);
+ /**
+ * Returns a {@link com.googlecode.atinject.auto.Car} constructed by an
+ * injector with the following configuration:
+ *
+ * <ul>
+ * <li>{@link com.googlecode.atinject.auto.Car} is implemented by
+ * {@link com.googlecode.atinject.auto.Convertible Convertible}.
+ * <li>{@link com.googlecode.atinject.auto.Drivers @Drivers} {@link com.googlecode.atinject.auto.Seat Seat} is
+ * implemented by {@link com.googlecode.atinject.auto.DriversSeat DriversSeat}.
+ * <li>{@link com.googlecode.atinject.auto.Engine Engine} is implemented by
+ * {@link com.googlecode.atinject.auto.V8Engine V8Engine}.
+ * <li>{@link javax.inject.Named @Named("spare")} {@link com.googlecode.atinject.auto.Tire Tire} is implemented by
+ * {@link com.googlecode.atinject.auto.accessories.SpareTire SpareTire}.
+ * <li>The following concrete classes may also be injected: {@link com.googlecode.atinject.auto.accessories.Cupholder
+ * Cupholder}, {@link com.googlecode.atinject.auto.Tire Tire} and {@link com.googlecode.atinject.auto.FuelTank
+ * FuelTank}.
+ * </ul>
+ *
+ * <p>The static members of the following types shall also be injected: {@link com.googlecode.atinject.auto.Convertible
+ * Convertible}, {@link com.googlecode.atinject.auto.Tire Tire}, and {@link
+ * com.googlecode.atinject.auto.accessories.SpareTire SpareTire}.
+ */
+ protected abstract Car getCar();
+
+ static class Failure implements Test {
+ final String message;
+ final Throwable t;
+ Failure(String message, Throwable t) {
+ this.message = message;
+ this.t = t;
+ }
+ public int countTestCases() {
+ return 1;
+ }
+ public void run(TestResult result) {
+ result.startTest(this);
+ result.addError(this, t);
+ result.endTest(this);
+ }
+ @Override public String toString() {
+ return message;
+ }
}
}
diff --git a/tck/com/googlecode/atinject/auto/Convertible.java b/tck/com/googlecode/atinject/auto/Convertible.java
index fdb2181..87ae97d 100644
--- a/tck/com/googlecode/atinject/auto/Convertible.java
+++ b/tck/com/googlecode/atinject/auto/Convertible.java
@@ -20,6 +20,8 @@ import com.googlecode.atinject.Tester;
import com.googlecode.atinject.auto.accessories.SpareTire;
import com.googlecode.atinject.auto.accessories.Cupholder;
+import junit.framework.TestCase;
+
import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Provider;
@@ -258,4 +260,18 @@ public class Convertible implements Car {
"Same instance returned by repeated calls to Provider.get()");
}
}
+
+ /**
+ * Tests against the Convertible instance.
+ *
+ * @see #car
+ */
+ public static class Test extends TestCase {
+
+ /**
+ * The instance to test. Making it static isn't ideal, but it saves
+ * us from having to repeat a boatload of JUnit code.
+ */
+ public static Convertible car;
+ }
}
diff --git a/tck/tck.iml b/tck/tck.iml
index 1a82711..b566c3d 100644
--- a/tck/tck.iml
+++ b/tck/tck.iml
@@ -15,7 +15,9 @@
<root url="jar://$MODULE_DIR$/../lib/junit.jar!/" />
</CLASSES>
<JAVADOC />
- <SOURCES />
+ <SOURCES>
+ <root url="jar://$MODULE_DIR$/../lib/junit-src.jar!/" />
+ </SOURCES>
</library>
</orderEntry>
</component>