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

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

Branch: refs/heads/master
Commit: a115ef5c740dfb01554cdd58bcc6485c8a08cc00
Parents: e775574
Author: Howard M. Lewis Ship <hl...@gmail.com>
Authored: Tue May 8 10:08:58 2012 -0700
Committer: Howard M. Lewis Ship <hl...@apache.org>
Committed: Wed May 16 11:50:13 2012 -0700

----------------------------------------------------------------------
 .../ioc/internal/LoggingDecoratorImplSpec.groovy   |  169 ++++++++++
 .../services/LoggingDecoratorImplTest.java         |  243 ---------------
 2 files changed, 169 insertions(+), 243 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/a115ef5c/tapestry-ioc/src/test/groovy/org/apache/tapestry5/ioc/internal/LoggingDecoratorImplSpec.groovy
----------------------------------------------------------------------
diff --git a/tapestry-ioc/src/test/groovy/org/apache/tapestry5/ioc/internal/LoggingDecoratorImplSpec.groovy b/tapestry-ioc/src/test/groovy/org/apache/tapestry5/ioc/internal/LoggingDecoratorImplSpec.groovy
new file mode 100644
index 0000000..f37d5b5
--- /dev/null
+++ b/tapestry-ioc/src/test/groovy/org/apache/tapestry5/ioc/internal/LoggingDecoratorImplSpec.groovy
@@ -0,0 +1,169 @@
+package org.apache.tapestry5.ioc.internal
+
+import org.apache.tapestry5.ioc.AbstractSharedRegistrySpecification
+import org.apache.tapestry5.ioc.services.LoggingDecorator
+import org.slf4j.Logger
+import org.xml.sax.SAXParseException
+
+interface AdderService {
+  long add(long operand1, long operand2);
+}
+
+interface ExceptionService {
+  void parse() throws SAXParseException;
+}
+
+class LoggingDecoratorImplSpec extends AbstractSharedRegistrySpecification {
+
+  LoggingDecorator decorator = getService LoggingDecorator
+
+  Logger logger = Mock()
+
+  def "logging of void method"() {
+
+    _ * logger.debugEnabled >> true
+
+    Runnable delegate = Mock()
+
+    Runnable interceptor = decorator.build(Runnable, delegate, "foo.Bar", logger)
+
+    when:
+
+    interceptor.run()
+
+    then:
+
+    1 * logger.debug("[ENTER] run()")
+
+    then:
+
+    1 * delegate.run()
+
+    then:
+
+    1 * logger.debug("[ EXIT] run")
+
+    interceptor.toString() == "<Logging interceptor for foo.Bar(java.lang.Runnable)>"
+  }
+
+  def "runtime exception inside method is logged"() {
+    _ * logger.debugEnabled >> true
+
+    Runnable delegate = Mock()
+
+    Runnable interceptor = decorator.build(Runnable, delegate, "foo.Bar", logger)
+
+    def t = new RuntimeException("From delegate.")
+
+    when:
+
+    interceptor.run()
+
+    then:
+
+    1 * logger.debug("[ENTER] run()")
+
+    then:
+
+    1 * delegate.run() >> {
+      throw t
+    }
+
+    then:
+
+    1 * logger.debug("[ FAIL] run -- ${RuntimeException.name}", t)
+
+    then:
+
+    RuntimeException e = thrown()
+
+    e.is t
+  }
+
+  def "method throws checked exception"() {
+    Throwable t = new SAXParseException("From delegate.", null)
+    _ * logger.debugEnabled >> true
+    ExceptionService delegate = Mock()
+
+    ExceptionService service = decorator.build(ExceptionService, delegate, "MyService", logger)
+
+    when:
+
+    service.parse()
+
+    then:
+
+    Throwable actual = thrown()
+
+    actual.is(t)
+
+    1 * logger.debug("[ENTER] parse()")
+
+    1 * delegate.parse() >> { throw t }
+
+    1 * logger.debug("[ FAIL] parse -- ${SAXParseException.name}", t)
+  }
+
+  def "handling of object parameter and return type"() {
+    _ * logger.debugEnabled >> true
+
+    UpcaseService delegate = Mock()
+
+    UpcaseService service = decorator.build(UpcaseService, delegate, "MyService", logger)
+
+    when:
+
+    assert service.upcase("barney") == "BARNEY"
+
+    then:
+
+    1 * logger.debug('[ENTER] upcase("barney")')
+
+    1 * delegate.upcase(_) >> { args -> args[0].toUpperCase() }
+
+    1 * logger.debug('[ EXIT] upcase ["BARNEY"]')
+  }
+
+  def "handling of primitive parameter and return type"() {
+    _ * logger.debugEnabled >> true
+
+    AdderService delegate = Mock()
+
+    AdderService service = decorator.build(AdderService, delegate, "Adder", logger)
+
+    when:
+
+    assert service.add(6, 13) == 19
+
+    then:
+
+    1 * logger.debug("[ENTER] add(6, 13)")
+
+    1 * delegate.add(_, _) >> { args -> args[0] + args[1] }
+
+    1 * logger.debug("[ EXIT] add [19]")
+  }
+
+  def "toString() method of service interface is delegated"() {
+    _ * logger.debugEnabled >> true
+
+    // Spock's Mocking doesn't seem to be as savvy as Tapestry's about letting toString()
+    // delegate through, so we can't implement ToStringService as a Mock
+
+    ToStringService delegate = new ToStringService() {
+      String toString() { "FROM DELEGATE" }
+    }
+
+    ToStringService service = decorator.build(ToStringService, delegate, "ToString", logger)
+
+    when:
+
+    assert service.toString() == "FROM DELEGATE"
+
+    then:
+
+    1 * logger.debug("[ENTER] toString()")
+    1 * logger.debug('[ EXIT] toString ["FROM DELEGATE"]')
+  }
+
+}

