Return-Path: X-Original-To: apmail-tapestry-dev-archive@www.apache.org Delivered-To: apmail-tapestry-dev-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 4AAE5E8D for ; Thu, 21 Jun 2012 19:35:36 +0000 (UTC) Received: (qmail 51181 invoked by uid 500); 21 Jun 2012 19:35:35 -0000 Delivered-To: apmail-tapestry-dev-archive@tapestry.apache.org Received: (qmail 51077 invoked by uid 500); 21 Jun 2012 19:35:35 -0000 Mailing-List: contact commits-help@tapestry.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@tapestry.apache.org Delivered-To: mailing list commits@tapestry.apache.org Received: (qmail 51034 invoked by uid 99); 21 Jun 2012 19:35:35 -0000 Received: from tyr.zones.apache.org (HELO tyr.zones.apache.org) (140.211.11.114) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 21 Jun 2012 19:35:35 +0000 Received: by tyr.zones.apache.org (Postfix, from userid 65534) id 0EF216355; Thu, 21 Jun 2012 19:35:35 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: hlship@apache.org To: commits@tapestry.apache.org X-Mailer: ASF-Git Admin Mailer Subject: [1/6] git commit: Convert TestNG to Spock Message-Id: <20120621193535.0EF216355@tyr.zones.apache.org> Date: Thu, 21 Jun 2012 19:35:35 +0000 (UTC) Updated Branches: refs/heads/master 26f41f2b2 -> a2ba4ad2d 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/a2ba4ad2 Tree: http://git-wip-us.apache.org/repos/asf/tapestry-5/tree/a2ba4ad2 Diff: http://git-wip-us.apache.org/repos/asf/tapestry-5/diff/a2ba4ad2 Branch: refs/heads/master Commit: a2ba4ad2db909682c9a6b59d6fc193ec71dfc9f2 Parents: 8169bde Author: Howard M. Lewis Ship Authored: Thu Jun 21 12:35:22 2012 -0700 Committer: Howard M. Lewis Ship Committed: Thu Jun 21 12:35:22 2012 -0700 ---------------------------------------------------------------------- .../test/groovy/ioc/specs/TypeCoercerSpec.groovy | 191 ++++++++ .../ioc/internal/services/TypeCoercerImplTest.java | 339 --------------- 2 files changed, 191 insertions(+), 339 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/a2ba4ad2/tapestry-ioc/src/test/groovy/ioc/specs/TypeCoercerSpec.groovy ---------------------------------------------------------------------- diff --git a/tapestry-ioc/src/test/groovy/ioc/specs/TypeCoercerSpec.groovy b/tapestry-ioc/src/test/groovy/ioc/specs/TypeCoercerSpec.groovy new file mode 100644 index 0000000..9a388fb --- /dev/null +++ b/tapestry-ioc/src/test/groovy/ioc/specs/TypeCoercerSpec.groovy @@ -0,0 +1,191 @@ +package ioc.specs + +import org.apache.tapestry5.func.F +import org.apache.tapestry5.ioc.annotations.AnnotationUseContext +import org.apache.tapestry5.ioc.services.TypeCoercer +import org.apache.tapestry5.ioc.util.TimeInterval +import org.apache.tapestry5.plastic.PlasticUtils +import spock.lang.Unroll + +class TypeCoercerSpec extends AbstractSharedRegistrySpecification { + + TypeCoercer coercer = getService TypeCoercer + + static String BIG_DECIMAL_VALUE = "12345656748352435842385234598234958234574358723485.35843534285293857298457234587" + + static String BIG_INTEGER_VALUE = "12384584574874385743"; + + static byte byte1 = 65, byte2 = 66 + + static short short1 = 22, short2 = 7 + + static int int1 = 200, int2 = 300 + + static long long1 = 100l, long2 = 200l + + static float float1 = 22.1f, float2 = -123.5 + + static double double1 = 123d, double2 = 999.9d + + static char char1 = "1".charAt(0), char2 = "2".charAt(0) + + + @Unroll + def "coerce #inputTypeName #input to #typeName should be #expected"() { + + expect: + + coercer.coerce(input, type) == expected + + where: + + + input | type | expected + "-15" | Double | -15d + "2.27" | Double | 2.27d + 227l | int | 227 + -37 | Number | -37 + new StringBuilder("12345") | int | 12345 + "52" | Integer | 52 + this | String | toString() + 55l | Integer | 55 + 256l | Boolean | true + "" | Boolean | false + " " | Boolean | false + "x" | Boolean | true + " z " | Boolean | true + "false" | Boolean | false + "FAlse" | Boolean | false + null | Boolean | false + 256d | Integer | 256 + 22.7 | Integer | 22 + 0 | Boolean | false + 32838l | Boolean | true + 127 | Byte | new Byte("127") + 58d | Short | new Short("58") + 33 | Long | 33l + 22 | Float | 22f + 1234 | Double | 1234d + 31.14f | Double | 31.14f.doubleValue() + [] | Boolean | false + ["a"] | Boolean | true + [null] | Boolean | true + new BigDecimal(BIG_DECIMAL_VALUE) | Double | 1.2345656748352436E49 + BIG_INTEGER_VALUE | BigInteger | new BigInteger(BIG_INTEGER_VALUE) + -12345678L | BigInteger | new BigInteger("-12345678") + "bravo" | List | ["bravo"] + null | Iterator | null + null | List | null + null | Collection | null + null | String | null + ["a", 123] as Object[] | List | ["a", 123] + ["a", "b"] as String[] | List | ["a", "b"] + [true, false] as boolean[] | List | [true, false] + [byte1, byte2] as byte[] | List | [byte1, byte2] + [char1, char2] as char[] | List | [char1, char2] + [short1, short2] as short[] | List | [short1, short2] + [int1, int2] as int[] | List | [int1, int2] + [long1, long2] as long[] | List | [long1, long2] + [float1, float2] as float[] | List | [float1, float2] + [double1, double2] as double[] | List | [double1, double2] + "foo/bar/baz.txt" | File | new File("foo/bar/baz.txt") + new TimeInterval("2 h") | Long | 2 * 60 * 60 * 1000l + "2 h" | TimeInterval | new TimeInterval("120 m") + F.flow() | Boolean | false + F.flow(1, 2, 3) | Boolean | true + F.flow(1, 2, 3) | List | [1, 2, 3] + "mixin" | AnnotationUseContext | AnnotationUseContext.MIXIN + 123 | Object[] | [123] as Object[] + [1, 2, 3] | Object[] | [1, 2, 3] as Object[] + + + inputTypeName = PlasticUtils.toTypeName(input.getClass()) + typeName = PlasticUtils.toTypeName(type) + } + + @Unroll + def "explain #fromName to #toName should be #expected"() { + + expect: + + coercer.explain(from, to) == expected + + where: + + from | to | expected + StringBuffer | Integer | "Object --> String, String --> Long, Long --> Integer" + void | Map | "null --> null" + void | Boolean | "null --> Boolean" + Object[] | Boolean | "Object[] --> Boolean" + String[] | List | "Object[] --> java.util.List" + Float | Double | "Float --> Double" + Double | BigDecimal | "Object --> String, String --> java.math.BigDecimal" + int | Integer | "" + Integer | Integer | "" + + fromName = PlasticUtils.toTypeName(from) + toName = PlasticUtils.toTypeName(to) + } + + def "no coercion yields the input value unchanged"() { + def input = new Integer(-37) + + expect: + + coercer.coerce(input, Integer).is(input) + } + + def "coercions for primitive types and wrappers are the same"() { + when: + + def c1 = coercer.getCoercion(int, Integer) + def c2 = coercer.getCoercion(Integer, Integer) + + then: + + c1.is c2 + } + + def "exception when no coercion is found"() { + when: + + coercer.coerce("", Map) + + then: + + RuntimeException e = thrown() + + e.message.contains "Could not find a coercion from type java.lang.String to type java.util.Map" + } + + def "exception when a coercion fails"() { + when: + + coercer.coerce([:], Float) + + then: + + RuntimeException e = thrown() + + e.message.contains "Coercion of {} to type java.lang.Float (via Object --> String, String --> Double, Double --> Float) failed" + e.cause instanceof NumberFormatException + } + + def "computed coercions are cached"() { + def c1 = coercer.getCoercion(StringBuilder, Integer) + def c2 = coercer.getCoercion(StringBuilder, Integer) + + expect: + + c1.is(c2) + + when: + + coercer.clearCache() + def c3 = coercer.getCoercion(StringBuilder, Integer) + + then: + + !c1.is(c3) + } +} http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/a2ba4ad2/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/internal/services/TypeCoercerImplTest.java ---------------------------------------------------------------------- diff --git a/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/internal/services/TypeCoercerImplTest.java b/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/internal/services/TypeCoercerImplTest.java deleted file mode 100644 index d759051..0000000 --- a/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/internal/services/TypeCoercerImplTest.java +++ /dev/null @@ -1,339 +0,0 @@ -// Copyright 2006, 2007, 2008, 2009, 2010, 2011, 2012 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.func.F; -import org.apache.tapestry5.ioc.annotations.AnnotationUseContext; -import org.apache.tapestry5.ioc.internal.IOCInternalTestCase; -import org.apache.tapestry5.ioc.internal.util.CollectionFactory; -import org.apache.tapestry5.ioc.services.Coercion; -import org.apache.tapestry5.ioc.services.TypeCoercer; -import org.apache.tapestry5.ioc.util.TimeInterval; -import org.testng.annotations.AfterClass; -import org.testng.annotations.BeforeClass; -import org.testng.annotations.DataProvider; -import org.testng.annotations.Test; -import org.xml.sax.XMLReader; - -import java.io.File; -import java.math.BigDecimal; -import java.math.BigInteger; -import java.util.*; - -public class TypeCoercerImplTest extends IOCInternalTestCase -{ - private TypeCoercer coercer; - - @BeforeClass - public void setup_coercer() - { - coercer = getService(TypeCoercer.class); - } - - @AfterClass - public void cleanup_coercer() - { - coercer = null; - } - - @Test - public void builtin_coercion() - { - // String to Double - - assertEquals(coercer.coerce("-15", Double.class), new Double(-15)); - - // Now a second pass through, to exercise the internal cache - - assertEquals(coercer.coerce("2.27", Double.class), new Double(2.27)); - } - - @Test - public void primitive_type_as_target() - { - assertEquals(coercer.coerce(227l, int.class), new Integer(227)); - } - - @SuppressWarnings("unchecked") - @Test - public void no_coercion_necessary() - { - Object input = new Integer(-37); - - assertSame(coercer.coerce(input, Number.class), input); - - Coercion coercion = coercer.getCoercion(int.class, Number.class); - - assertSame(coercion.coerce(input), input); - } - - @Test - public void explain_to_same_type() - { - assertEquals(coercer.explain(Integer.class, Integer.class), ""); - } - - /** - * TAP5-917 - */ - @Test - public void explain_primitive_to_wrapper_type() - { - assertEquals(coercer.explain(int.class, Integer.class), ""); - } - - /** - * TAP5-917 - */ - @Test - public void explain_wrapper_to_primitive_type() - { - assertEquals(coercer.explain(Integer.class, int.class), ""); - } - - @Test - public void combined_coercion() - { - StringBuilder builder = new StringBuilder("12345"); - - // This should trigger Object -> String, String -> Integer - - assertEquals(coercer.coerce(builder, int.class), new Integer(12345)); - - // This should trigger String -> Double, Number -> Integer - - assertEquals(coercer.coerce("52", Integer.class), new Integer(52)); - } - - @Test - public void no_coercion_found() - { - try - { - coercer.coerce("", Map.class); - unreachable(); - } catch (RuntimeException ex) - { - assertTrue(ex.getMessage().contains( - "Could not find a coercion from type java.lang.String to type java.util.Map")); - } - } - - @Test - public void coercion_failure() - { - try - { - coercer.coerce(Collections.EMPTY_MAP, Float.class); - unreachable(); - } catch (RuntimeException ex) - { - assertTrue(ex - .getMessage() - .contains( - "Coercion of {} to type java.lang.Float (via Object --> String, String --> Double, Double --> Float) failed")); - assertTrue(ex.getCause() instanceof NumberFormatException); - } - } - - @SuppressWarnings("unchecked") - @Test(dataProvider = "coercions_inputs") - public void builtin_coercions(Object input, Class targetType, Object expected) - { - Object actual = coercer.coerce(input, targetType); - - assertEquals(actual, expected); - - Coercion c = coercer.getCoercion(input == null ? void.class : input.getClass(), targetType); - - assertEquals(c.coerce(input), expected); - } - - @SuppressWarnings("unchecked") - @DataProvider - public Object[][] coercions_inputs() - { - String bigDecimalValue = "12345656748352435842385234598234958234574358723485.35843534285293857298457234587"; - String bigIntegerValue = "12384584574874385743"; - - Object object = new Object(); - // Over time, some of these may evolve from testing specific tuples to - // compound tuples (built around specific tuples). - - Float floatValue = new Float(31.14); - byte byte1 = 12, byte2 = 56; - short short1 = 34, short2 = 98; - return new Object[][] - { - // There's a lot of these! - - {this, String.class, toString()}, - - {55l, Integer.class, 55}, - - // TAP5-1881 - {256l, Boolean.class, true}, - - {"", Boolean.class, false}, - - {" ", Boolean.class, false}, - - {"x", Boolean.class, true}, - - {" z ", Boolean.class, true}, - - {"false", Boolean.class, false}, - - {" False ", Boolean.class, false}, - - {null, Boolean.class, false}, - - {new Double(256), Integer.class, new Integer(256)}, - - {new Double(22.7), Integer.class, new Integer(22)}, - - {new Integer(0), Boolean.class, false}, - - {new Long(32838), Boolean.class, true}, - - {new Integer(127), Byte.class, new Byte("127")}, - - {new Double(58), Short.class, new Short("58")}, - - {new Integer(33), Long.class, new Long(33)}, - - {new Integer(22), Float.class, new Float(22)}, - - {new Integer(1234), Double.class, new Double(1234)}, - - {floatValue, Double.class, floatValue.doubleValue()}, - - {Collections.EMPTY_LIST, Boolean.class, false}, - - {Collections.singleton(this), Boolean.class, true}, - - {bigDecimalValue, BigDecimal.class, new BigDecimal(bigDecimalValue)}, - - {new BigDecimal(bigDecimalValue), Double.class, 1.2345656748352436E49}, - - {bigIntegerValue, BigInteger.class, new BigInteger(bigIntegerValue)}, - - {new BigInteger("12345678"), Long.class, 12345678l}, - - {-12345678l, BigInteger.class, new BigInteger("-12345678")}, - - {object, List.class, Collections.singletonList(object)}, - - {null, Iterable.class, null}, - - {null, List.class, null}, - - {null, Collection.class, null}, - - {null, String.class, null}, - - {new Object[] - {"a", 123}, List.class, Arrays.asList("a", 123)}, - - {new String[] - {"a", "b"}, List.class, Arrays.asList("a", "b")}, - - {new byte[] - {byte1, byte2}, List.class, Arrays.asList(byte1, byte2)}, - - {new short[] - {short1, short2}, List.class, Arrays.asList(short1, short2)}, - - {new int[] - {1, 2}, List.class, Arrays.asList(1, 2)}, - - {new long[] - {123L, 321L}, List.class, Arrays.asList(123L, 321L)}, - - {new float[] - {3.4f, 7.777f}, List.class, Arrays.asList(3.4f, 7.777f)}, - - {new double[] - {3.4, 7.777}, List.class, Arrays.asList(3.4, 7.777)}, - - {new char[] - {'a', 'b'}, List.class, Arrays.asList('a', 'b')}, - - {new boolean[] - {true, false}, List.class, Arrays.asList(true, false)}, - - {"foo/bar/baz.txt", File.class, new File("foo/bar/baz.txt")}, - - {new TimeInterval("2 h"), Long.class, 2 * 60 * 60 * 1000l}, - - {"2 h", TimeInterval.class, new TimeInterval("120 m")}, - - {F.flow(), Boolean.class, false}, - - {F.flow(1, 2, 3), Boolean.class, true}, - - {F.flow(1, 2, 3), List.class, Arrays.asList(1, 2, 3)}, - - // TAP5-98: - - {"mixin", AnnotationUseContext.class, AnnotationUseContext.MIXIN}, - - // null to arbitrary object is still null - - {null, XMLReader.class, null}}; - } - - @Test(dataProvider = "explain_inputs") - public void explain(Class inputType, Class outputType, String expected) - { - assertEquals(coercer.explain(inputType, outputType), expected); - } - - @DataProvider - public Object[][] explain_inputs() - { - return new Object[][] - { - {StringBuffer.class, Integer.class, "Object --> String, String --> Long, Long --> Integer"}, - {void.class, Map.class, "null --> null"}, - {void.class, Boolean.class, "null --> Boolean"}, - {Object[].class, Boolean.class, "Object[] --> Boolean"}, - {String[].class, List.class, "Object[] --> java.util.List"}, - {Float.class, Double.class, "Float --> Double"}, - {Double.class, BigDecimal.class, "Object --> String, String --> java.math.BigDecimal"},}; - } - - @Test - public void object_to_object_array() - { - Object input = 51; - - Object[] result = coercer.coerce(input, Object[].class); - Object[] expected = {input}; - - assertArraysEqual(result, expected); - } - - @Test - public void collection_to_object_array() - { - List input = CollectionFactory.newList("fred", "barney", "wilma"); - - Object[] result = coercer.coerce(input, Object[].class); - - assertArraysEqual(result, input.toArray()); - } - -}