#41 - Added PluginRegistry.getRequiredPlugin(…).
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<String> is available to customize the exception message if needed.
This commit is contained in:
@@ -36,6 +36,27 @@ public interface PluginRegistry<T extends Plugin<S>, S> extends Iterable<T> {
|
||||
*/
|
||||
Optional<T> 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<String> message) throws IllegalArgumentException;
|
||||
|
||||
/**
|
||||
* Returns all plugins for the given delimiter.
|
||||
*
|
||||
|
||||
@@ -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<T extends Plugin<S>, 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<String> 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)
|
||||
|
||||
@@ -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<SamplePlugin, String> 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");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user