You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tapestry.apache.org by hl...@apache.org on 2012/05/16 20:50:32 UTC

[36/44] git commit: Convert TestNG to Spock

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/a548a76a
Tree: http://git-wip-us.apache.org/repos/asf/tapestry-5/tree/a548a76a
Diff: http://git-wip-us.apache.org/repos/asf/tapestry-5/diff/a548a76a

Branch: refs/heads/master
Commit: a548a76a699db26eb77d40a57d5d0e7f11e17bf9
Parents: 01fb5e3
Author: Howard M. Lewis Ship <hl...@gmail.com>
Authored: Fri Apr 20 16:55:26 2012 -0700
Committer: Howard M. Lewis Ship <hl...@apache.org>
Committed: Wed May 16 11:49:41 2012 -0700

----------------------------------------------------------------------
 .../ioc/AbstractSharedRegistrySpecification.groovy |   16 +-
 .../AspectInterceptorBuilderImplSpec.groovy        |  150 ++++++++++
 .../ioc/internal/services/ArraysSubject.java       |   20 --
 .../ioc/internal/services/ArraysSubjectImpl.java   |   23 --
 .../services/AspectInterceptorBuilderImplTest.java |  213 ---------------
 5 files changed, 151 insertions(+), 271 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/a548a76a/tapestry-ioc/src/test/groovy/org/apache/tapestry/ioc/AbstractSharedRegistrySpecification.groovy
