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
[4/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/67dcb2fa
Tree: http://git-wip-us.apache.org/repos/asf/tapestry-5/tree/67dcb2fa
Diff: http://git-wip-us.apache.org/repos/asf/tapestry-5/diff/67dcb2fa
Branch: refs/heads/master
Commit: 67dcb2fa6c1ab1c874acdace5b5df2c358367b5d
Parents: ecbc9ed
Author: Howard M. Lewis Ship <hl...@gmail.com>
Authored: Fri May 11 09:28:58 2012 -0700
Committer: Howard M. Lewis Ship <hl...@apache.org>
Committed: Wed May 16 11:50:14 2012 -0700
----------------------------------------------------------------------
.../internal/services/PerthreadManagerImpl.java | 3 +-
.../ioc/internal/services/ServiceMessages.java | 6 -
.../internal/services/ServiceStrings.properties | 1 -
.../services/PerthreadManagerImplSpec.groovy | 182 ++++++++++++
.../services/PerthreadManagerImplTest.java | 218 ---------------
5 files changed, 184 insertions(+), 226 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/67dcb2fa/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/services/PerthreadManagerImpl.java
----------------------------------------------------------------------
diff --git a/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/services/PerthreadManagerImpl.java b/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/services/PerthreadManagerImpl.java
index 54ef8f6..0ff0727 100644
--- a/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/services/PerthreadManagerImpl.java
+++ b/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/services/PerthreadManagerImpl.java
@@ -104,7 +104,8 @@ public class PerthreadManagerImpl implements PerthreadManager
listener.threadDidCleanup();
} catch (Exception ex)
{
- logger.warn(ServiceMessages.threadCleanupError(listener, ex), ex);
+ logger.warn(String.format("Error invoking listener %s: %s", listener, ex),
+ ex);
}
}
http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/67dcb2fa/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/services/ServiceMessages.java
----------------------------------------------------------------------
diff --git a/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/services/ServiceMessages.java b/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/services/ServiceMessages.java
index 0c3b50b..f35aaa5 100644
--- a/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/services/ServiceMessages.java
+++ b/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/services/ServiceMessages.java
@@ -17,7 +17,6 @@ package org.apache.tapestry5.ioc.internal.services;
import org.apache.tapestry5.ioc.Messages;
import org.apache.tapestry5.ioc.internal.util.MessagesImpl;
import org.apache.tapestry5.ioc.services.Coercion;
-import org.apache.tapestry5.ioc.services.ThreadCleanupListener;
import org.apache.tapestry5.plastic.PlasticUtils;
public class ServiceMessages
@@ -28,11 +27,6 @@ public class ServiceMessages
{
}
- public static String threadCleanupError(ThreadCleanupListener listener, Throwable cause)
- {
- return MESSAGES.format("thread-cleanup-error", listener, cause);
- }
-
public static String noSuchProperty(Class clazz, String propertyName)
{
return MESSAGES.format("no-such-property", clazz.getName(), propertyName);
http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/67dcb2fa/tapestry-ioc/src/main/resources/org/apache/tapestry5/ioc/internal/services/ServiceStrings.properties
----------------------------------------------------------------------
diff --git a/tapestry-ioc/src/main/resources/org/apache/tapestry5/ioc/internal/services/ServiceStrings.properties b/tapestry-ioc/src/main/resources/org/apache/tapestry5/ioc/internal/services/ServiceStrings.properties
index 1ce3799..2bc3922 100644
--- a/tapestry-ioc/src/main/resources/org/apache/tapestry5/ioc/internal/services/ServiceStrings.properties
+++ b/tapestry-ioc/src/main/resources/org/apache/tapestry5/ioc/internal/services/ServiceStrings.properties
@@ -12,7 +12,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.
-thread-cleanup-error=Error invoking listener %s: %s
no-such-property=Class %s does not contain a property named '%s'.
read-not-supported=Class %s does not provide an accessor ('getter') method for property '%s'.
write-not-supported=Class %s does not provide an mutator ('setter') method for property '%s'.
http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/67dcb2fa/tapestry-ioc/src/test/groovy/org/apache/tapestry5/ioc/internal/services/PerthreadManagerImplSpec.groovy
----------------------------------------------------------------------
diff --git a/tapestry-ioc/src/test/groovy/org/apache/tapestry5/ioc/internal/services/PerthreadManagerImplSpec.groovy b/tapestry-ioc/src/test/groovy/org/apache/tapestry5/ioc/internal/services/PerthreadManagerImplSpec.groovy
new file mode 100644
index 0000000..63acf36
--- /dev/null
+++ b/tapestry-ioc/src/test/groovy/org/apache/tapestry5/ioc/internal/services/PerthreadManagerImplSpec.groovy
@@ -0,0 +1,182 @@
+package org.apache.tapestry5.ioc.internal.services
+
+import org.apache.tapestry5.ioc.Invokable
+import org.apache.tapestry5.ioc.services.ThreadCleanupListener
+import org.slf4j.Logger
+import spock.lang.Specification
+
+class PerthreadManagerImplSpec extends Specification {
+
+ def "nothing is logged when cleaning up with no listeners"() {
+ Logger logger = Mock()
+
+ def manager = new PerthreadManagerImpl(logger)
+
+ when:
+
+ manager.cleanup()
+
+ then:
+
+ 0 * _
+ }
+
+ def "listeners will only be invoked a single time, then discarded"() {
+ Logger logger = Mock()
+ ThreadCleanupListener listener = Mock()
+
+ def manager = new PerthreadManagerImpl(logger)
+
+ when:
+
+ manager.addThreadCleanupListener(listener)
+ manager.cleanup()
+
+ then:
+
+ 1 * listener.threadDidCleanup()
+ 0 * _
+
+ when:
+
+ manager.cleanup()
+
+ then:
+
+ 0 * _
+ }
+
+ def "exceptions during thread cleanup are logged and other listeners still invoked"() {
+ RuntimeException t = new RuntimeException("Boom!")
+ Logger logger = Mock()
+ ThreadCleanupListener l1 = Mock()
+ ThreadCleanupListener l2 = Mock()
+
+ def manager = new PerthreadManagerImpl(logger)
+
+ manager.addThreadCleanupListener(l1)
+ manager.addThreadCleanupListener(l2)
+
+ when:
+
+ manager.cleanup()
+
+ then:
+
+ 1 * l1.threadDidCleanup() >> { throw t }
+ 1 * logger.warn({ it.contains "Error invoking listener"}, t)
+
+ then:
+
+ 1 * l2.threadDidCleanup()
+ 0 * _
+ }
+
+ def "PerThreadValue does not initially exist"() {
+ Logger logger = Mock()
+ def manager = new PerthreadManagerImpl(logger)
+
+ when:
+
+ def value = manager.createValue()
+
+ then:
+
+ !value.exists()
+ value.get() == null
+
+ when:
+
+ value.set(this)
+
+ then:
+
+ value.exists()
+ value.get() == this
+ }
+
+ def "PerThreadValue.get() with default returns the default value when the value does not exist"() {
+ Logger logger = Mock()
+ def manager = new PerthreadManagerImpl(logger)
+ def defaultValue = new Object()
+ def nonNull = new Object()
+
+ when:
+
+ def value = manager.createValue()
+
+ then:
+
+ value.get(defaultValue).is(defaultValue)
+
+ when:
+
+ value.set(null)
+
+ then:
+
+ value.exists()
+ value.get(defaultValue) == null
+
+ when:
+
+ value.set(nonNull)
+
+ then:
+
+ value.get(defaultValue).is(nonNull)
+ }
+
+ def "PerthreadManager.run() performs an implicit cleanup"() {
+ Logger logger = Mock()
+ ThreadCleanupListener listener = Mock()
+
+ def manager = new PerthreadManagerImpl(logger)
+ manager.addThreadCleanupListener listener
+ def value = manager.createValue()
+ def didRun = false
+
+ def runnable = {
+ didRun = true
+ value.set "bar"
+ }
+
+ when:
+
+ manager.run runnable
+
+ then:
+
+ 1 * listener.threadDidCleanup()
+
+ didRun
+ !value.exists()
+ }
+
+ def "PerthreadManager.invoke() performs an implicit cleanup"() {
+ Logger logger = Mock()
+ ThreadCleanupListener listener = Mock()
+
+ def manager = new PerthreadManagerImpl(logger)
+ manager.addThreadCleanupListener listener
+ def value = manager.createValue()
+
+ def inv = {
+ value.set "bar"
+ return "baz"
+ } as Invokable
+
+ when:
+
+ assert manager.invoke(inv) == "baz"
+
+ then:
+
+ 1 * listener.threadDidCleanup()
+
+ !value.exists()
+
+ }
+
+
+}
http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/67dcb2fa/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/internal/services/PerthreadManagerImplTest.java
----------------------------------------------------------------------
diff --git a/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/internal/services/PerthreadManagerImplTest.java b/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/internal/services/PerthreadManagerImplTest.java
deleted file mode 100644
index 2031652..0000000
--- a/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/internal/services/PerthreadManagerImplTest.java
+++ /dev/null
@@ -1,218 +0,0 @@
-// Copyright 2006, 2007, 2008, 2010, 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.Invokable;
-import org.apache.tapestry5.ioc.services.PerThreadValue;
-import org.apache.tapestry5.ioc.services.ThreadCleanupListener;
-import org.apache.tapestry5.ioc.test.IOCTestCase;
-import org.slf4j.Logger;
-import org.testng.annotations.Test;
-
-public class PerthreadManagerImplTest extends IOCTestCase
-{
- @Test
- public void no_listeners()
- {
- Logger logger = mockLogger();
-
- replay();
-
- new PerthreadManagerImpl(logger).cleanup();
-
- verify();
- }
-
- @Test
- public void listeners_are_one_shot()
- {
- Logger logger = mockLogger();
- ThreadCleanupListener listener = mockThreadCleanupListener();
-
- listener.threadDidCleanup();
-
- replay();
-
- PerthreadManagerImpl hub = new PerthreadManagerImpl(logger);
-
- hub.addThreadCleanupListener(listener);
-
- hub.cleanup();
-
- verify();
-
- // No more training.
-
- replay();
-
- // Listener not invoked.
-
- hub.cleanup();
-
- verify();
- }
-
- private ThreadCleanupListener mockThreadCleanupListener()
- {
- return newMock(ThreadCleanupListener.class);
- }
-
- @Test
- public void listener_cleanup_failure()
- {
- final RuntimeException t = new RuntimeException("Boom!");
-
- Logger logger = mockLogger();
-
- ThreadCleanupListener listener = new ThreadCleanupListener()
- {
-
- public void threadDidCleanup()
- {
- throw t;
- }
-
- };
-
- logger.warn(ServiceMessages.threadCleanupError(listener, t), t);
-
- replay();
-
- PerthreadManagerImpl hub = new PerthreadManagerImpl(logger);
-
- hub.addThreadCleanupListener(listener);
-
- hub.cleanup();
-
- verify();
- }
-
- // @Test
- // public void listener_list_is_per_thread()
- // {
- // ThreadCleanupListener l1 = newThreadCleanupListener();
- // final ThreadCleanupListener l2 = newThreadCleanupListener();
- //
- // Thread thread = new Thread();
- //
- // l1.threadDidCleanup();
- //
- // replay();
- //
- // final PerthreadManager hub = new PerthreadManagerImpl(log);
- //
- // hub.addThreadCleanupListener(l1);
- //
- // hub.cleanup();
- //
- // verify();
- // }
-
- @Test
- public void per_thread_value()
- {
- Object key = new Object();
- Object value = "Tapestry";
-
- PerthreadManagerImpl m = new PerthreadManagerImpl(null);
-
- PerThreadValue<Object> v = m.createValue(key);
-
- assertFalse(v.exists());
- assertNull(v.get());
-
- v.set(value);
-
- assertTrue(v.exists());
- assertSame(v.get(), value);
- }
-
- @Test
- public void get_with_default()
- {
- PerthreadManagerImpl m = new PerthreadManagerImpl(null);
-
- PerThreadValue<Object> v = m.createValue(new Object());
-
- Object def = new Object();
-
- assertSame(v.get(def), def);
-
- v.set(null);
-
- assertNull(v.get(def));
-
- Object x = new Object();
-
- v.set(x);
-
- assertSame(v.get(def), x);
- }
-
- @Test
- public void per_thread_null()
- {
- PerthreadManagerImpl m = new PerthreadManagerImpl(null);
-
- PerThreadValue<Object> v = m.createValue(new Object());
-
- v.set(null);
-
- assertTrue(v.exists());
-
- assertNull(v.get());
- }
-
- @Test
- public void run_performs_cleanup()
- {
- final PerthreadManagerImpl m = new PerthreadManagerImpl(null);
-
- final PerThreadValue<String> v = m.createValue();
-
- m.run(new Runnable()
- {
- public void run()
- {
- v.set("bar");
- }
- });
-
- assertNull(v.get());
- }
-
- @Test
- public void invoke_performs_cleanup()
- {
- final PerthreadManagerImpl m = new PerthreadManagerImpl(null);
-
- final PerThreadValue<String> v = m.createValue();
-
- String actual = m.invoke(new Invokable<String>()
- {
- public String invoke()
- {
- v.set("bar");
-
- return "baz";
- }
- });
-
- assertEquals(actual, "baz");
-
- assertNull(v.get());
-
- }
-}