From 856f662cf98929aeb7d40fff27643e3c1e05f608 Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Fri, 21 Apr 2017 08:01:43 +0200 Subject: [PATCH] =?UTF-8?q?#41=20-=20Added=20PluginRegistry.getRequiredPlu?= =?UTF-8?q?gin(=E2=80=A6).?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Added aforementioned method for clients to look up a required plugin with the registry throwing a default exception if none is found. An overload taking a Supplier is available to customize the exception message if needed. --- .../plugin/core/PluginRegistry.java | 21 ++++++++++++++ .../plugin/core/SimplePluginRegistry.java | 25 ++++++++++++++++ .../core/SimplePluginRegistryUnitTest.java | 29 +++++++++++++++++++ 3 files changed, 75 insertions(+) diff --git a/core/src/main/java/org/springframework/plugin/core/PluginRegistry.java b/core/src/main/java/org/springframework/plugin/core/PluginRegistry.java index 505b79b..fa20164 100644 --- a/core/src/main/java/org/springframework/plugin/core/PluginRegistry.java +++ b/core/src/main/java/org/springframework/plugin/core/PluginRegistry.java @@ -36,6 +36,27 @@ public interface PluginRegistry, S> extends Iterable { */ Optional getPluginFor(S delimiter); + /** + * Returns the first {@link Plugin} found for the given delimiter. Thus, further configured {@link Plugin}s are + * ignored. + * + * @param delimiter + * @return a {@link Plugin} for the given originating system or {@link Optional#empty()} if none found. + * @throws IllegalArgumentException in case no {@link Plugin} for the given delimiter + */ + T getRequiredPluginFor(S delimiter) throws IllegalArgumentException; + + /** + * Returns the first {@link Plugin} found for the given delimiter. Thus, further configured {@link Plugin}s are + * ignored. + * + * @param delimiter + * @param message a {@link Supplier} to produce an exception message in case no plugin is found. + * @return a {@link Plugin} for the given originating system or {@link Optional#empty()} if none found. + * @throws IllegalArgumentException in case no {@link Plugin} for the given delimiter + */ + T getRequiredPluginFor(S delimiter, Supplier message) throws IllegalArgumentException; + /** * Returns all plugins for the given delimiter. * diff --git a/core/src/main/java/org/springframework/plugin/core/SimplePluginRegistry.java b/core/src/main/java/org/springframework/plugin/core/SimplePluginRegistry.java index 7cb0cb1..f257cd7 100644 --- a/core/src/main/java/org/springframework/plugin/core/SimplePluginRegistry.java +++ b/core/src/main/java/org/springframework/plugin/core/SimplePluginRegistry.java @@ -22,6 +22,8 @@ import java.util.Optional; import java.util.function.Supplier; import java.util.stream.Collectors; +import org.springframework.util.Assert; + /** * Basic implementation of {@link PluginRegistry}. Simply holds all given plugins in a list dropping {@literal null} * values silently on adding. @@ -83,6 +85,29 @@ public class SimplePluginRegistry, S> extends PluginRegistry .findFirst(); } + /* + * (non-Javadoc) + * @see org.springframework.plugin.core.PluginRegistry#getRequiredPluginFor(java.lang.Object) + */ + @Override + public T getRequiredPluginFor(S delimiter) { + + return getRequiredPluginFor(delimiter, + () -> String.format("No plugin found for delimiter %s! Registered plugins: %s.", delimiter, getPlugins())); + } + + /* + * (non-Javadoc) + * @see org.springframework.plugin.core.PluginRegistry#getRequiredPluginFor(java.lang.Object, java.util.function.Supplier) + */ + @Override + public T getRequiredPluginFor(S delimiter, Supplier message) throws IllegalArgumentException { + + Assert.notNull(message, "Message must not be null!"); + + return getPluginFor(delimiter, () -> new IllegalArgumentException(message.get())); + } + /* * (non-Javadoc) * @see org.springframework.plugin.core.PluginRegistry#getPluginsFor(java.lang.Object) diff --git a/core/src/test/java/org/springframework/plugin/core/SimplePluginRegistryUnitTest.java b/core/src/test/java/org/springframework/plugin/core/SimplePluginRegistryUnitTest.java index 21ec937..51c7c0a 100644 --- a/core/src/test/java/org/springframework/plugin/core/SimplePluginRegistryUnitTest.java +++ b/core/src/test/java/org/springframework/plugin/core/SimplePluginRegistryUnitTest.java @@ -25,7 +25,9 @@ import java.util.Collections; import java.util.List; import org.junit.Before; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; /** * Unit test for {@link SimplePluginRegistry}. @@ -38,6 +40,8 @@ public class SimplePluginRegistryUnitTest { SimplePluginRegistry registry; + public @Rule ExpectedException o_O = ExpectedException.none(); + /** * Initializes a {@code PluginRegistry} and equips it with an {@code EmailNotificationProvider}. */ @@ -142,4 +146,29 @@ public class SimplePluginRegistryUnitTest { registry.getPluginFor("FOO", () -> new IllegalStateException()); } + + /** + * @see #41 + */ + public void throwsExceptionIfRequiredPluginIsNotFound() { + + registry = SimplePluginRegistry.create(Collections.emptyList()); + + o_O.expect(IllegalArgumentException.class); + + registry.getRequiredPluginFor("FOO"); + } + + /** + * @see #41 + */ + public void throwsExceptionWithMessafeIfRequiredPluginIsNotFound() { + + registry = SimplePluginRegistry.create(Collections.emptyList()); + + o_O.expect(IllegalArgumentException.class); + o_O.expectMessage("message"); + + registry.getRequiredPluginFor("FOO", () -> "message"); + } }