----------------------------------------------------------------------
diff --git a/tapestry-ioc/src/test/groovy/org/apache/tapestry/ioc/AbstractSharedRegistrySpecification.groovy b/tapestry-ioc/src/test/groovy/org/apache/tapestry/ioc/AbstractSharedRegistrySpecification.groovy
index 2b8c425..326c4ac 100644
--- a/tapestry-ioc/src/test/groovy/org/apache/tapestry/ioc/AbstractSharedRegistrySpecification.groovy
+++ b/tapestry-ioc/src/test/groovy/org/apache/tapestry/ioc/AbstractSharedRegistrySpecification.groovy
@@ -8,7 +8,7 @@ import spock.lang.Specification
  * a single instance of the Registry, that is created by whatever specification is created first. */
 abstract class AbstractSharedRegistrySpecification extends Specification {
 
-  static Registry registry;
+  static Registry registry
 
   /** Any unrecognized methods are evaluated against the shared Registry instance. */
   def methodMissing(String name, args) {
@@ -20,25 +20,11 @@ abstract class AbstractSharedRegistrySpecification extends Specification {
     if (registry == null) {
       registry = IOCUtilities.buildDefaultRegistry()
     }
-
-    helpSetupSpec()
-  }
-
-  /** Does nothing; override in subclasses to do work that should occur inside setupSpec(). */
-  def helpSetupSpec() {
-
   }
 
   /** Invokes {@link Registry#cleanupThread()}. */
   def cleanupSpec() {
     registry.cleanupThread();
-
-    helpCleanupSpec()
-  }
-
-  /** Does nothing; override in subclasses to do work that should occur inside cleanupSpec(), after {@link Registry#cleanupThread()}. */
-  def helpCleanupSpec() {
-
   }
 
   // TODO: the Registry is never shutdown, since there's no notification

http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/a548a76a/tapestry-ioc/src/test/groovy/org/apache/tapestry5/ioc/internal/services/AspectInterceptorBuilderImplSpec.groovy
----------------------------------------------------------------------
diff --git a/tapestry-ioc/src/test/groovy/org/apache/tapestry5/ioc/internal/services/AspectInterceptorBuilderImplSpec.groovy b/tapestry-ioc/src/test/groovy/org/apache/tapestry5/ioc/internal/services/AspectInterceptorBuilderImplSpec.groovy
new file mode 100644
index 0000000..a477a14
--- /dev/null
+++ b/tapestry-ioc/src/test/groovy/org/apache/tapestry5/ioc/internal/services/AspectInterceptorBuilderImplSpec.groovy
@@ -0,0 +1,150 @@
+package org.apache.tapestry5.ioc.internal.services
+
+import org.apache.commons.lang.StringUtils
+import org.apache.tapestry.ioc.AbstractSharedRegistrySpecification
+import org.apache.tapestry5.ioc.services.AspectDecorator
+import org.apache.tapestry5.plastic.MethodAdvice
+import org.apache.tapestry5.plastic.MethodInvocation
+import spock.lang.Shared
+
+
+interface Subject {
+  void advised();
+
+  void notAdvised();
+}
+
+interface ArraysSubject {
+  String[] operation(String[] inputs);
+}
+
+class AspectInterceptorBuilderImplSpec extends AbstractSharedRegistrySpecification {
+
+  @Shared
+  private AspectDecorator decorator
+
+  def setupSpec() {
+    decorator = getService AspectDecorator
+  }
+
+  def "ensure that non-advised methods are not passed through the MethodAdvice object"() {
+    Subject delegate = Mock()
+    MethodAdvice advice = Mock()
+
+    def builder = decorator.createBuilder Subject, delegate, "<Subject>"
+
+    builder.adviseMethod Subject.getMethod("advised"), advice
+
+    Subject interceptor = builder.build()
+
+    when:
+
+    interceptor.advised()
+
+    then:
+
+    1 * advice.advise(_) >> { MethodInvocation mi ->
+      assert mi.method.name == "advised"
+      mi.proceed()
+    }
+    1 * delegate.advised()
+    0 * _
+
+    when:
+
+    interceptor.notAdvised()
+
+    then:
+
+    1 * delegate.notAdvised()
+    0 * _
+  }
+
+  def "failure when advising a method that is not in the service interface"() {
+    Subject delegate = Mock()
+    MethodAdvice advice = Mock()
+
+    def builder = decorator.createBuilder Subject, delegate, "<Subject>"
+
+    when:
+
+    builder.adviseMethod Runnable.getMethod("run"), advice
+
+    then:
+
+    IllegalArgumentException e = thrown()
+
+    e.message == "Method public abstract void java.lang.Runnable.run() is not defined for interface interface org.apache.tapestry5.ioc.internal.services.Subject."
+  }
+
+  def "multiple advice for single method is processed in order"() {
+    TextTransformer delegate = Mock()
+    MethodAdvice stripFirstLetter = Mock()
+    MethodAdvice reverse = Mock()
+
+    def builder = decorator.createBuilder TextTransformer, delegate, "<TextTransformer>"
+
+    def method = TextTransformer.getMethod "transform", String
+
+    builder.adviseMethod method, stripFirstLetter
+    builder.adviseMethod method, reverse
+
+    TextTransformer advised = builder.build()
+
+    when:
+
+    def result = advised.transform "Tapestry"
+
+    then:
+
+    result == "[yrtsepa]"
+
+    1 * stripFirstLetter.advise(_) >> { MethodInvocation mi ->
+      assert mi.getParameter(0) == "Tapestry"
+      mi.setParameter 0, mi.getParameter(0).substring(1)
+      mi.proceed()
+    }
+
+    1 * reverse.advise(_) >> { MethodInvocation mi ->
+      assert mi.getParameter(0) == "apestry"
+      mi.setParameter 0, StringUtils.reverse(mi.getParameter(0))
+      mi.proceed()
+    }
+
+    1 * delegate.transform(_) >> { it }
+  }
+
+  def "arrays are allowed as method parameters and return values"() {
+    ArraysSubject delegate = Mock()
+    MethodAdvice advice = Mock()
+
+    def builder = decorator.createBuilder ArraysSubject, delegate, "unused"
+    builder.adviseAllMethods advice
+
+    ArraysSubject advised = builder.build()
+
+    when:
+
+    def result = advised.operation(["Fred", "Barney"] as String[])
+
+    then:
+
+    1 * advice.advise(_) >> { MethodInvocation it ->
+      String[] inputs = it.getParameter(0)
+
+      it.setParameter 0, inputs.collect({it.toUpperCase() }) as String[]
+
+      it.proceed()
+
+      def index = 0
+
+      it.setReturnValue it.getReturnValue().collect({ value -> "${index++}:$value" }) as String[]
+    }
+
+    1 * delegate.operation(_) >> { it[0] }
+
+    result.class == ([] as String[]).class
+    result.asType(List) == ["0:FRED", "1:BARNEY"]
+  }
+
+}

http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/a548a76a/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/internal/services/ArraysSubject.java
----------------------------------------------------------------------
diff --git a/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/internal/services/ArraysSubject.java b/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/internal/services/ArraysSubject.java
deleted file mode 100644
index d7c0871..0000000
--- a/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/internal/services/ArraysSubject.java
+++ /dev/null
@@ -1,20 +0,0 @@
-// Copyright 2008 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.services;
-
-public interface ArraysSubject
-{
-    String[] operation(String[] inputs);
-}

http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/a548a76a/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/internal/services/ArraysSubjectImpl.java
----------------------------------------------------------------------
diff --git a/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/internal/services/ArraysSubjectImpl.java b/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/internal/services/ArraysSubjectImpl.java
deleted file mode 100644
index 2982679..0000000
--- a/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/internal/services/ArraysSubjectImpl.java
+++ /dev/null
@@ -1,23 +0,0 @@
-// Copyright 2008 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.services;
-
-public class ArraysSubjectImpl implements ArraysSubject
-{
-    public String[] operation(String[] inputs)
-    {
-        return inputs;
-    }
-}

http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/a548a76a/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/internal/services/AspectInterceptorBuilderImplTest.java
----------------------------------------------------------------------
diff --git a/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/internal/services/AspectInterceptorBuilderImplTest.java b/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/internal/services/AspectInterceptorBuilderImplTest.java
deleted file mode 100644
index 34591df..0000000
--- a/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/internal/services/AspectInterceptorBuilderImplTest.java
+++ /dev/null
@@ -1,213 +0,0 @@
-// Copyright 2008, 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.services;
-
-import org.apache.tapestry5.ioc.internal.IOCInternalTestCase;
-import org.apache.tapestry5.ioc.services.AspectDecorator;
-import org.apache.tapestry5.ioc.services.AspectInterceptorBuilder;
-import org.apache.tapestry5.plastic.MethodAdvice;
-import org.apache.tapestry5.plastic.MethodInvocation;
-import org.testng.annotations.BeforeClass;
-import org.testng.annotations.Test;
-
-import java.lang.reflect.Method;
-
-/**
- * Tests a few edge and error cases not covered by {@link org.apache.tapestry5.ioc.internal.services.LoggingDecoratorImplTest}.
- */
-public class AspectInterceptorBuilderImplTest extends IOCInternalTestCase
-{
-    private AspectDecorator decorator;
-
-    @BeforeClass
-    public void setup()
-    {
-        decorator = getService(AspectDecorator.class);
-    }
-
-    public interface Subject
-    {
-        void advised();
-
-        void notAdvised();
-    }
-
-    @Test
-    public void some_methods_not_intercepted() throws Exception
-    {
-        Subject delegate = mockSubject();
-
-        MethodAdvice advice = new MethodAdvice()
-        {
-            public void advise(MethodInvocation invocation)
-            {
-                assertEquals(invocation.getMethod().getName(), "advised");
-
-                invocation.proceed();
-            }
-        };
-
-        delegate.advised();
-        delegate.notAdvised();
-
-        replay();
-
-        AspectInterceptorBuilder<Subject> builder = decorator.createBuilder(Subject.class, delegate, "<Subject>");
-
-        builder.adviseMethod(Subject.class.getMethod("advised"), advice);
-
-        Subject interceptor = builder.build();
-
-        interceptor.advised();
-        interceptor.notAdvised();
-
-        verify();
-    }
-
-    @Test
-    public void method_not_in_service_interface() throws Exception
-    {
-        Subject delegate = mockSubject();
-
-        MethodAdvice advice = mockAdvice();
-
-        replay();
-
-        AspectInterceptorBuilder<Subject> builder = decorator.createBuilder(Subject.class, delegate, "<Subject>");
-
-        // This method doesn't belong.
-
-        try
-        {
-            builder.adviseMethod(Runnable.class.getMethod("run"), advice);
-
-            unreachable();
-        } catch (IllegalArgumentException ex)
-        {
-            assertEquals(ex.getMessage(),
-                    "Method public abstract void java.lang.Runnable.run() is not defined for interface interface org.apache.tapestry5.ioc.internal.services.AspectInterceptorBuilderImplTest$Subject.");
-        }
-
-
-        verify();
-    }
-
-    @Test
-    public void method_with_duplicate_advice() throws Exception
-    {
-        TextTransformer delegate = new TextTransformer()
-        {
-            public String transform(String input)
-            {
-                return input;
-            }
-        };
-
-        MethodAdvice stripFirstLetter = new MethodAdvice()
-        {
-            public void advise(MethodInvocation invocation)
-            {
-                String param = (String) invocation.getParameter(0);
-
-                invocation.setParameter(0, param.substring(1));
-
-                invocation.proceed();
-            }
-        };
-
-        MethodAdvice reverse = new MethodAdvice()
-        {
-            public void advise(MethodInvocation invocation)
-            {
-                String param = (String) invocation.getParameter(0);
-
-                char[] input = param.toCharArray();
-                int count = input.length;
-
-                char[] output = new char[count];
-
-                for (int i = 0; i < count; i++)
-                    output[count - i - 1] = input[i];
-
-                invocation.setParameter(0, new String(output));
-
-                invocation.proceed();
-            }
-        };
-
-        AspectInterceptorBuilder<TextTransformer> builder = decorator.createBuilder(TextTransformer.class, delegate,
-                "<TextTransformer>");
-
-        Method method = TextTransformer.class.getMethod("transform", String.class);
-        builder.adviseMethod(method, stripFirstLetter);
-        builder.adviseMethod(method, reverse);
-
-        TextTransformer advised = builder.build();
-
-        // strip first letter THEN reverse
-        assertEquals(advised.transform("Tapestry"), "yrtsepa");
-    }
-
-    @Test
-    public void arrays_as_parameters_and_result()
-    {
-        ArraysSubject delegate = new ArraysSubjectImpl();
-
-        MethodAdvice advice = new MethodAdvice()
-        {
-            public void advise(MethodInvocation invocation)
-            {
-                String[] param = (String[]) invocation.getParameter(0);
-
-                for (int i = 0; i < param.length; i++)
-                {
-                    param[i] = param[i].toUpperCase();
-                }
-
-                invocation.proceed();
-
-                String[] result = (String[]) invocation.getReturnValue();
-
-                for (int i = 0; i < result.length; i++)
-                {
-                    result[i] = i + ":" + result[i];
-                }
-            }
-        };
-
-        AspectInterceptorBuilder<ArraysSubject> builder = decorator.createBuilder(ArraysSubject.class, delegate, "whatever");
-
-        builder.adviseAllMethods(advice);
-
-        ArraysSubject advised = builder.build();
-
-        String[] inputs = {"Fred", "Barney"};
-
-        String[] result = advised.operation(inputs);
-
-        assertEquals(result[0], "0:FRED");
-        assertEquals(result[1], "1:BARNEY");
-    }
-
-    protected final MethodAdvice mockAdvice()
-    {
-        return newMock(MethodAdvice.class);
-    }
-
-    protected final Subject mockSubject()
-    {
-        return newMock(Subject.class);
-    }
-}