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/33a2ac85 Tree: http://git-wip-us.apache.org/repos/asf/tapestry-5/tree/33a2ac85 Diff: http://git-wip-us.apache.org/repos/asf/tapestry-5/diff/33a2ac85 Branch: refs/heads/master Commit: 33a2ac855e3360ca6bacc9b7a013007cf402c1c2 Parents: 180766c Author: Howard M. Lewis Ship <[email protected]> Authored: Fri Jun 22 14:27:38 2012 -0700 Committer: Howard M. Lewis Ship <[email protected]> Committed: Fri Jun 22 14:27:38 2012 -0700 ---------------------------------------------------------------------- .../AbstractSharedRegistrySpecification.groovy | 4 + .../specs/ServiceBuilderMethodInvokerSpec.groovy | 7 +- .../ioc/internal/ServiceDecoratorFixture.java | 16 +-- .../ioc/internal/ServiceDecoratorImplTest.java | 194 --------------- .../services/ServiceDecoratorImplSpec.groovy | 132 ++++++++++ 5 files changed, 141 insertions(+), 212 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/33a2ac85/tapestry-ioc/src/test/groovy/ioc/specs/AbstractSharedRegistrySpecification.groovy ---------------------------------------------------------------------- diff --git a/tapestry-ioc/src/test/groovy/ioc/specs/AbstractSharedRegistrySpecification.groovy b/tapestry-ioc/src/test/groovy/ioc/specs/AbstractSharedRegistrySpecification.groovy index cd4a006..f787fe8 100644 --- a/tapestry-ioc/src/test/groovy/ioc/specs/AbstractSharedRegistrySpecification.groovy +++ b/tapestry-ioc/src/test/groovy/ioc/specs/AbstractSharedRegistrySpecification.groovy @@ -1,7 +1,9 @@ package ioc.specs import org.apache.tapestry5.ioc.IOCUtilities +import org.apache.tapestry5.ioc.OperationTracker import org.apache.tapestry5.ioc.Registry +import org.apache.tapestry5.ioc.internal.QuietOperationTracker import spock.lang.Specification import java.lang.reflect.Method @@ -15,6 +17,8 @@ abstract class AbstractSharedRegistrySpecification extends Specification { static Registry registry + static OperationTracker tracker = new QuietOperationTracker() + /** Any unrecognized methods are evaluated against the shared Registry instance. */ def methodMissing(String name, args) { registry."$name"(* args) http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/33a2ac85/tapestry-ioc/src/test/groovy/ioc/specs/ServiceBuilderMethodInvokerSpec.groovy ---------------------------------------------------------------------- diff --git a/tapestry-ioc/src/test/groovy/ioc/specs/ServiceBuilderMethodInvokerSpec.groovy b/tapestry-ioc/src/test/groovy/ioc/specs/ServiceBuilderMethodInvokerSpec.groovy index 459d7ed..9588a17 100644 --- a/tapestry-ioc/src/test/groovy/ioc/specs/ServiceBuilderMethodInvokerSpec.groovy +++ b/tapestry-ioc/src/test/groovy/ioc/specs/ServiceBuilderMethodInvokerSpec.groovy @@ -2,10 +2,12 @@ package ioc.specs 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.apache.tapestry5.ioc.internal.FieService +import org.apache.tapestry5.ioc.internal.FoeService +import org.apache.tapestry5.ioc.internal.ServiceBuilderMethodFixture +import org.apache.tapestry5.ioc.internal.ServiceBuilderMethodInvoker import org.slf4j.Logger -import org.apache.tapestry5.ioc.internal.* class ServiceBuilderMethodInvokerSpec extends AbstractSharedRegistrySpecification { @@ -14,7 +16,6 @@ class ServiceBuilderMethodInvokerSpec extends AbstractSharedRegistrySpecificatio Logger logger = Mock() FieService implementation = Mock() - OperationTracker tracker = new QuietOperationTracker() ServiceBuilderResources resources = Mock() ServiceBuilderMethodFixture fixture = new ServiceBuilderMethodFixture(); http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/33a2ac85/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/internal/ServiceDecoratorFixture.java ---------------------------------------------------------------------- diff --git a/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/internal/ServiceDecoratorFixture.java b/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/internal/ServiceDecoratorFixture.java index 0353b09..1364346 100644 --- a/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/internal/ServiceDecoratorFixture.java +++ b/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/internal/ServiceDecoratorFixture.java @@ -1,23 +1,9 @@ -// Copyright 2006 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.testng.Assert; /** - * Used by {@link org.apache.tapestry5.ioc.internal.ServiceDecoratorImplTest}. + * Used by {@link org.apache.tapestry5.ioc.internal.services.ServiceDecoratorImplSpec}. */ public class ServiceDecoratorFixture extends Assert { http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/33a2ac85/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/internal/ServiceDecoratorImplTest.java ---------------------------------------------------------------------- diff --git a/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/internal/ServiceDecoratorImplTest.java b/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/internal/ServiceDecoratorImplTest.java deleted file mode 100644 index 3791291..0000000 --- a/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/internal/ServiceDecoratorImplTest.java +++ /dev/null @@ -1,194 +0,0 @@ -// Copyright 2006, 2007, 2008, 2009, 2011 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.ModuleBuilderSource; -import org.apache.tapestry5.ioc.OperationTracker; -import org.apache.tapestry5.ioc.ServiceResources; -import org.easymock.EasyMock; -import org.slf4j.Logger; -import org.testng.annotations.Test; - -import java.lang.reflect.Method; - -public class ServiceDecoratorImplTest extends IOCInternalTestCase -{ - private static final String SERVICE_ID = "ioc.Fie"; - - private final OperationTracker tracker = new QuietOperationTracker(); - - private ModuleBuilderSource newSource(final Object moduleInstance) - { - return new ModuleBuilderSource() - { - public Object getModuleBuilder() - { - return moduleInstance; - } - }; - } - - /** - * Also, test logging of decorator method invocation. - * - * @throws Exception - */ - @Test - public void decorator_returns_interceptor() throws Exception - { - ServiceDecoratorFixture fixture = new ServiceDecoratorFixture(); - Method m = findMethod(fixture, "decoratorReturnsInterceptor"); - - ServiceResources resources = mockServiceResources(tracker); - Logger logger = mockLogger(); - fixture.expectedDelegate = mockFieService(); - fixture.interceptorToReturn = mockFieService(); - ModuleBuilderSource source = newSource(fixture); - - trainForConstructor(resources, logger); - - logger.debug(EasyMock - .contains("Invoking method org.apache.tapestry5.ioc.internal.ServiceDecoratorFixture.decoratorReturnsInterceptor(Class, Object)")); - - replay(); - - // Check that the delegate gets passed in; check that the return value of the - // decorator method is the return value of the ServiceDecorator. - - ServiceDecoratorImpl decorator = new ServiceDecoratorImpl(m, source, resources, getProxyFactory()); - - Object interceptor = decorator.createInterceptor(fixture.expectedDelegate); - - assertSame(interceptor, fixture.interceptorToReturn); - - verify(); - } - - @Test - public void decorator_returns_null_interceptor() throws Exception - { - ServiceDecoratorFixture fixture = new ServiceDecoratorFixture(); - ModuleBuilderSource source = newSource(fixture); - ServiceResources resources = mockServiceResources(tracker); - Logger logger = mockLogger(); - Object delegate = mockFieService(); - - trainForConstructor(resources, logger); - - logger.debug(EasyMock.anyObject(String.class)); - - replay(); - - Method m = findMethod(fixture, "decorateReturnNull"); - - ServiceDecoratorImpl decorator = new ServiceDecoratorImpl(m, source, resources, getProxyFactory()); - - Object interceptor = decorator.createInterceptor(delegate); - - assertNull(interceptor); - - verify(); - } - - @Test - public void decorator_returns_incorrect_type() throws Exception - { - ServiceDecoratorFixture fixture = new ServiceDecoratorFixture(); - ModuleBuilderSource source = newSource(fixture); - ServiceResources resources = mockServiceResources(tracker); - Logger logger = mockLogger(); - fixture.expectedDelegate = mockFieService(); - fixture.interceptorToReturn = newMock(FoeService.class); - - Method m = findMethod(fixture, "decoratorUntyped"); - - trainForConstructor(resources, logger); - - logger.debug(EasyMock.anyObject(String.class)); - - replay(); - - ServiceDecoratorImpl decorator = new ServiceDecoratorImpl(m, source, resources, getProxyFactory()); - - try - { - decorator.createInterceptor(fixture.expectedDelegate); - unreachable(); - } catch (RuntimeException ex) - { - assertMessageContains( - ex, - "Decorator method org.apache.tapestry5.ioc.internal.ServiceDecoratorFixture.decoratorUntyped(Object)", - "(invoked for service 'ioc.Fie') returned FoeService, which is not assignable to the org.apache.tapestry5.ioc.internal.FieService service interface."); - - } - - verify(); - } - - @Test - public void decorator_method_throws_exception() throws Exception - { - ServiceDecoratorFixture fixture = new ServiceDecoratorFixture(); - ModuleBuilderSource source = newSource(fixture); - ServiceResources resources = mockServiceResources(tracker); - Logger logger = mockLogger(); - Object delegate = mockFieService(); - fixture.exception = new RuntimeException("Ouch!"); - - trainForConstructor(resources, logger); - - logger.debug(EasyMock.anyObject(String.class)); - - replay(); - - Method m = findMethod(fixture, "decoratorThrowsException"); - - ServiceDecoratorImpl decorator = new ServiceDecoratorImpl(m, source, resources, getProxyFactory()); - - try - { - decorator.createInterceptor(delegate); - unreachable(); - } catch (RuntimeException ex) - { - assertMessageContains( - ex, - "Error invoking", - "org.apache.tapestry5.ioc.internal.ServiceDecoratorFixture.decoratorThrowsException(java.lang.Object)", - "Ouch!"); - - Throwable cause = ex.getCause(); - - assertSame(cause, fixture.exception); - } - - verify(); - } - - private FieService mockFieService() - { - return newMock(FieService.class); - } - - private void trainForConstructor(ServiceResources resources, Logger logger) - { - train_getServiceId(resources, SERVICE_ID); - - train_getServiceInterface(resources, FieService.class); - - train_getLogger(resources, logger); - } -} http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/33a2ac85/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/internal/services/ServiceDecoratorImplSpec.groovy ---------------------------------------------------------------------- diff --git a/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/internal/services/ServiceDecoratorImplSpec.groovy b/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/internal/services/ServiceDecoratorImplSpec.groovy new file mode 100644 index 0000000..bf53e06 --- /dev/null +++ b/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/internal/services/ServiceDecoratorImplSpec.groovy @@ -0,0 +1,132 @@ +package org.apache.tapestry5.ioc.internal.services + +import ioc.specs.AbstractSharedRegistrySpecification +import org.apache.tapestry5.ioc.ModuleBuilderSource +import org.apache.tapestry5.ioc.ServiceDecorator +import org.apache.tapestry5.ioc.ServiceResources +import org.apache.tapestry5.ioc.internal.FieService +import org.apache.tapestry5.ioc.internal.ServiceDecoratorFixture +import org.apache.tapestry5.ioc.internal.ServiceDecoratorImpl +import org.apache.tapestry5.ioc.services.PlasticProxyFactory +import org.slf4j.Logger + +class ServiceDecoratorImplSpec extends AbstractSharedRegistrySpecification { + + final static String SERVICE_ID = "Fie" + + ServiceDecoratorFixture fixture = new ServiceDecoratorFixture() + ModuleBuilderSource source = { return fixture } as ModuleBuilderSource + ServiceResources resources = Mock() + Logger logger = Mock() + PlasticProxyFactory proxyFactory = getService PlasticProxyFactory + + def findMethod(name) { + def method = fixture.class.methods.find { it.name.equalsIgnoreCase(name)} + + assert method != null + + return method + } + + def "decorator method returns an interceptor"() { + def method = findMethod "decoratorReturnsInterceptor" + FieService delegate = Mock() + FieService expectedInterceptor = Mock() + + fixture.expectedDelegate = delegate + fixture.interceptorToReturn = expectedInterceptor + + when: + + ServiceDecorator sd = new ServiceDecoratorImpl(method, source, resources, proxyFactory) + + def interceptor = sd.createInterceptor(delegate) + + then: + + 1 * resources.serviceId >> SERVICE_ID + 1 * resources.serviceInterface >> FieService + 1 * resources.logger >> logger + _ * resources.tracker >> tracker + + interceptor.is(expectedInterceptor) + } + + def "decorator method returns null"() { + def method = findMethod "decorateReturnNull" + FieService delegate = Mock() + + when: + + ServiceDecorator sd = new ServiceDecoratorImpl(method, source, resources, proxyFactory) + + def interceptor = sd.createInterceptor(delegate) + + then: + + 1 * resources.serviceId >> SERVICE_ID + 1 * resources.serviceInterface >> FieService + 1 * resources.logger >> logger + _ * resources.tracker >> tracker + 1 * logger.debug(_) + + interceptor == null + } + + def "decorator method returns instance of wrong type"() { + def method = findMethod "decoratorUntyped" + FieService delegate = Mock() + Runnable wrongTypeInterceptor = Mock() + + fixture.expectedDelegate = delegate + fixture.interceptorToReturn = wrongTypeInterceptor + + when: + + ServiceDecorator sd = new ServiceDecoratorImpl(method, source, resources, proxyFactory) + + sd.createInterceptor(delegate) + + then: + + 1 * resources.serviceId >> SERVICE_ID + 1 * resources.serviceInterface >> FieService + 1 * resources.logger >> logger + _ * resources.tracker >> tracker + 1 * logger.debug(_) + + RuntimeException e = thrown() + + e.message.contains "Decorator method org.apache.tapestry5.ioc.internal.ServiceDecoratorFixture.decoratorUntyped(Object)" + e.message.contains "(invoked for service '$SERVICE_ID') returned" + e.message.contains "which is not assignable to the org.apache.tapestry5.ioc.internal.FieService service interface." + + } + + def "exception thrown by decorator method is wrapped and rethrown"() { + def method = findMethod "decoratorThrowsException" + fixture.exception = new RuntimeException("Ouch!"); + + when: + + ServiceDecorator sd = new ServiceDecoratorImpl(method, source, resources, proxyFactory) + + def interceptor = sd.createInterceptor(fixture.expectedDelegate) + + then: + + 1 * resources.serviceId >> SERVICE_ID + 1 * resources.serviceInterface >> FieService + 1 * resources.logger >> logger + _ * resources.tracker >> tracker + + 1 * logger.debug(_) + + RuntimeException e = thrown() + + e.message.contains "Error invoking" + e.message.contains "org.apache.tapestry5.ioc.internal.ServiceDecoratorFixture.decoratorThrowsException(java.lang.Object)" + e.message.contains "Ouch!" + + } +}
