Updated Branches: refs/heads/master 17f0c48f3 -> bd1da989c
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/bd1da989 Tree: http://git-wip-us.apache.org/repos/asf/tapestry-5/tree/bd1da989 Diff: http://git-wip-us.apache.org/repos/asf/tapestry-5/diff/bd1da989 Branch: refs/heads/master Commit: bd1da989cb90888ade152199cb0a75dc31993937 Parents: f491e98 Author: Howard M. Lewis Ship <[email protected]> Authored: Mon Jun 11 16:22:15 2012 -0700 Committer: Howard M. Lewis Ship <[email protected]> Committed: Mon Jun 11 16:22:15 2012 -0700 ---------------------------------------------------------------------- ...ValidatingMappedConfigurationWrapperSpec.groovy | 152 ++++++++++ .../ValidatingMappedConfigurationWrapperTest.java | 225 --------------- 2 files changed, 152 insertions(+), 225 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/bd1da989/tapestry-ioc/src/test/groovy/org/apache/tapestry5/ioc/internal/ValidatingMappedConfigurationWrapperSpec.groovy ---------------------------------------------------------------------- diff --git a/tapestry-ioc/src/test/groovy/org/apache/tapestry5/ioc/internal/ValidatingMappedConfigurationWrapperSpec.groovy b/tapestry-ioc/src/test/groovy/org/apache/tapestry5/ioc/internal/ValidatingMappedConfigurationWrapperSpec.groovy new file mode 100644 index 0000000..5fec1ff --- /dev/null +++ b/tapestry-ioc/src/test/groovy/org/apache/tapestry5/ioc/internal/ValidatingMappedConfigurationWrapperSpec.groovy @@ -0,0 +1,152 @@ +package org.apache.tapestry5.ioc.internal + +import org.apache.tapestry5.ioc.AbstractSharedRegistrySpecification +import org.apache.tapestry5.ioc.MappedConfiguration +import org.apache.tapestry5.ioc.ObjectLocator +import org.apache.tapestry5.ioc.def.ContributionDef +import org.apache.tapestry5.ioc.services.PlasticProxyFactory + +import java.sql.SQLException + +class ValidatingMappedConfigurationWrapperSpec extends AbstractSharedRegistrySpecification { + + static String SERVICE_ID = "Baz" + + ObjectLocator locator = Mock() + TypeCoercerProxy tc = Mock() + Map keyToContribution = [:] + Map map = [:] + + def "contribute a property key and value"() { + ContributionDef cd = Mock() + def keyToContribution = [:] + ObjectLocator locator = Mock() + def map = [:] + TypeCoercerProxy tc = Mock() + Runnable value = Mock() + + MappedConfiguration config = new ValidatingMappedConfigurationWrapper(Runnable, locator, tc, map, null, SERVICE_ID, cd, Class, keyToContribution) + + when: + + config.add(Integer, value) + + then: + + tc.coerce(value, Runnable) >> value + + map[Integer].is(value) + keyToContribution[Integer].is(cd) + } + + def "an added value may be coerced to the correct type"() { + + ContributionDef cd = Mock() + def value = "coerce-me" + Runnable coerced = Mock() + + MappedConfiguration config = new ValidatingMappedConfigurationWrapper(Runnable, locator, tc, map, null, SERVICE_ID, cd, Class, keyToContribution) + + when: + + config.add(Integer, value) + + then: + + tc.coerce(value, Runnable) >> coerced + + map[Integer].is(coerced) + keyToContribution[Integer].is(cd) + } + + def ContributionDef newContributionDef(methodName) { + + def proxyFactory = getService PlasticProxyFactory + + return new ContributionDefImpl(SERVICE_ID, findMethod(methodName), false, proxyFactory, null, null); + } + + def findMethod(name) { + return this.class.methods.find() { it.name == name } + } + + + public void contributionPlaceholder1() { + + } + + public void contributionPlaceholder2() { + + } + + def "may not contribute a duplicate key"() { + ContributionDef def1 = newContributionDef "contributionPlaceholder1" + ContributionDef def2 = newContributionDef "contributionPlaceholder2" + + keyToContribution[Integer] = def1 + + MappedConfiguration config = new ValidatingMappedConfigurationWrapper(Runnable, locator, tc, map, null, SERVICE_ID, def2, Class, keyToContribution) + + when: + + config.add(Integer, "does-not-matter") + + then: + + IllegalArgumentException e = thrown() + + e.message.contains "Service contribution (to service 'Baz') conflicts with existing contribution" + + keyToContribution[Integer].is(def1) + map.isEmpty() + } + + def "the contributed key may not be null"() { + ContributionDef cd = newContributionDef "contributionPlaceholder1" + + MappedConfiguration config = new ValidatingMappedConfigurationWrapper(Runnable, locator, tc, map, null, SERVICE_ID, cd, Class, keyToContribution) + + when: + + config.add(null, "does-not-matter") + + then: + + NullPointerException e = thrown() + + e.message == "Key for service contribution (to service '$SERVICE_ID') was null." + } + + def "adding a key of the wrong type is an exception"() { + ContributionDef cd = newContributionDef "contributionPlaceholder1" + + MappedConfiguration config = new ValidatingMappedConfigurationWrapper(Runnable, locator, tc, map, null, SERVICE_ID, cd, Class, keyToContribution) + + when: + + config.add("java.util.List", "does-not-matter") + + then: + + IllegalArgumentException e = thrown() + + e.message == "Key for service contribution (to service 'Baz') was an instance of java.lang.String, but the expected key type was java.lang.Class." + } + + def "contributing a null value is an exception"() { + ContributionDef cd = newContributionDef "contributionPlaceholder1" + + MappedConfiguration config = new ValidatingMappedConfigurationWrapper(Runnable, locator, tc, map, null, SERVICE_ID, cd, Class, keyToContribution) + + when: + + config.add(SQLException, null) + + then: + + NullPointerException e = thrown() + + e.message == "Service contribution (to service 'Baz') was null." + map.isEmpty() + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/bd1da989/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/internal/ValidatingMappedConfigurationWrapperTest.java ---------------------------------------------------------------------- diff --git a/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/internal/ValidatingMappedConfigurationWrapperTest.java b/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/internal/ValidatingMappedConfigurationWrapperTest.java deleted file mode 100644 index f5c214b..0000000 --- a/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/internal/ValidatingMappedConfigurationWrapperTest.java +++ /dev/null @@ -1,225 +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.MappedConfiguration; -import org.apache.tapestry5.ioc.ObjectLocator; -import org.apache.tapestry5.ioc.def.ContributionDef; -import org.apache.tapestry5.ioc.internal.util.CollectionFactory; -import org.testng.annotations.Test; - -import java.util.Map; - -import static org.apache.tapestry5.ioc.internal.util.CollectionFactory.newMap; - -@SuppressWarnings( - {"rawtypes", "unchecked"}) -public class ValidatingMappedConfigurationWrapperTest extends IOCInternalTestCase -{ - private static final String SERVICE_ID = "Baz"; - - @Test - public void proper_key_and_value() - { - ContributionDef def = mockContributionDef(); - Map<Class, ContributionDef> keyToContribution = newMap(); - ObjectLocator locator = mockObjectLocator(); - Map<Class, Runnable> map = CollectionFactory.newMap(); - TypeCoercerProxy tc = mockTypeCoercerProxy(); - - Class key = Integer.class; - Runnable value = mockRunnable(); - - expect(tc.coerce(value, Runnable.class)).andReturn(value); - - replay(); - - MappedConfiguration<Class, Runnable> wrapper = new ValidatingMappedConfigurationWrapper<Class, Runnable>( - Runnable.class, locator, tc, map, null, SERVICE_ID, def, Class.class, keyToContribution); - - wrapper.add(key, value); - - verify(); - - assertSame(map.get(key), value); - assertSame(keyToContribution.get(Integer.class), def); - } - - @Test - public void coerced_value() - { - ContributionDef def = mockContributionDef(); - Map<Class, ContributionDef> keyToContribution = newMap(); - ObjectLocator locator = mockObjectLocator(); - Map<Class, Runnable> map = CollectionFactory.newMap(); - TypeCoercerProxy tc = mockTypeCoercerProxy(); - String contributedValue = "coerceme"; - - Class key = Integer.class; - Runnable value = mockRunnable(); - - expect(tc.coerce(contributedValue, Runnable.class)).andReturn(value); - - replay(); - - MappedConfiguration<Class, Object> wrapper = new ValidatingMappedConfigurationWrapper(Runnable.class, locator, - tc, map, null, SERVICE_ID, def, Class.class, keyToContribution); - - wrapper.add(key, contributedValue); - - verify(); - - assertSame(map.get(key), value); - assertSame(keyToContribution.get(Integer.class), def); - } - - @Test - public void duplicate_key() - { - ContributionDef def1 = newContributionDef("contributionPlaceholder1"); - ContributionDef def2 = newContributionDef("contributionPlaceholder2"); - Map<Class, ContributionDef> keyToContribution = newMap(); - ObjectLocator locator = mockObjectLocator(); - Map<Class, Runnable> map = CollectionFactory.newMap(); - TypeCoercerProxy tc = mockTypeCoercerProxy(); - - keyToContribution.put(Integer.class, def1); - - Class key = Integer.class; - Runnable value = mockRunnable(); - - expect(tc.coerce(value, Runnable.class)).andReturn(value); - - replay(); - - MappedConfiguration<Class, Runnable> wrapper = new ValidatingMappedConfigurationWrapper<Class, Runnable>( - Runnable.class, locator, tc, map, null, SERVICE_ID, def2, Class.class, keyToContribution); - - try - { - wrapper.add(key, value); - unreachable(); - } catch (IllegalArgumentException ex) - { - assertMessageContains(ex, "Service contribution (to service 'Baz') conflicts with existing contribution"); - } - - verify(); - - assertSame(keyToContribution.get(Integer.class), def1); - assertTrue(map.isEmpty()); - } - - @Test - public void null_key() - { - ContributionDef def = newContributionDef("contributionPlaceholder1"); - Map<Class, ContributionDef> keyToContribution = newMap(); - Runnable value = mockRunnable(); - ObjectLocator locator = mockObjectLocator(); - Map<Class, Runnable> map = CollectionFactory.newMap(); - - replay(); - - MappedConfiguration<Class, Runnable> wrapper = new ValidatingMappedConfigurationWrapper<Class, Runnable>( - Runnable.class, locator, null, map, null, SERVICE_ID, def, Class.class, keyToContribution); - - try - { - wrapper.add(null, value); - unreachable(); - } catch (NullPointerException ex) - { - assertEquals(ex.getMessage(), "Key for service contribution (to service 'Baz') was null."); - } - - verify(); - - assertTrue(map.isEmpty()); - } - - @SuppressWarnings("unchecked") - @Test - public void wrong_key_type() - { - ContributionDef def = newContributionDef("contributionPlaceholder1"); - Map<?, ContributionDef> keyToContribution = CollectionFactory.newMap(); - Runnable value = mockRunnable(); - ObjectLocator locator = mockObjectLocator(); - Map<Class, Runnable> map = CollectionFactory.newMap(); - - replay(); - - MappedConfiguration wrapper = new ValidatingMappedConfigurationWrapper(Runnable.class, locator, null, map, - null, SERVICE_ID, def, Class.class, keyToContribution); - - try - { - wrapper.add("java.util.List", value); - unreachable(); - } catch (IllegalArgumentException ex) - { - assertEquals( - ex.getMessage(), - "Key for service contribution (to service 'Baz') was an instance of java.lang.String, but the expected key type was java.lang.Class."); - } - - verify(); - - assertTrue(map.isEmpty()); - } - - @Test - public void null_value() - { - ContributionDef def = newContributionDef("contributionPlaceholder1"); - Map<Class, ContributionDef> keyToContribution = CollectionFactory.newMap(); - Map<Class, Runnable> map = CollectionFactory.newMap(); - ObjectLocator locator = mockObjectLocator(); - - replay(); - - MappedConfiguration<Class, Runnable> wrapper = new ValidatingMappedConfigurationWrapper<Class, Runnable>( - Runnable.class, locator, null, map, null, SERVICE_ID, def, Class.class, keyToContribution); - - try - { - wrapper.add(Integer.class, null); - unreachable(); - } catch (NullPointerException ex) - { - assertEquals(ex.getMessage(), "Service contribution (to service 'Baz') was null."); - } - - verify(); - - assertTrue(map.isEmpty()); - } - - private ContributionDef newContributionDef(String methodName) - { - return new ContributionDefImpl(SERVICE_ID, findMethod(methodName), false, getProxyFactory(), null, null); - } - - public void contributionPlaceholder1() - { - - } - - public void contributionPlaceholder2() - { - - } -}
