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/2fcbeefa Tree: http://git-wip-us.apache.org/repos/asf/tapestry-5/tree/2fcbeefa Diff: http://git-wip-us.apache.org/repos/asf/tapestry-5/diff/2fcbeefa Branch: refs/heads/master Commit: 2fcbeefa611e45f2d4d9f3d2143007cd4ec5f4b7 Parents: 0028c83 Author: Howard M. Lewis Ship <[email protected]> Authored: Mon Jun 11 14:07:19 2012 -0700 Committer: Howard M. Lewis Ship <[email protected]> Committed: Mon Jun 11 14:07:19 2012 -0700 ---------------------------------------------------------------------- .../ioc/util/CaseInsensitiveMapSpec.groovy | 303 ++++++++++++ .../tapestry5/ioc/util/CaseInsensitiveMapTest.java | 357 --------------- 2 files changed, 303 insertions(+), 357 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/2fcbeefa/tapestry-ioc/src/test/groovy/org/apache/tapestry5/ioc/util/CaseInsensitiveMapSpec.groovy ---------------------------------------------------------------------- diff --git a/tapestry-ioc/src/test/groovy/org/apache/tapestry5/ioc/util/CaseInsensitiveMapSpec.groovy b/tapestry-ioc/src/test/groovy/org/apache/tapestry5/ioc/util/CaseInsensitiveMapSpec.groovy new file mode 100644 index 0000000..b413af2 --- /dev/null +++ b/tapestry-ioc/src/test/groovy/org/apache/tapestry5/ioc/util/CaseInsensitiveMapSpec.groovy @@ -0,0 +1,303 @@ +package org.apache.tapestry5.ioc.util + +import spock.lang.Specification + + +class CaseInsensitiveMapSpec extends Specification { + + CaseInsensitiveMap map = new CaseInsensitiveMap([fred: "flintstone", barney: "rubble", wilma: "flinstone", betty: "rubble"]) + + def "get() is case insensitive"() { + def map = new CaseInsensitiveMap() + + def value = "flintstone" + + when: + + map.put("fred", value) + + then: + + map.get("fred").is(value) + map.get("Fred").is(value) + } + + def "containsKey() is case insensitive"() { + + expect: + + map.containsKey("fred") + map.containsKey("Fred") + map.containsKey("barney") + map.containsKey("wilma") + !map.containsKey("dino") + } + + def "remove() is case insensitive"() { + expect: + + map.containsKey("fred") + !map.isEmpty() + + when: + + map.remove("FrED") + + then: + + map.keySet() == ["barney", "wilma", "betty"] as Set + } + + def "copying Map constructor"() { + def standard = [fred: "flintstone", barney: "rubble", wilma: "flintstone"] + + when: + + def original = new CaseInsensitiveMap(standard) + + then: + + original == standard + + when: + + def copy = new CaseInsensitiveMap(original) + + then: + + copy == original + } + + def "comparison of two CaseInsensitiveMaps ignores case"() { + def lower = new CaseInsensitiveMap([fred: "flintstone", barney: "rubble"]) + def upper = new CaseInsensitiveMap([Fred: "flintstone", Barney: "rubble"]) + + expect: + + upper == lower + } + + def "put with different case replaces the old key"() { + + expect: + + map.keySet() == ["fred", "barney", "betty", "wilma"] as Set + + + when: + + map.put("FRED", "flintstone") + + then: + + map.keySet() == ["FRED", "barney", "betty", "wilma"] as Set + } + + def "get with missing key is null"() { + expect: + + map.notFound == null + } + + def "get with non-string key is null"() { + expect: + + map.get(this) == null + } + + def "expansion of the internal entry array"() { + + def count = 2000 + + def map = new CaseInsensitiveMap() + + count.times { it -> + assert map.put("key_$it" as String, it) == null + } + + when: + + count.times { it -> + assert map.get("key_$it" as String) == it + } + + then: + + map.size() == count + map.entrySet().size() == count + + when: + + map.clear() + + then: + + map.size() == 0 + + } + + def "change value via entrySet()"() { + def map = new CaseInsensitiveMap() + + map.put("fred", "flintstone") + + when: + + map.entrySet().each { entry -> entry.value = "murray" } + + then: + + map.get("fred") == "murray" + } + + def "entrySet iterator fails fast after remove"() { + + def i = map.entrySet().iterator() + + i.next() + map.remove("betty") + + when: + + i.next() + + then: + + thrown(ConcurrentModificationException) + } + + def "entrySet iterator fails fast after put"() { + + def i = map.entrySet().iterator() + + i.next() + map.put("zaphod", "breeblebrox") + + when: + + i.next() + + then: + + thrown(ConcurrentModificationException) + } + + def "iterator may remove without concurrent exception"() { + + def i = map.entrySet().iterator() + + while (i.hasNext()) { + if (i.next().key == "wilma") { i.remove() } + } + + expect: + + map.keySet() == ["barney", "betty", "fred"] as Set + } + + def "contains via entrySet"() { + + def set = map.entrySet() + + expect: + + set.contains(newMapEntry("fred", "flintstone")) + set.contains(newMapEntry("Fred", "flintstone")) + + !set.contains(newMapEntry("Zaphod", "Breeblebox")) + !set.contains(newMapEntry("fred", "murray")) + } + + def "remove via entrySet"() { + + def set = map.entrySet() + + when: + + assert set.remove(newMapEntry("Zaphod", "Breeblrox")) == false + assert set.remove(newMapEntry("fred", "murray")) == false + + assert set.remove(newMapEntry("fred", "flintstone")) == true + + then: + + map.keySet() == ["barney", "wilma", "betty"] as Set + } + + def newMapEntry(key, value) { + return new Map.Entry() { + + @Override + Object getKey() { + return key + } + + @Override + Object getValue() { + return value; + } + + @Override + Object setValue(Object newValue) { + value = newValue + } + } + } + + def "null is a valid key"() { + when: + + map.put(null, "NULL") + + then: + + map.get(null) == "NULL" + } + + def "clearing the entrySet clears the map"() { + expect: + + !map.isEmpty() + !map.entrySet().isEmpty() + + when: + + map.entrySet().clear() + + then: + + map.isEmpty() + } + + def "next() after last entry in entrySet is a failure"() { + Iterator i = map.entrySet().iterator() + + while (i.hasNext()) { i.next() } + + when: + + i.next() + + then: + + thrown(NoSuchElementException) + } + + def "serialize/deserialize copies all data"() { + + def baos = new ByteArrayOutputStream() + def oos = new ObjectOutputStream(baos) + + oos.writeObject(map) + oos.close() + + def bais = new ByteArrayInputStream(baos.toByteArray()) + ObjectInputStream ois = new ObjectInputStream(bais) + + def copy = ois.readObject() + + expect: + + copy == map + } +} http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/2fcbeefa/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/util/CaseInsensitiveMapTest.java ---------------------------------------------------------------------- diff --git a/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/util/CaseInsensitiveMapTest.java b/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/util/CaseInsensitiveMapTest.java deleted file mode 100644 index 4cf13ec..0000000 --- a/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/util/CaseInsensitiveMapTest.java +++ /dev/null @@ -1,357 +0,0 @@ -// Copyright 2007 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.util; - -import org.apache.tapestry5.ioc.internal.util.CollectionFactory; -import static org.apache.tapestry5.ioc.internal.util.CollectionFactory.newCaseInsensitiveMap; -import org.testng.Assert; -import org.testng.annotations.Test; - -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import java.io.ObjectInputStream; -import java.io.ObjectOutputStream; -import java.util.*; - -public class CaseInsensitiveMapTest extends Assert -{ - @Test - public void basic_get_put_remove() - { - Map<String, String> map = newCaseInsensitiveMap(); - - String value = "flintstone"; - - map.put("fred", value); - - assertEquals(map.toString(), "{fred=flintstone}"); - - assertSame(map.get("fred"), value); - assertSame(map.get("Fred"), value); - - assertSame(map.remove("FRED"), value); - - assertFalse(map.containsKey("fred")); - - assertTrue(map.isEmpty()); - } - - @Test - public void copy_map_constructor() - { - Map<String, String> map = newCaseInsensitiveMap(); - - map.put("fred", "flintstone"); - map.put("barney", "rubble"); - map.put("wilma", "flinstone"); - map.put("betty", "rubble"); - - Map<String, String> copy = newCaseInsensitiveMap(map); - - assertEquals(copy, map); - } - - @Test - public void put_with_different_case_replaces() - { - Map<String, String> map = newCaseInsensitiveMap(); - - map.put("fred", "flintstone"); - - String value = "Murray"; - - map.put("Fred", value); - - assertEquals(map.size(), 1); - - assertSame(map.get("fred"), value); - - assertEquals(map.toString(), "{Fred=Murray}"); - } - - @Test - public void get_with_missing_key_is_null() - { - Map<String, String> map = newCaseInsensitiveMap(); - - map.put("fred", "flintstone"); - - assertNull(map.get("barney")); - } - - @SuppressWarnings("unchecked") - @Test - public void get_with_non_string_key_is_null() - { - Map map = newCaseInsensitiveMap(); - - map.put("fred", "flintstone"); - - assertNull(map.get(this)); - } - - /** - * Add a large number of keys which should stress the code that adds and expands values into the map. - */ - @Test - public void expansion_of_internal_entry_array() - { - Map<String, Integer> map = newCaseInsensitiveMap(); - - int COUNT = 2000; - - for (int i = 0; i < COUNT; i++) - { - assertNull(map.put("key_" + i, i)); - } - - // Now check that the values are still there. - - for (int i = 0; i < COUNT; i++) - { - assertEquals(map.get("KEY_" + i).intValue(), i); - } - - assertEquals(map.size(), COUNT); - assertEquals(map.entrySet().size(), COUNT); - - map.clear(); - - assertEquals(map.size(), 0); - } - - @Test - public void change_value_via_entry_set() - { - Map<String, String> map = newCaseInsensitiveMap(); - - map.put("fred", "flintstone"); - - Map.Entry<String, String> me = map.entrySet().iterator().next(); - - String value = "murray"; - - me.setValue(value); - - assertSame(map.get("fred"), value); - } - - @Test(expectedExceptions = - { ConcurrentModificationException.class }) - public void iterator_fail_fast_after_remove() - { - Map<String, String> map = newCaseInsensitiveMap(); - - map.put("fred", "flintstone"); - map.put("barney", "rubble"); - map.put("wilma", "flinstone"); - map.put("betty", "rubble"); - - Iterator i = map.entrySet().iterator(); - - i.next(); - - map.remove("betty"); - - i.next(); - } - - @Test(expectedExceptions = - { ConcurrentModificationException.class }) - public void iterator_fail_fast_on_next() - { - Map<String, String> map = newCaseInsensitiveMap(); - - map.put("fred", "flintstone"); - map.put("barney", "rubble"); - map.put("wilma", "flinstone"); - map.put("betty", "rubble"); - - Iterator<Map.Entry<String, String>> i = map.entrySet().iterator(); - - while (i.hasNext()) - { - if (i.next().getKey().equals("betty")) map.put("pebbles", "flintstone"); - } - } - - @Test - public void iterator_may_remove_without_concurrent_exception() - { - Map<String, String> map = newCaseInsensitiveMap(); - - map.put("fred", "flintstone"); - map.put("barney", "rubble"); - map.put("wilma", "flinstone"); - map.put("betty", "rubble"); - - Iterator<Map.Entry<String, String>> i = map.entrySet().iterator(); - - while (i.hasNext()) - { - if (i.next().getKey().equals("wilma")) i.remove(); - } - - List<String> keys = CollectionFactory.newList(map.keySet()); - Collections.sort(keys); - - assertEquals(keys, Arrays.asList("barney", "betty", "fred")); - } - - @Test - public void contains_via_entry_set() - { - Map<String, String> map = newCaseInsensitiveMap(); - - map.put("fred", "flintstone"); - map.put("barney", "rubble"); - map.put("wilma", "flinstone"); - map.put("betty", "rubble"); - - Set<Map.Entry<String, String>> entrySet = map.entrySet(); - - assertTrue(entrySet.contains(newMapEntry("fred", "flintstone"))); - assertTrue(entrySet.contains(newMapEntry("Fred", "flintstone"))); - - assertFalse(entrySet.contains(newMapEntry("Zaphod", "Beeblebox"))); - assertFalse(entrySet.contains(newMapEntry("fred", "murray"))); - } - - @Test - public void remove_via_entry_set() - { - Map<String, String> map = newCaseInsensitiveMap(); - - map.put("fred", "flintstone"); - map.put("barney", "rubble"); - map.put("wilma", "flinstone"); - map.put("betty", "rubble"); - - Set<Map.Entry<String, String>> entrySet = map.entrySet(); - - assertFalse(entrySet.remove(newMapEntry("Zaphod", "Beeblebox"))); - assertFalse(entrySet.remove(newMapEntry("fred", "murray"))); - - assertTrue(entrySet.remove(newMapEntry("Fred", "flintstone"))); - } - - @Test - public void null_key() - { - Map<String, String> map = newCaseInsensitiveMap(); - - map.put(null, "NULL"); - - assertEquals(map.get(null), "NULL"); - } - - @Test - public void clear_entry_set_clears_map() - { - Map<String, String> map = newCaseInsensitiveMap(); - - map.put("fred", "flintstone"); - - map.entrySet().clear(); - - assertTrue(map.isEmpty()); - } - - @Test(expectedExceptions = - { NoSuchElementException.class }) - public void next_after_last_entry_is_failure() - { - Map<String, String> map = newCaseInsensitiveMap(); - - map.put("fred", "flintstone"); - - Iterator i = map.entrySet().iterator(); - - while (i.hasNext()) - i.next(); - - i.next(); - } - - @Test - public void entry_set_iterator_sees_all_keys() - { - Map<String, String> map = newCaseInsensitiveMap(); - - map.put("fred", "flintstone"); - map.put("barney", "rubble"); - map.put("wilma", "flinstone"); - map.put("betty", "rubble"); - - Iterator<Map.Entry<String, String>> i = map.entrySet().iterator(); - List<String> keys = CollectionFactory.newList(); - - while (i.hasNext()) - keys.add(i.next().getKey()); - - Collections.sort(keys); - - assertEquals(keys, Arrays.asList("barney", "betty", "fred", "wilma")); - } - - @SuppressWarnings("unchecked") - @Test - public void serialize_deserialize() throws Exception - { - Map<String, String> map = newCaseInsensitiveMap(); - - map.put("fred", "flintstone"); - map.put("barney", "rubble"); - map.put("wilma", "flinstone"); - map.put("betty", "rubble"); - - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - ObjectOutputStream oos = new ObjectOutputStream(baos); - - oos.writeObject(map); - oos.close(); - - ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); - ObjectInputStream ois = new ObjectInputStream(bais); - - Map<String, String> copy = (Map<String, String>) ois.readObject(); - - assertEquals(copy, map); - } - - @SuppressWarnings("unchecked") - private <K, V> Map.Entry<K, V> newMapEntry(final K key, final V value) - { - return new Map.Entry() - { - - public Object getKey() - { - return key; - } - - public Object getValue() - { - return value; - } - - public Object setValue(Object value) - { - throw new UnsupportedOperationException(); - } - - }; - } -}
