Repository: tapestry-5 Updated Branches: refs/heads/master 4780b04b5 -> 04ea6d0e5
TAP5-2273 - Rename tapestry-ioc-test to tapestry-ioc-junit Project: http://git-wip-us.apache.org/repos/asf/tapestry-5/repo Commit: http://git-wip-us.apache.org/repos/asf/tapestry-5/commit/04ea6d0e Tree: http://git-wip-us.apache.org/repos/asf/tapestry-5/tree/04ea6d0e Diff: http://git-wip-us.apache.org/repos/asf/tapestry-5/diff/04ea6d0e Branch: refs/heads/master Commit: 04ea6d0e531dace325d6d0e53763ebf52e454126 Parents: 4780b04 Author: uklance <[email protected]> Authored: Tue Jun 17 20:01:37 2014 +0100 Committer: uklance <[email protected]> Committed: Tue Jun 17 20:02:31 2014 +0100 ---------------------------------------------------------------------- settings.gradle | 2 +- tapestry-ioc-junit/build.gradle | 10 ++ .../apache/tapestry5/ioc/junit/ModuleDef.java | 33 ++++ .../apache/tapestry5/ioc/junit/Registry.java | 41 +++++ .../ioc/junit/RegistryShutdownType.java | 30 ++++ .../ioc/junit/TapestryIOCJUnit4ClassRunner.java | 152 ++++++++++++++++++ .../ioc/junit/TestRegistryManager.java | 131 ++++++++++++++++ .../tapestry5/ioc/junit/MapModuleDef.java | 116 ++++++++++++++ ...estryIOCJUnit4ClassRunnerAfterClassTest.java | 63 ++++++++ ...stryIOCJUnit4ClassRunnerAfterMethodTest.java | 65 ++++++++ ...pestryIOCJUnit4ClassRunnerModuleDefTest.java | 69 +++++++++ tapestry-ioc-test/build.gradle | 10 -- .../apache/tapestry5/ioc/test/ModuleDef.java | 33 ---- .../org/apache/tapestry5/ioc/test/Registry.java | 41 ----- .../ioc/test/RegistryShutdownType.java | 30 ---- .../ioc/test/TapestryIOCJUnit4ClassRunner.java | 153 ------------------- .../tapestry5/ioc/test/TestRegistryManager.java | 131 ---------------- .../apache/tapestry5/ioc/test/MapModuleDef.java | 116 -------------- ...estryIOCJUnit4ClassRunnerAfterClassTest.java | 61 -------- ...stryIOCJUnit4ClassRunnerAfterMethodTest.java | 63 -------- ...pestryIOCJUnit4ClassRunnerModuleDefTest.java | 66 -------- 21 files changed, 711 insertions(+), 705 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/04ea6d0e/settings.gradle ---------------------------------------------------------------------- diff --git a/settings.gradle b/settings.gradle index 5f1ddf9..bf87d6f 100644 --- a/settings.gradle +++ b/settings.gradle @@ -2,6 +2,6 @@ include "plastic", "tapestry5-annotations", "tapestry-test", "tapestry-func", "t include "tapestry-hibernate-core", "tapestry-hibernate", "tapestry-jmx", "tapestry-upload", "tapestry-spring" include "tapestry-beanvalidator", "tapestry-jpa", "tapestry-kaptcha" include "tapestry-javadoc", "quickstart", "tapestry-clojure", "tapestry-mongodb" -include "tapestry-test-data", 'tapestry-internal-test', "tapestry-ioc-test" +include "tapestry-test-data", 'tapestry-internal-test', "tapestry-ioc-junit" include "tapestry-webresources", "tapestry-runner", "tapestry-test-constants" // include "tapestry-cdi" http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/04ea6d0e/tapestry-ioc-junit/build.gradle ---------------------------------------------------------------------- diff --git a/tapestry-ioc-junit/build.gradle b/tapestry-ioc-junit/build.gradle new file mode 100644 index 0000000..6b2b625 --- /dev/null +++ b/tapestry-ioc-junit/build.gradle @@ -0,0 +1,10 @@ +description = "Utilities for junit testing a tapestry-ioc application" + +dependencies { + compile project(':tapestry-ioc') + compile 'junit:junit:4.11' +} + +test { + useJUnit() +} http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/04ea6d0e/tapestry-ioc-junit/src/main/java/org/apache/tapestry5/ioc/junit/ModuleDef.java ---------------------------------------------------------------------- diff --git a/tapestry-ioc-junit/src/main/java/org/apache/tapestry5/ioc/junit/ModuleDef.java b/tapestry-ioc-junit/src/main/java/org/apache/tapestry5/ioc/junit/ModuleDef.java new file mode 100644 index 0000000..aa8a20e --- /dev/null +++ b/tapestry-ioc-junit/src/main/java/org/apache/tapestry5/ioc/junit/ModuleDef.java @@ -0,0 +1,33 @@ +// Copyright 2006, 2007, 2008, 2009, 2010, 2011, 2012 The Apache Software Foundation +// +// 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 org.apache.tapestry5.ioc.junit; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Inherited; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * Marker annotation for a method which creates a {@link org.apache.tapestry5.ioc.def.ModuleDef}. + * Used by the {@link TapestryIOCJUnit4ClassRunner}, methods with this annotation must be public, static, no-args methods + * which return {@link org.apache.tapestry5.ioc.def.ModuleDef} (or a subclass) + */ +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.METHOD) +@Inherited +public @interface ModuleDef { + +} http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/04ea6d0e/tapestry-ioc-junit/src/main/java/org/apache/tapestry5/ioc/junit/Registry.java ---------------------------------------------------------------------- diff --git a/tapestry-ioc-junit/src/main/java/org/apache/tapestry5/ioc/junit/Registry.java b/tapestry-ioc-junit/src/main/java/org/apache/tapestry5/ioc/junit/Registry.java new file mode 100644 index 0000000..55f3e1a --- /dev/null +++ b/tapestry-ioc-junit/src/main/java/org/apache/tapestry5/ioc/junit/Registry.java @@ -0,0 +1,41 @@ +// Copyright 2006, 2007, 2008, 2009, 2010, 2011, 2012 The Apache Software Foundation +// +// 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 org.apache.tapestry5.ioc.junit; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Inherited; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * Used by the {@link TapestryIOCJUnit4ClassRunner} to configure a test {@link org.apache.tapestry5.ioc.Registry} + */ +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.TYPE) +@Inherited +public @interface Registry { + /** + * Tapestry module classes + * @return module class array + */ + Class<?>[] modules(); + + /** + * Determines the registry lifecycle + * @return RegistryShutdownType + */ + RegistryShutdownType shutdown() default RegistryShutdownType.AFTER_CLASS; +} http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/04ea6d0e/tapestry-ioc-junit/src/main/java/org/apache/tapestry5/ioc/junit/RegistryShutdownType.java ---------------------------------------------------------------------- diff --git a/tapestry-ioc-junit/src/main/java/org/apache/tapestry5/ioc/junit/RegistryShutdownType.java b/tapestry-ioc-junit/src/main/java/org/apache/tapestry5/ioc/junit/RegistryShutdownType.java new file mode 100644 index 0000000..468dd86 --- /dev/null +++ b/tapestry-ioc-junit/src/main/java/org/apache/tapestry5/ioc/junit/RegistryShutdownType.java @@ -0,0 +1,30 @@ +// Copyright 2006, 2007, 2008, 2009, 2010, 2011, 2012 The Apache Software Foundation +// +// 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 org.apache.tapestry5.ioc.junit; + +/** + * Used in conjunction with the {@link TapestryIOCJUnit4ClassRunner} to determine the registry lifecycle + */ +public enum RegistryShutdownType { + /** + * Test registry will be shut down once per test class + */ + AFTER_CLASS, + + /** + * Test registry will be shut down after each test method + */ + AFTER_METHOD; +} http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/04ea6d0e/tapestry-ioc-junit/src/main/java/org/apache/tapestry5/ioc/junit/TapestryIOCJUnit4ClassRunner.java ---------------------------------------------------------------------- diff --git a/tapestry-ioc-junit/src/main/java/org/apache/tapestry5/ioc/junit/TapestryIOCJUnit4ClassRunner.java b/tapestry-ioc-junit/src/main/java/org/apache/tapestry5/ioc/junit/TapestryIOCJUnit4ClassRunner.java new file mode 100644 index 0000000..5b47317 --- /dev/null +++ b/tapestry-ioc-junit/src/main/java/org/apache/tapestry5/ioc/junit/TapestryIOCJUnit4ClassRunner.java @@ -0,0 +1,152 @@ +// Copyright 2006, 2007, 2008, 2009, 2010, 2011, 2012 The Apache Software Foundation +// +// 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 org.apache.tapestry5.ioc.junit; + +import org.apache.tapestry5.ioc.annotations.Inject; +import org.apache.tapestry5.ioc.def.ModuleDef; +import org.junit.runner.Description; +import org.junit.runner.Result; +import org.junit.runner.notification.Failure; +import org.junit.runner.notification.RunListener; +import org.junit.runner.notification.RunNotifier; +import org.junit.runner.notification.StoppedByUserException; +import org.junit.runners.BlockJUnit4ClassRunner; +import org.junit.runners.model.InitializationError; +import org.junit.runners.model.Statement; + +/** + * <p> + * A JUnit4ClassRunner to help with Tapestry IOC integration tests. The test + * runner requires a registry configuration to be defined in a {@link Registry} + * annotation. A {@link RegistryShutdownType} can be specified to configure the + * lifecycle of the test registry and it's services + * </p> + * + * <p> + * {@link org.apache.tapestry5.ioc.junit.ModuleDef}s can be added to the + * {@link org.apache.tapestry5.ioc.Registry} by annotating a factory method(s) + * with {@link ModuleDef}. These {@link ModuleDef} factory methods must be + * <ul> + * <li>public</li> + * <li>static</li> + * <li>take zero arguments</li> + * <li>return a subclass of {@link org.apache.tapestry5.ioc.junit.ModuleDef}</li> + * </ul> + * </p> + * + * <p> + * Any services defined in the registry can be {@link Inject}ed into the test + * class to be used during testing. + * </p> + */ +public class TapestryIOCJUnit4ClassRunner extends BlockJUnit4ClassRunner { + private final TestRegistryManager registryManager; + + public TapestryIOCJUnit4ClassRunner(Class<?> type) throws InitializationError { + super(type); + this.registryManager = new TestRegistryManager(type); + } + + @Override + public void run(RunNotifier notifier) { + RunNotifier wrapper = new RegistryManagerRunNotifier(registryManager, notifier); + super.run(wrapper); + } + + @Override + protected Statement withAfterClasses(Statement statement) { + final Statement superStatement = super.withAfterClasses(statement); + return new Statement() { + @Override + public void evaluate() throws Throwable { + superStatement.evaluate(); + registryManager.afterTestClass(); + } + }; + } + + @Override + protected Object createTest() throws Exception { + org.apache.tapestry5.ioc.Registry registry = registryManager.getOrCreateRegistry(); + return registry.autobuild(getTestClass().getJavaClass()); + } + + public static class RegistryManagerRunNotifier extends RunNotifier { + private final RunNotifier delegate; + private final TestRegistryManager registryManager; + + public RegistryManagerRunNotifier(TestRegistryManager registryManager, RunNotifier delegate) { + super(); + this.delegate = delegate; + this.registryManager = registryManager; + } + + @Override + public void addListener(RunListener listener) { + delegate.addListener(listener); + } + + @Override + public void removeListener(RunListener listener) { + delegate.removeListener(listener); + } + + @Override + public void fireTestRunStarted(Description description) { + delegate.fireTestRunStarted(description); + } + + @Override + public void fireTestRunFinished(Result result) { + delegate.fireTestRunFinished(result); + } + + @Override + public void fireTestStarted(Description description) throws StoppedByUserException { + delegate.fireTestStarted(description); + } + + @Override + public void fireTestFailure(Failure failure) { + delegate.fireTestFailure(failure); + } + + @Override + public void fireTestAssumptionFailed(Failure failure) { + delegate.fireTestAssumptionFailed(failure); + } + + @Override + public void fireTestIgnored(Description description) { + delegate.fireTestIgnored(description); + } + + @Override + public void fireTestFinished(Description description) { + registryManager.afterTestMethod(); + delegate.fireTestFinished(description); + } + + @Override + public void pleaseStop() { + delegate.pleaseStop(); + } + + @Override + public void addFirstListener(RunListener listener) { + delegate.addFirstListener(listener); + } + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/04ea6d0e/tapestry-ioc-junit/src/main/java/org/apache/tapestry5/ioc/junit/TestRegistryManager.java ---------------------------------------------------------------------- diff --git a/tapestry-ioc-junit/src/main/java/org/apache/tapestry5/ioc/junit/TestRegistryManager.java b/tapestry-ioc-junit/src/main/java/org/apache/tapestry5/ioc/junit/TestRegistryManager.java new file mode 100644 index 0000000..fd8ac45 --- /dev/null +++ b/tapestry-ioc-junit/src/main/java/org/apache/tapestry5/ioc/junit/TestRegistryManager.java @@ -0,0 +1,131 @@ +// Copyright 2006, 2007, 2008, 2009, 2010, 2011, 2012 The Apache Software Foundation +// +// 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 org.apache.tapestry5.ioc.junit; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.lang.reflect.Modifier; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +import org.apache.tapestry5.ioc.RegistryBuilder; +import org.junit.runners.model.InitializationError; + +/** + * Helper class used by the {@link TapestryIOCJUnit4ClassRunner} to manage the test registry + */ +public class TestRegistryManager { + private final Registry annotation; + private final List<Method> moduleDefFactories; + + private org.apache.tapestry5.ioc.Registry registry; + + public TestRegistryManager(Class<?> type) throws InitializationError { + super(); + + Registry annotation = type.getAnnotation(Registry.class); + if (annotation == null) { + throw new InitializationError(type.getName() + " does not specify a @Registry"); + } + + this.annotation = annotation; + this.moduleDefFactories = findModuleDefFactories(type); + } + + protected List<Method> findModuleDefFactories(Class<?> type) throws InitializationError { + List<Method> factoryMethods = new ArrayList<Method>(); + for (Method method : type.getMethods()) { + if (method.getAnnotation(ModuleDef.class) != null) { + validateModuleDefMethod(method); + factoryMethods.add(method); + } + } + return factoryMethods.isEmpty() ? Collections.<Method> emptyList() : factoryMethods; + } + + protected void validateModuleDefMethod(Method method) throws InitializationError { + int modifiers = method.getModifiers(); + if (method.getParameterTypes().length != 0 + || !Modifier.isStatic(modifiers) + || !Modifier.isPublic(modifiers)) { + + throw new InitializationError( + String.format("@ModuleDef method %s must be public static and accept no arguments", + method.getName())); + } + if (!org.apache.tapestry5.ioc.def.ModuleDef.class.isAssignableFrom(method.getReturnType())) { + throw new InitializationError( + String.format("@ModuleDef method %s return type %s is not valid", + method.getName(), method.getReturnType().getName())); + } + } + + /** + * Get the existing registry or create one if required. + * @return The test Registry + * @throws Exception + */ + public org.apache.tapestry5.ioc.Registry getOrCreateRegistry() throws Exception { + if (registry == null) { + RegistryBuilder builder = new RegistryBuilder(); + if (annotation.modules() != null) { + builder.add(annotation.modules()); + } + for (Method moduleDefFactory : moduleDefFactories) { + try { + org.apache.tapestry5.ioc.def.ModuleDef moduleDef = + (org.apache.tapestry5.ioc.def.ModuleDef) moduleDefFactory.invoke(null); + + builder.add(moduleDef); + } catch (InvocationTargetException e) { + if (e.getTargetException() instanceof Exception) { + throw (Exception) e.getTargetException(); + } + throw e; + } + } + registry = builder.build(); + registry.performRegistryStartup(); + } + return registry; + } + + /** + * Notify that the current test method has completed + */ + public void afterTestMethod() { + if (annotation.shutdown() == RegistryShutdownType.AFTER_METHOD) { + shutdownRegistry(); + } + } + + /** + * Notify that the current test class has completed + */ + public void afterTestClass() { + if (annotation.shutdown() == RegistryShutdownType.AFTER_CLASS) { + shutdownRegistry(); + } + } + + protected void shutdownRegistry() { + try { + registry.shutdown(); + } finally { + registry = null; + } + } +} http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/04ea6d0e/tapestry-ioc-junit/src/test/java/org/apache/tapestry5/ioc/junit/MapModuleDef.java ---------------------------------------------------------------------- diff --git a/tapestry-ioc-junit/src/test/java/org/apache/tapestry5/ioc/junit/MapModuleDef.java b/tapestry-ioc-junit/src/test/java/org/apache/tapestry5/ioc/junit/MapModuleDef.java new file mode 100644 index 0000000..7f750dc --- /dev/null +++ b/tapestry-ioc-junit/src/test/java/org/apache/tapestry5/ioc/junit/MapModuleDef.java @@ -0,0 +1,116 @@ +// Copyright 2006, 2007, 2008, 2009, 2010, 2011, 2012 The Apache Software Foundation +// +// 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 org.apache.tapestry5.ioc.junit; + +import java.util.Collections; +import java.util.Map; +import java.util.Set; + +import org.apache.tapestry5.ioc.ObjectCreator; +import org.apache.tapestry5.ioc.ScopeConstants; +import org.apache.tapestry5.ioc.ServiceBuilderResources; +import org.apache.tapestry5.ioc.def.ContributionDef; +import org.apache.tapestry5.ioc.def.DecoratorDef; +import org.apache.tapestry5.ioc.def.ModuleDef; +import org.apache.tapestry5.ioc.def.ServiceDef; + +/** + * Test ModuleDef implementation based on a {@link Map} + */ +@SuppressWarnings("rawtypes") +public class MapModuleDef implements ModuleDef { + private final Map<String, Object> map; + + public MapModuleDef(Map<String, Object> map) { + super(); + this.map = map; + } + + @Override + public Class getBuilderClass() { + return null; + } + + @Override + public Set<ContributionDef> getContributionDefs() { + return Collections.emptySet(); + } + + @Override + public Set<DecoratorDef> getDecoratorDefs() { + return Collections.emptySet(); + } + + @Override + public String getLoggerName() { + return "MapModuleDef"; + } + + @Override + public Set<String> getServiceIds() { + return map.keySet(); + } + + @Override + public ServiceDef getServiceDef(String serviceId) { + return new MapServiceDef(map, serviceId); + } + + public static class MapServiceDef implements ServiceDef { + private final Map<String, Object> map; + private final String serviceId; + + public MapServiceDef(Map<String, Object> map, String serviceId) { + super(); + this.map = map; + this.serviceId = serviceId; + } + + @Override + public ObjectCreator createServiceCreator(ServiceBuilderResources resources) { + return new ObjectCreator() { + @Override + public Object createObject() { + return map.get(serviceId); + } + }; + } + + @Override + public String getServiceId() { + return serviceId; + } + + @Override + public Set<Class> getMarkers() { + return Collections.emptySet(); + } + + @Override + public Class getServiceInterface() { + return map.get(serviceId).getClass(); + } + + @Override + public String getServiceScope() { + return ScopeConstants.DEFAULT; + } + + @Override + public boolean isEagerLoad() { + return false; + } + } +} http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/04ea6d0e/tapestry-ioc-junit/src/test/java/org/apache/tapestry5/ioc/junit/TapestryIOCJUnit4ClassRunnerAfterClassTest.java ---------------------------------------------------------------------- diff --git a/tapestry-ioc-junit/src/test/java/org/apache/tapestry5/ioc/junit/TapestryIOCJUnit4ClassRunnerAfterClassTest.java b/tapestry-ioc-junit/src/test/java/org/apache/tapestry5/ioc/junit/TapestryIOCJUnit4ClassRunnerAfterClassTest.java new file mode 100644 index 0000000..67be2ef --- /dev/null +++ b/tapestry-ioc-junit/src/test/java/org/apache/tapestry5/ioc/junit/TapestryIOCJUnit4ClassRunnerAfterClassTest.java @@ -0,0 +1,63 @@ +// Copyright 2006, 2007, 2008, 2009, 2010, 2011, 2012 The Apache Software Foundation +// +// 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 org.apache.tapestry5.ioc.junit; + +import static org.apache.tapestry5.ioc.junit.RegistryShutdownType.AFTER_CLASS; +import static org.junit.Assert.assertArrayEquals; +import static org.junit.runners.MethodSorters.NAME_ASCENDING; + +import java.util.ArrayList; +import java.util.List; + +import org.apache.tapestry5.ioc.annotations.Inject; +import org.apache.tapestry5.ioc.junit.Registry; +import org.apache.tapestry5.ioc.junit.TapestryIOCJUnit4ClassRunner; +import org.apache.tapestry5.ioc.junit.TapestryIOCJUnit4ClassRunnerAfterClassTest.AfterClassTestModule; +import org.junit.FixMethodOrder; +import org.junit.Test; +import org.junit.runner.RunWith; + +@RunWith(TapestryIOCJUnit4ClassRunner.class) +@Registry(modules=AfterClassTestModule.class, shutdown=AFTER_CLASS) +@FixMethodOrder(NAME_ASCENDING) // guarantees test ordering +public class TapestryIOCJUnit4ClassRunnerAfterClassTest { + public static class AfterClassTestModule { + public List<String> buildList() { + List<String> list = new ArrayList<String>(); + list.add("foo"); + return list; + } + } + + @Inject + private List<String> list; + + @Test + public void testInjectA() { + assertArrayEquals(new Object[] { "foo" }, list.toArray()); + list.add("bar"); + } + + @Test + public void testInjectB() { + assertArrayEquals(new Object[] { "foo", "bar" }, list.toArray()); + list.add("baz"); + } + + @Test + public void testInjectC() { + assertArrayEquals(new Object[] { "foo", "bar", "baz" }, list.toArray()); + } +} http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/04ea6d0e/tapestry-ioc-junit/src/test/java/org/apache/tapestry5/ioc/junit/TapestryIOCJUnit4ClassRunnerAfterMethodTest.java ---------------------------------------------------------------------- diff --git a/tapestry-ioc-junit/src/test/java/org/apache/tapestry5/ioc/junit/TapestryIOCJUnit4ClassRunnerAfterMethodTest.java b/tapestry-ioc-junit/src/test/java/org/apache/tapestry5/ioc/junit/TapestryIOCJUnit4ClassRunnerAfterMethodTest.java new file mode 100644 index 0000000..acbaa81 --- /dev/null +++ b/tapestry-ioc-junit/src/test/java/org/apache/tapestry5/ioc/junit/TapestryIOCJUnit4ClassRunnerAfterMethodTest.java @@ -0,0 +1,65 @@ +// Copyright 2006, 2007, 2008, 2009, 2010, 2011, 2012 The Apache Software Foundation +// +// 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 org.apache.tapestry5.ioc.junit; + +import static org.apache.tapestry5.ioc.junit.RegistryShutdownType.AFTER_METHOD; +import static org.junit.Assert.assertArrayEquals; +import static org.junit.runners.MethodSorters.NAME_ASCENDING; + +import java.util.ArrayList; +import java.util.List; + +import org.apache.tapestry5.ioc.annotations.Inject; +import org.apache.tapestry5.ioc.junit.Registry; +import org.apache.tapestry5.ioc.junit.TapestryIOCJUnit4ClassRunner; +import org.apache.tapestry5.ioc.junit.TapestryIOCJUnit4ClassRunnerAfterMethodTest.AfterMethodTestModule; +import org.junit.FixMethodOrder; +import org.junit.Test; +import org.junit.runner.RunWith; + +@RunWith(TapestryIOCJUnit4ClassRunner.class) +@Registry(modules=AfterMethodTestModule.class, shutdown=AFTER_METHOD) +@FixMethodOrder(NAME_ASCENDING) // guarantees test ordering +public class TapestryIOCJUnit4ClassRunnerAfterMethodTest { + public static class AfterMethodTestModule { + public List<String> buildList() { + List<String> list = new ArrayList<String>(); + list.add("foo"); + return list; + } + } + + @Inject + private List<String> list; + + @Test + public void testInjectA() { + assertArrayEquals(new Object[] { "foo" }, list.toArray()); + + list.add("bar"); + } + + @Test + public void testInjectB() { + assertArrayEquals(new Object[] { "foo" }, list.toArray()); + + list.add("baz"); + } + + @Test + public void testInjectC() { + assertArrayEquals(new Object[] { "foo" }, list.toArray()); + } +} http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/04ea6d0e/tapestry-ioc-junit/src/test/java/org/apache/tapestry5/ioc/junit/TapestryIOCJUnit4ClassRunnerModuleDefTest.java ---------------------------------------------------------------------- diff --git a/tapestry-ioc-junit/src/test/java/org/apache/tapestry5/ioc/junit/TapestryIOCJUnit4ClassRunnerModuleDefTest.java b/tapestry-ioc-junit/src/test/java/org/apache/tapestry5/ioc/junit/TapestryIOCJUnit4ClassRunnerModuleDefTest.java new file mode 100644 index 0000000..3915584 --- /dev/null +++ b/tapestry-ioc-junit/src/test/java/org/apache/tapestry5/ioc/junit/TapestryIOCJUnit4ClassRunnerModuleDefTest.java @@ -0,0 +1,69 @@ +// Copyright 2006, 2007, 2008, 2009, 2010, 2011, 2012 The Apache Software Foundation +// +// 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 org.apache.tapestry5.ioc.junit; + +import static org.junit.Assert.assertEquals; + +import java.util.Date; +import java.util.HashMap; +import java.util.Map; + +import org.apache.tapestry5.ioc.annotations.Inject; +import org.apache.tapestry5.ioc.junit.ModuleDef; +import org.apache.tapestry5.ioc.junit.Registry; +import org.apache.tapestry5.ioc.junit.TapestryIOCJUnit4ClassRunner; +import org.apache.tapestry5.ioc.junit.TapestryIOCJUnit4ClassRunnerModuleDefTest.ModuleDefTestModule; +import org.junit.Test; +import org.junit.runner.RunWith; + +@RunWith(TapestryIOCJUnit4ClassRunner.class) +@Registry(modules=ModuleDefTestModule.class) +public class TapestryIOCJUnit4ClassRunnerModuleDefTest { + @ModuleDef + public static MapModuleDef createModuleDef1() { + Map<String,Object> map = new HashMap<String, Object>(); + map.put("a", new Date(111)); + map.put("b", 222L); + + return new MapModuleDef(map); + } + + @ModuleDef + public static org.apache.tapestry5.ioc.def.ModuleDef createModuleDef2() { + Map<String,Object> map = new HashMap<String, Object>(); + map.put("c", 333); + + return new MapModuleDef(map); + } + + public static class ModuleDefTestModule { + public String buildD() { + return "444"; + } + } + + @Inject private Date a; + @Inject private Long b; + @Inject private Integer c; + @Inject private String d; + + @Test + public void testModuleDefInject() { + assertEquals(new Date(111), a); + assertEquals(new Long(222L), b); + assertEquals(new Integer(333), c); + assertEquals("444", d); + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/04ea6d0e/tapestry-ioc-test/build.gradle ---------------------------------------------------------------------- diff --git a/tapestry-ioc-test/build.gradle b/tapestry-ioc-test/build.gradle deleted file mode 100644 index 36261dc..0000000 --- a/tapestry-ioc-test/build.gradle +++ /dev/null @@ -1,10 +0,0 @@ -description = "Utilities for integration testing a tapestry-ioc app" - -dependencies { - compile project(':tapestry-ioc') - compile 'junit:junit:4.11' -} - -test { - useJUnit() -} http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/04ea6d0e/tapestry-ioc-test/src/main/java/org/apache/tapestry5/ioc/test/ModuleDef.java ---------------------------------------------------------------------- diff --git a/tapestry-ioc-test/src/main/java/org/apache/tapestry5/ioc/test/ModuleDef.java b/tapestry-ioc-test/src/main/java/org/apache/tapestry5/ioc/test/ModuleDef.java deleted file mode 100644 index 529bad6..0000000 --- a/tapestry-ioc-test/src/main/java/org/apache/tapestry5/ioc/test/ModuleDef.java +++ /dev/null @@ -1,33 +0,0 @@ -// Copyright 2006, 2007, 2008, 2009, 2010, 2011, 2012 The Apache Software Foundation -// -// 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 org.apache.tapestry5.ioc.test; - -import java.lang.annotation.ElementType; -import java.lang.annotation.Inherited; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; - -/** - * Marker annotation for a method which creates a {@link org.apache.tapestry5.ioc.def.ModuleDef}. - * Used by the {@link TapestryIOCJUnit4ClassRunner}, methods with this annotation must be public, static, no-args methods - * which return {@link ModuleDef} (or a subclass) - */ -@Retention(RetentionPolicy.RUNTIME) -@Target(ElementType.METHOD) -@Inherited -public @interface ModuleDef { - -} http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/04ea6d0e/tapestry-ioc-test/src/main/java/org/apache/tapestry5/ioc/test/Registry.java ---------------------------------------------------------------------- diff --git a/tapestry-ioc-test/src/main/java/org/apache/tapestry5/ioc/test/Registry.java b/tapestry-ioc-test/src/main/java/org/apache/tapestry5/ioc/test/Registry.java deleted file mode 100644 index e11c995..0000000 --- a/tapestry-ioc-test/src/main/java/org/apache/tapestry5/ioc/test/Registry.java +++ /dev/null @@ -1,41 +0,0 @@ -// Copyright 2006, 2007, 2008, 2009, 2010, 2011, 2012 The Apache Software Foundation -// -// 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 org.apache.tapestry5.ioc.test; - -import java.lang.annotation.ElementType; -import java.lang.annotation.Inherited; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; - -/** - * Used by the {@link TapestryIOCJUnit4ClassRunner} to configure a test {@link org.apache.tapestry5.ioc.Registry} - */ -@Retention(RetentionPolicy.RUNTIME) -@Target(ElementType.TYPE) -@Inherited -public @interface Registry { - /** - * Tapestry module classes - * @return module class array - */ - Class<?>[] modules(); - - /** - * Determines the registry lifecycle - * @return RegistryShutdownType - */ - RegistryShutdownType shutdown() default RegistryShutdownType.AFTER_CLASS; -} http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/04ea6d0e/tapestry-ioc-test/src/main/java/org/apache/tapestry5/ioc/test/RegistryShutdownType.java ---------------------------------------------------------------------- diff --git a/tapestry-ioc-test/src/main/java/org/apache/tapestry5/ioc/test/RegistryShutdownType.java b/tapestry-ioc-test/src/main/java/org/apache/tapestry5/ioc/test/RegistryShutdownType.java deleted file mode 100644 index a457043..0000000 --- a/tapestry-ioc-test/src/main/java/org/apache/tapestry5/ioc/test/RegistryShutdownType.java +++ /dev/null @@ -1,30 +0,0 @@ -// Copyright 2006, 2007, 2008, 2009, 2010, 2011, 2012 The Apache Software Foundation -// -// 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 org.apache.tapestry5.ioc.test; - -/** - * @see TapestryIOCJUnit4ClassRunner - */ -public enum RegistryShutdownType { - /** - * Test registry will be shut down once per test class - */ - AFTER_CLASS, - - /** - * Test registry will be shut down after each test method - */ - AFTER_METHOD; -} http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/04ea6d0e/tapestry-ioc-test/src/main/java/org/apache/tapestry5/ioc/test/TapestryIOCJUnit4ClassRunner.java ---------------------------------------------------------------------- diff --git a/tapestry-ioc-test/src/main/java/org/apache/tapestry5/ioc/test/TapestryIOCJUnit4ClassRunner.java b/tapestry-ioc-test/src/main/java/org/apache/tapestry5/ioc/test/TapestryIOCJUnit4ClassRunner.java deleted file mode 100644 index 54d33af..0000000 --- a/tapestry-ioc-test/src/main/java/org/apache/tapestry5/ioc/test/TapestryIOCJUnit4ClassRunner.java +++ /dev/null @@ -1,153 +0,0 @@ -// Copyright 2006, 2007, 2008, 2009, 2010, 2011, 2012 The Apache Software Foundation -// -// 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 org.apache.tapestry5.ioc.test; - -import org.apache.tapestry5.ioc.annotations.Inject; -import org.apache.tapestry5.ioc.def.ModuleDef; -import org.junit.runner.Description; -import org.junit.runner.Result; -import org.junit.runner.notification.Failure; -import org.junit.runner.notification.RunListener; -import org.junit.runner.notification.RunNotifier; -import org.junit.runner.notification.StoppedByUserException; -import org.junit.runners.BlockJUnit4ClassRunner; -import org.junit.runners.model.InitializationError; -import org.junit.runners.model.Statement; - -/** - * <p> - * A JUnit4ClassRunner to help with Tapestry IOC integration tests. The test - * runner requires a registry configuration to be defined in a {@link Registry} - * annotation. A {@link RegistryShutdownType} can be specified to configure the - * lifecycle of the test registry and it's services - * </p> - * - * <p> - * {@link ModuleDef}s can be added to the - * {@link org.apache.tapestry5.ioc.Registry} by annotating a factory method(s) - * with {@link org.apache.tapestry5.ioc.test.ModuleDef}. These {@link ModuleDef} - * factory methods must be - * <ul> - * <li>public</li> - * <li>static</li> - * <li>take zero arguments</li> - * <li>return a subclass of {@link ModuleDef}</li> - * </ul> - * </p> - * - * <p> - * Any services defined in the registry can be {@link Inject}ed into the test - * class to be used during testing. - * </p> - */ -public class TapestryIOCJUnit4ClassRunner extends BlockJUnit4ClassRunner { - private final TestRegistryManager registryManager; - - public TapestryIOCJUnit4ClassRunner(Class<?> type) throws InitializationError { - super(type); - this.registryManager = new TestRegistryManager(type); - } - - @Override - public void run(RunNotifier notifier) { - RunNotifier wrapper = new RegistryManagerRunNotifier(registryManager, notifier); - super.run(wrapper); - } - - @Override - protected Statement withAfterClasses(Statement statement) { - final Statement superStatement = super.withAfterClasses(statement); - return new Statement() { - @Override - public void evaluate() throws Throwable { - superStatement.evaluate(); - registryManager.afterTestClass(); - } - }; - } - - @Override - protected Object createTest() throws Exception { - org.apache.tapestry5.ioc.Registry registry = registryManager.getOrCreateRegistry(); - return registry.autobuild(getTestClass().getJavaClass()); - } - - public static class RegistryManagerRunNotifier extends RunNotifier { - private final RunNotifier delegate; - private final TestRegistryManager registryManager; - - public RegistryManagerRunNotifier(TestRegistryManager registryManager, RunNotifier delegate) { - super(); - this.delegate = delegate; - this.registryManager = registryManager; - } - - @Override - public void addListener(RunListener listener) { - delegate.addListener(listener); - } - - @Override - public void removeListener(RunListener listener) { - delegate.removeListener(listener); - } - - @Override - public void fireTestRunStarted(Description description) { - delegate.fireTestRunStarted(description); - } - - @Override - public void fireTestRunFinished(Result result) { - delegate.fireTestRunFinished(result); - } - - @Override - public void fireTestStarted(Description description) throws StoppedByUserException { - delegate.fireTestStarted(description); - } - - @Override - public void fireTestFailure(Failure failure) { - delegate.fireTestFailure(failure); - } - - @Override - public void fireTestAssumptionFailed(Failure failure) { - delegate.fireTestAssumptionFailed(failure); - } - - @Override - public void fireTestIgnored(Description description) { - delegate.fireTestIgnored(description); - } - - @Override - public void fireTestFinished(Description description) { - registryManager.afterTestMethod(); - delegate.fireTestFinished(description); - } - - @Override - public void pleaseStop() { - delegate.pleaseStop(); - } - - @Override - public void addFirstListener(RunListener listener) { - delegate.addFirstListener(listener); - } - } -} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/04ea6d0e/tapestry-ioc-test/src/main/java/org/apache/tapestry5/ioc/test/TestRegistryManager.java ---------------------------------------------------------------------- diff --git a/tapestry-ioc-test/src/main/java/org/apache/tapestry5/ioc/test/TestRegistryManager.java b/tapestry-ioc-test/src/main/java/org/apache/tapestry5/ioc/test/TestRegistryManager.java deleted file mode 100644 index a5e0145..0000000 --- a/tapestry-ioc-test/src/main/java/org/apache/tapestry5/ioc/test/TestRegistryManager.java +++ /dev/null @@ -1,131 +0,0 @@ -// Copyright 2006, 2007, 2008, 2009, 2010, 2011, 2012 The Apache Software Foundation -// -// 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 org.apache.tapestry5.ioc.test; - -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; -import java.lang.reflect.Modifier; -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; - -import org.apache.tapestry5.ioc.RegistryBuilder; -import org.junit.runners.model.InitializationError; - -/** - * Helper class used by the {@link TapestryIOCJUnit4ClassRunner} to manage the test registry - */ -public class TestRegistryManager { - private final Registry annotation; - private final List<Method> moduleDefFactories; - - private org.apache.tapestry5.ioc.Registry registry; - - public TestRegistryManager(Class<?> type) throws InitializationError { - super(); - - Registry annotation = type.getAnnotation(Registry.class); - if (annotation == null) { - throw new InitializationError(type.getName() + " does not specify a @Registry"); - } - - this.annotation = annotation; - this.moduleDefFactories = findModuleDefFactories(type); - } - - protected List<Method> findModuleDefFactories(Class<?> type) throws InitializationError { - List<Method> factoryMethods = new ArrayList<Method>(); - for (Method method : type.getMethods()) { - if (method.getAnnotation(ModuleDef.class) != null) { - validateModuleDefMethod(method); - factoryMethods.add(method); - } - } - return factoryMethods.isEmpty() ? Collections.<Method> emptyList() : factoryMethods; - } - - protected void validateModuleDefMethod(Method method) throws InitializationError { - int modifiers = method.getModifiers(); - if (method.getParameterTypes().length != 0 - || !Modifier.isStatic(modifiers) - || !Modifier.isPublic(modifiers)) { - - throw new InitializationError( - String.format("@ModuleDef method %s must be public static and accept no arguments", - method.getName())); - } - if (!org.apache.tapestry5.ioc.def.ModuleDef.class.isAssignableFrom(method.getReturnType())) { - throw new InitializationError( - String.format("@ModuleDef method %s return type %s is not valid", - method.getName(), method.getReturnType().getName())); - } - } - - /** - * Get the existing registry or create one if required. - * @return The test Registry - * @throws Exception - */ - public org.apache.tapestry5.ioc.Registry getOrCreateRegistry() throws Exception { - if (registry == null) { - RegistryBuilder builder = new RegistryBuilder(); - if (annotation.modules() != null) { - builder.add(annotation.modules()); - } - for (Method moduleDefFactory : moduleDefFactories) { - try { - org.apache.tapestry5.ioc.def.ModuleDef moduleDef = - (org.apache.tapestry5.ioc.def.ModuleDef) moduleDefFactory.invoke(null); - - builder.add(moduleDef); - } catch (InvocationTargetException e) { - if (e.getTargetException() instanceof Exception) { - throw (Exception) e.getTargetException(); - } - throw e; - } - } - registry = builder.build(); - registry.performRegistryStartup(); - } - return registry; - } - - /** - * Notify that the current test method has completed - */ - public void afterTestMethod() { - if (annotation.shutdown() == RegistryShutdownType.AFTER_METHOD) { - shutdownRegistry(); - } - } - - /** - * Notify that the current test class has completed - */ - public void afterTestClass() { - if (annotation.shutdown() == RegistryShutdownType.AFTER_CLASS) { - shutdownRegistry(); - } - } - - protected void shutdownRegistry() { - try { - registry.shutdown(); - } finally { - registry = null; - } - } -} http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/04ea6d0e/tapestry-ioc-test/src/test/java/org/apache/tapestry5/ioc/test/MapModuleDef.java ---------------------------------------------------------------------- diff --git a/tapestry-ioc-test/src/test/java/org/apache/tapestry5/ioc/test/MapModuleDef.java b/tapestry-ioc-test/src/test/java/org/apache/tapestry5/ioc/test/MapModuleDef.java deleted file mode 100644 index 6001b2c..0000000 --- a/tapestry-ioc-test/src/test/java/org/apache/tapestry5/ioc/test/MapModuleDef.java +++ /dev/null @@ -1,116 +0,0 @@ -// Copyright 2006, 2007, 2008, 2009, 2010, 2011, 2012 The Apache Software Foundation -// -// 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 org.apache.tapestry5.ioc.test; - -import java.util.Collections; -import java.util.Map; -import java.util.Set; - -import org.apache.tapestry5.ioc.ObjectCreator; -import org.apache.tapestry5.ioc.ScopeConstants; -import org.apache.tapestry5.ioc.ServiceBuilderResources; -import org.apache.tapestry5.ioc.def.ContributionDef; -import org.apache.tapestry5.ioc.def.DecoratorDef; -import org.apache.tapestry5.ioc.def.ModuleDef; -import org.apache.tapestry5.ioc.def.ServiceDef; - -/** - * Test ModuleDef implementation based on a {@link Map} - */ -@SuppressWarnings("rawtypes") -public class MapModuleDef implements ModuleDef { - private final Map<String, Object> map; - - public MapModuleDef(Map<String, Object> map) { - super(); - this.map = map; - } - - @Override - public Class getBuilderClass() { - return null; - } - - @Override - public Set<ContributionDef> getContributionDefs() { - return Collections.emptySet(); - } - - @Override - public Set<DecoratorDef> getDecoratorDefs() { - return Collections.emptySet(); - } - - @Override - public String getLoggerName() { - return "MapModuleDef"; - } - - @Override - public Set<String> getServiceIds() { - return map.keySet(); - } - - @Override - public ServiceDef getServiceDef(String serviceId) { - return new MapServiceDef(map, serviceId); - } - - public static class MapServiceDef implements ServiceDef { - private final Map<String, Object> map; - private final String serviceId; - - public MapServiceDef(Map<String, Object> map, String serviceId) { - super(); - this.map = map; - this.serviceId = serviceId; - } - - @Override - public ObjectCreator createServiceCreator(ServiceBuilderResources resources) { - return new ObjectCreator() { - @Override - public Object createObject() { - return map.get(serviceId); - } - }; - } - - @Override - public String getServiceId() { - return serviceId; - } - - @Override - public Set<Class> getMarkers() { - return Collections.emptySet(); - } - - @Override - public Class getServiceInterface() { - return map.get(serviceId).getClass(); - } - - @Override - public String getServiceScope() { - return ScopeConstants.DEFAULT; - } - - @Override - public boolean isEagerLoad() { - return false; - } - } -} http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/04ea6d0e/tapestry-ioc-test/src/test/java/org/apache/tapestry5/ioc/test/TapestryIOCJUnit4ClassRunnerAfterClassTest.java ---------------------------------------------------------------------- diff --git a/tapestry-ioc-test/src/test/java/org/apache/tapestry5/ioc/test/TapestryIOCJUnit4ClassRunnerAfterClassTest.java b/tapestry-ioc-test/src/test/java/org/apache/tapestry5/ioc/test/TapestryIOCJUnit4ClassRunnerAfterClassTest.java deleted file mode 100644 index 354f491..0000000 --- a/tapestry-ioc-test/src/test/java/org/apache/tapestry5/ioc/test/TapestryIOCJUnit4ClassRunnerAfterClassTest.java +++ /dev/null @@ -1,61 +0,0 @@ -// Copyright 2006, 2007, 2008, 2009, 2010, 2011, 2012 The Apache Software Foundation -// -// 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 org.apache.tapestry5.ioc.test; - -import static org.apache.tapestry5.ioc.test.RegistryShutdownType.AFTER_CLASS; -import static org.junit.Assert.assertArrayEquals; -import static org.junit.runners.MethodSorters.NAME_ASCENDING; - -import java.util.ArrayList; -import java.util.List; - -import org.apache.tapestry5.ioc.annotations.Inject; -import org.apache.tapestry5.ioc.test.TapestryIOCJUnit4ClassRunnerAfterClassTest.AfterClassTestModule; -import org.junit.FixMethodOrder; -import org.junit.Test; -import org.junit.runner.RunWith; - -@RunWith(TapestryIOCJUnit4ClassRunner.class) -@Registry(modules=AfterClassTestModule.class, shutdown=AFTER_CLASS) -@FixMethodOrder(NAME_ASCENDING) // guarantees test ordering -public class TapestryIOCJUnit4ClassRunnerAfterClassTest { - public static class AfterClassTestModule { - public List<String> buildList() { - List<String> list = new ArrayList<String>(); - list.add("foo"); - return list; - } - } - - @Inject - private List<String> list; - - @Test - public void testInjectA() { - assertArrayEquals(new Object[] { "foo" }, list.toArray()); - list.add("bar"); - } - - @Test - public void testInjectB() { - assertArrayEquals(new Object[] { "foo", "bar" }, list.toArray()); - list.add("baz"); - } - - @Test - public void testInjectC() { - assertArrayEquals(new Object[] { "foo", "bar", "baz" }, list.toArray()); - } -} http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/04ea6d0e/tapestry-ioc-test/src/test/java/org/apache/tapestry5/ioc/test/TapestryIOCJUnit4ClassRunnerAfterMethodTest.java ---------------------------------------------------------------------- diff --git a/tapestry-ioc-test/src/test/java/org/apache/tapestry5/ioc/test/TapestryIOCJUnit4ClassRunnerAfterMethodTest.java b/tapestry-ioc-test/src/test/java/org/apache/tapestry5/ioc/test/TapestryIOCJUnit4ClassRunnerAfterMethodTest.java deleted file mode 100644 index cc51c0b..0000000 --- a/tapestry-ioc-test/src/test/java/org/apache/tapestry5/ioc/test/TapestryIOCJUnit4ClassRunnerAfterMethodTest.java +++ /dev/null @@ -1,63 +0,0 @@ -// Copyright 2006, 2007, 2008, 2009, 2010, 2011, 2012 The Apache Software Foundation -// -// 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 org.apache.tapestry5.ioc.test; - -import static org.apache.tapestry5.ioc.test.RegistryShutdownType.AFTER_METHOD; -import static org.junit.Assert.assertArrayEquals; -import static org.junit.runners.MethodSorters.NAME_ASCENDING; - -import java.util.ArrayList; -import java.util.List; - -import org.apache.tapestry5.ioc.annotations.Inject; -import org.apache.tapestry5.ioc.test.TapestryIOCJUnit4ClassRunnerAfterMethodTest.AfterMethodTestModule; -import org.junit.FixMethodOrder; -import org.junit.Test; -import org.junit.runner.RunWith; - -@RunWith(TapestryIOCJUnit4ClassRunner.class) -@Registry(modules=AfterMethodTestModule.class, shutdown=AFTER_METHOD) -@FixMethodOrder(NAME_ASCENDING) // guarantees test ordering -public class TapestryIOCJUnit4ClassRunnerAfterMethodTest { - public static class AfterMethodTestModule { - public List<String> buildList() { - List<String> list = new ArrayList<String>(); - list.add("foo"); - return list; - } - } - - @Inject - private List<String> list; - - @Test - public void testInjectA() { - assertArrayEquals(new Object[] { "foo" }, list.toArray()); - - list.add("bar"); - } - - @Test - public void testInjectB() { - assertArrayEquals(new Object[] { "foo" }, list.toArray()); - - list.add("baz"); - } - - @Test - public void testInjectC() { - assertArrayEquals(new Object[] { "foo" }, list.toArray()); - } -} http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/04ea6d0e/tapestry-ioc-test/src/test/java/org/apache/tapestry5/ioc/test/TapestryIOCJUnit4ClassRunnerModuleDefTest.java ---------------------------------------------------------------------- diff --git a/tapestry-ioc-test/src/test/java/org/apache/tapestry5/ioc/test/TapestryIOCJUnit4ClassRunnerModuleDefTest.java b/tapestry-ioc-test/src/test/java/org/apache/tapestry5/ioc/test/TapestryIOCJUnit4ClassRunnerModuleDefTest.java deleted file mode 100644 index 6426648..0000000 --- a/tapestry-ioc-test/src/test/java/org/apache/tapestry5/ioc/test/TapestryIOCJUnit4ClassRunnerModuleDefTest.java +++ /dev/null @@ -1,66 +0,0 @@ -// Copyright 2006, 2007, 2008, 2009, 2010, 2011, 2012 The Apache Software Foundation -// -// 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 org.apache.tapestry5.ioc.test; - -import static org.junit.Assert.assertEquals; - -import java.util.Date; -import java.util.HashMap; -import java.util.Map; - -import org.apache.tapestry5.ioc.annotations.Inject; -import org.apache.tapestry5.ioc.test.TapestryIOCJUnit4ClassRunnerModuleDefTest.ModuleDefTestModule; -import org.junit.Test; -import org.junit.runner.RunWith; - -@RunWith(TapestryIOCJUnit4ClassRunner.class) -@Registry(modules=ModuleDefTestModule.class) -public class TapestryIOCJUnit4ClassRunnerModuleDefTest { - @ModuleDef - public static MapModuleDef createModuleDef1() { - Map<String,Object> map = new HashMap<String, Object>(); - map.put("a", new Date(111)); - map.put("b", 222L); - - return new MapModuleDef(map); - } - - @ModuleDef - public static org.apache.tapestry5.ioc.def.ModuleDef createModuleDef2() { - Map<String,Object> map = new HashMap<String, Object>(); - map.put("c", 333); - - return new MapModuleDef(map); - } - - public static class ModuleDefTestModule { - public String buildD() { - return "444"; - } - } - - @Inject private Date a; - @Inject private Long b; - @Inject private Integer c; - @Inject private String d; - - @Test - public void testModuleDefInject() { - assertEquals(new Date(111), a); - assertEquals(new Long(222L), b); - assertEquals(new Integer(333), c); - assertEquals("444", d); - } -} \ No newline at end of file
