Added:
/trunk/dependency-shot-test/src/main/java/cx/ath/mancel01/dependencyshot/test/junit/BinderSource.java
/trunk/dependency-shot-test/src/main/java/cx/ath/mancel01/dependencyshot/test/junit/DependencyShot.java
Deleted:
/trunk/dependency-shot-test/src/main/java/cx/ath/mancel01/dependencyshot/test/junit/ConfigureWith.java
/trunk/dependency-shot-test/src/main/java/cx/ath/mancel01/dependencyshot/test/junit/DependencyShotRunner.java
Modified:
/trunk/dependency-shot-test/src/test/java/cx/ath/mancel01/dependencyshot/test/TCKFluentTest.java
=======================================
--- /dev/null
+++
/trunk/dependency-shot-test/src/main/java/cx/ath/mancel01/dependencyshot/test/junit/BinderSource.java
Thu Apr 7 09:12:54 2011
@@ -0,0 +1,29 @@
+/*
+ * Copyright 2010 mathieuancelin.
+ *
+ * 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.
+ * under the License.
+ */
+
+package cx.ath.mancel01.dependencyshot.test.junit;
+
+import java.lang.annotation.*;
+import static java.lang.annotation.ElementType.*;
+import static java.lang.annotation.RetentionPolicy.*;
+
+@Documented
+@Retention (RUNTIME)
+@Target(METHOD)
+public @interface BinderSource {
+ boolean testStaging() default false;
+}
=======================================
--- /dev/null
+++
/trunk/dependency-shot-test/src/main/java/cx/ath/mancel01/dependencyshot/test/junit/DependencyShot.java
Thu Apr 7 09:12:54 2011
@@ -0,0 +1,89 @@
+/*
+ * Copyright 2010 mathieuancelin.
+ *
+ * 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.
+ * under the License.
+ */
+package cx.ath.mancel01.dependencyshot.test.junit;
+
+import cx.ath.mancel01.dependencyshot.api.DSInjector;
+import cx.ath.mancel01.dependencyshot.api.Stage;
+import cx.ath.mancel01.dependencyshot.graph.Binder;
+import java.lang.reflect.Method;
+import java.lang.reflect.Modifier;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+import org.junit.runners.BlockJUnit4ClassRunner;
+import org.junit.runners.model.InitializationError;
+
+/**
+ * JUnit code runner that allow dependency injection in JUnit tests.
+ *
+ * @author Mathieu ANCELIN
+ */
+public class DependencyShot extends BlockJUnit4ClassRunner {
+
+ private static final Logger logger =
Logger.getLogger(DependencyShot.class.getSimpleName());
+ private Class<?> clazz;
+ private DSInjector injector;
+
+ public DependencyShot(Class<?> clazz) throws InitializationError {
+ super(clazz);
+ this.clazz = clazz;
+ boolean staging = false;
+ Collection<Binder> binders = new ArrayList<Binder>();
+ for (Method m : clazz.getDeclaredMethods()) {
+ if (Modifier.isStatic(m.getModifiers()) &&
m.isAnnotationPresent(BinderSource.class)) {
+ BinderSource source = m.getAnnotation(BinderSource.class);
+ if (source.testStaging()) {
+ staging = true;
+ }
+ if (Collection.class.isAssignableFrom(m.getReturnType())) {
+ try {
+ binders.addAll((Collection<Binder>)
m.invoke(null));
+ } catch (Exception ex) {
+ logger.log(Level.SEVERE, null, ex);
+ }
+ } else if
(Binder.class.isAssignableFrom(m.getReturnType())) {
+ try {
+ binders.add((Binder) m.invoke(null));
+ } catch (Exception ex) {
+ logger.log(Level.SEVERE, null, ex);
+ }
+ } else {
+ throw new RuntimeException("Your @BinderSource static
method should return a Binder or a collection of binders.");
+ }
+ }
+ }
+ if (staging) {
+ injector =
cx.ath.mancel01.dependencyshot.DependencyShot.getInjector(
+ Stage.TEST, binders.toArray(new Binder[]{}));
+ } else {
+ injector =
cx.ath.mancel01.dependencyshot.DependencyShot.getInjector(
+ binders.toArray(new Binder[]{}));
+ }
+ injector.allowCircularDependencies(true);
+ System.out.println(new StringBuilder()
+ .append("Running test class \"")
+ .append(clazz.getSimpleName())
+ .append(".java\" with Dependency-Shot container ...")
+ .toString());
+ }
+
+ @Override
+ protected Object createTest() throws Exception {
+ return injector.getInstance(getTestClass().getJavaClass());
+ }
+}
=======================================
---
/trunk/dependency-shot-test/src/main/java/cx/ath/mancel01/dependencyshot/test/junit/ConfigureWith.java
Fri Aug 6 05:25:36 2010
+++ /dev/null
@@ -1,35 +0,0 @@
-/*
- * Copyright 2010 mathieuancelin.
- *
- * 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.
- * under the License.
- */
-
-package cx.ath.mancel01.dependencyshot.test.junit;
-
-import cx.ath.mancel01.dependencyshot.graph.Binder;
-import java.lang.annotation.*;
-import static java.lang.annotation.ElementType.*;
-import static java.lang.annotation.RetentionPolicy.*;
-
-/**
- *
- */
-@Documented
-@Retention (RUNTIME)
-@Target(TYPE)
-public @interface ConfigureWith {
- Class<? extends Binder>[] value();
- boolean staging() default false;
- boolean allowCircularDependencies() default false;
-}
=======================================
---
/trunk/dependency-shot-test/src/main/java/cx/ath/mancel01/dependencyshot/test/junit/DependencyShotRunner.java
Fri Aug 6 05:26:12 2010
+++ /dev/null
@@ -1,80 +0,0 @@
-/*
- * Copyright 2010 mathieuancelin.
- *
- * 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.
- * under the License.
- */
-
-package cx.ath.mancel01.dependencyshot.test.junit;
-
-import cx.ath.mancel01.dependencyshot.DependencyShot;
-import cx.ath.mancel01.dependencyshot.api.DSInjector;
-import cx.ath.mancel01.dependencyshot.api.Stage;
-import cx.ath.mancel01.dependencyshot.graph.Binder;
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.logging.Level;
-import java.util.logging.Logger;
-import org.junit.runners.BlockJUnit4ClassRunner;
-import org.junit.runners.model.InitializationError;
-
-/**
- * JUnit code runner that allow dependency injection in JUnit tests.
- *
- * @author Mathieu ANCELIN
- */
-public class DependencyShotRunner extends BlockJUnit4ClassRunner {
-
- private static final Logger logger =
Logger.getLogger(DependencyShotRunner.class.getSimpleName());
-
- private Class<?> clazz;
-
- private DSInjector injector;
-
- public DependencyShotRunner(Class<?> clazz) throws InitializationError
{
- super(clazz);
- this.clazz = clazz;
- ConfigureWith config = clazz.getAnnotation(ConfigureWith.class);
- if (config != null) {
- Collection<Binder> binders = new ArrayList<Binder>();
- for (Class cl : config.value()) {
- try {
- binders.add((Binder) cl.newInstance());
- } catch (Exception ex) {
- logger.log(Level.SEVERE, null, ex);
- }
- }
- Binder[] bins = new Binder[binders.size()];
- if (config.staging()) {
- injector = DependencyShot.getInjector(Stage.TEST,
binders.toArray(bins));
- } else {
- injector =
DependencyShot.getInjector(binders.toArray(bins));
- }
- } else {
- injector = DependencyShot.getInjector();
- }
- if (config.allowCircularDependencies()) {
- injector.allowCircularDependencies(true);
- }
- logger.info(new StringBuilder()
- .append("Running test class \"")
- .append(clazz.getSimpleName())
- .append(".java\" with Dependency-Shot container ...")
- .toString());
- }
-
- @Override
- protected Object createTest() throws Exception {
- return injector.getInstance(getTestClass().getJavaClass());
- }
-}
=======================================
---
/trunk/dependency-shot-test/src/test/java/cx/ath/mancel01/dependencyshot/test/TCKFluentTest.java
Fri Aug 6 05:25:36 2010
+++
/trunk/dependency-shot-test/src/test/java/cx/ath/mancel01/dependencyshot/test/TCKFluentTest.java
Thu Apr 7 09:12:54 2011
@@ -17,10 +17,11 @@
package cx.ath.mancel01.dependencyshot.test;
-import cx.ath.mancel01.dependencyshot.test.junit.ConfigureWith;
-import cx.ath.mancel01.dependencyshot.test.junit.DependencyShotRunner;
+import org.junit.Test;
+import cx.ath.mancel01.dependencyshot.graph.Binder;
+import cx.ath.mancel01.dependencyshot.test.junit.BinderSource;
+import cx.ath.mancel01.dependencyshot.test.junit.DependencyShot;
import javax.inject.Inject;
-import junit.framework.Test;
import junit.framework.TestResult;
import org.atinject.tck.Tck;
import org.atinject.tck.auto.Car;
@@ -32,16 +33,20 @@
*
* @author Mathieu ANCELIN
*/
-@RunWith(DependencyShotRunner.class)
-@ConfigureWith(TCKFluentBinder.class)
+@RunWith(DependencyShot.class)
public class TCKFluentTest {
+
+ @BinderSource
+ public static Binder getBinder() {
+ return new TCKFluentBinder();
+ }
@Inject
private Car car;
- @org.junit.Test
+ @Test
public void passTCK() {
- Test test = Tck.testsFor(car, false, true);
+ junit.framework.Test test = Tck.testsFor(car, false, true);
TestResult result = new TestResult();
result.startTest(test);
assertTrue(result.wasSuccessful());