diff --git a/clustering/marshalling/spi/src/main/java/org/wildfly/clustering/marshalling/spi/util/CollectionExternalizer.java b/clustering/marshalling/spi/src/main/java/org/wildfly/clustering/marshalling/spi/util/CollectionExternalizer.java index 01c2d0a161dc..2690d0fc3c38 100644 --- a/clustering/marshalling/spi/src/main/java/org/wildfly/clustering/marshalling/spi/util/CollectionExternalizer.java +++ b/clustering/marshalling/spi/src/main/java/org/wildfly/clustering/marshalling/spi/util/CollectionExternalizer.java @@ -31,6 +31,7 @@ import java.util.HashSet; import java.util.LinkedHashSet; import java.util.LinkedList; +import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentLinkedDeque; import java.util.concurrent.ConcurrentLinkedQueue; import java.util.function.IntFunction; @@ -98,6 +99,13 @@ public ArrayListExternalizer() { } } + @MetaInfServices(Externalizer.class) + public static class ConcurrentHashSetExternalizer extends CollectionExternalizer> { + public ConcurrentHashSetExternalizer() { + super(ConcurrentHashMap.KeySetView.class, capacity -> ConcurrentHashMap.newKeySet(capacity)); + } + } + @MetaInfServices(Externalizer.class) public static class ConcurrentLinkedDequeExternalizer extends CollectionExternalizer> { public ConcurrentLinkedDequeExternalizer() { diff --git a/clustering/marshalling/spi/src/main/java/org/wildfly/clustering/marshalling/spi/util/EmptyCollectionExternalizer.java b/clustering/marshalling/spi/src/main/java/org/wildfly/clustering/marshalling/spi/util/EmptyCollectionExternalizer.java new file mode 100644 index 000000000000..f4d8d9202cf9 --- /dev/null +++ b/clustering/marshalling/spi/src/main/java/org/wildfly/clustering/marshalling/spi/util/EmptyCollectionExternalizer.java @@ -0,0 +1,140 @@ +/* + * JBoss, Home of Professional Open Source. + * Copyright 2016, Red Hat, Inc., and individual contributors + * as indicated by the @author tags. See the copyright.txt file in the + * distribution for a full listing of individual contributors. + * + * This is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * This software is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this software; if not, write to the Free + * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA + * 02110-1301 USA, or see the FSF site: http://www.fsf.org. + */ + +package org.wildfly.clustering.marshalling.spi.util; + +import java.io.IOException; +import java.io.ObjectInput; +import java.io.ObjectOutput; +import java.util.Collections; +import java.util.Enumeration; +import java.util.Iterator; +import java.util.List; +import java.util.ListIterator; +import java.util.Map; +import java.util.NavigableMap; +import java.util.NavigableSet; +import java.util.Set; +import java.util.SortedMap; +import java.util.SortedSet; +import java.util.function.Supplier; + +import org.kohsuke.MetaInfServices; +import org.wildfly.clustering.marshalling.Externalizer; + +/** + * @author Paul Ferraro + */ +public class EmptyCollectionExternalizer implements Externalizer { + + private final Supplier factory; + + EmptyCollectionExternalizer(Supplier factory) { + this.factory = factory; + } + + @Override + public void writeObject(ObjectOutput output, T object) throws IOException { + // Nothing to write + } + + @Override + public T readObject(ObjectInput input) throws IOException, ClassNotFoundException { + return this.factory.get(); + } + + @SuppressWarnings("unchecked") + @Override + public Class getTargetClass() { + return (Class) this.factory.get().getClass(); + } + + @MetaInfServices(Externalizer.class) + public static class EmptyEnumerationExternalizer extends EmptyCollectionExternalizer> { + public EmptyEnumerationExternalizer() { + super(() -> Collections.emptyEnumeration()); + } + } + + @MetaInfServices(Externalizer.class) + public static class EmptyIteratorExternalizer extends EmptyCollectionExternalizer> { + public EmptyIteratorExternalizer() { + super(() -> Collections.emptyIterator()); + } + } + + @MetaInfServices(Externalizer.class) + public static class EmptyListExternalizer extends EmptyCollectionExternalizer> { + public EmptyListExternalizer() { + super(() -> Collections.emptyList()); + } + } + + @MetaInfServices(Externalizer.class) + public static class EmptyListIteratorExternalizer extends EmptyCollectionExternalizer> { + public EmptyListIteratorExternalizer() { + super(() -> Collections.emptyListIterator()); + } + } + + @MetaInfServices(Externalizer.class) + public static class EmptyMapExternalizer extends EmptyCollectionExternalizer> { + public EmptyMapExternalizer() { + super(() -> Collections.emptyMap()); + } + } + + @MetaInfServices(Externalizer.class) + public static class EmptyNavigableMapExternalizer extends EmptyCollectionExternalizer> { + public EmptyNavigableMapExternalizer() { + super(() -> Collections.emptyNavigableMap()); + } + } + + @MetaInfServices(Externalizer.class) + public static class EmptyNavigableSetExternalizer extends EmptyCollectionExternalizer> { + public EmptyNavigableSetExternalizer() { + super(() -> Collections.emptyNavigableSet()); + } + } + + @MetaInfServices(Externalizer.class) + public static class EmptySetExternalizer extends EmptyCollectionExternalizer> { + public EmptySetExternalizer() { + super(() -> Collections.emptySet()); + } + } + + @MetaInfServices(Externalizer.class) + public static class EmptySortedMapExternalizer extends EmptyCollectionExternalizer> { + public EmptySortedMapExternalizer() { + super(() -> Collections.emptySortedMap()); + } + } + + @MetaInfServices(Externalizer.class) + public static class EmptySortedSetExternalizer extends EmptyCollectionExternalizer> { + public EmptySortedSetExternalizer() { + super(() -> Collections.emptySortedSet()); + } + } +} diff --git a/clustering/marshalling/spi/src/main/java/org/wildfly/clustering/marshalling/spi/util/SetExternalizer.java b/clustering/marshalling/spi/src/main/java/org/wildfly/clustering/marshalling/spi/util/SetExternalizer.java deleted file mode 100644 index b73feefea2b0..000000000000 --- a/clustering/marshalling/spi/src/main/java/org/wildfly/clustering/marshalling/spi/util/SetExternalizer.java +++ /dev/null @@ -1,103 +0,0 @@ -/* - * JBoss, Home of Professional Open Source. - * Copyright 2016, Red Hat, Inc., and individual contributors - * as indicated by the @author tags. See the copyright.txt file in the - * distribution for a full listing of individual contributors. - * - * This is free software; you can redistribute it and/or modify it - * under the terms of the GNU Lesser General Public License as - * published by the Free Software Foundation; either version 2.1 of - * the License, or (at your option) any later version. - * - * This software is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this software; if not, write to the Free - * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA - * 02110-1301 USA, or see the FSF site: http://www.fsf.org. - */ - -package org.wildfly.clustering.marshalling.spi.util; - -import java.io.IOException; -import java.io.ObjectInput; -import java.io.ObjectOutput; -import java.util.HashSet; -import java.util.LinkedHashSet; -import java.util.Set; -import java.util.concurrent.ConcurrentHashMap; -import java.util.function.IntFunction; - -import org.kohsuke.MetaInfServices; -import org.wildfly.clustering.marshalling.Externalizer; -import org.wildfly.clustering.marshalling.spi.IndexExternalizer; - -/** - * Externalizers for implementations of {@link Set}. - * @author Paul Ferraro - */ -public class SetExternalizer> implements Externalizer { - - private final Class targetClass; - private final IntFunction factory; - - @SuppressWarnings({ "unchecked", "rawtypes" }) - SetExternalizer(Class targetClass, IntFunction factory) { - this.targetClass = targetClass; - this.factory = factory; - } - - @Override - public void writeObject(ObjectOutput output, T set) throws IOException { - writeSet(output, set); - } - - static > void writeSet(ObjectOutput output, T set) throws IOException { - IndexExternalizer.VARIABLE.writeData(output, set.size()); - for (Object e : set) { - output.writeObject(e); - } - } - - @Override - public T readObject(ObjectInput input) throws IOException, ClassNotFoundException { - int size = IndexExternalizer.VARIABLE.readData(input); - return readSet(input, this.factory.apply(size), size); - } - - static > T readSet(ObjectInput input, T set, int size) throws IOException, ClassNotFoundException { - for (int i = 0; i < size; ++i) { - set.add(input.readObject()); - } - return set; - } - - @Override - public Class getTargetClass() { - return this.targetClass; - } - - @MetaInfServices(Externalizer.class) - public static class ConcurrentHashSetExternalizer extends SetExternalizer> { - public ConcurrentHashSetExternalizer() { - super(ConcurrentHashMap.KeySetView.class, capacity -> ConcurrentHashMap.newKeySet(capacity)); - } - } - - @MetaInfServices(Externalizer.class) - public static class HashSetExternalizer extends SetExternalizer> { - public HashSetExternalizer() { - super(HashSet.class, HashSet::new); - } - } - - @MetaInfServices(Externalizer.class) - public static class LinkedHashSetExternalizer extends SetExternalizer> { - public LinkedHashSetExternalizer() { - super(LinkedHashSet.class, LinkedHashSet::new); - } - } -} diff --git a/clustering/marshalling/spi/src/main/java/org/wildfly/clustering/marshalling/spi/util/SingletonCollectionExternalizer.java b/clustering/marshalling/spi/src/main/java/org/wildfly/clustering/marshalling/spi/util/SingletonCollectionExternalizer.java new file mode 100644 index 000000000000..91e5ab424fc6 --- /dev/null +++ b/clustering/marshalling/spi/src/main/java/org/wildfly/clustering/marshalling/spi/util/SingletonCollectionExternalizer.java @@ -0,0 +1,77 @@ +/* + * JBoss, Home of Professional Open Source. + * Copyright 2016, Red Hat, Inc., and individual contributors + * as indicated by the @author tags. See the copyright.txt file in the + * distribution for a full listing of individual contributors. + * + * This is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * This software is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this software; if not, write to the Free + * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA + * 02110-1301 USA, or see the FSF site: http://www.fsf.org. + */ + +package org.wildfly.clustering.marshalling.spi.util; + +import java.io.IOException; +import java.io.ObjectInput; +import java.io.ObjectOutput; +import java.util.Collection; +import java.util.Collections; +import java.util.List; +import java.util.Set; +import java.util.function.Function; + +import org.kohsuke.MetaInfServices; +import org.wildfly.clustering.marshalling.Externalizer; + +/** + * @author Paul Ferraro + */ +public class SingletonCollectionExternalizer> implements Externalizer { + + private final Function factory; + + SingletonCollectionExternalizer(Function factory) { + this.factory = factory; + } + + @Override + public void writeObject(ObjectOutput output, T collection) throws IOException { + output.writeObject(collection.stream().findFirst().get()); + } + + @Override + public T readObject(ObjectInput input) throws IOException, ClassNotFoundException { + return this.factory.apply(input.readObject()); + } + + @SuppressWarnings("unchecked") + @Override + public Class getTargetClass() { + return (Class) this.factory.apply(null).getClass(); + } + + @MetaInfServices(Externalizer.class) + public static class SingletonListExternalizer extends SingletonCollectionExternalizer> { + public SingletonListExternalizer() { + super(Collections::singletonList); + } + } + + @MetaInfServices(Externalizer.class) + public static class SingletonSetExternalizer extends SingletonCollectionExternalizer> { + public SingletonSetExternalizer() { + super(Collections::singleton); + } + } +} diff --git a/clustering/marshalling/spi/src/test/java/org/wildfly/clustering/marshalling/spi/util/SortedMapExternalizerTestCase.java b/clustering/marshalling/spi/src/main/java/org/wildfly/clustering/marshalling/spi/util/SingletonMapExternalizer.java similarity index 50% rename from clustering/marshalling/spi/src/test/java/org/wildfly/clustering/marshalling/spi/util/SortedMapExternalizerTestCase.java rename to clustering/marshalling/spi/src/main/java/org/wildfly/clustering/marshalling/spi/util/SingletonMapExternalizer.java index f996965f2716..4350e854ed23 100644 --- a/clustering/marshalling/spi/src/test/java/org/wildfly/clustering/marshalling/spi/util/SortedMapExternalizerTestCase.java +++ b/clustering/marshalling/spi/src/main/java/org/wildfly/clustering/marshalling/spi/util/SingletonMapExternalizer.java @@ -1,6 +1,6 @@ /* * JBoss, Home of Professional Open Source. - * Copyright 2015, Red Hat, Inc., and individual contributors + * Copyright 2016, Red Hat, Inc., and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * @@ -23,25 +23,35 @@ package org.wildfly.clustering.marshalling.spi.util; import java.io.IOException; +import java.io.ObjectInput; +import java.io.ObjectOutput; +import java.util.Collections; import java.util.Map; -import java.util.TreeMap; -import java.util.concurrent.ConcurrentSkipListMap; -import java.util.stream.Collectors; -import java.util.stream.Stream; -import org.junit.Test; -import org.wildfly.clustering.marshalling.spi.ExternalizerTestUtil; +import org.kohsuke.MetaInfServices; +import org.wildfly.clustering.marshalling.Externalizer; /** - * Unit test for {@link SortedMapExternalizer} externalizers. * @author Paul Ferraro */ -public class SortedMapExternalizerTestCase { +@MetaInfServices(Externalizer.class) +public class SingletonMapExternalizer implements Externalizer> { - @Test - public void test() throws ClassNotFoundException, IOException { - Map basis = Stream.of(1, 2, 3, 4, 5).collect(Collectors.toMap(i -> i, i -> Integer.toString(i))); - ExternalizerTestUtil.test(new SortedMapExternalizer.ConcurrentSkipListMapExternalizer(), new ConcurrentSkipListMap<>(basis)); - ExternalizerTestUtil.test(new SortedMapExternalizer.TreeMapExternalizer(), new TreeMap<>(basis)); + @Override + public void writeObject(ObjectOutput output, Map map) throws IOException { + Map.Entry entry = map.entrySet().stream().findFirst().get(); + output.writeObject(entry.getKey()); + output.writeObject(entry.getValue()); + } + + @Override + public Map readObject(ObjectInput input) throws IOException, ClassNotFoundException { + return Collections.singletonMap(input.readObject(), input.readObject()); + } + + @SuppressWarnings("unchecked") + @Override + public Class> getTargetClass() { + return (Class>) Collections.singletonMap(null, null).getClass(); } } diff --git a/clustering/marshalling/spi/src/test/java/org/wildfly/clustering/marshalling/spi/ExternalizerTestUtil.java b/clustering/marshalling/spi/src/test/java/org/wildfly/clustering/marshalling/spi/ExternalizerTestUtil.java index 5fa0170070cb..f5b8c8da32ae 100644 --- a/clustering/marshalling/spi/src/test/java/org/wildfly/clustering/marshalling/spi/ExternalizerTestUtil.java +++ b/clustering/marshalling/spi/src/test/java/org/wildfly/clustering/marshalling/spi/ExternalizerTestUtil.java @@ -53,6 +53,7 @@ public static void test(Externalizer externalizer, T subject, BiConsumer< try (ObjectInputStream input = new ObjectInputStream(new ByteArrayInputStream(out.toByteArray()))) { T result = externalizer.readObject(input); assertion.accept(subject, result); + assertTrue(externalizer.getTargetClass().isInstance(result)); } } } diff --git a/clustering/marshalling/spi/src/test/java/org/wildfly/clustering/marshalling/spi/util/CollectionExternalizerTestCase.java b/clustering/marshalling/spi/src/test/java/org/wildfly/clustering/marshalling/spi/util/CollectionExternalizerTestCase.java index 3a09c2500576..89e116d5b456 100644 --- a/clustering/marshalling/spi/src/test/java/org/wildfly/clustering/marshalling/spi/util/CollectionExternalizerTestCase.java +++ b/clustering/marshalling/spi/src/test/java/org/wildfly/clustering/marshalling/spi/util/CollectionExternalizerTestCase.java @@ -22,6 +22,7 @@ package org.wildfly.clustering.marshalling.spi.util; +import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; @@ -30,11 +31,19 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; +import java.util.Collections; +import java.util.Enumeration; import java.util.HashSet; +import java.util.Iterator; import java.util.LinkedHashSet; import java.util.LinkedList; +import java.util.TreeSet; +import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentLinkedDeque; import java.util.concurrent.ConcurrentLinkedQueue; +import java.util.concurrent.ConcurrentSkipListSet; +import java.util.concurrent.CopyOnWriteArrayList; +import java.util.concurrent.CopyOnWriteArraySet; import java.util.function.BiConsumer; import org.junit.Test; @@ -57,6 +66,26 @@ public void test() throws ClassNotFoundException, IOException { test(new CollectionExternalizer.HashSetExternalizer(), new HashSet<>(basis)); test(new CollectionExternalizer.LinkedHashSetExternalizer(), new LinkedHashSet<>(basis)); test(new CollectionExternalizer.LinkedListExternalizer(), new LinkedList<>(basis)); + ConcurrentHashMap.KeySetView keySetView = ConcurrentHashMap.newKeySet(); + keySetView.addAll(basis); + test(new CollectionExternalizer.ConcurrentHashSetExternalizer(), keySetView); + + test(new CopyOnWriteCollectionExternalizer.CopyOnWriteArrayListExternalizer(), new CopyOnWriteArrayList<>(basis)); + test(new CopyOnWriteCollectionExternalizer.CopyOnWriteArraySetExternalizer(), new CopyOnWriteArraySet<>(basis)); + + test(new EmptyCollectionExternalizer.EmptyEnumerationExternalizer(), Collections.emptyEnumeration()); + test(new EmptyCollectionExternalizer.EmptyIteratorExternalizer(), Collections.emptyIterator()); + test(new EmptyCollectionExternalizer.EmptyListExternalizer(), Collections.emptyList()); + test(new EmptyCollectionExternalizer.EmptyListIteratorExternalizer(), Collections.emptyListIterator()); + test(new EmptyCollectionExternalizer.EmptyNavigableSetExternalizer(), Collections.emptyNavigableSet()); + test(new EmptyCollectionExternalizer.EmptySetExternalizer(), Collections.emptySet()); + test(new EmptyCollectionExternalizer.EmptySortedSetExternalizer(), Collections.emptySortedSet()); + + test(new SingletonCollectionExternalizer.SingletonListExternalizer(), Collections.singletonList(1)); + test(new SingletonCollectionExternalizer.SingletonSetExternalizer(), Collections.singleton(1)); + + test(new SortedSetExternalizer.ConcurrentSkipListSetExternalizer(), new ConcurrentSkipListSet<>(basis)); + test(new SortedSetExternalizer.TreeSetExternalizer(), new TreeSet<>(basis)); } public static > void test(Externalizer externalizer, T collection) throws ClassNotFoundException, IOException { @@ -64,4 +93,12 @@ public static > void test(Externalizer externali BiConsumer assertContents = (expected, actual) -> assertTrue(actual.containsAll(expected)); ExternalizerTestUtil.test(externalizer, collection, assertSize.andThen(assertContents)); } + + public static > void test(Externalizer externalizer, T enumeration) throws ClassNotFoundException, IOException { + ExternalizerTestUtil.test(externalizer, enumeration, (expected, actual) -> assertFalse(actual.hasMoreElements())); + } + + public static > void test(Externalizer externalizer, T iterator) throws ClassNotFoundException, IOException { + ExternalizerTestUtil.test(externalizer, iterator, (expected, actual) -> assertFalse(actual.hasNext())); + } } diff --git a/clustering/marshalling/spi/src/test/java/org/wildfly/clustering/marshalling/spi/util/CopyOnWriteCollectionExternalizerTestCase.java b/clustering/marshalling/spi/src/test/java/org/wildfly/clustering/marshalling/spi/util/CopyOnWriteCollectionExternalizerTestCase.java deleted file mode 100644 index 92cf7ab42f07..000000000000 --- a/clustering/marshalling/spi/src/test/java/org/wildfly/clustering/marshalling/spi/util/CopyOnWriteCollectionExternalizerTestCase.java +++ /dev/null @@ -1,45 +0,0 @@ -/* - * JBoss, Home of Professional Open Source. - * Copyright 2015, Red Hat, Inc., and individual contributors - * as indicated by the @author tags. See the copyright.txt file in the - * distribution for a full listing of individual contributors. - * - * This is free software; you can redistribute it and/or modify it - * under the terms of the GNU Lesser General Public License as - * published by the Free Software Foundation; either version 2.1 of - * the License, or (at your option) any later version. - * - * This software is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this software; if not, write to the Free - * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA - * 02110-1301 USA, or see the FSF site: http://www.fsf.org. - */ - -package org.wildfly.clustering.marshalling.spi.util; - -import java.io.IOException; -import java.util.Arrays; -import java.util.Collection; -import java.util.concurrent.CopyOnWriteArrayList; -import java.util.concurrent.CopyOnWriteArraySet; - -import org.junit.Test; - -/** - * Unit test for {@link CopyOnWriteCollectionExternalizer} externalizers. - * @author Paul Ferraro - */ -public class CopyOnWriteCollectionExternalizerTestCase { - - @Test - public void test() throws ClassNotFoundException, IOException { - Collection basis = Arrays.asList(1, 2, 3, 4, 5); - CollectionExternalizerTestCase.test(new CopyOnWriteCollectionExternalizer.CopyOnWriteArrayListExternalizer(), new CopyOnWriteArrayList<>(basis)); - CollectionExternalizerTestCase.test(new CopyOnWriteCollectionExternalizer.CopyOnWriteArraySetExternalizer(), new CopyOnWriteArraySet<>(basis)); - } -} diff --git a/clustering/marshalling/spi/src/test/java/org/wildfly/clustering/marshalling/spi/util/MapExternalizerTestCase.java b/clustering/marshalling/spi/src/test/java/org/wildfly/clustering/marshalling/spi/util/MapExternalizerTestCase.java index 6801481ed6f0..1426257b8a9c 100644 --- a/clustering/marshalling/spi/src/test/java/org/wildfly/clustering/marshalling/spi/util/MapExternalizerTestCase.java +++ b/clustering/marshalling/spi/src/test/java/org/wildfly/clustering/marshalling/spi/util/MapExternalizerTestCase.java @@ -22,15 +22,23 @@ package org.wildfly.clustering.marshalling.spi.util; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + import java.io.IOException; +import java.util.Collections; import java.util.HashMap; import java.util.LinkedHashMap; import java.util.Map; +import java.util.TreeMap; import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ConcurrentSkipListMap; +import java.util.function.BiConsumer; import java.util.stream.Collectors; import java.util.stream.Stream; import org.junit.Test; +import org.wildfly.clustering.marshalling.Externalizer; import org.wildfly.clustering.marshalling.spi.ExternalizerTestUtil; /** @@ -42,8 +50,23 @@ public class MapExternalizerTestCase { @Test public void test() throws ClassNotFoundException, IOException { Map basis = Stream.of(1, 2, 3, 4, 5).collect(Collectors.toMap(i -> i, i -> Integer.toString(i))); - ExternalizerTestUtil.test(new MapExternalizer.ConcurrentHashMapExternalizer(), new ConcurrentHashMap<>(basis)); - ExternalizerTestUtil.test(new MapExternalizer.HashMapExternalizer(), new HashMap<>(basis)); - ExternalizerTestUtil.test(new MapExternalizer.LinkedHashMapExternalizer(), new LinkedHashMap<>(basis)); + test(new MapExternalizer.ConcurrentHashMapExternalizer(), new ConcurrentHashMap<>(basis)); + test(new MapExternalizer.HashMapExternalizer(), new HashMap<>(basis)); + test(new MapExternalizer.LinkedHashMapExternalizer(), new LinkedHashMap<>(basis)); + + test(new EmptyCollectionExternalizer.EmptyMapExternalizer(), Collections.emptyMap()); + test(new EmptyCollectionExternalizer.EmptyNavigableMapExternalizer(), Collections.emptyNavigableMap()); + test(new EmptyCollectionExternalizer.EmptySortedMapExternalizer(), Collections.emptySortedMap()); + + test(new SingletonMapExternalizer(), Collections.singletonMap(1, 2)); + + test(new SortedMapExternalizer.ConcurrentSkipListMapExternalizer(), new ConcurrentSkipListMap<>(basis)); + test(new SortedMapExternalizer.TreeMapExternalizer(), new TreeMap<>(basis)); + } + + public static > void test(Externalizer externalizer, T collection) throws ClassNotFoundException, IOException { + BiConsumer assertSize = (expected, actual) -> assertEquals(expected.size(), actual.size()); + BiConsumer assertContents = (expected, actual) -> assertTrue(actual.keySet().containsAll(expected.keySet())); + ExternalizerTestUtil.test(externalizer, collection, assertSize.andThen(assertContents)); } } diff --git a/clustering/marshalling/spi/src/test/java/org/wildfly/clustering/marshalling/spi/util/SortedSetExternalizerTestCase.java b/clustering/marshalling/spi/src/test/java/org/wildfly/clustering/marshalling/spi/util/SortedSetExternalizerTestCase.java deleted file mode 100644 index ebc32d2c8f1e..000000000000 --- a/clustering/marshalling/spi/src/test/java/org/wildfly/clustering/marshalling/spi/util/SortedSetExternalizerTestCase.java +++ /dev/null @@ -1,46 +0,0 @@ -/* - * JBoss, Home of Professional Open Source. - * Copyright 2015, Red Hat, Inc., and individual contributors - * as indicated by the @author tags. See the copyright.txt file in the - * distribution for a full listing of individual contributors. - * - * This is free software; you can redistribute it and/or modify it - * under the terms of the GNU Lesser General Public License as - * published by the Free Software Foundation; either version 2.1 of - * the License, or (at your option) any later version. - * - * This software is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this software; if not, write to the Free - * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA - * 02110-1301 USA, or see the FSF site: http://www.fsf.org. - */ - -package org.wildfly.clustering.marshalling.spi.util; - -import java.io.IOException; -import java.util.Arrays; -import java.util.Collection; -import java.util.TreeSet; -import java.util.concurrent.ConcurrentSkipListSet; - -import org.junit.Test; -import org.wildfly.clustering.marshalling.spi.ExternalizerTestUtil; - -/** - * Unit test for {@link SortedSetExternalizer} externalizers - * @author Paul Ferraro - */ -public class SortedSetExternalizerTestCase { - - @Test - public void test() throws ClassNotFoundException, IOException { - Collection basis = Arrays.asList(1, 2, 3, 4, 5); - ExternalizerTestUtil.test(new SortedSetExternalizer.ConcurrentSkipListSetExternalizer(), new ConcurrentSkipListSet<>(basis)); - ExternalizerTestUtil.test(new SortedSetExternalizer.TreeSetExternalizer(), new TreeSet<>(basis)); - } -}