Updated Branches: refs/heads/master 399b52c77 -> 8215e9d1b
Convert TestNG to Spock Project: http://git-wip-us.apache.org/repos/asf/tapestry-5/repo Commit: http://git-wip-us.apache.org/repos/asf/tapestry-5/commit/8215e9d1 Tree: http://git-wip-us.apache.org/repos/asf/tapestry-5/tree/8215e9d1 Diff: http://git-wip-us.apache.org/repos/asf/tapestry-5/diff/8215e9d1 Branch: refs/heads/master Commit: 8215e9d1b3591be365f983edce606685842aface Parents: 399b52c Author: Howard M. Lewis Ship <[email protected]> Authored: Fri Jun 1 13:37:37 2012 -0700 Committer: Howard M. Lewis Ship <[email protected]> Committed: Fri Jun 1 13:37:37 2012 -0700 ---------------------------------------------------------------------- .../ioc/AbstractSharedRegistrySpecification.groovy | 10 + .../ServiceBuilderMethodInvokerSpec.groovy | 195 +++++++ .../ioc/internal/ServiceCreatorGenericsSpec.groovy | 79 +++ .../ioc/internal/ServiceBuilderMethodFixture.java | 15 +- .../internal/ServiceBuilderMethodInvokerTest.java | 433 --------------- 5 files changed, 291 insertions(+), 441 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/8215e9d1/tapestry-ioc/src/test/groovy/org/apache/tapestry5/ioc/AbstractSharedRegistrySpecification.groovy ---------------------------------------------------------------------- diff --git a/tapestry-ioc/src/test/groovy/org/apache/tapestry5/ioc/AbstractSharedRegistrySpecification.groovy b/tapestry-ioc/src/test/groovy/org/apache/tapestry5/ioc/AbstractSharedRegistrySpecification.groovy index 8f61051..7115ae9 100644 --- a/tapestry-ioc/src/test/groovy/org/apache/tapestry5/ioc/AbstractSharedRegistrySpecification.groovy +++ b/tapestry-ioc/src/test/groovy/org/apache/tapestry5/ioc/AbstractSharedRegistrySpecification.groovy @@ -2,6 +2,8 @@ package org.apache.tapestry5.ioc import spock.lang.Specification +import java.lang.reflect.Method + /** * Uses a static, shared instance of the {@link org.apache.tapestry5.ioc.Registry}. * All specifications that extend from this class will share @@ -31,4 +33,12 @@ abstract class AbstractSharedRegistrySpecification extends Specification { // TODO: the Registry is never shutdown, since there's no notification // that all tests are completing. + protected Method findMethod(Object subject, String methodName) { + def method = subject.class.methods.find { it.name == methodName } + + assert method != null + + return method + } + } http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/8215e9d1/tapestry-ioc/src/test/groovy/org/apache/tapestry5/ioc/internal/ServiceBuilderMethodInvokerSpec.groovy ---------------------------------------------------------------------- diff --git a/tapestry-ioc/src/test/groovy/org/apache/tapestry5/ioc/internal/ServiceBuilderMethodInvokerSpec.groovy b/tapestry-ioc/src/test/groovy/org/apache/tapestry5/ioc/internal/ServiceBuilderMethodInvokerSpec.groovy new file mode 100644 index 0000000..5226b87 --- /dev/null +++ b/tapestry-ioc/src/test/groovy/org/apache/tapestry5/ioc/internal/ServiceBuilderMethodInvokerSpec.groovy @@ -0,0 +1,195 @@ +package org.apache.tapestry5.ioc.internal + +import org.slf4j.Logger +import org.apache.tapestry5.ioc.* + +class ServiceBuilderMethodInvokerSpec extends AbstractSharedRegistrySpecification { + + static String DESCRIPTION = "{CREATOR DESCRIPTION}" + static String SERVICE_ID = "Fie" + + Logger logger = Mock() + FieService implementation = Mock() + OperationTracker tracker = new QuietOperationTracker() + ServiceBuilderResources resources = Mock() + ServiceBuilderMethodFixture fixture = new ServiceBuilderMethodFixture(); + + def setup() { + + fixture.fie = implementation + + _ * resources.tracker >> tracker + _ * resources.moduleBuilder >> fixture + _ * resources.serviceId >> SERVICE_ID + _ * resources.serviceInterface >> FieService + _ * resources.logger >> logger + } + + def "invoke a service builder method with no arguments"() { + + when: + + ObjectCreator oc = createObjectCreator "build_noargs" + + def actual = oc.createObject() + + then: + + actual.is implementation + } + + def ServiceBuilderMethodInvoker createObjectCreator(methodName) { + new ServiceBuilderMethodInvoker(resources, DESCRIPTION, + findMethod(fixture, methodName)) + } + + def invoke(methodName) { + createObjectCreator(methodName).createObject() + } + + def "invoke a method with injected parameters"() { + + fixture.expectedServiceInterface = FieService + fixture.expectedServiceResources = resources + fixture.expectedLogger = logger + + when: + + def actual = invoke "build_args" + + then: + + actual.is implementation + } + + def "@Inject annotation bypasses service resources when resolving value to inject"() { + + fixture.expectedString = "Injected" + + when: + + def actual = invoke "build_with_forced_injection" + + then: + + actual.is implementation + + 1 * resources.getObject(String, _ as AnnotationProvider) >> "Injected" + } + + def "@InjectService on method parameter"() { + + FoeService foe = Mock() + + fixture.expectedFoe = foe + + when: + + def actual = invoke "build_injected" + + then: + + actual.is implementation + + 1 * resources.getService("Foe", FoeService) >> foe + } + + def "@Named annotation on method parameter"() { + + FoeService foe = Mock() + + fixture.expectedFoe = foe + + when: + + def actual = invoke "build_named_injected" + + then: + + actual.is implementation + + 1 * resources.getService("Foe", FoeService) >> foe + } + + def "injection of ordered configuration as List"() { + + List<Runnable> configuration = Mock() + + fixture.expectedConfiguration = configuration + + when: + + def actual = invoke "buildWithOrderedConfiguration" + + then: + + actual.is implementation + + 1 * resources.getOrderedConfiguration(Runnable) >> configuration + } + + def "injection of unordered collection (as Collection)"() { + + Collection<Runnable> configuration = Mock() + + fixture.expectedConfiguration = configuration + + when: + + def actual = invoke "buildWithUnorderedConfiguration" + + then: + + actual.is implementation + + 1 * resources.getUnorderedConfiguration(Runnable) >> configuration + } + + def "builder method returns null"() { + + fixture.fie = null + + when: + + createObjectCreator("buildWithUnorderedConfiguration").createObject() + + then: + + RuntimeException e = thrown() + + e.message == "Builder method ${DESCRIPTION} (for service 'Fie') returned null." + } + + def "builder method failure"() { + + when: + + createObjectCreator("build_fail").createObject() + + then: + + RuntimeException e= thrown() + + e.message.contains "build_fail()" + e.message.contains "Method failed." + + e.cause.message == "Method failed." + } + + def "automatically injected dependency (without an annotation)"() { + + FoeService foe = Mock() + + fixture.expectedFoe = foe + + when: + + def actual = invoke "build_auto" + + then: + + actual.is implementation + + 1 * resources.getObject(FoeService, _ as AnnotationProvider) >> foe + } +} http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/8215e9d1/tapestry-ioc/src/test/groovy/org/apache/tapestry5/ioc/internal/ServiceCreatorGenericsSpec.groovy ---------------------------------------------------------------------- diff --git a/tapestry-ioc/src/test/groovy/org/apache/tapestry5/ioc/internal/ServiceCreatorGenericsSpec.groovy b/tapestry-ioc/src/test/groovy/org/apache/tapestry5/ioc/internal/ServiceCreatorGenericsSpec.groovy new file mode 100644 index 0000000..6084e30 --- /dev/null +++ b/tapestry-ioc/src/test/groovy/org/apache/tapestry5/ioc/internal/ServiceCreatorGenericsSpec.groovy @@ -0,0 +1,79 @@ +package org.apache.tapestry5.ioc.internal + +import spock.lang.Specification + +import java.lang.reflect.Method + +import static org.apache.tapestry5.ioc.internal.AbstractServiceCreator.findParameterizedTypeFromGenericType + +class ServiceCreatorGenericsSpec extends Specification { + + Method findMethod(name) { + Method method = ServiceBuilderMethodFixture.methods.find { it.name == name} + + assert method != null + + return method + } + + def methodMissing(String name, args) { + AbstractServiceCreator."$name"(* args) + } + + def "parameterized type of generic method parameter is extracted"() { + + when: + + def method = findMethod "methodWithParameterizedList" + + then: + + method.parameterTypes[0] == List + + def type = method.genericParameterTypes[0] + + type.toString() == "java.util.List<java.lang.Runnable>" + + findParameterizedTypeFromGenericType(type) == Runnable + } + + def "parameterized type of a non-generic parameter is Object"() { + + when: + + def method = findMethod "methodWithList" + + then: + + method.parameterTypes[0] == List + + def type = method.genericParameterTypes[0] + + type.toString() == "interface java.util.List" + findParameterizedTypeFromGenericType(type) == Object + } + + def "getting parameterized type for a non-support type is a failure"() { + + when: + + def method = findMethod "methodWithWildcardList" + + then: + + method.parameterTypes[0] == List + + def type = method.genericParameterTypes[0] + + when: + + findParameterizedTypeFromGenericType(type) + + then: + + IllegalArgumentException e = thrown() + + e.message == IOCMessages.genericTypeNotSupported(type) + } + +} http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/8215e9d1/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/internal/ServiceBuilderMethodFixture.java ---------------------------------------------------------------------- diff --git a/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/internal/ServiceBuilderMethodFixture.java b/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/internal/ServiceBuilderMethodFixture.java index 03d6f04..f67ebdc 100644 --- a/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/internal/ServiceBuilderMethodFixture.java +++ b/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/internal/ServiceBuilderMethodFixture.java @@ -1,4 +1,4 @@ -// Copyright 2006, 2007, 2010 The Apache Software Foundation +// Copyright 2006, 2007, 2010, 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. @@ -14,20 +14,19 @@ package org.apache.tapestry5.ioc.internal; -import java.util.ArrayList; -import java.util.Collection; -import java.util.List; - -import javax.inject.Named; - import org.apache.tapestry5.ioc.ServiceResources; import org.apache.tapestry5.ioc.annotations.InjectService; import org.apache.tapestry5.ioc.annotations.Value; import org.slf4j.Logger; import org.testng.Assert; +import javax.inject.Named; +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; + /** - * Used by {@link org.apache.tapestry5.ioc.internal.ServiceBuilderMethodInvokerTest}. + * Used by {@link org.apache.tapestry5.ioc.internal.ServiceBuilderMethodInvokerSpec}. */ public class ServiceBuilderMethodFixture extends Assert { http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/8215e9d1/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/internal/ServiceBuilderMethodInvokerTest.java ---------------------------------------------------------------------- diff --git a/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/internal/ServiceBuilderMethodInvokerTest.java b/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/internal/ServiceBuilderMethodInvokerTest.java deleted file mode 100644 index 44e0cf6..0000000 --- a/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/internal/ServiceBuilderMethodInvokerTest.java +++ /dev/null @@ -1,433 +0,0 @@ -// Copyright 2006, 2007, 2008, 2009 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.internal; - -import org.apache.tapestry5.ioc.AnnotationProvider; -import org.apache.tapestry5.ioc.ObjectCreator; -import org.apache.tapestry5.ioc.OperationTracker; -import org.apache.tapestry5.ioc.ServiceBuilderResources; -import org.easymock.EasyMock; -import org.slf4j.Logger; -import org.testng.Assert; -import org.testng.annotations.Test; - -import java.lang.reflect.Method; -import java.lang.reflect.Type; -import java.util.Collection; -import java.util.List; - -import static org.apache.tapestry5.ioc.internal.AbstractServiceCreator.findParameterizedTypeFromGenericType; -import static org.easymock.EasyMock.eq; -import static org.easymock.EasyMock.isA; - -public class ServiceBuilderMethodInvokerTest extends IOCInternalTestCase -{ - private static final String SERVICE_ID = "Fie"; - - private static final String CREATOR_DESCRIPTION = "{CREATOR DESCRIPTION}"; - - private final OperationTracker tracker = new QuietOperationTracker(); - - private void ignoreDebug(Logger logger) - { - logger.debug(EasyMock.anyObject(String.class)); - } - - @Test - public void noargs_method() - { - ServiceBuilderMethodFixture fixture = new ServiceBuilderMethodFixture(); - ServiceBuilderResources resources = mockServiceBuilderResources(tracker); - Logger logger = mockLogger(); - - fixture.fie = mockFieService(); - - trainForConstructor(resources, logger); - - train_getModuleBuilder(resources, fixture); - - ignoreDebug(logger); - - replay(); - - ObjectCreator sc = new ServiceBuilderMethodInvoker(resources, CREATOR_DESCRIPTION, findMethod(fixture, - "build_noargs")); - - Object actual = sc.createObject(); - - assertSame(actual, fixture.fie); - - verify(); - } - - private void trainForConstructor(ServiceBuilderResources resources, Logger logger) - { - train_getServiceId(resources, SERVICE_ID); - - train_getLogger(resources, logger); - - train_getServiceInterface(resources, FieService.class); - } - - @Test - public void method_with_args() - { - ServiceBuilderMethodFixture fixture = new ServiceBuilderMethodFixture(); - Method method = findMethod(fixture, "build_args"); - ServiceBuilderResources resources = mockServiceBuilderResources(tracker); - - Logger logger = mockLogger(); - - fixture.expectedServiceInterface = FieService.class; - fixture.expectedServiceResources = resources; - fixture.expectedLogger = logger; - - fixture.fie = mockFieService(); - - trainForConstructor(resources, logger); - - train_getModuleBuilder(resources, fixture); - - ignoreDebug(logger); - - replay(); - - ObjectCreator sc = new ServiceBuilderMethodInvoker(resources, CREATOR_DESCRIPTION, method); - - Object actual = sc.createObject(); - - assertSame(actual, fixture.fie); - - verify(); - } - - @Test - public void inject_annotation_bypasses_resources() - { - ServiceBuilderMethodFixture fixture = new ServiceBuilderMethodFixture(); - Method method = findMethod(fixture, "build_with_forced_injection"); - ServiceBuilderResources resources = mockServiceBuilderResources(tracker); - - Logger logger = mockLogger(); - - fixture.expectedString = "Injected"; - - fixture.fie = mockFieService(); - - trainForConstructor(resources, logger); - - train_getModuleBuilder(resources, fixture); - - ignoreDebug(logger); - - // This simulates what the real stack does when it sees @Value("Injected") - - expect(resources.getObject(eq(String.class), isA(AnnotationProvider.class))).andReturn("Injected"); - - replay(); - - ObjectCreator sc = new ServiceBuilderMethodInvoker(resources, CREATOR_DESCRIPTION, method); - - Object actual = sc.createObject(); - - assertSame(actual, fixture.fie); - - verify(); - } - - @Test - public void injected_service_method() - { - ServiceBuilderMethodFixture fixture = new ServiceBuilderMethodFixture(); - ServiceBuilderResources resources = mockServiceBuilderResources(tracker); - Logger logger = mockLogger(); - - fixture.fie = mockFieService(); - fixture.expectedFoe = newFoe(); - - trainForConstructor(resources, logger); - - train_getModuleBuilder(resources, fixture); - - train_getService(resources, "Foe", FoeService.class, fixture.expectedFoe); - - ignoreDebug(logger); - - replay(); - - ObjectCreator sc = new ServiceBuilderMethodInvoker(resources, CREATOR_DESCRIPTION, findMethod(fixture, - "build_injected")); - - Object actual = sc.createObject(); - - assertSame(actual, fixture.fie); - - verify(); - } - - @Test - public void named_injected_service_method() - { - ServiceBuilderMethodFixture fixture = new ServiceBuilderMethodFixture(); - ServiceBuilderResources resources = mockServiceBuilderResources(tracker); - Logger logger = mockLogger(); - - fixture.fie = mockFieService(); - fixture.expectedFoe = newFoe(); - - trainForConstructor(resources, logger); - - train_getModuleBuilder(resources, fixture); - - train_getService(resources, "Foe", FoeService.class, fixture.expectedFoe); - - ignoreDebug(logger); - - replay(); - - ObjectCreator sc = new ServiceBuilderMethodInvoker(resources, CREATOR_DESCRIPTION, findMethod(fixture, - "build_named_injected")); - - Object actual = sc.createObject(); - - assertSame(actual, fixture.fie); - - verify(); - } - - @SuppressWarnings("unchecked") - @Test - public void injected_ordered_collection() - { - ServiceBuilderMethodFixture fixture = new ServiceBuilderMethodFixture(); - ServiceBuilderResources resources = mockServiceBuilderResources(tracker); - Logger logger = mockLogger(); - - fixture.fie = mockFieService(); - List<Runnable> result = newMock(List.class); - fixture.expectedConfiguration = result; - - trainForConstructor(resources, logger); - - train_getModuleBuilder(resources, fixture); - - expect(resources.getOrderedConfiguration(Runnable.class)).andReturn(result); - - ignoreDebug(logger); - - replay(); - - ObjectCreator sc = new ServiceBuilderMethodInvoker(resources, CREATOR_DESCRIPTION, findMethod(fixture, - "buildWithOrderedConfiguration")); - - Object actual = sc.createObject(); - - assertSame(actual, fixture.fie); - - verify(); - } - - @SuppressWarnings("unchecked") - @Test - public void injected_unordered_collection() - { - ServiceBuilderMethodFixture fixture = new ServiceBuilderMethodFixture(); - ServiceBuilderResources resources = mockServiceBuilderResources(tracker); - Logger logger = mockLogger(); - - fixture.fie = mockFieService(); - Collection<Runnable> result = newMock(Collection.class); - fixture.expectedConfiguration = result; - - trainForConstructor(resources, logger); - - train_getModuleBuilder(resources, fixture); - - expect(resources.getUnorderedConfiguration(Runnable.class)).andReturn(result); - - ignoreDebug(logger); - - replay(); - - ObjectCreator sc = new ServiceBuilderMethodInvoker(resources, CREATOR_DESCRIPTION, findMethod(fixture, - "buildWithUnorderedConfiguration")); - - Object actual = sc.createObject(); - - assertSame(actual, fixture.fie); - - verify(); - } - - private FoeService newFoe() - { - return mockFoeService(); - } - - @Test - public void builder_method_returns_null() - { - ServiceBuilderMethodFixture fixture = new ServiceBuilderMethodFixture(); - ServiceBuilderResources resources = mockServiceBuilderResources(tracker); - Logger logger = mockLogger(); - - Method method = findMethod(fixture, "build_noargs"); - - trainForConstructor(resources, logger); - - train_getModuleBuilder(resources, fixture); - - ignoreDebug(logger); - - replay(); - - ObjectCreator sc = new ServiceBuilderMethodInvoker(resources, CREATOR_DESCRIPTION, method); - - try - { - sc.createObject(); - unreachable(); - } catch (RuntimeException ex) - { - Assert.assertEquals(ex.getMessage(), "Builder method " + CREATOR_DESCRIPTION - + " (for service 'Fie') returned null."); - } - - verify(); - } - - @Test - public void builder_method_failed() - { - ServiceBuilderMethodFixture fixture = new ServiceBuilderMethodFixture(); - ServiceBuilderResources resources = mockServiceBuilderResources(tracker); - Logger logger = mockLogger(); - - Method method = findMethod(fixture, "build_fail"); - - trainForConstructor(resources, logger); - - train_getModuleBuilder(resources, fixture); - - ignoreDebug(logger); - - replay(); - - ObjectCreator sc = new ServiceBuilderMethodInvoker(resources, CREATOR_DESCRIPTION, method); - - try - { - sc.createObject(); - unreachable(); - } catch (RuntimeException ex) - { - assertMessageContains(ex, "build_fail()", "Method failed."); - - Throwable cause = ex.getCause(); - - assertEquals(cause.getMessage(), "Method failed."); - } - - verify(); - } - - @Test - public void auto_dependency() - { - ServiceBuilderMethodFixture fixture = new ServiceBuilderMethodFixture(); - Method method = findMethod(fixture, "build_auto"); - - ServiceBuilderResources resources = mockServiceBuilderResources(tracker); - Logger logger = mockLogger(); - - fixture.fie = mockFieService(); - fixture.expectedFoe = mockFoeService(); - - trainForConstructor(resources, logger); - - train_getModuleBuilder(resources, fixture); - - expect(resources.getObject(eq(FoeService.class), isA(AnnotationProvider.class))).andReturn(fixture.expectedFoe); - - ignoreDebug(logger); - - replay(); - - ObjectCreator sc = new ServiceBuilderMethodInvoker(resources, CREATOR_DESCRIPTION, method); - - Object actual = sc.createObject(); - - verify(); - - assertSame(actual, fixture.fie); - } - - protected final void train_getModuleBuilder(ServiceBuilderResources resources, Object moduleInstance) - { - expect(resources.getModuleBuilder()).andReturn(moduleInstance); - } - - @Test - public void parameterized_type_of_generic_parameter() - { - Method m = findMethod(ServiceBuilderMethodFixture.class, "methodWithParameterizedList"); - - assertEquals(m.getParameterTypes()[0], List.class); - Type type = m.getGenericParameterTypes()[0]; - - assertEquals(type.toString(), "java.util.List<java.lang.Runnable>"); - assertEquals(findParameterizedTypeFromGenericType(type), Runnable.class); - } - - @Test - public void parameterized_type_of_nongeneric_parameter() - { - Method m = findMethod(ServiceBuilderMethodFixture.class, "methodWithList"); - - assertEquals(m.getParameterTypes()[0], List.class); - Type type = m.getGenericParameterTypes()[0]; - - assertEquals(type.toString(), "interface java.util.List"); - assertEquals(findParameterizedTypeFromGenericType(type), Object.class); - } - - @Test - public void parameterize_type_for_non_supported_type() - { - Method m = findMethod(ServiceBuilderMethodFixture.class, "methodWithWildcardList"); - - assertEquals(m.getParameterTypes()[0], List.class); - Type type = m.getGenericParameterTypes()[0]; - - try - { - findParameterizedTypeFromGenericType(type); - unreachable(); - } catch (IllegalArgumentException ex) - { - assertEquals(ex.getMessage(), IOCMessages.genericTypeNotSupported(type)); - } - } - - private FoeService mockFoeService() - { - return newMock(FoeService.class); - } - - private FieService mockFieService() - { - return newMock(FieService.class); - } -}
