tapestry-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From hls...@apache.org
Subject [5/44] git commit: Convert TestNG to Spock
Date Wed, 16 May 2012 18:50:31 GMT
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/45bda7b4
Tree: http://git-wip-us.apache.org/repos/asf/tapestry-5/tree/45bda7b4
Diff: http://git-wip-us.apache.org/repos/asf/tapestry-5/diff/45bda7b4

Branch: refs/heads/master
Commit: 45bda7b4729ff123d8bc3739b30c86dc6c28fccf
Parents: 7859756
Author: Howard M. Lewis Ship <hlship@gmail.com>
Authored: Wed May 9 17:03:29 2012 -0700
Committer: Howard M. Lewis Ship <hlship@apache.org>
Committed: Wed May 16 11:50:14 2012 -0700

----------------------------------------------------------------------
 .../ioc/internal/services/BridgeBuilderSpec.groovy |  165 ++++++++++++
 .../ioc/internal/services/BridgeBuilderTest.java   |  207 ---------------
 2 files changed, 165 insertions(+), 207 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/45bda7b4/tapestry-ioc/src/test/groovy/org/apache/tapestry5/ioc/internal/services/BridgeBuilderSpec.groovy
----------------------------------------------------------------------
diff --git a/tapestry-ioc/src/test/groovy/org/apache/tapestry5/ioc/internal/services/BridgeBuilderSpec.groovy
b/tapestry-ioc/src/test/groovy/org/apache/tapestry5/ioc/internal/services/BridgeBuilderSpec.groovy
new file mode 100644
index 0000000..2b65ace
--- /dev/null
+++ b/tapestry-ioc/src/test/groovy/org/apache/tapestry5/ioc/internal/services/BridgeBuilderSpec.groovy
@@ -0,0 +1,165 @@
+package org.apache.tapestry5.ioc.internal.services
+
+import org.apache.tapestry5.ioc.AbstractSharedRegistrySpecification
+import org.apache.tapestry5.ioc.services.PlasticProxyFactory
+import org.slf4j.Logger
+import spock.lang.Shared
+
+class BridgeBuilderSpec extends AbstractSharedRegistrySpecification {
+
+  @Shared
+  PlasticProxyFactory proxyFactory;
+
+  def setupSpec() {
+    proxyFactory = getService PlasticProxyFactory
+  }
+
+  def "toString() of proxy is as expected"() {
+    Logger logger = Mock()
+    StandardFilter sf = Mock()
+    StandardService ss = Mock()
+
+    BridgeBuilder builder = new BridgeBuilder(logger, StandardService, StandardFilter, proxyFactory)
+
+    when:
+
+    def bridge = builder.instantiateBridge(ss, sf)
+
+    then:
+
+    bridge.toString() == "<PipelineBridge from org.apache.tapestry5.ioc.internal.services.StandardService
to org.apache.tapestry5.ioc.internal.services.StandardFilter>"
+  }
+
+  def "standard service and interface"() {
+    Logger logger = Mock()
+    StandardFilter sf = Mock()
+    StandardService ss = Mock()
+
+    BridgeBuilder builder = new BridgeBuilder(logger, StandardService, StandardFilter, proxyFactory)
+    def bridge = builder.instantiateBridge(ss, sf)
+
+    when:
+
+    assert bridge.run(5) == 18
+
+    // 18 =  3 * (5 + 1)
+    // so the filter runs first, and passes 6 to the service
+    // seems there's an issue in Spock with chaining mocks this way
+
+    then:
+
+    1 * sf.run(_, _) >> { i, service -> service.run(i + 1) }
+
+    1 * ss.run(_) >> { i -> 3 * i }
+
+    0 * _
+  }
+
+  def "when toString() is part of service interface, it is forwarded through the filter"()
{
+    Logger logger = Mock()
+
+    ToStringService service = new ToStringService() {
+      String toString() { "Service" }
+    }
+
+    ToStringFilter filter = new ToStringFilter() {
+      String toString(ToStringService s) {
+        s.toString().toUpperCase()
+      }
+    }
+
+    BridgeBuilder builder = new BridgeBuilder(logger, ToStringService, ToStringFilter, proxyFactory)
+
+    when:
+
+    ToStringService bridge = builder.instantiateBridge(service, filter)
+
+    then:
+
+    bridge.toString() == "SERVICE"
+  }
+
+  def "unmatched service interface method is logged and exception thrown"() {
+    Logger logger = Mock()
+    ExtraServiceMethod next = Mock()
+    Serializable filter = Mock()
+
+    BridgeBuilder builder = new BridgeBuilder(logger, ExtraServiceMethod, Serializable, proxyFactory)
+
+    when:
+
+    ExtraServiceMethod esm = builder.instantiateBridge(next, filter)
+
+    then:
+
+    1 * logger.error("Method void extraServiceMethod() has no match in filter interface java.io.Serializable.")
+
+    when:
+
+    esm.extraServiceMethod()
+
+    then:
+
+    RuntimeException e = thrown()
+
+    e.message == "Method void extraServiceMethod() has no match in filter interface java.io.Serializable."
+  }
+
+  def "extra methods in filter interface are logged and ignored"() {
+    Logger logger = Mock()
+    Serializable next = Mock()
+    ExtraFilterMethod filter = Mock()
+
+    BridgeBuilder builder = new BridgeBuilder(logger, Serializable, ExtraFilterMethod, proxyFactory)
+
+    when:
+
+    assert builder.instantiateBridge(next, filter) != null
+
+    then:
+
+    1 * logger.error("Method void extraFilterMethod() of filter interface org.apache.tapestry5.ioc.internal.services.ExtraFilterMethod
does not have a matching method in java.io.Serializable.")
+
+    0 * _
+  }
+
+  def "the service parameter may be a middle parameter of the filter method"() {
+    Logger logger = Mock()
+
+    MiddleFilter mf = new MiddleFilter() {
+      @Override
+      void execute(int count, char ch, MiddleService service, StringBuilder buffer) {
+        service.execute(count, ch, buffer)
+
+        buffer.append(' ')
+
+        service.execute(count + 1, Character.toUpperCase(ch), buffer)
+      }
+    }
+
+    MiddleService ms = new MiddleService() {
+      @Override
+      void execute(int count, char ch, StringBuilder buffer) {
+        count.times() { buffer.append ch }
+      }
+    }
+
+    BridgeBuilder builder = new BridgeBuilder(logger, MiddleService, MiddleFilter, proxyFactory)
+
+
+    MiddleService bridge = builder.instantiateBridge(ms, mf)
+
+    StringBuilder buffer = new StringBuilder("CODE: ")
+
+    when:
+
+
+    bridge.execute(3, 'a' as char, buffer)
+
+    then:
+
+    buffer.toString() == "CODE: aaa AAAA"
+
+  }
+
+}

http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/45bda7b4/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/internal/services/BridgeBuilderTest.java
----------------------------------------------------------------------
diff --git a/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/internal/services/BridgeBuilderTest.java
b/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/internal/services/BridgeBuilderTest.java
deleted file mode 100644
index cf910de..0000000
--- a/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/internal/services/BridgeBuilderTest.java
+++ /dev/null
@@ -1,207 +0,0 @@
-// Copyright 2006, 2007, 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 java.io.Serializable;
-
-import org.apache.tapestry5.ioc.internal.IOCInternalTestCase;
-import org.apache.tapestry5.ioc.services.PlasticProxyFactory;
-import org.slf4j.Logger;
-import org.testng.annotations.BeforeClass;
-import org.testng.annotations.Test;
-
-public class BridgeBuilderTest extends IOCInternalTestCase
-{
-    private PlasticProxyFactory proxyFactory;
-
-    @BeforeClass
-    public void setup()
-    {
-        proxyFactory = getService(PlasticProxyFactory.class);
-    }
-
-    @Test
-    public void standard_interface_and_filter()
-    {
-        Logger logger = mockLogger();
-
-        replay();
-
-        BridgeBuilder<StandardService, StandardFilter> bb = new BridgeBuilder<StandardService,
StandardFilter>(logger,
-                StandardService.class, StandardFilter.class, proxyFactory);
-
-        StandardFilter sf = new StandardFilter()
-        {
-            public int run(int i, StandardService ss)
-            {
-                return ss.run(i + 1);
-            }
-        };
-
-        StandardService ss = new StandardService()
-        {
-            public int run(int i)
-            {
-                return i * 3;
-            }
-        };
-
-        StandardService bridge = bb.instantiateBridge(ss, sf);
-
-        // The filter adds 1, then the service multiplies by 3.
-        // (5 +_1) * 3 = 18.
-
-        assertEquals(bridge.run(5), 18);
-
-        // Since toString() is not part of the service interface,
-        // it will be implemented in the bridge.
-
-        assertEquals(
-                bridge.toString(),
-                "<PipelineBridge from org.apache.tapestry5.ioc.internal.services.StandardService
to org.apache.tapestry5.ioc.internal.services.StandardFilter>");
-
-        verify();
-    }
-
-    @Test
-    public void toString_part_of_service_interface()
-    {
-        Logger logger = mockLogger();
-
-        replay();
-
-        BridgeBuilder<ToStringService, ToStringFilter> bb = new BridgeBuilder<ToStringService,
ToStringFilter>(logger,
-                ToStringService.class, ToStringFilter.class, proxyFactory);
-
-        ToStringFilter f = new ToStringFilter()
-        {
-            public String toString(ToStringService s)
-            {
-                return s.toString().toUpperCase();
-            }
-        };
-
-        ToStringService s = new ToStringService()
-        {
-            @Override
-            public String toString()
-            {
-                return "Service";
-            }
-        };
-
-        ToStringService bridge = bb.instantiateBridge(s, f);
-
-        assertEquals("SERVICE", bridge.toString());
-
-        verify();
-    }
-
-    @Test
-    public void service_interface_method_not_matched_in_filter_interface()
-    {
-        Logger logger = mockLogger();
-        ExtraServiceMethod next = newMock(ExtraServiceMethod.class);
-        Serializable filter = newMock(Serializable.class);
-
-        logger.error("Method void extraServiceMethod() has no match in filter interface java.io.Serializable.");
-
-        replay();
-
-        BridgeBuilder<ExtraServiceMethod, Serializable> bb = new BridgeBuilder<ExtraServiceMethod,
Serializable>(
-                logger, ExtraServiceMethod.class, Serializable.class, proxyFactory);
-
-        ExtraServiceMethod esm = bb.instantiateBridge(next, filter);
-
-        try
-        {
-            esm.extraServiceMethod();
-            unreachable();
-        }
-        catch (RuntimeException ex)
-        {
-            assertEquals(ex.getMessage(),
-                    "Method void extraServiceMethod() has no match in filter interface java.io.Serializable.");
-        }
-
-        verify();
-    }
-
-    @Test
-    public void filter_interface_contains_extra_methods()
-    {
-        Logger logger = mockLogger();
-        Serializable next = newMock(Serializable.class);
-        ExtraFilterMethod filter = newMock(ExtraFilterMethod.class);
-
-        logger.error("Method void extraFilterMethod() of filter interface "
-                + "org.apache.tapestry5.ioc.internal.services.ExtraFilterMethod does not
have a matching method "
-                + "in java.io.Serializable.");
-
-        replay();
-
-        BridgeBuilder<Serializable, ExtraFilterMethod> bb = new BridgeBuilder<Serializable,
ExtraFilterMethod>(logger,
-                Serializable.class, ExtraFilterMethod.class, proxyFactory);
-
-        assertNotNull(bb.instantiateBridge(next, filter));
-
-        verify();
-    }
-
-    @Test
-    public void service_parameter_in_middle_of_filter_method()
-    {
-        Logger logger = mockLogger();
-
-        replay();
-
-        BridgeBuilder<MiddleService, MiddleFilter> bb = new BridgeBuilder<MiddleService,
MiddleFilter>(logger,
-                MiddleService.class, MiddleFilter.class, proxyFactory);
-
-        MiddleFilter mf = new MiddleFilter()
-        {
-            public void execute(int count, char ch, MiddleService service, StringBuilder
buffer)
-            {
-                service.execute(count, ch, buffer);
-
-                buffer.append(' ');
-
-                service.execute(count + 1, Character.toUpperCase(ch), buffer);
-
-            }
-        };
-
-        MiddleService ms = new MiddleService()
-        {
-            public void execute(int count, char ch, StringBuilder buffer)
-            {
-                for (int i = 0; i < count; i++)
-                    buffer.append(ch);
-            }
-        };
-
-        // This also tests building the bridge methods with a void return type.
-
-        MiddleService bridge = bb.instantiateBridge(ms, mf);
-
-        StringBuilder buffer = new StringBuilder("CODE: ");
-
-        bridge.execute(3, 'a', buffer);
-
-        assertEquals("CODE: aaa AAAA", buffer.toString());
-
-        verify();
-    }
-}


Mime
View raw message