Author: painter
Date: Mon Jan 7 21:02:00 2019
New Revision: 1850690
URL: http://svn.apache.org/viewvc?rev=1850690&view=rev
Log:
Turbine coding standards compliance, update tests with annotations
Modified:
turbine/fulcrum/trunk/factory/src/java/org/apache/fulcrum/factory/DefaultFactoryService.java
turbine/fulcrum/trunk/factory/src/java/org/apache/fulcrum/factory/FactoryException.java
turbine/fulcrum/trunk/factory/src/java/org/apache/fulcrum/factory/FactoryService.java
turbine/fulcrum/trunk/factory/src/java/org/apache/fulcrum/factory/utils/ObjectInputStreamForContext.java
turbine/fulcrum/trunk/factory/src/test/org/apache/fulcrum/factory/FactoryServiceTest.java
turbine/fulcrum/trunk/factory/src/test/org/apache/fulcrum/factory/utils/ObjectInputStreamForContextTest.java
Modified:
turbine/fulcrum/trunk/factory/src/java/org/apache/fulcrum/factory/DefaultFactoryService.java
URL:
http://svn.apache.org/viewvc/turbine/fulcrum/trunk/factory/src/java/org/apache/fulcrum/factory/DefaultFactoryService.java?rev=1850690&r1=1850689&r2=1850690&view=diff
==============================================================================
---
turbine/fulcrum/trunk/factory/src/java/org/apache/fulcrum/factory/DefaultFactoryService.java
(original)
+++
turbine/fulcrum/trunk/factory/src/java/org/apache/fulcrum/factory/DefaultFactoryService.java
Mon Jan 7 21:02:00 2019
@@ -106,7 +106,8 @@ public class DefaultFactoryService exten
* @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 exten
* @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 exten
* @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 exten
* @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 exten
* <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 exten
*/
@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 exten
/**
* 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 exten
* @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 exten
* @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 exten
* @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 exten
* @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 exten
* @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 exten
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 exten
* @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 exten
* @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);
}
}
Modified:
turbine/fulcrum/trunk/factory/src/java/org/apache/fulcrum/factory/FactoryException.java
URL:
http://svn.apache.org/viewvc/turbine/fulcrum/trunk/factory/src/java/org/apache/fulcrum/factory/FactoryException.java?rev=1850690&r1=1850689&r2=1850690&view=diff
==============================================================================
---
turbine/fulcrum/trunk/factory/src/java/org/apache/fulcrum/factory/FactoryException.java
(original)
+++
turbine/fulcrum/trunk/factory/src/java/org/apache/fulcrum/factory/FactoryException.java
Mon Jan 7 21:02:00 2019
@@ -34,7 +34,8 @@ public class FactoryException extends Ex
/**
* Default constructor
*/
- public FactoryException() {
+ public FactoryException()
+ {
super();
}
@@ -44,7 +45,8 @@ public class FactoryException extends Ex
* @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 Ex
*
* @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 Ex
*
* @param msg the message to bubble up
*/
- public FactoryException(String msg) {
+ public FactoryException(String msg)
+ {
super(msg);
}
}
Modified:
turbine/fulcrum/trunk/factory/src/java/org/apache/fulcrum/factory/FactoryService.java
URL:
http://svn.apache.org/viewvc/turbine/fulcrum/trunk/factory/src/java/org/apache/fulcrum/factory/FactoryService.java?rev=1850690&r1=1850689&r2=1850690&view=diff
==============================================================================
---
turbine/fulcrum/trunk/factory/src/java/org/apache/fulcrum/factory/FactoryService.java
(original)
+++
turbine/fulcrum/trunk/factory/src/java/org/apache/fulcrum/factory/FactoryService.java
Mon Jan 7 21:02:00 2019
@@ -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,
Modified:
turbine/fulcrum/trunk/factory/src/java/org/apache/fulcrum/factory/utils/ObjectInputStreamForContext.java
URL:
http://svn.apache.org/viewvc/turbine/fulcrum/trunk/factory/src/java/org/apache/fulcrum/factory/utils/ObjectInputStreamForContext.java?rev=1850690&r1=1850689&r2=1850690&view=diff
==============================================================================
---
turbine/fulcrum/trunk/factory/src/java/org/apache/fulcrum/factory/utils/ObjectInputStreamForContext.java
(original)
+++
turbine/fulcrum/trunk/factory/src/java/org/apache/fulcrum/factory/utils/ObjectInputStreamForContext.java
Mon Jan 7 21:02:00 2019
@@ -38,7 +38,7 @@ public class ObjectInputStreamForContext
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
}
/**
- * 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.
Modified:
turbine/fulcrum/trunk/factory/src/test/org/apache/fulcrum/factory/FactoryServiceTest.java
URL:
http://svn.apache.org/viewvc/turbine/fulcrum/trunk/factory/src/test/org/apache/fulcrum/factory/FactoryServiceTest.java?rev=1850690&r1=1850689&r2=1850690&view=diff
==============================================================================
---
turbine/fulcrum/trunk/factory/src/test/org/apache/fulcrum/factory/FactoryServiceTest.java
(original)
+++
turbine/fulcrum/trunk/factory/src/test/org/apache/fulcrum/factory/FactoryServiceTest.java
Mon Jan 7 21:02:00 2019
@@ -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
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
params,
signature);
assertTrue(object instanceof StringBuilder);
- assertEquals(sourceValu, object.toString());
+ assertEquals(sourceValue, object.toString());
}
@@ -106,19 +122,26 @@ public class FactoryServiceTest extends
*
* @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));
Modified:
turbine/fulcrum/trunk/factory/src/test/org/apache/fulcrum/factory/utils/ObjectInputStreamForContextTest.java
URL:
http://svn.apache.org/viewvc/turbine/fulcrum/trunk/factory/src/test/org/apache/fulcrum/factory/utils/ObjectInputStreamForContextTest.java?rev=1850690&r1=1850689&r2=1850690&view=diff
==============================================================================
---
turbine/fulcrum/trunk/factory/src/test/org/apache/fulcrum/factory/utils/ObjectInputStreamForContextTest.java
(original)
+++
turbine/fulcrum/trunk/factory/src/test/org/apache/fulcrum/factory/utils/ObjectInputStreamForContextTest.java
Mon Jan 7 21:02:00 2019
@@ -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);
}