mirror of
https://github.com/haraldk/TwelveMonkeys.git
synced 2025-08-03 11:35:29 -04:00
Added (long overdue) test cases for ServiceRegistry.
This commit is contained in:
parent
b92caf121d
commit
6c6c08a8f5
@ -59,7 +59,7 @@ import java.util.*;
|
|||||||
* </small>
|
* </small>
|
||||||
*
|
*
|
||||||
* @author <a href="mailto:harald.kuhr@gmail.com">Harald Kuhr</a>
|
* @author <a href="mailto:harald.kuhr@gmail.com">Harald Kuhr</a>
|
||||||
* @version $Id: //depot/branches/personal/haraldk/twelvemonkeys/release-2/twelvemonkeys-core/src/main/java/com/twelvemonkeys/util/service/ServiceRegistry.java#2 $
|
* @version $Id: com/twelvemonkeys/util/service/ServiceRegistry.java#2 $
|
||||||
* @see RegisterableService
|
* @see RegisterableService
|
||||||
* @see <a href="http://java.sun.com/j2se/1.3/docs/guide/jar/jar.html#Service%20Provider">JAR File Specification</a>
|
* @see <a href="http://java.sun.com/j2se/1.3/docs/guide/jar/jar.html#Service%20Provider">JAR File Specification</a>
|
||||||
*/
|
*/
|
||||||
@ -110,7 +110,7 @@ public class ServiceRegistry {
|
|||||||
* Registers all provider implementations for this {@code ServiceRegistry}
|
* Registers all provider implementations for this {@code ServiceRegistry}
|
||||||
* found in the application classpath.
|
* found in the application classpath.
|
||||||
*
|
*
|
||||||
* @throws ServiceConfigurationError if an error occured during registration
|
* @throws ServiceConfigurationError if an error occurred during registration
|
||||||
*/
|
*/
|
||||||
public void registerApplicationClasspathSPIs() {
|
public void registerApplicationClasspathSPIs() {
|
||||||
ClassLoader loader = Thread.currentThread().getContextClassLoader();
|
ClassLoader loader = Thread.currentThread().getContextClassLoader();
|
||||||
@ -140,7 +140,7 @@ public class ServiceRegistry {
|
|||||||
*
|
*
|
||||||
* @param pResource the resource to load SPIs from
|
* @param pResource the resource to load SPIs from
|
||||||
* @param pCategory the category class
|
* @param pCategory the category class
|
||||||
* @param pLoader the classloader to use
|
* @param pLoader the class loader to use
|
||||||
*/
|
*/
|
||||||
<T> void registerSPIs(final URL pResource, final Class<T> pCategory, final ClassLoader pLoader) {
|
<T> void registerSPIs(final URL pResource, final Class<T> pCategory, final ClassLoader pLoader) {
|
||||||
Properties classNames = new Properties();
|
Properties classNames = new Properties();
|
||||||
@ -242,7 +242,7 @@ public class ServiceRegistry {
|
|||||||
* The iterator supports removal.
|
* The iterator supports removal.
|
||||||
* <p/>
|
* <p/>
|
||||||
* <small>
|
* <small>
|
||||||
* NOTE: Removing a category from the iterator, deregisters
|
* NOTE: Removing a category from the iterator, de-registers
|
||||||
* {@code pProvider} from the current category (as returned by the last
|
* {@code pProvider} from the current category (as returned by the last
|
||||||
* invocation of {@code next()}), it does <em>not</em> remove the category
|
* invocation of {@code next()}), it does <em>not</em> remove the category
|
||||||
* itself from the registry.
|
* itself from the registry.
|
||||||
@ -257,7 +257,7 @@ public class ServiceRegistry {
|
|||||||
return new FilterIterator<Class<?>>(categories(),
|
return new FilterIterator<Class<?>>(categories(),
|
||||||
new FilterIterator.Filter<Class<?>>() {
|
new FilterIterator.Filter<Class<?>>() {
|
||||||
public boolean accept(Class<?> pElement) {
|
public boolean accept(Class<?> pElement) {
|
||||||
return getRegistry(pElement).contatins(pProvider);
|
return getRegistry(pElement).contains(pProvider);
|
||||||
}
|
}
|
||||||
}) {
|
}) {
|
||||||
Class<?> current;
|
Class<?> current;
|
||||||
@ -297,7 +297,7 @@ public class ServiceRegistry {
|
|||||||
*
|
*
|
||||||
* @param pProvider the provider instance
|
* @param pProvider the provider instance
|
||||||
* @return {@code true} if {@code pProvider} is now registered in
|
* @return {@code true} if {@code pProvider} is now registered in
|
||||||
* one or more categories
|
* one or more categories it was not registered in before.
|
||||||
* @see #compatibleCategories(Object)
|
* @see #compatibleCategories(Object)
|
||||||
*/
|
*/
|
||||||
public boolean register(final Object pProvider) {
|
public boolean register(final Object pProvider) {
|
||||||
@ -329,12 +329,12 @@ public class ServiceRegistry {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Deregisters the given provider from all categories it's currently
|
* De-registers the given provider from all categories it's currently
|
||||||
* registered in.
|
* registered in.
|
||||||
*
|
*
|
||||||
* @param pProvider the provider instance
|
* @param pProvider the provider instance
|
||||||
* @return {@code true} if {@code pProvider} was previously registered in
|
* @return {@code true} if {@code pProvider} was previously registered in
|
||||||
* any category
|
* any category and is now de-registered.
|
||||||
* @see #containingCategories(Object)
|
* @see #containingCategories(Object)
|
||||||
*/
|
*/
|
||||||
public boolean deregister(final Object pProvider) {
|
public boolean deregister(final Object pProvider) {
|
||||||
@ -384,9 +384,8 @@ public class ServiceRegistry {
|
|||||||
public boolean register(final T pProvider) {
|
public boolean register(final T pProvider) {
|
||||||
checkCategory(pProvider);
|
checkCategory(pProvider);
|
||||||
|
|
||||||
// NOTE: We only register the new instance, if we don't allready
|
// NOTE: We only register the new instance, if we don't already have an instance of pProvider's class.
|
||||||
// have an instance of pProvider's class.
|
if (!contains(pProvider)) {
|
||||||
if (!contatins(pProvider)) {
|
|
||||||
providers.put(pProvider.getClass(), pProvider);
|
providers.put(pProvider.getClass(), pProvider);
|
||||||
processRegistration(pProvider);
|
processRegistration(pProvider);
|
||||||
return true;
|
return true;
|
||||||
@ -424,8 +423,8 @@ public class ServiceRegistry {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean contatins(final Object pProvider) {
|
public boolean contains(final Object pProvider) {
|
||||||
return providers.containsKey(pProvider.getClass());
|
return providers.containsKey(pProvider != null ? pProvider.getClass() : null);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Iterator<T> providers() {
|
public Iterator<T> providers() {
|
||||||
|
@ -0,0 +1,39 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2012, Harald Kuhr
|
||||||
|
* All rights reserved.
|
||||||
|
*
|
||||||
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
* modification, are permitted provided that the following conditions are met:
|
||||||
|
* * Redistributions of source code must retain the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer.
|
||||||
|
* * Redistributions in binary form must reproduce the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer in the
|
||||||
|
* documentation and/or other materials provided with the distribution.
|
||||||
|
* * Neither the name "TwelveMonkeys" nor the
|
||||||
|
* names of its contributors may be used to endorse or promote products
|
||||||
|
* derived from this software without specific prior written permission.
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||||
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||||
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||||
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
||||||
|
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||||
|
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||||
|
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
||||||
|
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
||||||
|
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||||
|
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||||
|
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package com.twelvemonkeys.util.service;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* ExampleSPI
|
||||||
|
*
|
||||||
|
* @author <a href="mailto:harald.kuhr@gmail.com">Harald Kuhr</a>
|
||||||
|
* @author last modified by $Author: haraldk$
|
||||||
|
* @version $Id: ExampleSPI.java,v 1.0 25.01.12 16:24 haraldk Exp$
|
||||||
|
*/
|
||||||
|
abstract class DummySPI {
|
||||||
|
}
|
@ -0,0 +1,39 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2012, Harald Kuhr
|
||||||
|
* All rights reserved.
|
||||||
|
*
|
||||||
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
* modification, are permitted provided that the following conditions are met:
|
||||||
|
* * Redistributions of source code must retain the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer.
|
||||||
|
* * Redistributions in binary form must reproduce the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer in the
|
||||||
|
* documentation and/or other materials provided with the distribution.
|
||||||
|
* * Neither the name "TwelveMonkeys" nor the
|
||||||
|
* names of its contributors may be used to endorse or promote products
|
||||||
|
* derived from this software without specific prior written permission.
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||||
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||||
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||||
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
||||||
|
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||||
|
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||||
|
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
||||||
|
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
||||||
|
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||||
|
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||||
|
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package com.twelvemonkeys.util.service;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* DummySPIImpl
|
||||||
|
*
|
||||||
|
* @author <a href="mailto:harald.kuhr@gmail.com">Harald Kuhr</a>
|
||||||
|
* @author last modified by $Author: haraldk$
|
||||||
|
* @version $Id: DummySPIImpl.java,v 1.0 25.01.12 16:25 haraldk Exp$
|
||||||
|
*/
|
||||||
|
class DummySPIImpl extends DummySPI {
|
||||||
|
}
|
@ -0,0 +1,39 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2012, Harald Kuhr
|
||||||
|
* All rights reserved.
|
||||||
|
*
|
||||||
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
* modification, are permitted provided that the following conditions are met:
|
||||||
|
* * Redistributions of source code must retain the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer.
|
||||||
|
* * Redistributions in binary form must reproduce the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer in the
|
||||||
|
* documentation and/or other materials provided with the distribution.
|
||||||
|
* * Neither the name "TwelveMonkeys" nor the
|
||||||
|
* names of its contributors may be used to endorse or promote products
|
||||||
|
* derived from this software without specific prior written permission.
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||||
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||||
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||||
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
||||||
|
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||||
|
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||||
|
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
||||||
|
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
||||||
|
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||||
|
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||||
|
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package com.twelvemonkeys.util.service;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* DummySPIToo
|
||||||
|
*
|
||||||
|
* @author <a href="mailto:harald.kuhr@gmail.com">Harald Kuhr</a>
|
||||||
|
* @author last modified by $Author: haraldk$
|
||||||
|
* @version $Id: DummySPIToo.java,v 1.0 25.01.12 16:27 haraldk Exp$
|
||||||
|
*/
|
||||||
|
class DummySPIToo extends DummySPI {
|
||||||
|
}
|
@ -0,0 +1,297 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2012, Harald Kuhr
|
||||||
|
* All rights reserved.
|
||||||
|
*
|
||||||
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
* modification, are permitted provided that the following conditions are met:
|
||||||
|
* * Redistributions of source code must retain the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer.
|
||||||
|
* * Redistributions in binary form must reproduce the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer in the
|
||||||
|
* documentation and/or other materials provided with the distribution.
|
||||||
|
* * Neither the name "TwelveMonkeys" nor the
|
||||||
|
* names of its contributors may be used to endorse or promote products
|
||||||
|
* derived from this software without specific prior written permission.
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||||
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||||
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||||
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
||||||
|
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||||
|
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||||
|
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
||||||
|
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
||||||
|
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||||
|
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||||
|
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package com.twelvemonkeys.util.service;
|
||||||
|
|
||||||
|
import com.twelvemonkeys.util.CollectionUtil;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import java.util.*;
|
||||||
|
|
||||||
|
import static org.junit.Assert.*;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* ServiceRegistryTest
|
||||||
|
*
|
||||||
|
* @author <a href="mailto:harald.kuhr@gmail.com">Harald Kuhr</a>
|
||||||
|
* @author last modified by $Author: haraldk$
|
||||||
|
* @version $Id: ServiceRegistryTest.java,v 1.0 25.01.12 16:16 haraldk Exp$
|
||||||
|
*/
|
||||||
|
public class ServiceRegistryTest {
|
||||||
|
|
||||||
|
private final TestRegistry registry = new TestRegistry();
|
||||||
|
|
||||||
|
@Test(expected = IllegalArgumentException.class)
|
||||||
|
public void testCreateNull() {
|
||||||
|
new ServiceRegistry(null);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testCreateEmptyIterator() {
|
||||||
|
// A completely useless registry...
|
||||||
|
ServiceRegistry registry = new ServiceRegistry(Collections.<Class<?>>emptyList().iterator());
|
||||||
|
registry.registerApplicationClasspathSPIs();
|
||||||
|
|
||||||
|
while (registry.categories().hasNext()) {
|
||||||
|
fail("No categories");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test(expected = ServiceConfigurationError.class)
|
||||||
|
public void testCreateBadConfig() {
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
ServiceRegistry registry = new ServiceRegistry(Arrays.asList(BadSPI.class).iterator());
|
||||||
|
registry.registerApplicationClasspathSPIs();
|
||||||
|
|
||||||
|
// DONE: Test non-class
|
||||||
|
|
||||||
|
// TODO: Test class not implementing SPI category
|
||||||
|
// TODO: Test class that throws exception in constructor
|
||||||
|
// TODO: Test class that has no public no-args constructor
|
||||||
|
// TODO: Test IOException
|
||||||
|
// Some of these can be tested using stubs, via the package protected registerSPIs method
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testCategories() {
|
||||||
|
// Categories
|
||||||
|
Iterator<Class<?>> categories = registry.categories();
|
||||||
|
assertTrue(categories.hasNext());
|
||||||
|
Class<?> category = categories.next();
|
||||||
|
assertEquals(DummySPI.class, category);
|
||||||
|
assertFalse(categories.hasNext());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testProviders() {
|
||||||
|
// Providers
|
||||||
|
Iterator<DummySPI> providers = registry.providers(DummySPI.class);
|
||||||
|
List<DummySPI> providerList = new ArrayList<DummySPI>();
|
||||||
|
CollectionUtil.addAll(providerList, providers);
|
||||||
|
|
||||||
|
assertEquals(2, providerList.size());
|
||||||
|
|
||||||
|
// Order should be as in configuration file
|
||||||
|
assertNotNull(providerList.get(0));
|
||||||
|
assertEquals(DummySPIImpl.class, providerList.get(0).getClass());
|
||||||
|
assertNotNull(providerList.get(1));
|
||||||
|
assertEquals(DummySPIToo.class, providerList.get(1).getClass());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testCompatibleCategoriesNull() {
|
||||||
|
// Compatible categories
|
||||||
|
Iterator<Class<?>> categories = registry.compatibleCategories(null);
|
||||||
|
assertFalse(categories.hasNext());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testCompatibleCategoriesImpl() {
|
||||||
|
Iterator<Class<?>> categories = registry.compatibleCategories(new DummySPIImpl());
|
||||||
|
assertTrue(categories.hasNext());
|
||||||
|
assertEquals(DummySPI.class, categories.next());
|
||||||
|
assertFalse(categories.hasNext());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testCompatibleCategoriesToo() {
|
||||||
|
Iterator<Class<?>> categories = registry.compatibleCategories(new DummySPIToo());
|
||||||
|
assertTrue(categories.hasNext());
|
||||||
|
assertEquals(DummySPI.class, categories.next());
|
||||||
|
assertFalse(categories.hasNext());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testCompatibleCategoriesNonRegistered() {
|
||||||
|
Iterator<Class<?>> categories = registry.compatibleCategories(new DummySPI() {});
|
||||||
|
assertTrue(categories.hasNext());
|
||||||
|
assertEquals(DummySPI.class, categories.next());
|
||||||
|
assertFalse(categories.hasNext());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testCompatibleCategoriesUnknownType() {
|
||||||
|
Iterator<Class<?>> categories = registry.compatibleCategories(new Object());
|
||||||
|
assertFalse(categories.hasNext());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testContainingCategoriesNull() {
|
||||||
|
// Containing categories
|
||||||
|
Iterator<Class<?>> categories = registry.containingCategories(null);
|
||||||
|
assertFalse(categories.hasNext());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testContainingCategoriesKnownInstanceImpl() {
|
||||||
|
Iterator<DummySPI> providers = registry.providers(DummySPI.class);
|
||||||
|
assertTrue(providers.hasNext()); // Sanity check
|
||||||
|
|
||||||
|
Iterator<Class<?>> categories = registry.containingCategories(providers.next());
|
||||||
|
assertTrue(categories.hasNext());
|
||||||
|
assertEquals(DummySPI.class, categories.next());
|
||||||
|
assertFalse(categories.hasNext());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testContainingCategoriesKnownInstanceToo() {
|
||||||
|
Iterator<DummySPI> providers = registry.providers(DummySPI.class);
|
||||||
|
providers.next();
|
||||||
|
assertTrue(providers.hasNext()); // Sanity check
|
||||||
|
|
||||||
|
Iterator<Class<?>> categories = registry.containingCategories(providers.next());
|
||||||
|
assertTrue(categories.hasNext());
|
||||||
|
assertEquals(DummySPI.class, categories.next());
|
||||||
|
assertFalse(categories.hasNext());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testContainingCategoriesNewInstanceRegisteredImpl() {
|
||||||
|
// NOTE: Currently we match based on type, rather than instance, but it does make sense...
|
||||||
|
Iterator<Class<?>> categories = registry.containingCategories(new DummySPIImpl());
|
||||||
|
assertTrue(categories.hasNext());
|
||||||
|
assertEquals(DummySPI.class, categories.next());
|
||||||
|
assertFalse(categories.hasNext());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testContainingCategoriesNewInstanceRegisteredToo() {
|
||||||
|
// NOTE: Currently we match based on type, rather than instance, but it does make sense...
|
||||||
|
Iterator<Class<?>> categories = registry.containingCategories(new DummySPIToo());
|
||||||
|
assertTrue(categories.hasNext());
|
||||||
|
assertEquals(DummySPI.class, categories.next());
|
||||||
|
assertFalse(categories.hasNext());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testContainingCategoriesCompatibleNonRegisteredType() {
|
||||||
|
Iterator<Class<?>> categories = registry.containingCategories(new DummySPI() {});
|
||||||
|
assertFalse(categories.hasNext());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testContainingCategoriesUnknownType() {
|
||||||
|
Iterator<Class<?>> categories = registry.containingCategories(new Object());
|
||||||
|
assertFalse(categories.hasNext());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testRegister() {
|
||||||
|
// Register
|
||||||
|
DummySPI dummy = new DummySPI() {};
|
||||||
|
assertTrue(registry.register(dummy));
|
||||||
|
|
||||||
|
// Should now have category
|
||||||
|
Iterator<Class<?>> categories = registry.containingCategories(dummy);
|
||||||
|
assertTrue(categories.hasNext());
|
||||||
|
assertEquals(DummySPI.class, categories.next());
|
||||||
|
assertFalse(categories.hasNext());
|
||||||
|
|
||||||
|
// Should now be in providers
|
||||||
|
Iterator<DummySPI> providers = registry.providers(DummySPI.class);
|
||||||
|
List<DummySPI> providerList = new ArrayList<DummySPI>();
|
||||||
|
CollectionUtil.addAll(providerList, providers);
|
||||||
|
|
||||||
|
assertEquals(3, providerList.size());
|
||||||
|
|
||||||
|
assertNotNull(providerList.get(1));
|
||||||
|
assertSame(dummy, providerList.get(2));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testRegisterAlreadyRegistered() {
|
||||||
|
Iterator<DummySPI> providers = registry.providers(DummySPI.class);
|
||||||
|
assertTrue(providers.hasNext()); // Sanity check
|
||||||
|
|
||||||
|
assertFalse(registry.register(providers.next()));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testRegisterNull() {
|
||||||
|
assertFalse(registry.register(null));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testRegisterIncompatible() {
|
||||||
|
assertFalse(registry.register(new Object()));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testDeregisterNull() {
|
||||||
|
assertFalse(registry.deregister(null));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testDeregisterIncompatible() {
|
||||||
|
assertFalse(registry.deregister(new Object()));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testDeregisterCompatibleNonRegistered() {
|
||||||
|
DummySPI dummy = new DummySPI() {};
|
||||||
|
assertFalse(registry.deregister(dummy));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testDeregister() {
|
||||||
|
Iterator<DummySPI> providers = registry.providers(DummySPI.class);
|
||||||
|
assertTrue(providers.hasNext()); // Sanity check
|
||||||
|
DummySPI instance = providers.next();
|
||||||
|
assertTrue(registry.deregister(instance));
|
||||||
|
|
||||||
|
// Test no longer in registry
|
||||||
|
providers = registry.providers(DummySPI.class);
|
||||||
|
int count = 0;
|
||||||
|
while (providers.hasNext()) {
|
||||||
|
DummySPI next = providers.next();
|
||||||
|
assertNotSame(instance, next);
|
||||||
|
count++;
|
||||||
|
}
|
||||||
|
|
||||||
|
assertEquals(1, count);
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: Test register with category
|
||||||
|
// TODO: Test register with unknown category
|
||||||
|
// TODO: Test register with null category
|
||||||
|
|
||||||
|
// TODO: Test de-register with category
|
||||||
|
// TODO: Test de-register with unknown category
|
||||||
|
// TODO: Test de-register with null category
|
||||||
|
|
||||||
|
|
||||||
|
private static class TestRegistry extends ServiceRegistry {
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
public TestRegistry() {
|
||||||
|
super(Arrays.asList(DummySPI.class).iterator());
|
||||||
|
registerApplicationClasspathSPIs();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class BadSPI {}
|
||||||
|
}
|
@ -0,0 +1,2 @@
|
|||||||
|
com.twelvemonkeys.util.service.DummySPIImpl
|
||||||
|
com.twelvemonkeys.util.service.DummySPIToo
|
@ -0,0 +1,3 @@
|
|||||||
|
# Any line that does not resolve to a class name, will make registerApplicationClasspathSPIs throw error
|
||||||
|
Bad, bad spi
|
||||||
|
So bad. So bad.
|
Loading…
x
Reference in New Issue
Block a user