From d20e4298791221ae5c1a16880bcf11d33bdc1926 Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Mon, 11 Jan 2010 14:40:30 +0000 Subject: [PATCH] * restructured test cases * do sorting in the appropriate place git-svn-id: svn+ssh://svn.synyx.de/var/svn/synyx/opensource/hera/trunk@8547 5a64d73e-33d6-4ccc-9058-23f8668ecac9 --- .../hera/core/OrderAwarePluginRegistry.java | 6 +- ...AbstractMutablePluginRegistryUnitTest.java | 68 +++++++++++++++++ .../OrderAwarePluginRegistryUnitTest.java | 49 ++++++++++-- .../core/SimplePluginRegistryUnitTest.java | 74 ++++++++++++++++--- 4 files changed, 176 insertions(+), 21 deletions(-) create mode 100644 core/src/test/java/org/synyx/hera/core/AbstractMutablePluginRegistryUnitTest.java diff --git a/core/src/main/java/org/synyx/hera/core/OrderAwarePluginRegistry.java b/core/src/main/java/org/synyx/hera/core/OrderAwarePluginRegistry.java index 39c4cef..f5d00a2 100644 --- a/core/src/main/java/org/synyx/hera/core/OrderAwarePluginRegistry.java +++ b/core/src/main/java/org/synyx/hera/core/OrderAwarePluginRegistry.java @@ -174,8 +174,12 @@ public class OrderAwarePluginRegistry, S> extends @Override public void setPlugins(List plugins) { - Collections.sort(plugins, comparator); super.setPlugins(plugins); + + if (!this.plugins.isEmpty()) { + Collections.sort(this.plugins, comparator); + } + } diff --git a/core/src/test/java/org/synyx/hera/core/AbstractMutablePluginRegistryUnitTest.java b/core/src/test/java/org/synyx/hera/core/AbstractMutablePluginRegistryUnitTest.java new file mode 100644 index 0000000..12412ab --- /dev/null +++ b/core/src/test/java/org/synyx/hera/core/AbstractMutablePluginRegistryUnitTest.java @@ -0,0 +1,68 @@ +/* + * Copyright 2008-2010 the original author or authors. + * + * 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.synyx.hera.core; + +import static org.hamcrest.CoreMatchers.*; +import static org.junit.Assert.*; + +import java.util.Arrays; + +import org.junit.Test; + + +/** + * Unit test for implementations of {@link MutablePluginRegistry}. + * + * @author Oliver Gierke - gierke@synyx.de + */ +public abstract class AbstractMutablePluginRegistryUnitTest { + + private SamplePlugin plugin = new SamplePluginImplementation(); + + + @Test + public void allowsAddingPluginsAfterCreation() throws Exception { + + MutablePluginRegistry registry = getRegistry(); + registry.addPlugin(plugin); + + assertTrue(registry.contains(plugin)); + assertThat(registry.getPlugins().size(), is(1)); + } + + + @Test + public void settingPluginsRemovesOldOnes() throws Exception { + + MutablePluginRegistry registry = getRegistry(); + registry.addPlugin(plugin); + + SamplePlugin anotherPlugin = new SamplePluginImplementation(); + + registry.setPlugins(Arrays.asList(anotherPlugin)); + assertTrue(registry.contains(anotherPlugin)); + assertFalse(registry.contains(plugin)); + } + + + /** + * Return the {@link MutablePluginRegistry} to test. + * + * @return + */ + protected abstract MutablePluginRegistry getRegistry(); +} diff --git a/core/src/test/java/org/synyx/hera/core/OrderAwarePluginRegistryUnitTest.java b/core/src/test/java/org/synyx/hera/core/OrderAwarePluginRegistryUnitTest.java index 59e22ad..224a9b0 100644 --- a/core/src/test/java/org/synyx/hera/core/OrderAwarePluginRegistryUnitTest.java +++ b/core/src/test/java/org/synyx/hera/core/OrderAwarePluginRegistryUnitTest.java @@ -55,6 +55,18 @@ public class OrderAwarePluginRegistryUnitTest extends } + /* + * (non-Javadoc) + * + * @see org.synyx.hera.core.SimplePluginRegistryUnitTest#getRegistry() + */ + @Override + protected OrderAwarePluginRegistry getRegistry() { + + return OrderAwarePluginRegistry.create(); + } + + /** * Adds the plugin implementations in order of their names, expecting the * registry to order them correctly. @@ -66,7 +78,7 @@ public class OrderAwarePluginRegistryUnitTest extends registry.setPlugins(Arrays.asList(firstPlugin, secondPlugin)); - assertOrder(); + assertOrder(registry, secondPlugin, firstPlugin); } @@ -76,19 +88,40 @@ public class OrderAwarePluginRegistryUnitTest extends registry.setPlugins(Arrays.asList(firstPlugin)); registry.addPlugin(secondPlugin); - assertOrder(); + assertOrder(registry, secondPlugin, firstPlugin); } - private void assertOrder() { + private void assertOrder(PluginRegistry registry, + TestPlugin... plugins) { - List plugins = registry.getPluginsFor(null); + List result = registry.getPluginsFor(null); - assertEquals(2, plugins.size()); - assertEquals(secondPlugin, plugins.get(0)); - assertEquals(firstPlugin, plugins.get(1)); + assertThat(plugins.length, is(result.size())); - assertEquals(secondPlugin, registry.getPluginFor(null)); + for (int i = 0; i < plugins.length; i++) { + assertThat(result.get(i), is(result.get(i))); + } + + assertThat(registry.getPluginFor(null), is(plugins[0])); + } + + + @Test + public void createsRevertedRegistryCorrectly() throws Exception { + + basicPrepare(); + PluginRegistry reverse = registry.reverse(); + + assertOrder(registry, secondPlugin, firstPlugin); + assertOrder(reverse, firstPlugin, secondPlugin); + } + + + private void basicPrepare() { + + registry.setPlugins(Arrays.asList(firstPlugin, secondPlugin)); + assertOrder(registry, secondPlugin, firstPlugin); } /** diff --git a/core/src/test/java/org/synyx/hera/core/SimplePluginRegistryUnitTest.java b/core/src/test/java/org/synyx/hera/core/SimplePluginRegistryUnitTest.java index 05fb36e..5d4a816 100644 --- a/core/src/test/java/org/synyx/hera/core/SimplePluginRegistryUnitTest.java +++ b/core/src/test/java/org/synyx/hera/core/SimplePluginRegistryUnitTest.java @@ -31,11 +31,12 @@ import org.junit.Test; * * @author Oliver Gierke - gierke@synyx.de */ -public class SimplePluginRegistryUnitTest { +public class SimplePluginRegistryUnitTest extends + AbstractMutablePluginRegistryUnitTest { private SamplePlugin plugin; - private PluginRegistry registry; + private SimplePluginRegistry registry; /** @@ -47,7 +48,20 @@ public class SimplePluginRegistryUnitTest { plugin = new SamplePluginImplementation(); - registry = SimplePluginRegistry.create(Arrays.asList(plugin)); + registry = SimplePluginRegistry.create(); + } + + + /* + * (non-Javadoc) + * + * @see + * org.synyx.hera.core.AbstractMutablePluginRegistryUnitTest#getRegistry() + */ + @Override + protected MutablePluginRegistry getRegistry() { + + return SimplePluginRegistry.create(); } @@ -59,7 +73,9 @@ public class SimplePluginRegistryUnitTest { @Test public void assertRegistryInitialized() throws Exception { - assertEquals(1, registry.countPlugins()); + registry.addPlugin(plugin); + + assertThat(registry.countPlugins(), is(1)); assertTrue(registry.contains(plugin)); } @@ -71,14 +87,16 @@ public class SimplePluginRegistryUnitTest { @Test public void assertFindsEmailNotificationProvider() { - String metadata = "FOO"; + registry.addPlugin(plugin); - List plugins = registry.getPluginsFor(metadata); - assertNotNull(plugins); - assertEquals(1, plugins.size()); + String delimiter = "FOO"; + + List plugins = registry.getPluginsFor(delimiter); + assertThat(plugins, is(notNullValue())); + assertThat(plugins.size(), is(1)); SamplePlugin provider = plugins.get(0); - assertTrue(provider instanceof SamplePluginImplementation); + assertThat(provider, is(instanceOf(SamplePluginImplementation.class))); } @@ -110,7 +128,8 @@ public class SimplePluginRegistryUnitTest { SamplePlugin defaultPlugin = new SamplePluginImplementation(); - assertEquals(defaultPlugin, registry.getPluginFor("BAR", defaultPlugin)); + assertThat(registry.getPluginFor("BAR", defaultPlugin), + is(defaultPlugin)); } @@ -123,7 +142,38 @@ public class SimplePluginRegistryUnitTest { List defaultPlugins = Arrays.asList(new SamplePluginImplementation()); - assertEquals(defaultPlugins, registry.getPluginsFor("BAR", - defaultPlugins)); + List result = + registry.getPluginsFor("BAR", defaultPlugins); + assertTrue(result.containsAll(defaultPlugins)); + } + + + @Test + public void handlesAddingNullPluginsCorrecty() throws Exception { + + registry.addPlugin(null); + + assertThat(registry.countPlugins(), is(0)); + } + + + @Test + public void doesNotAddNullsOnSettingPlugins() throws Exception { + + registry.setPlugins(Arrays.asList(null, plugin, null)); + + assertThat(registry.countPlugins(), is(1)); + assertThat(registry.getPlugins().size(), is(1)); + } + + + @Test + public void dropsNullValuesOnCreation() throws Exception { + + registry = + SimplePluginRegistry.create(Arrays.asList(null, plugin, null)); + + assertThat(registry.countPlugins(), is(1)); + assertThat(registry.getPlugins().size(), is(1)); } }