#45 - Introduced PluginRegistry.of(…) factory methods.

The introduced methods mostly mimic the ones available on OrderAwarePluginRegistry except the one that's implying a certain order in the first place. Also introduced ….of(…) factory methods on OrderAwarePluginRegistry and deprecated the ….create(…) ones.
This commit is contained in:
Oliver Drotbohm
2019-02-01 15:26:40 +01:00
parent e6b652d240
commit 764772cb13
3 changed files with 186 additions and 35 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2008-2017 the original author or authors.
* Copyright 2008-2019 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.
@@ -17,9 +17,8 @@ package org.springframework.plugin.core;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import static org.springframework.plugin.core.OrderAwarePluginRegistry.*;
import static org.springframework.plugin.core.PluginRegistry.*;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
@@ -54,29 +53,14 @@ public class OrderAwarePluginRegistryUnitTest extends SimplePluginRegistryUnitTe
@Test
public void honorsOrderOnAddPlugins() throws Exception {
PluginRegistry<TestPlugin, String> registry = OrderAwarePluginRegistry
.create(Arrays.asList(firstPlugin, secondPlugin));
PluginRegistry<TestPlugin, String> registry = of(firstPlugin, secondPlugin);
assertOrder(registry, secondPlugin, firstPlugin);
}
private void assertOrder(PluginRegistry<TestPlugin, String> registry, TestPlugin... plugins) {
List<TestPlugin> result = registry.getPluginsFor(null);
assertThat(plugins.length, is(result.size()));
for (int i = 0; i < plugins.length; i++) {
assertThat(result.get(i), is(plugins[i]));
}
assertThat(registry.getPluginFor(null), is(Optional.of(plugins[0])));
}
@Test
public void createsRevertedRegistryCorrectly() throws Exception {
OrderAwarePluginRegistry<TestPlugin, String> registry = OrderAwarePluginRegistry
.create(Arrays.asList(firstPlugin, secondPlugin));
OrderAwarePluginRegistry<TestPlugin, String> registry = OrderAwarePluginRegistry.of(firstPlugin, secondPlugin);
PluginRegistry<TestPlugin, String> reverse = registry.reverse();
assertOrder(registry, secondPlugin, firstPlugin);
@@ -92,25 +76,41 @@ public class OrderAwarePluginRegistryUnitTest extends SimplePluginRegistryUnitTe
ThirdImplementation plugin = new ThirdImplementation();
TestPlugin thirdPlugin = (TestPlugin) new ProxyFactory(plugin).getProxy();
OrderAwarePluginRegistry<TestPlugin, String> registry = create(
Arrays.asList(firstPlugin, secondPlugin, thirdPlugin));
OrderAwarePluginRegistry<TestPlugin, String> registry = OrderAwarePluginRegistry.of(firstPlugin, secondPlugin,
thirdPlugin);
assertOrder(registry, secondPlugin, thirdPlugin, firstPlugin);
assertOrder(registry.reverse(), firstPlugin, thirdPlugin, secondPlugin);
}
@Test
public void defaultSetupUsesDefaultComparator() {
assertDefaultComparator(OrderAwarePluginRegistry.<String, TestPlugin> create());
assertDefaultComparator(OrderAwarePluginRegistry.empty());
}
@Test
public void defaultSetupUsesDefaultReverseComparator() {
OrderAwarePluginRegistry<Plugin<Object>, Object> registry = OrderAwarePluginRegistry
.createReverse(Collections.<Plugin<Object>> emptyList());
.ofReverse(Collections.emptyList());
Object field = ReflectionTestUtils.getField(registry, "comparator");
assertThat(field, is(ReflectionTestUtils.getField(registry, "DEFAULT_REVERSE_COMPARATOR")));
}
private static void assertOrder(PluginRegistry<TestPlugin, String> registry, TestPlugin... plugins) {
List<TestPlugin> result = registry.getPluginsFor(null);
assertThat(plugins.length, is(result.size()));
for (int i = 0; i < plugins.length; i++) {
assertThat(result.get(i), is(plugins[i]));
}
assertThat(registry.getPluginFor(null), is(Optional.of(plugins[0])));
}
private static void assertDefaultComparator(OrderAwarePluginRegistry<?, ?> registry) {
Object field = ReflectionTestUtils.getField(registry, "comparator");