From c142ab1d709194037d1185de8938ad79dc7d58e0 Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Thu, 12 Feb 2009 11:07:41 +0000 Subject: [PATCH] * fixed #60 - created @OrderAwarePluginRegistry@ that uses Spring ordering API to ensure correct order of plugins * let @PluginRegistryBeanFactoryPostProcessor@ use the new one instead of the old one * renamed old @PluginRegistry@ class intro @SimplePluginRegistry@ * extracted @PluginRegistry@ interface * added some unittests for both implementations git-svn-id: svn+ssh://svn.synyx.de/var/svn/synyx/opensource/hera/trunk@4487 5a64d73e-33d6-4ccc-9058-23f8668ecac9 --- .../hera/core/OrderAwarePluginRegistry.java | 63 ++++ .../org/synyx/hera/core/PluginRegistry.java | 350 ++++++------------ .../synyx/hera/core/SimplePluginRegistry.java | 218 +++++++++++ .../BeanListBeanFactoryPostProcessor.java | 4 +- ...luginRegistryBeanFactoryPostProcessor.java | 14 +- .../OrderAwarePluginRegistryUnitTest.java | 122 ++++++ .../hera/core/PluginRegistryUnitTest.java | 68 ---- .../org/synyx/hera/core/SamplePluginHost.java | 2 +- .../core/SimplePluginRegistryUnitTest.java | 115 ++++++ 9 files changed, 639 insertions(+), 317 deletions(-) create mode 100644 hera-core/src/main/java/org/synyx/hera/core/OrderAwarePluginRegistry.java create mode 100644 hera-core/src/main/java/org/synyx/hera/core/SimplePluginRegistry.java create mode 100644 hera-core/src/test/java/org/synyx/hera/core/OrderAwarePluginRegistryUnitTest.java delete mode 100644 hera-core/src/test/java/org/synyx/hera/core/PluginRegistryUnitTest.java create mode 100644 hera-core/src/test/java/org/synyx/hera/core/SimplePluginRegistryUnitTest.java diff --git a/hera-core/src/main/java/org/synyx/hera/core/OrderAwarePluginRegistry.java b/hera-core/src/main/java/org/synyx/hera/core/OrderAwarePluginRegistry.java new file mode 100644 index 0000000..4508210 --- /dev/null +++ b/hera-core/src/main/java/org/synyx/hera/core/OrderAwarePluginRegistry.java @@ -0,0 +1,63 @@ +package org.synyx.hera.core; + +import java.util.Collections; +import java.util.Comparator; +import java.util.List; + +import org.springframework.core.Ordered; +import org.springframework.core.annotation.AnnotationAwareOrderComparator; +import org.springframework.core.annotation.Order; + + +/** + * {@link PluginRegistry} implementation that can handle {@link Plugin}s using + * the {@link Ordered} interface or {@link Order} annotation. + * + * @author Oliver Gierke - gierke@synyx.de + */ +public class OrderAwarePluginRegistry, S> extends + SimplePluginRegistry { + + @SuppressWarnings("unchecked") + private Comparator comparator = new AnnotationAwareOrderComparator(); + + + /** + * Creates a new {@link SimplePluginRegistry}. + * + * @param + * @param + * @return + */ + public static > OrderAwarePluginRegistry create() { + + return new OrderAwarePluginRegistry(); + } + + + /* + * (non-Javadoc) + * + * @see org.synyx.hera.core.PluginRegistry#setPlugins(java.util.List) + */ + @Override + public void setPlugins(List plugins) { + + Collections.sort(plugins, comparator); + super.setPlugins(plugins); + } + + + /* + * (non-Javadoc) + * + * @see + * org.synyx.hera.core.PluginRegistry#addPlugin(org.synyx.hera.core.Plugin) + */ + @Override + public void addPlugin(T plugin) { + + super.addPlugin(plugin); + Collections.sort(getPlugins(), comparator); + } +} diff --git a/hera-core/src/main/java/org/synyx/hera/core/PluginRegistry.java b/hera-core/src/main/java/org/synyx/hera/core/PluginRegistry.java index 259e87d..c9c0073 100644 --- a/hera-core/src/main/java/org/synyx/hera/core/PluginRegistry.java +++ b/hera-core/src/main/java/org/synyx/hera/core/PluginRegistry.java @@ -1,240 +1,110 @@ -/* - * Copyright 2002-2008 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 java.util.ArrayList; -import java.util.Iterator; -import java.util.List; - - -/** - * Registry for plugins. Allows sophisticated typesafe access to implementations - * of interfaces extending {link Plugin}. - * - * @param the concrete plugin interface - * @param the delimiter type - * @author Oliver Gierke - gierke@synyx.de - */ -public class PluginRegistry, S> implements Iterable { - - // Registered plugins - private List plugins; - - - /** - * Creates a new {@code PluginRegistry}. - */ - public PluginRegistry() { - - plugins = new ArrayList(); - } - - - /** - * Creates a new {@link PluginRegistry}. - * - * @param - * @param - * @return - */ - public static > PluginRegistry create() { - - return new PluginRegistry(); - } - - - /** - * Register plugins. - * - * @param plugins the plugins to set - */ - public void setPlugins(List plugins) { - - this.plugins = new ArrayList(); - this.plugins.addAll(plugins); - } - - - /** - * Adds a given plugin to the registry. - * - * @param plugin - */ - public void addPlugin(T plugin) { - - this.plugins.add(plugin); - } - - - /** - * Returns the first plugin found for the given originating system. Thus, - * further configured plugins are ignored. - * - * @param originatingSystem - * @return a plugin for the given originating system or {@code null} if none - * found - */ - public T getPluginFor(S delimiter) { - - List plugins = getPluginsFor(delimiter); - - if (0 < plugins.size()) { - return plugins.get(0); - } - - return null; - } - - - /** - * Returns all plugins for the given delimiter. - * - * @param delimiter - * @return a list of plugins or an empty list if none found - */ - public List getPluginsFor(S delimiter) { - - List result = new ArrayList(); - - for (T plugin : plugins) { - if (plugin.supports(delimiter)) { - result.add(plugin); - } - } - - return result; - } - - - /** - * Retrieves a required plugin from the registry or throw the given - * exception if none can be found. If more than one plugins are found the - * first one will be returned. - * - * @param the exception type to be thrown in case no plugin can be found - * @param delimiter - * @param ex the exception to be thrown in case no plugin can be found - * @return a single plugin for the given delimiter - * @throws E if no plugin can be found for the given delimiter - */ - public T getPluginFor(S delimiter, E ex) throws E { - - T plugin = getPluginFor(delimiter); - - if (null == plugin) { - throw ex; - } - - return plugin; - } - - - /** - * Retrieves all plugins for the given delimiter or throws an exception if - * no plugin can be found. - * - * @param the exception type to be thrown - * @param delimiter - * @param ex - * @return all plugins for the given delimiter - * @throws E if no plugin can be found - */ - public List getPluginsFor(S delimiter, E ex) - throws E { - - List plugins = getPluginsFor(delimiter); - - if (0 == plugins.size()) { - throw ex; - } - - return plugins; - } - - - /** - * Returns the first {@link Plugin} supporting the given delimiter or the - * given plugin if none can be found. - * - * @param delimiter - * @param plugin - * @return a single {@link Plugin} supporting the given delimiter or the - * given {@link Plugin} if none found - */ - public T getPluginFor(S delimiter, T plugin) { - - T candidate = getPluginFor(delimiter); - - return null == candidate ? plugin : candidate; - } - - - /** - * Returns all {@link Plugin}s supporting the given delimiter or the given - * plugins if none found. - * - * @param delimiter - * @param plugins - * @return all {@link Plugin}s supporting the given delimiter or the given - * {@link Plugin}s if none found - */ - public List getPluginsFor(S delimiter, List plugins) { - - List candidates = getPluginsFor(delimiter); - - return candidates.size() == 0 ? plugins : candidates; - } - - - /** - * Returns the number of registered plugins. - * - * @return the number of plugins in the registry - */ - public int countPlugins() { - - return plugins.size(); - } - - - /** - * Returns all registered plugins. Only use this method if you really need - * to access all plugins. For distinguished access to certain plugins favour - * accessor methods like {link #getPluginFor} over this one. This method - * should only be used for testing purposes to check registry configuration. - *

