This is an automated email from the ASF dual-hosted git repository. gk pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/turbine-fulcrum-factory.git
commit ce263e0da1553a77afd15d71af33a47b727fa6df Author: Jeffery Painter <[email protected]> AuthorDate: Mon Jan 7 21:02:00 2019 +0000 Turbine coding standards compliance, update tests with annotations git-svn-id: https://svn.apache.org/repos/asf/turbine/fulcrum/trunk/factory@1850690 13f79535-47bb-0310-9956-ffa450edef68 --- .../fulcrum/factory/DefaultFactoryService.java | 140 ++++++++++++++------- .../apache/fulcrum/factory/FactoryException.java | 12 +- .../org/apache/fulcrum/factory/FactoryService.java | 15 ++- .../factory/utils/ObjectInputStreamForContext.java | 4 +- .../apache/fulcrum/factory/FactoryServiceTest.java | 75 +++++++---- .../utils/ObjectInputStreamForContextTest.java | 31 ++++- 6 files changed, 193 insertions(+), 84 deletions(-) diff --git a/src/java/org/apache/fulcrum/factory/DefaultFactoryService.java b/src/java/org/apache/fulcrum/factory/DefaultFactoryService.java index 0e6d599..69532f0 100644 --- a/src/java/org/apache/fulcrum/factory/DefaultFactoryService.java +++ b/src/java/org/apache/fulcrum/factory/DefaultFactoryService.java @@ -106,7 +106,8 @@ public class DefaultFactoryService extends AbstractLogEnabled * @param type a primitive type. * @return the corresponding class, or null. */ - protected static Class<?> getPrimitiveClass(String type) { + protected static Class<?> getPrimitiveClass(String type) + { return primitiveClasses.get(type); } @@ -118,7 +119,8 @@ public class DefaultFactoryService extends AbstractLogEnabled * @throws FactoryException if instantiation fails. */ @Override - public <T> T getInstance(String className) throws FactoryException { + public <T> T getInstance(String className) throws FactoryException + { if (className == null) { throw new FactoryException("Missing String className"); } @@ -149,7 +151,8 @@ public class DefaultFactoryService extends AbstractLogEnabled * @throws FactoryException if instantiation fails. */ @Override - public <T> T getInstance(String className, ClassLoader loader) throws FactoryException { + public <T> T getInstance(String className, ClassLoader loader) throws FactoryException + { Factory<T> factory = getFactory(className); if (factory == null) { if (loader != null) { @@ -180,7 +183,8 @@ public class DefaultFactoryService extends AbstractLogEnabled * @throws FactoryException if instantiation fails. */ @Override - public <T> T getInstance(String className, Object[] params, String[] signature) throws FactoryException { + public <T> T getInstance(String className, Object[] params, String[] signature) throws FactoryException + { Factory<T> factory = getFactory(className); if (factory == null) { Class<T> clazz; @@ -203,7 +207,9 @@ public class DefaultFactoryService extends AbstractLogEnabled * <p> * Class loaders are supported only if the isLoaderSupported method returns * true. Otherwise the loader parameter is ignored. + * </p> * + * @param <T> Type of the class * @param className the name of the class. * @param loader the class loader. * @param params an array containing the parameters of the constructor. @@ -213,7 +219,8 @@ public class DefaultFactoryService extends AbstractLogEnabled */ @Override public <T> T getInstance(String className, ClassLoader loader, Object[] params, String[] signature) - throws FactoryException { + throws FactoryException + { Factory<T> factory = getFactory(className); if (factory == null) { if (loader != null) { @@ -248,6 +255,7 @@ public class DefaultFactoryService extends AbstractLogEnabled /** * Gets an instance of a specified class. * + * @param <T> Type of the class * @param clazz the class. * @return the instance. * @throws FactoryException if instantiation fails. @@ -295,7 +303,8 @@ public class DefaultFactoryService extends AbstractLogEnabled * @throws ClassNotFoundException if any of the classes is not found. */ @Override - public Class<?>[] getSignature(Class<?> clazz, Object params[], String signature[]) throws ClassNotFoundException { + public Class<?>[] getSignature(Class<?> clazz, Object params[], String signature[]) throws ClassNotFoundException + { if (signature != null) { /* We have parameters. */ ClassLoader tempLoader; @@ -337,30 +346,43 @@ public class DefaultFactoryService extends AbstractLogEnabled * @param loader the loader of the new context. * @return the object */ - protected Object switchObjectContext(Object object, ClassLoader loader) { + protected Object switchObjectContext(Object object, ClassLoader loader) + { ByteArrayOutputStream bout = new ByteArrayOutputStream(); - try { + try + { ObjectOutputStream out = new ObjectOutputStream(bout); out.writeObject(object); out.flush(); - } catch (IOException x) { + } + catch (IOException x) + { return object; } ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); ObjectInputStreamForContext in = null; - try { + try + { in = new ObjectInputStreamForContext(bin, loader); return in.readObject(); - } catch (Exception x) { + } + catch (Exception x) + { return object; - } finally { - if (in != null) { - try { + } + finally + { + if (in != null) + { + try + { in.close(); - } catch (IOException e) { + } + catch (IOException e) + { // close quietly } } @@ -375,24 +397,35 @@ public class DefaultFactoryService extends AbstractLogEnabled * @throws ClassNotFoundException if the class was not found. */ @SuppressWarnings("unchecked") - protected <T> Class<T> loadClass(String className) throws ClassNotFoundException { + protected <T> Class<T> loadClass(String className) throws ClassNotFoundException + { ClassLoader loader = this.getClass().getClassLoader(); - try { + try + { Class<T> clazz; - if (loader != null) { + if (loader != null) + { clazz = (Class<T>) loader.loadClass(className); - } else { + } + else + { clazz = (Class<T>) Class.forName(className); } return clazz; - } catch (ClassNotFoundException x) { + } + catch (ClassNotFoundException x) + { /* Go through additional loaders. */ - for (ClassLoader l : classLoaders) { - try { + for (ClassLoader l : classLoaders) + { + try + { return (Class<T>) l.loadClass(className); - } catch (ClassNotFoundException xx) { + } + catch (ClassNotFoundException xx) + { // continue } } @@ -410,10 +443,14 @@ public class DefaultFactoryService extends AbstractLogEnabled * @throws ClassNotFoundException if the class was not found. */ @SuppressWarnings("unchecked") - protected <T> Class<T> loadClass(String className, ClassLoader loader) throws ClassNotFoundException { - if (loader != null) { + protected <T> Class<T> loadClass(String className, ClassLoader loader) throws ClassNotFoundException + { + if (loader != null) + { return (Class<T>) loader.loadClass(className); - } else { + } + else + { return loadClass(className); } } @@ -427,19 +464,24 @@ public class DefaultFactoryService extends AbstractLogEnabled * @throws FactoryException if instantiation of the factory fails. */ @SuppressWarnings("unchecked") - protected <T> Factory<T> getFactory(String className) throws FactoryException { + protected <T> Factory<T> getFactory(String className) throws FactoryException + { Factory<T> factory = (Factory<T>) objectFactories.get(className); - if (factory == null) { - // No named factory for this; try the default, if one - // exists. + if (factory == null) + { + // No named factory for this; try the default, if one exists factory = (Factory<T>) objectFactories.get(DEFAULT_FACTORY); } + if (factory == null) { + /* Not yet instantiated... */ String factoryClass = objectFactoryClasses.get(className); - if (factoryClass == null) { + if (factoryClass == null) + { factoryClass = objectFactoryClasses.get(DEFAULT_FACTORY); } + if (factoryClass == null) { return null; } @@ -447,11 +489,15 @@ public class DefaultFactoryService extends AbstractLogEnabled try { factory = getInstance(factoryClass); factory.init(className); - } catch (ClassCastException x) { + } + catch (ClassCastException x) + { throw new FactoryException("Incorrect factory " + factoryClass + " for class " + className, x); } + Factory<T> _factory = (Factory<T>) objectFactories.putIfAbsent(className, factory); - if (_factory != null) { + if (_factory != null) + { // Already created - take first instance factory = _factory; } @@ -467,17 +513,21 @@ public class DefaultFactoryService extends AbstractLogEnabled * @see org.apache.avalon.framework.configuration.Configurable#configure(org.apache.avalon.framework.configuration.Configuration) */ @Override - public void configure(Configuration conf) throws ConfigurationException { + public void configure(Configuration conf) throws ConfigurationException + { final Configuration[] loaders = conf.getChildren(CLASS_LOADER); - if (loaders != null) { + if (loaders != null) + { loaderNames = new String[loaders.length]; - for (int i = 0; i < loaders.length; i++) { + for (int i = 0; i < loaders.length; i++) + { loaderNames[i] = loaders[i].getValue(); } } final Configuration factories = conf.getChild(OBJECT_FACTORY, false); - if (factories != null) { + if (factories != null) + { // Store the factory to the table as a string and // instantiate it by using the service when needed. Configuration[] nameVal = factories.getChildren(); @@ -494,13 +544,19 @@ public class DefaultFactoryService extends AbstractLogEnabled * @throws Exception if initialization fails. */ @Override - public void initialize() throws Exception { - if (loaderNames != null) { - for (String className : loaderNames) { - try { + public void initialize() throws Exception + { + if (loaderNames != null) + { + for (String className : loaderNames) + { + try + { ClassLoader loader = (ClassLoader) loadClass(className).newInstance(); classLoaders.add(loader); - } catch (Exception x) { + } + catch (Exception x) + { throw new Exception("No such class loader '" + className + "' for DefaultFactoryService", x); } } diff --git a/src/java/org/apache/fulcrum/factory/FactoryException.java b/src/java/org/apache/fulcrum/factory/FactoryException.java index 1e1cae1..d87033b 100644 --- a/src/java/org/apache/fulcrum/factory/FactoryException.java +++ b/src/java/org/apache/fulcrum/factory/FactoryException.java @@ -34,7 +34,8 @@ public class FactoryException extends Exception { /** * Default constructor */ - public FactoryException() { + public FactoryException() + { super(); } @@ -44,7 +45,8 @@ public class FactoryException extends Exception { * @param message the message * @param e the exception */ - public FactoryException(String message, Throwable e) { + public FactoryException(String message, Throwable e) + { super(message, e); } @@ -53,7 +55,8 @@ public class FactoryException extends Exception { * * @param e the exception to bubble up */ - public FactoryException(Throwable e) { + public FactoryException(Throwable e) + { super(e); } @@ -62,7 +65,8 @@ public class FactoryException extends Exception { * * @param msg the message to bubble up */ - public FactoryException(String msg) { + public FactoryException(String msg) + { super(msg); } } diff --git a/src/java/org/apache/fulcrum/factory/FactoryService.java b/src/java/org/apache/fulcrum/factory/FactoryService.java index f48ecd4..8f4347f 100644 --- a/src/java/org/apache/fulcrum/factory/FactoryService.java +++ b/src/java/org/apache/fulcrum/factory/FactoryService.java @@ -40,6 +40,7 @@ public interface FactoryService /** * Gets an instance of a class. * + * @param <T> Type of the class * @param clazz the name of the class. * @return {@inheritDoc} the instance. * @throws FactoryException if instantiation fails. @@ -50,6 +51,7 @@ public interface FactoryService /** * Gets an instance of a named class. * + * @param <T> Type of the class * @param className the name of the class. * @return {@inheritDoc} the instance. * @throws FactoryException if instantiation fails. @@ -60,9 +62,10 @@ public interface FactoryService /** * Gets an instance of a named class using a specified class loader. * - * <p>Class loaders are supported only if the isLoaderSupported + * Class loaders are supported only if the isLoaderSupported * method returns true. Otherwise the loader parameter is ignored. - * + * + * @param <T> Type of the class * @param className the name of the class. * @param loader the class loader. * @return {@inheritDoc} the instance. @@ -77,6 +80,7 @@ public interface FactoryService * Parameters for its constructor are given as an array of objects, * primitive types must be wrapped with a corresponding class. * + * @param <T> Type of the class * @param className the name of the class. * @param params an array containing the parameters of the constructor. * @param signature an array containing the signature of the constructor. @@ -93,9 +97,10 @@ public interface FactoryService * Parameters for its constructor are given as an array of objects, * primitive types must be wrapped with a corresponding class. * - * <p>Class loaders are supported only if the isLoaderSupported + * Class loaders are supported only if the isLoaderSupported * method returns true. Otherwise the loader parameter is ignored. * + * @param <T> Type of the class * @param className the name of the class. * @param loader the class loader. * @param params an array containing the parameters of the constructor. @@ -125,9 +130,11 @@ public interface FactoryService * @param clazz the class. * @param params an array containing the parameters of the method. * @param signature an array containing the signature of the method. + * * @return {@inheritDoc} an array of signature classes. Note that in some cases * objects in the parameter array can be switched to the context - * of a different class loader. + * of a different class loader + * * @throws ClassNotFoundException if any of the classes is not found. */ Class<?>[] getSignature(Class<?> clazz, diff --git a/src/java/org/apache/fulcrum/factory/utils/ObjectInputStreamForContext.java b/src/java/org/apache/fulcrum/factory/utils/ObjectInputStreamForContext.java index bf9338b..f7b2e40 100644 --- a/src/java/org/apache/fulcrum/factory/utils/ObjectInputStreamForContext.java +++ b/src/java/org/apache/fulcrum/factory/utils/ObjectInputStreamForContext.java @@ -38,7 +38,7 @@ public class ObjectInputStreamForContext extends ObjectInputStream private ClassLoader classLoader; /** - * this is to make the proxy happy. + * Required to make satisfy the proxy methods * * @throws IOException Generic exception */ @@ -48,7 +48,7 @@ public class ObjectInputStreamForContext extends ObjectInputStream } /** - * Contructs a new object stream for a context. + * This method will construct a new object stream for a context. * * @param in the serialized input stream. * @param loader the class loader of the context. diff --git a/src/test/org/apache/fulcrum/factory/FactoryServiceTest.java b/src/test/org/apache/fulcrum/factory/FactoryServiceTest.java index e86a229..1a85700 100644 --- a/src/test/org/apache/fulcrum/factory/FactoryServiceTest.java +++ b/src/test/org/apache/fulcrum/factory/FactoryServiceTest.java @@ -23,15 +23,21 @@ package org.apache.fulcrum.factory; import java.util.ArrayList; import org.apache.fulcrum.testcontainer.BaseUnitTest; +import org.junit.Before; +import org.junit.Test; /** - * @author Eric Pugh + * Basic tests of the fulcrum factory service + * + * @author <a href="mailto:[email protected]">Eric Pugh</a> * @author <a href="mailto:[email protected]">Stephen McConnell</a> - * + * + * @version $Id$ */ public class FactoryServiceTest extends BaseUnitTest { private FactoryService factoryService = null; + /** * Defines the testcase name for JUnit. * @@ -42,54 +48,64 @@ public class FactoryServiceTest extends BaseUnitTest super(name); } - @Override + @Before public void setUp() throws Exception { super.setUp(); factoryService = (FactoryService) this.resolve( FactoryService.class.getName() ); - } - /* + + /** * Class to test for Object getInstance(String) + * @throws Exception if factory fails to generate object */ + @Test public void testGetInstanceString() throws Exception { Object object = factoryService.getInstance("java.lang.StringBuilder"); assertTrue(object instanceof StringBuilder); } - /* + + /** * Class to test for Object getInstance(String, ClassLoader) + * + * @throws Exception Generic exception */ + @Test public void testGetInstanceStringClassLoader() throws Exception { Object object = factoryService.getInstance("java.lang.StringBuilder", StringBuilder.class.getClassLoader()); assertTrue(object instanceof StringBuilder); } - /* + + /** * Class to test for Object getInstance(String, Object[], String[]) + * @throws Exception Generic exception */ + @Test public void testGetInstanceStringObjectArrayStringArray() throws Exception { - Object params[] = new Object[1]; - String sourceValue = "testing"; - params[0] = sourceValue; - String signature[] = new String[1]; - signature[0] = "java.lang.String"; + String sourceValue = "testing"; + Object params[] = new Object[] { sourceValue }; + String signature[] = new String[] { "java.lang.String" }; + Object object = factoryService.getInstance("java.lang.StringBuilder", params, signature); assertTrue(object instanceof StringBuilder); assertEquals(sourceValue, object.toString()); - } - /* + + /** * Class to test for Object getInstance(String, ClassLoader, Object[], String[]) - */ + * + * @throws Exception Generic exception + */ + @Test public void testGetInstanceStringClassLoaderObjectArrayStringArray() throws Exception { - Object params[] = new Object[1]; - String sourceValu = "testing"; - params[0] = sourceValu; - String signature[] = new String[1]; - signature[0] = "java.lang.String"; + String sourceValue = "testing"; + Object params[] = new Object[] { sourceValue }; + String signature[] = new String[] { "java.lang.String" }; + Object object = factoryService.getInstance( "java.lang.StringBuilder", @@ -97,7 +113,7 @@ public class FactoryServiceTest extends BaseUnitTest params, signature); assertTrue(object instanceof StringBuilder); - assertEquals(sourceValu, object.toString()); + assertEquals(sourceValue, object.toString()); } @@ -106,19 +122,26 @@ public class FactoryServiceTest extends BaseUnitTest * * @throws Exception Generic exception */ + @Test public void testIsLoaderSupported() throws Exception { // TODO Need to run a test where the loader is NOT supported. assertTrue(factoryService.isLoaderSupported("java.lang.String")); } + + /** + * Test get signature + * + * @throws Exception Generic exception + */ + @Test public void testGetSignature() throws Exception { - Object params[] = new Object[1]; - String sourceValu = "testing"; - params[0] = sourceValu; - String signature[] = new String[1]; - signature[0] = "java.lang.String"; + String sourceValue = "testing"; + Object params[] = new Object[] { sourceValue }; + String signature[] = new String[] { "java.lang.String" }; + Class<?>[] results = factoryService.getSignature(StringBuilder.class, params, signature); assertEquals(1, results.length); assertTrue(results[0].equals(String.class)); diff --git a/src/test/org/apache/fulcrum/factory/utils/ObjectInputStreamForContextTest.java b/src/test/org/apache/fulcrum/factory/utils/ObjectInputStreamForContextTest.java index d8010a1..e533e03 100644 --- a/src/test/org/apache/fulcrum/factory/utils/ObjectInputStreamForContextTest.java +++ b/src/test/org/apache/fulcrum/factory/utils/ObjectInputStreamForContextTest.java @@ -22,31 +22,50 @@ package org.apache.fulcrum.factory.utils; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.ObjectOutputStream; -import junit.framework.TestCase; + +import org.apache.fulcrum.testcontainer.BaseUnitTest; +import org.junit.Test; /** - * @author Eric Pugh + * Basic test for object input stream for fulcrum factory + * + * @author <a href="mailto:[email protected]">Eric Pugh</a> + * @version $Id$ */ -public class ObjectInputStreamForContextTest extends TestCase +public class ObjectInputStreamForContextTest extends BaseUnitTest { - public static void main(String[] args) + + /** + * Defines the testcase name for JUnit. + * + * @param name the testcase's name. + */ + public ObjectInputStreamForContextTest(String name) { - junit.textui.TestRunner.run(ObjectInputStreamForContextTest.class); + super(name); } - /* + + /** + * * Class to test for void ObjectInputStreamForContext(InputStream, ClassLoader) + * + * @throws Exception generic exception */ + @Test public void testObjectInputStreamForContextInputStreamClassLoader() throws Exception { Object object = new String("I am testing"); Object object2 = null; + ByteArrayOutputStream bout = new ByteArrayOutputStream(); ObjectOutputStream out = new ObjectOutputStream(bout); out.writeObject(object); out.flush(); + ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); ObjectInputStreamForContext in = new ObjectInputStreamForContext(bin, String.class.getClassLoader()); object2 = in.readObject(); + assertEquals(object.toString(), object2.toString()); assertEquals(object, object2); }
