|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
package org.geotools.util.factory; |
|
|
|
import static java.util.stream.Collectors.toList; |
|
import static java.util.stream.Collectors.toSet; |
|
import static org.junit.Assert.assertEquals; |
|
import static org.junit.Assert.assertFalse; |
|
import static org.junit.Assert.assertNotNull; |
|
import static org.junit.Assert.assertNotSame; |
|
import static org.junit.Assert.assertSame; |
|
import static org.junit.Assert.assertTrue; |
|
import static org.junit.Assert.fail; |
|
|
|
import java.io.IOException; |
|
import java.net.URL; |
|
import java.net.URLClassLoader; |
|
import java.util.Collections; |
|
import java.util.List; |
|
import java.util.Optional; |
|
import java.util.Set; |
|
import java.util.stream.Stream; |
|
import org.junit.Before; |
|
import org.junit.Ignore; |
|
import org.junit.Test; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public final class FactoryRegistryTest { |
|
|
|
|
|
|
|
|
|
|
|
@Before |
|
public void ensureHintsLoaded() { |
|
assertNotNull(Hints.DATUM_FACTORY.toString()); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@SuppressWarnings("PMD.UnusedPrivateMethod") |
|
private FactoryRegistry getRegistry( |
|
final boolean creator, |
|
final Factory factory1, |
|
final Factory factory2, |
|
final Factory factory3) { |
|
@SuppressWarnings("unchecked") |
|
final Set<Class<?>> categories = Collections.singleton(DummyFactory.class); |
|
|
|
final FactoryRegistry registry; |
|
if (creator) { |
|
registry = new FactoryCreator(categories); |
|
} else { |
|
registry = new FactoryRegistry(categories); |
|
} |
|
registry.registerFactory(factory1); |
|
registry.registerFactory(factory2); |
|
registry.registerFactory(factory3); |
|
assertTrue( |
|
registry.setOrdering( |
|
DummyFactory.class, (DummyFactory) factory1, (DummyFactory) factory2)); |
|
assertTrue( |
|
registry.setOrdering( |
|
DummyFactory.class, (DummyFactory) factory2, (DummyFactory) factory3)); |
|
assertTrue( |
|
registry.setOrdering( |
|
DummyFactory.class, (DummyFactory) factory1, (DummyFactory) factory3)); |
|
|
|
final List<?> factories = |
|
registry.getFactories(DummyFactory.class, null, null).collect(toList()); |
|
assertTrue(factories.contains(factory1)); |
|
assertTrue(factories.contains(factory2)); |
|
assertTrue(factories.contains(factory3)); |
|
assertTrue(factories.indexOf(factory1) < factories.indexOf(factory2)); |
|
assertTrue(factories.indexOf(factory2) < factories.indexOf(factory3)); |
|
return registry; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
public void testGetProvider() { |
|
final Hints.Key key = DummyFactory.DUMMY_FACTORY; |
|
final DummyFactory factory1 = new DummyFactory.Example1(); |
|
final DummyFactory factory2 = new DummyFactory.Example2(); |
|
final DummyFactory factory3 = new DummyFactory.Example3(); |
|
final FactoryRegistry registry = getRegistry(false, factory1, factory2, factory3); |
|
|
|
|
|
|
|
|
|
|
|
|
|
Hints hints = null; |
|
DummyFactory factory = registry.getFactory(DummyFactory.class, null, hints, key); |
|
assertSame("No preferences; should select the first factory. ", factory1, factory); |
|
|
|
|
|
|
|
|
|
|
|
hints = new Hints(Hints.KEY_INTERPOLATION, Hints.VALUE_INTERPOLATION_BILINEAR); |
|
factory = registry.getFactory(DummyFactory.class, null, hints, key); |
|
assertSame("First factory matches; it should be selected. ", factory1, factory); |
|
|
|
|
|
|
|
|
|
|
|
hints = new Hints(Hints.KEY_INTERPOLATION, Hints.VALUE_INTERPOLATION_BICUBIC); |
|
try { |
|
factory = registry.getFactory(DummyFactory.class, null, hints, key); |
|
fail("Found factory " + factory + ", while the hint should have been rejected."); |
|
} catch (FactoryNotFoundException exception) { |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
final DummyFactory factory4 = new DummyFactory.Example4(); |
|
registry.registerFactory(factory4); |
|
assertTrue(registry.setOrdering(DummyFactory.class, factory1, factory4)); |
|
factory = registry.getFactory(DummyFactory.class, null, hints, key); |
|
assertSame("The new factory should be selected. ", factory4, factory); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
DummyFactory explicit = new DummyFactory.Example3(); |
|
hints = new Hints(DummyFactory.DUMMY_FACTORY, explicit); |
|
factory = registry.getFactory(DummyFactory.class, null, hints, key); |
|
assertSame("The user-specified factory should have been selected. ", explicit, factory); |
|
|
|
|
|
|
|
hints = new Hints(DummyFactory.DUMMY_FACTORY, DummyFactory.Example2.class); |
|
factory = registry.getFactory(DummyFactory.class, null, hints, key); |
|
assertSame("Factory of class #2 were requested. ", factory2, factory); |
|
|
|
|
|
|
|
hints = |
|
new Hints( |
|
DummyFactory.DUMMY_FACTORY, |
|
new Class<?>[] {DummyFactory.Example3.class, DummyFactory.Example2.class}); |
|
factory = registry.getFactory(DummyFactory.class, null, hints, key); |
|
assertSame("Factory of class #3 were requested. ", factory3, factory); |
|
|
|
|
|
|
|
|
|
|
|
hints = new Hints(DummyFactory.INTERNAL_FACTORY, DummyFactory.Example2.class); |
|
factory = registry.getFactory(DummyFactory.class, null, hints, key); |
|
assertSame("Expected factory #1. ", factory1, factory); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
final Hints implementations = |
|
new Hints( |
|
DummyFactory.DUMMY_FACTORY, |
|
new Class[] {DummyFactory.Example2.class, DummyFactory.Example3.class}); |
|
|
|
|
|
|
|
|
|
hints = new Hints(DummyFactory.INTERNAL_FACTORY, DummyFactory.Example1.class); |
|
hints.add(implementations); |
|
factory = registry.getFactory(DummyFactory.class, null, hints, key); |
|
assertSame("Expected a factory using #1 internally. ", factory2, factory); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
public void testCreateProvider() { |
|
final Hints.Key key = DummyFactory.DUMMY_FACTORY; |
|
final DummyFactory factory1 = new DummyFactory.Example1(); |
|
final DummyFactory factory2 = new DummyFactory.Example2(); |
|
final DummyFactory factory3 = new DummyFactory.Example3(); |
|
final FactoryRegistry registry = getRegistry(true, factory1, factory2, factory3); |
|
|
|
|
|
|
|
|
|
Hints hints = new Hints(Hints.KEY_INTERPOLATION, Hints.VALUE_INTERPOLATION_BILINEAR); |
|
DummyFactory factory = registry.getFactory(DummyFactory.class, null, hints, key); |
|
assertSame("First factory matches; it should be selected. ", factory1, factory); |
|
|
|
hints = new Hints(DummyFactory.DUMMY_FACTORY, DummyFactory.Example2.class); |
|
factory = registry.getFactory(DummyFactory.class, null, hints, key); |
|
assertSame("Factory of class #2 were requested. ", factory2, factory); |
|
|
|
|
|
|
|
|
|
|
|
|
|
hints = new Hints(Hints.KEY_INTERPOLATION, Hints.VALUE_INTERPOLATION_BICUBIC); |
|
try { |
|
factory = registry.getFactory(DummyFactory.class, null, hints, key); |
|
fail( |
|
"Found or created factory " |
|
+ factory |
|
+ ", while it should not have been allowed."); |
|
} catch (FactoryNotFoundException exception) { |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
final DummyFactory factory5 = new DummyFactory.Example5(null); |
|
registry.registerFactory(factory5); |
|
assertTrue(registry.setOrdering(DummyFactory.class, factory1, factory5)); |
|
factory = registry.getFactory(DummyFactory.class, null, hints, key); |
|
assertSame( |
|
"An instance of Factory #5 should have been created.", |
|
factory5.getClass(), |
|
factory.getClass()); |
|
assertNotSame("A NEW instance of Factory #5 should have been created", factory5, factory); |
|
|
|
|
|
|
|
|
|
hints.put(DummyFactory.DUMMY_FACTORY, DummyFactory.Example4.class); |
|
factory = registry.getFactory(DummyFactory.class, null, hints, key); |
|
assertEquals( |
|
"An instance of Factory #4 should have been created.", |
|
DummyFactory.Example4.class, |
|
factory.getClass()); |
|
} |
|
|
|
@Ignore |
|
@Test |
|
public void testLookupWithExtendedClasspath() throws IOException { |
|
URL url = getClass().getResource("foo.jar"); |
|
assertNotNull(url); |
|
|
|
FactoryRegistry reg = new FactoryCreator(DummyInterface.class); |
|
Stream<DummyInterface> factories = reg.getFactories(DummyInterface.class, false); |
|
assertFalse(factories.findAny().isPresent()); |
|
|
|
try (URLClassLoader cl = new URLClassLoader(new URL[] {url})) { |
|
GeoTools.addClassLoader(cl); |
|
reg.scanForPlugins(); |
|
|
|
Set<String> classes = |
|
reg.getFactories(DummyInterface.class, false) |
|
.map(factory -> factory.getClass().getName()) |
|
.collect(toSet()); |
|
|
|
assertEquals(2, classes.size()); |
|
assertTrue(classes.contains("pkg.Foo")); |
|
assertTrue(classes.contains("org.geotools.util.factory.DummyInterfaceImpl")); |
|
} |
|
} |
|
|
|
|
|
@Test |
|
public void testLookupWithSameFactoryInTwoClassLoaders() |
|
throws IOException, ClassNotFoundException { |
|
|
|
URL projectClasses = getClass().getResource("/"); |
|
|
|
|
|
|
|
try (URLClassLoader cl1 = new URLClassLoader(new URL[] {projectClasses}, null); |
|
URLClassLoader cl2 = new URLClassLoader(new URL[] {projectClasses}, null)) { |
|
|
|
GeoTools.addClassLoader(cl1); |
|
GeoTools.addClassLoader(cl2); |
|
|
|
|
|
for (int i = 0; i < 2; i++) { |
|
ClassLoader loader = (i == 0 ? cl1 : cl2); |
|
Class dummy = loader.loadClass("org.geotools.util.factory.DummyInterface"); |
|
FactoryRegistry reg = new FactoryCreator(dummy); |
|
reg.scanForPlugins(); |
|
|
|
|
|
@SuppressWarnings("unchecked") |
|
Optional factory = reg.getFactories(dummy, false).findFirst(); |
|
assertTrue(factory.isPresent()); |
|
|
|
assertSame(loader, factory.get().getClass().getClassLoader()); |
|
} |
|
} |
|
} |
|
} |
|
|