Improvements in PluginRegistry implementations.
Improved constructor delegation in OrderAwarePluginRegistry correctly passing empty collections instead of null now. SimplePluginRegistry avoids to look up all plugins for the getPluginFor(…) call. Instead it eagerly returns the first one matching immediately.
This commit is contained in:
@@ -55,6 +55,7 @@ public class OrderAwarePluginRegistry<T extends Plugin<S>, S> extends SimplePlug
|
||||
* @param comparator the {@link Comparator} to be used for ordering the {@link Plugin}s or {@literal null} if the
|
||||
* {@code #DEFAULT_COMPARATOR} shall be used.
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
protected OrderAwarePluginRegistry(List<? extends T> plugins, Comparator<? super T> comparator) {
|
||||
|
||||
super(plugins);
|
||||
@@ -69,8 +70,7 @@ public class OrderAwarePluginRegistry<T extends Plugin<S>, S> extends SimplePlug
|
||||
* @return
|
||||
*/
|
||||
public static <S, T extends Plugin<S>> OrderAwarePluginRegistry<T, S> create() {
|
||||
|
||||
return create(null, null);
|
||||
return create(Collections.<T> emptyList());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -82,7 +82,7 @@ public class OrderAwarePluginRegistry<T extends Plugin<S>, S> extends SimplePlug
|
||||
* @return
|
||||
*/
|
||||
public static <S, T extends Plugin<S>> OrderAwarePluginRegistry<T, S> create(Comparator<? super T> comparator) {
|
||||
return create(null, comparator);
|
||||
return create(Collections.<T> emptyList(), comparator);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -74,10 +74,10 @@ public class SimplePluginRegistry<T extends Plugin<S>, S> extends PluginRegistry
|
||||
*/
|
||||
public T getPluginFor(S delimiter) {
|
||||
|
||||
List<T> result = getPluginsFor(delimiter);
|
||||
|
||||
if (0 < result.size()) {
|
||||
return result.get(0);
|
||||
for (T plugin : super.getPlugins()) {
|
||||
if (plugin != null && plugin.supports(delimiter)) {
|
||||
return plugin;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
|
||||
@@ -20,6 +20,7 @@ import static org.junit.Assert.*;
|
||||
import static org.springframework.plugin.core.OrderAwarePluginRegistry.*;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Before;
|
||||
@@ -27,6 +28,7 @@ import org.junit.Test;
|
||||
import org.springframework.aop.framework.ProxyFactory;
|
||||
import org.springframework.core.Ordered;
|
||||
import org.springframework.core.annotation.Order;
|
||||
import org.springframework.test.util.ReflectionTestUtils;
|
||||
|
||||
/**
|
||||
* Unit test for {@link OrderAwarePluginRegistry} that especially concentrates on testing ordering functionality.
|
||||
@@ -95,6 +97,25 @@ public class OrderAwarePluginRegistryUnitTest extends SimplePluginRegistryUnitTe
|
||||
assertOrder(registry.reverse(), firstPlugin, thirdPlugin, secondPlugin);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void defaultSetupUsesDefaultComparator() {
|
||||
assertDefaultComparator(OrderAwarePluginRegistry.<String, TestPlugin> create());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void defaultSetupUsesDefaultReverseComparator() {
|
||||
OrderAwarePluginRegistry<Plugin<Object>, Object> registry = OrderAwarePluginRegistry.createReverse(Collections
|
||||
.<Plugin<Object>> emptyList());
|
||||
Object field = ReflectionTestUtils.getField(registry, "comparator");
|
||||
assertThat(field, is(ReflectionTestUtils.getField(registry, "DEFAULT_REVERSE_COMPARATOR")));
|
||||
}
|
||||
|
||||
private static void assertDefaultComparator(OrderAwarePluginRegistry<?, ?> registry) {
|
||||
|
||||
Object field = ReflectionTestUtils.getField(registry, "comparator");
|
||||
assertThat(field, is(ReflectionTestUtils.getField(registry, "DEFAULT_COMPARATOR")));
|
||||
}
|
||||
|
||||
private static interface TestPlugin extends Plugin<String> {
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user