- * TODO: decide whether to make this method public - * - * @return all plugins of the registry - */ - @SuppressWarnings("unused") - private List getPlugins() { - - return plugins; - } - - - /* - * (non-Javadoc) - * - * @see java.lang.Iterable#iterator() - */ - public Iterator iterator() { - - return plugins.iterator(); - } -} +package org.synyx.hera.core; + +import java.util.List; + + +/** + * Registry for plugins. Allows sophisticated typesafe access to implementations + * of interfaces extending {link Plugin}. + * + * @param the concrete plugin interface + * @param the delimiter type + * @author Oliver Gierke - gierke@synyx.de + */ +public interface PluginRegistry, S> extends Iterable { + + /** + * Register plugins. + * + * @param plugins the plugins to set + */ + void setPlugins(List plugins); + + + /** + * Adds a given plugin to the registry. + * + * @param plugin + */ + void addPlugin(T plugin); + + + /** + * Returns the first plugin found for the given originating system. Thus, + * further configured plugins are ignored. + * + * @param originatingSystem + * @return a plugin for the given originating system or {@code null} if none + * found + */ + T getPluginFor(S delimiter); + + + /** + * Returns all plugins for the given delimiter. + * + * @param delimiter + * @return a list of plugins or an empty list if none found + */ + List getPluginsFor(S delimiter); + + + /** + * Retrieves a required plugin from the registry or throw the given + * exception if none can be found. If more than one plugins are found the + * first one will be returned. + * + * @param the exception type to be thrown in case no plugin can be found + * @param delimiter + * @param ex the exception to be thrown in case no plugin can be found + * @return a single plugin for the given delimiter + * @throws E if no plugin can be found for the given delimiter + */ + T getPluginFor(S delimiter, E ex) throws E; + + + /** + * Retrieves all plugins for the given delimiter or throws an exception if + * no plugin can be found. + * + * @param the exception type to be thrown + * @param delimiter + * @param ex + * @return all plugins for the given delimiter + * @throws E if no plugin can be found + */ + List getPluginsFor(S delimiter, E ex) throws E; + + + /** + * Returns the first {@link Plugin} supporting the given delimiter or the + * given plugin if none can be found. + * + * @param delimiter + * @param plugin + * @return a single {@link Plugin} supporting the given delimiter or the + * given {@link Plugin} if none found + */ + T getPluginFor(S delimiter, T plugin); + + + /** + * Returns all {@link Plugin}s supporting the given delimiter or the given + * plugins if none found. + * + * @param delimiter + * @param plugins + * @return all {@link Plugin}s supporting the given delimiter or the given + * {@link Plugin}s if none found + */ + List getPluginsFor(S delimiter, List plugins); + + + /** + * Returns the number of registered plugins. + * + * @return the number of plugins in the registry + */ + int countPlugins(); + +} \ No newline at end of file diff --git a/hera-core/src/main/java/org/synyx/hera/core/SimplePluginRegistry.java b/hera-core/src/main/java/org/synyx/hera/core/SimplePluginRegistry.java new file mode 100644 index 0000000..3e8d27c --- /dev/null +++ b/hera-core/src/main/java/org/synyx/hera/core/SimplePluginRegistry.java @@ -0,0 +1,218 @@ +/* + * Copyright 2002-2008 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 java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + + +/** + * Basic implementation of {@link PluginRegistry}. Simply holds all given + * plugins in a list. + * + * @param the concrete plugin interface + * @param the delimiter type + * @author Oliver Gierke - gierke@synyx.de + */ +public class SimplePluginRegistry, S> implements + PluginRegistry { + + // Registered plugins + private List plugins; + + + /** + * Creates a new {@code PluginRegistry}. + */ + public SimplePluginRegistry() { + + plugins = new ArrayList(); + } + + + /** + * Creates a new {@link SimplePluginRegistry}. + * + * @param + * @param + * @return + */ + public static > PluginRegistry create() { + + return new SimplePluginRegistry(); + } + + + /* + * (non-Javadoc) + * + * @see org.synyx.hera.core.PluginRegistry#setPlugins(java.util.List) + */ + public void setPlugins(List plugins) { + + this.plugins = new ArrayList(); + this.plugins.addAll(plugins); + } + + + /* + * (non-Javadoc) + * + * @see org.synyx.hera.core.PluginRegistry#addPlugin(T) + */ + public void addPlugin(T plugin) { + + this.plugins.add(plugin); + } + + + /* + * (non-Javadoc) + * + * @see org.synyx.hera.core.PluginRegistry#getPluginFor(S) + */ + public T getPluginFor(S delimiter) { + + List plugins = getPluginsFor(delimiter); + + if (0 < plugins.size()) { + return plugins.get(0); + } + + return null; + } + + + /* + * (non-Javadoc) + * + * @see org.synyx.hera.core.PluginRegistry#getPluginsFor(S) + */ + public List getPluginsFor(S delimiter) { + + List result = new ArrayList(); + + for (T plugin : plugins) { + if (plugin.supports(delimiter)) { + result.add(plugin); + } + } + + return result; + } + + + /* + * (non-Javadoc) + * + * @see org.synyx.hera.core.PluginRegistry#getPluginFor(S, E) + */ + public T getPluginFor(S delimiter, E ex) throws E { + + T plugin = getPluginFor(delimiter); + + if (null == plugin) { + throw ex; + } + + return plugin; + } + + + /* + * (non-Javadoc) + * + * @see org.synyx.hera.core.PluginRegistry#getPluginsFor(S, E) + */ + public List getPluginsFor(S delimiter, E ex) + throws E { + + List plugins = getPluginsFor(delimiter); + + if (0 == plugins.size()) { + throw ex; + } + + return plugins; + } + + + /* + * (non-Javadoc) + * + * @see org.synyx.hera.core.PluginRegistry#getPluginFor(S, T) + */ + public T getPluginFor(S delimiter, T plugin) { + + T candidate = getPluginFor(delimiter); + + return null == candidate ? plugin : candidate; + } + + + /* + * (non-Javadoc) + * + * @see org.synyx.hera.core.PluginRegistry#getPluginsFor(S, java.util.List) + */ + public List getPluginsFor(S delimiter, + List plugins) { + + List candidates = getPluginsFor(delimiter); + + return candidates.size() == 0 ? plugins : candidates; + } + + + /* + * (non-Javadoc) + * + * @see org.synyx.hera.core.PluginRegistry#countPlugins() + */ + public int countPlugins() { + + return plugins.size(); + } + + + /** + * Returns all registered plugins. Only use this method if you really need + * to access all plugins. For distinguished access to certain plugins favour + * accessor methods like {link #getPluginFor} over this one. This method + * should only be used for testing purposes to check registry configuration. + *

+ * TODO: decide whether to make this method public + * + * @return all plugins of the registry + */ + protected List getPlugins() { + + return plugins; + } + + + /* + * (non-Javadoc) + * + * @see java.lang.Iterable#iterator() + */ + public Iterator iterator() { + + return plugins.iterator(); + } +} diff --git a/hera-core/src/main/java/org/synyx/hera/core/support/BeanListBeanFactoryPostProcessor.java b/hera-core/src/main/java/org/synyx/hera/core/support/BeanListBeanFactoryPostProcessor.java index 365a96b..f0b0426 100644 --- a/hera-core/src/main/java/org/synyx/hera/core/support/BeanListBeanFactoryPostProcessor.java +++ b/hera-core/src/main/java/org/synyx/hera/core/support/BeanListBeanFactoryPostProcessor.java @@ -33,7 +33,7 @@ import org.springframework.beans.factory.support.BeanDefinitionBuilder; import org.springframework.beans.factory.support.BeanDefinitionRegistry; import org.springframework.beans.factory.support.ManagedList; import org.springframework.core.Ordered; -import org.synyx.hera.core.PluginRegistry; +import org.synyx.hera.core.SimplePluginRegistry; /** @@ -72,7 +72,7 @@ public class BeanListBeanFactoryPostProcessor implements /** * Setter to inject required plugin registry configuration. The map's keys - * will be used as bean ids for the resulting {@link PluginRegistry} + * will be used as bean ids for the resulting {@link SimplePluginRegistry} * instances. These registry instances will contain all beans having the * given type. * diff --git a/hera-core/src/main/java/org/synyx/hera/core/support/PluginRegistryBeanFactoryPostProcessor.java b/hera-core/src/main/java/org/synyx/hera/core/support/PluginRegistryBeanFactoryPostProcessor.java index 5af63ed..3013248 100644 --- a/hera-core/src/main/java/org/synyx/hera/core/support/PluginRegistryBeanFactoryPostProcessor.java +++ b/hera-core/src/main/java/org/synyx/hera/core/support/PluginRegistryBeanFactoryPostProcessor.java @@ -19,13 +19,13 @@ import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.config.BeanDefinition; import org.springframework.beans.factory.config.BeanFactoryPostProcessor; import org.springframework.beans.factory.support.BeanDefinitionBuilder; -import org.synyx.hera.core.PluginRegistry; +import org.synyx.hera.core.OrderAwarePluginRegistry; /** * This {@link BeanFactoryPostProcessor} automatically looksup bean instances - * from the {@link BeanFactory} hierarchy and registers {@link PluginRegistry} - * instances for them. + * from the {@link BeanFactory} hierarchy and registers + * {@link OrderAwarePluginRegistry} instances for them. * * @see org.synyx.hera.core.support.BeanListBeanFactoryPostProcessor * @author Oliver Gierke - gierke@synyx.de @@ -36,12 +36,13 @@ public class PluginRegistryBeanFactoryPostProcessor extends /** * Additionally wraps the * {@link org.springframework.beans.factory.config.ListFactoryBean} - * {@link BeanDefinition} into a {@link PluginRegistry}. + * {@link BeanDefinition} into a {@link OrderAwarePluginRegistry}. * * @see com.synyx.minos.core.plugin.support.BeanListBeanFactoryPostProcessor# * wrapBeanDefinition * (org.springframework.beans.factory.config.BeanDefinition) - * @return a {@link BeanDefinition} containing a {@link PluginRegistry} + * @return a {@link BeanDefinition} containing a + * {@link OrderAwarePluginRegistry} */ @Override protected BeanDefinition wrapListBeanDefinition( @@ -49,7 +50,8 @@ public class PluginRegistryBeanFactoryPostProcessor extends // Create PluginRegistry bean definition to wrap actual bean definition BeanDefinitionBuilder builder = - BeanDefinitionBuilder.rootBeanDefinition(PluginRegistry.class); + BeanDefinitionBuilder + .rootBeanDefinition(OrderAwarePluginRegistry.class); builder.addPropertyValue("plugins", beanDefinition); diff --git a/hera-core/src/test/java/org/synyx/hera/core/OrderAwarePluginRegistryUnitTest.java b/hera-core/src/test/java/org/synyx/hera/core/OrderAwarePluginRegistryUnitTest.java new file mode 100644 index 0000000..92f1d1c --- /dev/null +++ b/hera-core/src/test/java/org/synyx/hera/core/OrderAwarePluginRegistryUnitTest.java @@ -0,0 +1,122 @@ +package org.synyx.hera.core; + +import static org.junit.Assert.*; + +import java.util.Arrays; +import java.util.List; + +import org.junit.Before; +import org.junit.Test; +import org.springframework.core.annotation.Order; + + +/** + * Unit test for {@link OrderAwarePluginRegistry} that especially concentrates + * on testing ordering functionality. + * + * @author Oliver Gierke - gierke@synyx.de + */ +public class OrderAwarePluginRegistryUnitTest extends + SimplePluginRegistryUnitTest { + + private PluginRegistry registry; + + private TestPlugin firstPlugin; + private TestPlugin secondPlugin; + + + @Before + public void setUp() { + + super.setUp(); + + registry = OrderAwarePluginRegistry.create(); + + firstPlugin = new FirstImplementation(); + secondPlugin = new SecondImplementation(); + } + + + /** + * Adds the plugin implementations in order of their names, expecting the + * registry to order them correctly. + * + * @throws Exception + */ + @Test + public void honorsOrderOnAddPlugins() throws Exception { + + registry.setPlugins(Arrays.asList(firstPlugin, secondPlugin)); + + assertOrder(); + } + + + @Test + public void assertsOrderOnAddingPlugins() throws Exception { + + registry.setPlugins(Arrays.asList(firstPlugin)); + registry.addPlugin(secondPlugin); + + assertOrder(); + } + + + private void assertOrder() { + + List plugins = registry.getPluginsFor(null); + + assertEquals(2, plugins.size()); + assertEquals(secondPlugin, plugins.get(0)); + assertEquals(firstPlugin, plugins.get(1)); + + assertEquals(secondPlugin, registry.getPluginFor(null)); + } + + /** + * Simple test interface. + * + * @author Oliver Gierke - gierke@synyx.de + */ + private static interface TestPlugin extends Plugin { + + } + + /** + * Plugin implementation, that is orderd right AFTER the second one. + * + * @author Oliver Gierke - gierke@synyx.de + */ + @Order(5) + private static class FirstImplementation implements TestPlugin { + + /* + * (non-Javadoc) + * + * @see org.synyx.hera.core.Plugin#supports(java.lang.Object) + */ + public boolean supports(String delimiter) { + + return true; + } + } + + /** + * Plugin implementation that is ordered BEFORE the first one. + * + * @author Oliver Gierke - gierke@synyx.de + */ + @Order(1) + private static class SecondImplementation implements TestPlugin { + + /* + * (non-Javadoc) + * + * @see org.synyx.hera.core.Plugin#supports(java.lang.Object) + */ + public boolean supports(String delimiter) { + + return true; + } + } +} diff --git a/hera-core/src/test/java/org/synyx/hera/core/PluginRegistryUnitTest.java b/hera-core/src/test/java/org/synyx/hera/core/PluginRegistryUnitTest.java deleted file mode 100644 index 98b7cba..0000000 --- a/hera-core/src/test/java/org/synyx/hera/core/PluginRegistryUnitTest.java +++ /dev/null @@ -1,68 +0,0 @@ -/* - * Copyright 2002-2008 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 java.util.Arrays; -import java.util.List; - -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; - - -/** - * Unit test for {@link PluginRegistry}. - * - * @author Oliver Gierke - gierke@synyx.de - */ -public class PluginRegistryUnitTest { - - private SamplePlugin provider; - - private PluginRegistry registry; - - - /** - * Initializes a {@code PluginRegistry} and equips it with an {@code - * EmailNotificationProvider}. - */ - @Before - public void setUp() { - - provider = new SamplePluginImplementation(); - - registry = new PluginRegistry(); - registry.setPlugins(Arrays.asList(provider)); - } - - - /** - * Asserts asking for a plugin with the {@code PluginMetadata} provided by - * the {@link EmailNotificationProvider}. - */ - @Test - public void assertFindsEmailNotificationProvider() { - - String metadata = "FOO"; - - List plugins = registry.getPluginsFor(metadata); - Assert.assertNotNull(plugins); - Assert.assertEquals(1, plugins.size()); - - SamplePlugin provider = plugins.get(0); - Assert.assertTrue(provider instanceof SamplePluginImplementation); - } -} diff --git a/hera-core/src/test/java/org/synyx/hera/core/SamplePluginHost.java b/hera-core/src/test/java/org/synyx/hera/core/SamplePluginHost.java index c85c2e2..934bd99 100644 --- a/hera-core/src/test/java/org/synyx/hera/core/SamplePluginHost.java +++ b/hera-core/src/test/java/org/synyx/hera/core/SamplePluginHost.java @@ -6,7 +6,7 @@ package org.synyx.hera.core; public class SamplePluginHost { private PluginRegistry registry = - PluginRegistry.create(); + SimplePluginRegistry.create(); /** diff --git a/hera-core/src/test/java/org/synyx/hera/core/SimplePluginRegistryUnitTest.java b/hera-core/src/test/java/org/synyx/hera/core/SimplePluginRegistryUnitTest.java new file mode 100644 index 0000000..b52689d --- /dev/null +++ b/hera-core/src/test/java/org/synyx/hera/core/SimplePluginRegistryUnitTest.java @@ -0,0 +1,115 @@ +/* + * Copyright 2002-2008 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.junit.Assert.*; + +import java.util.Arrays; +import java.util.List; + +import org.junit.Before; +import org.junit.Test; + + +/** + * Unit test for {@link SimplePluginRegistry}. + * + * @author Oliver Gierke - gierke@synyx.de + */ +public class SimplePluginRegistryUnitTest { + + private SamplePlugin provider; + + private PluginRegistry registry; + + + /** + * Initializes a {@code PluginRegistry} and equips it with an {@code + * EmailNotificationProvider}. + */ + @Before + public void setUp() { + + provider = new SamplePluginImplementation(); + + registry = new SimplePluginRegistry(); + registry.setPlugins(Arrays.asList(provider)); + } + + + /** + * Asserts asking for a plugin with the {@code PluginMetadata} provided by + * the {@link EmailNotificationProvider}. + */ + @Test + public void assertFindsEmailNotificationProvider() { + + String metadata = "FOO"; + + List plugins = registry.getPluginsFor(metadata); + assertNotNull(plugins); + assertEquals(1, plugins.size()); + + SamplePlugin provider = plugins.get(0); + assertTrue(provider instanceof SamplePluginImplementation); + } + + + /** + * Expects the given exception to be thrown if no {@link Plugin} found. + */ + @Test(expected = IllegalArgumentException.class) + public void throwsExceptionIfNoPluginFound() { + + registry.getPluginFor("BAR", new IllegalArgumentException()); + } + + + /** + * Expects the given exception to be thrown if no {@link Plugin}s found. + */ + @Test(expected = IllegalArgumentException.class) + public void throwsExceptionIfNoPluginsFound() { + + registry.getPluginsFor("BAR", new IllegalArgumentException()); + } + + + /** + * Expect the defualt plugin to be returned if none found. + */ + @Test + public void returnsDefaultIfNoneFound() { + + SamplePlugin defaultPlugin = new SamplePluginImplementation(); + + assertEquals(defaultPlugin, registry.getPluginFor("BAR", defaultPlugin)); + } + + + /** + * Expect the given default plugins to be returned if none found. + */ + @Test + public void returnsDefaultsIfNoneFound() { + + List defaultPlugins = + Arrays.asList(new SamplePluginImplementation()); + + assertEquals(defaultPlugins, registry.getPluginsFor("BAR", + defaultPlugins)); + } +}