http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/a115ef5c/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/internal/services/LoggingDecoratorImplTest.java
----------------------------------------------------------------------
diff --git a/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/internal/services/LoggingDecoratorImplTest.java b/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/internal/services/LoggingDecoratorImplTest.java
deleted file mode 100644
index 7c6c7f4..0000000
--- a/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/internal/services/LoggingDecoratorImplTest.java
+++ /dev/null
@@ -1,243 +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.services;
-
-import org.apache.tapestry5.ioc.internal.IOCInternalTestCase;
-import org.apache.tapestry5.ioc.services.AspectDecorator;
-import org.apache.tapestry5.ioc.services.LoggingDecorator;
-import org.slf4j.Logger;
-import org.testng.Assert;
-import org.testng.annotations.BeforeClass;
-import org.testng.annotations.Test;
-import org.xml.sax.SAXParseException;
-
-/**
- * Use the LoggingDecorator in a number of ways to verify its behavior. In some ways we are testing the code dynamically
- * generated by the LoggingDecorator as much as we are testing the decorator itself -- one proves the other.
- * <p/>
- * And now this test is used to integration test {@link org.apache.tapestry5.ioc.internal.services.AspectDecoratorImpl}
- * as well.
- */
-public class LoggingDecoratorImplTest extends IOCInternalTestCase
-{
-    private AspectDecorator aspectDecorator;
-
-    @BeforeClass
-    public void setup()
-    {
-        aspectDecorator = getService(AspectDecorator.class);
-    }
-
-    public interface UpcaseService
-    {
-        String upcase(String input);
-    }
-
-    public interface AdderService
-    {
-        long add(long operand1, long operand2);
-    }
-
-    public interface ToStringService
-    {
-        String toString();
-    }
-
-    public interface ExceptionService
-    {
-        void parse() throws SAXParseException;
-    }
-
-    @Test
-    public void void_method()
-    {
-        Logger logger = mockLogger();
-        Runnable delegate = mockRunnable();
-
-        train_isDebugEnabled(logger, true);
-        logger.debug("[ENTER] run()");
-
-        delegate.run();
-
-        logger.debug("[ EXIT] run");
-
-        replay();
-
-        LoggingDecorator ld = newLoggingDecorator();
-        Runnable interceptor = ld.build(Runnable.class, delegate, "foo.Bar", logger);
-
-        interceptor.run();
-
-        assertEquals(
-                interceptor.toString(),
-                "<Logging interceptor for foo.Bar(java.lang.Runnable)>");
-
-        verify();
-    }
-
-    private LoggingDecoratorImpl newLoggingDecorator()
-    {
-        return new LoggingDecoratorImpl(aspectDecorator, new LoggingAdvisorImpl(new ExceptionTrackerImpl()));
-    }
-
-    @Test
-    public void method_throws_runtime_exception()
-    {
-        Throwable t = new RuntimeException("From delegate.");
-        Logger logger = mockLogger();
-        Runnable delegate = mockRunnable();
-
-        train_isDebugEnabled(logger, true);
-        logger.debug("[ENTER] run()");
-
-        delegate.run();
-        setThrowable(t);
-
-        logger.debug("[ FAIL] run -- " + t.getClass().getName(), t);
-
-        replay();
-
-        LoggingDecorator ld = newLoggingDecorator();
-        Runnable interceptor = ld.build(Runnable.class, delegate, "foo.Bar", logger);
-
-        try
-        {
-            interceptor.run();
-            unreachable();
-        }
-        catch (RuntimeException ex)
-        {
-            Assert.assertSame(ex, t);
-        }
-
-        verify();
-    }
-
-    @Test
-    public void method_throws_checked_exception() throws Exception
-    {
-        Throwable t = new SAXParseException("From delegate.", null);
-        Logger logger = mockLogger();
-        ExceptionService delegate = newMock(ExceptionService.class);
-
-        train_isDebugEnabled(logger, true);
-        logger.debug("[ENTER] parse()");
-
-        delegate.parse();
-        setThrowable(t);
-
-        logger.debug("[ FAIL] parse -- " + t.getClass().getName(), t);
-
-        replay();
-
-        LoggingDecorator ld = newLoggingDecorator();
-        ExceptionService interceptor = ld
-                .build(ExceptionService.class, delegate, "foo.Bar", logger);
-
-        try
-        {
-            interceptor.parse();
-            unreachable();
-        }
-        catch (SAXParseException ex)
-        {
-            Assert.assertSame(ex, t);
-        }
-
-        verify();
-    }
-
-    @Test
-    public void object_parameter_and_return_type()
-    {
-        Logger logger = mockLogger();
-        UpcaseService delegate = new UpcaseService()
-        {
-            public String upcase(String input)
-            {
-                return input.toUpperCase();
-            }
-        };
-
-        train_isDebugEnabled(logger, true);
-        logger.debug("[ENTER] upcase(\"barney\")");
-
-        logger.debug("[ EXIT] upcase [\"BARNEY\"]");
-
-        replay();
-
-        LoggingDecorator ld = newLoggingDecorator();
-        UpcaseService interceptor = ld.build(UpcaseService.class, delegate, "foo.Bar", logger);
-
-        assertEquals(interceptor.upcase("barney"), "BARNEY");
-
-        verify();
-    }
-
-    @Test
-    public void primitive_parameter_and_return_type()
-    {
-        Logger logger = mockLogger();
-        AdderService delegate = new AdderService()
-        {
-            public long add(long operand1, long operand2)
-            {
-                return operand1 + operand2;
-            }
-        };
-
-        train_isDebugEnabled(logger, true);
-        logger.debug("[ENTER] add(6, 13)");
-
-        logger.debug("[ EXIT] add [19]");
-
-        replay();
-
-        LoggingDecorator ld = newLoggingDecorator();
-        AdderService interceptor = ld.build(AdderService.class, delegate, "foo.Bar", logger);
-
-        assertEquals(interceptor.add(6, 13), 19);
-
-        verify();
-    }
-
-    @Test
-    public void to_string_method_in_service_interface_is_delegated()
-    {
-        Logger logger = mockLogger();
-        ToStringService delegate = new ToStringService()
-        {
-            @Override
-            public String toString()
-            {
-                return "FROM DELEGATE";
-            }
-        };
-
-        train_isDebugEnabled(logger, true);
-        logger.debug("[ENTER] toString()");
-
-        logger.debug("[ EXIT] toString [\"FROM DELEGATE\"]");
-
-        replay();
-
-        LoggingDecorator ld = newLoggingDecorator();
-        ToStringService interceptor = ld.build(ToStringService.class, delegate, "foo.Bar", logger);
-
-        assertEquals(interceptor.toString(), "FROM DELEGATE");
-
-        verify();
-    }
-}