Author: philharveyonline Date: Fri Jan 25 12:26:58 2013 New Revision: 1438469
URL: http://svn.apache.org/viewvc?rev=1438469&view=rev Log: PROTON-192: added convenience methods to ProtonFactoryLoader so it is useful in both our Java and our Python test code. Updated tests accordingly. Added: qpid/proton/branches/jni-binding/tests/src/test/java/org/apache/qpid/proton/systemtests/ProtonFactoryTest.java - copied, changed from r1438468, qpid/proton/branches/jni-binding/tests/src/test/java/org/apache/qpid/proton/systemtests/FactoryTest.java Removed: qpid/proton/branches/jni-binding/tests/src/test/java/org/apache/qpid/proton/systemtests/FactoryTest.java Modified: qpid/proton/branches/jni-binding/proton-j/proton-api/src/main/java/org/apache/qpid/proton/ProtonFactoryLoader.java Modified: qpid/proton/branches/jni-binding/proton-j/proton-api/src/main/java/org/apache/qpid/proton/ProtonFactoryLoader.java URL: http://svn.apache.org/viewvc/qpid/proton/branches/jni-binding/proton-j/proton-api/src/main/java/org/apache/qpid/proton/ProtonFactoryLoader.java?rev=1438469&r1=1438468&r2=1438469&view=diff ============================================================================== --- qpid/proton/branches/jni-binding/proton-j/proton-api/src/main/java/org/apache/qpid/proton/ProtonFactoryLoader.java (original) +++ qpid/proton/branches/jni-binding/proton-j/proton-api/src/main/java/org/apache/qpid/proton/ProtonFactoryLoader.java Fri Jan 25 12:26:58 2013 @@ -23,23 +23,50 @@ import java.util.ServiceLoader; import java.util.logging.Level; import java.util.logging.Logger; +/** + * A thin wrapper around {@link ServiceLoader} intended for loading Proton object factories. + */ public class ProtonFactoryLoader<C> { private static final Logger LOGGER = Logger.getLogger(ProtonFactoryLoader.class.getName()); private Class<C> _factoryInterface; - + + /** + * Use this constructor if you intend to explicitly provide factory interface later, + * i.e. by calling {@link #loadFactory(Class)}. This is useful if you want to use the same + * ProtonFactoryLoader instance for loading multiple factory types. + */ + public ProtonFactoryLoader() + { + } + + /** + * @param factoryInterface will be used as the factory interface class in calls to {@link #loadFactory()}. + */ public ProtonFactoryLoader(Class<C> factoryInterface) { _factoryInterface = factoryInterface; } + /** + * Returns the Proton factory that implements the stored {@link ProtonFactoryLoader#_factoryInterface} class. + */ public C loadFactory() { - ServiceLoader<C> serviceLoader = ServiceLoader.load(_factoryInterface); + return loadFactory(_factoryInterface); + } + + public C loadFactory(Class<C> factoryInterface) + { + if(factoryInterface == null) + { + throw new IllegalStateException("factoryInterface has not been set."); + } + ServiceLoader<C> serviceLoader = ServiceLoader.load(factoryInterface); Iterator<C> serviceLoaderIterator = serviceLoader.iterator(); if(!serviceLoaderIterator.hasNext()) { - throw new IllegalStateException("Can't find service loader for " + _factoryInterface.getName()); + throw new IllegalStateException("Can't find service loader for " + factoryInterface.getName()); } C factory = serviceLoaderIterator.next(); if(LOGGER.isLoggable(Level.FINE)) Copied: qpid/proton/branches/jni-binding/tests/src/test/java/org/apache/qpid/proton/systemtests/ProtonFactoryTest.java (from r1438468, qpid/proton/branches/jni-binding/tests/src/test/java/org/apache/qpid/proton/systemtests/FactoryTest.java) URL: http://svn.apache.org/viewvc/qpid/proton/branches/jni-binding/tests/src/test/java/org/apache/qpid/proton/systemtests/ProtonFactoryTest.java?p2=qpid/proton/branches/jni-binding/tests/src/test/java/org/apache/qpid/proton/systemtests/ProtonFactoryTest.java&p1=qpid/proton/branches/jni-binding/tests/src/test/java/org/apache/qpid/proton/systemtests/FactoryTest.java&r1=1438468&r2=1438469&rev=1438469&view=diff ============================================================================== --- qpid/proton/branches/jni-binding/tests/src/test/java/org/apache/qpid/proton/systemtests/FactoryTest.java (original) +++ qpid/proton/branches/jni-binding/tests/src/test/java/org/apache/qpid/proton/systemtests/ProtonFactoryTest.java Fri Jan 25 12:26:58 2013 @@ -18,22 +18,35 @@ */ package org.apache.qpid.proton.systemtests; -import static org.junit.Assert.*; - +import static org.junit.Assert.assertNotNull; import org.apache.qpid.proton.ProtonFactoryLoader; import org.apache.qpid.proton.engine.EngineFactory; import org.apache.qpid.proton.message.MessageFactory; import org.junit.Test; -public class FactoryTest +public class ProtonFactoryTest { + @SuppressWarnings({ "rawtypes", "unchecked" }) + @Test + public void testLoadFactoryWithExplicitClass() + { + ProtonFactoryLoader factoryLoader = new ProtonFactoryLoader(); + MessageFactory messageFactory = (MessageFactory) factoryLoader.loadFactory(MessageFactory.class); + assertNotNull(messageFactory); + } @Test public void testMessageFactory() { - MessageFactory messageFactory = new ProtonFactoryLoader<MessageFactory>(MessageFactory.class).loadFactory(); - assertNotNull(messageFactory); + ProtonFactoryLoader<MessageFactory> factoryLoader = new ProtonFactoryLoader<MessageFactory>(MessageFactory.class); + assertNotNull(factoryLoader.loadFactory()); } + @Test + public void testEngineFactory() + { + ProtonFactoryLoader<EngineFactory> factoryLoader = new ProtonFactoryLoader<EngineFactory>(EngineFactory.class); + assertNotNull(factoryLoader.loadFactory()); + } } --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
