Update of /cvsroot/jicarilla/jicarilla-sandbox/platform/container/impl/src/test/org/jicarilla/container/test/util
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv25586/platform/container/impl/src/test/org/jicarilla/container/test/util
Added Files:
ClassUtilTestCase.java
Log Message:
moved testcase
--- NEW FILE: ClassUtilTestCase.java ---
/* ====================================================================
The Jicarilla Software License
Copyright (c) 2003 Leo Simons.
All rights reserved.
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
==================================================================== */
package org.jicarilla.container.test.util;
import junit.framework.TestCase;
import org.apache.avalon.framework.configuration.DefaultConfiguration;
import org.apache.avalon.framework.context.ContextException;
import org.apache.avalon.framework.logger.ConsoleLogger;
import org.apache.avalon.framework.logger.Logger;
import org.apache.avalon.framework.service.ServiceException;
import org.jicarilla.container.NoPublicConstructorAvailableException;
import org.jicarilla.container.NoSatisfiableConstructorAvailableException;
import org.jicarilla.container.test.AbstractSimpson_test_case;
import org.jicarilla.container.test.DefaultContainerTestCase;
import org.jicarilla.container.adapters.SingletonAdapter;
import org.jicarilla.container.factories.ManualComponentFactory;
import org.jicarilla.container.selectors.ClassSelector;
import org.jicarilla.container.tck.components.interfaces.Bart;
import org.jicarilla.container.tck.components.interfaces.BartAware;
import org.jicarilla.container.tck.components.interfaces.Homer;
import org.jicarilla.container.tck.components.interfaces.Lisa;
import org.jicarilla.container.tck.components.interfaces.LisaAware;
import org.jicarilla.container.tck.components.interfaces.Maggie;
import org.jicarilla.container.tck.components.interfaces.Marge;
import org.jicarilla.container.tck.components.type1.avalon.AvalonAllInTheFamilyScript;
import org.jicarilla.container.tck.components.type1.avalon.AvalonBart;
import org.jicarilla.container.tck.components.type1.avalon.AvalonConfigurableHomer;
import org.jicarilla.container.tck.components.type1.avalon.AvalonConfigurableHomerConfiguration;
import org.jicarilla.container.tck.components.type1.avalon.AvalonContextualizableHomer;
import org.jicarilla.container.tck.components.type1.avalon.AvalonHomer;
import org.jicarilla.container.tck.components.type1.avalon.AvalonMarge;
import org.jicarilla.container.tck.components.type1.avalon.bad.AvalonBartWontLog;
import org.jicarilla.container.tck.components.type2.aware.HomerAware;
import org.jicarilla.container.tck.components.type2.aware.MaggieAware;
import org.jicarilla.container.tck.components.type2.aware.MargeAware;
import org.jicarilla.container.tck.components.type2.aware.bad.EmptyAwareImpl;
import org.jicarilla.container.tck.components.type2.aware.bad.HomerAndMargeAwareImpl;
import org.jicarilla.container.tck.components.type2.aware.bad.NonVoidAwareImpl;
import org.jicarilla.container.tck.components.type3.rich.HomerImpl;
import org.jicarilla.container.util.ConfigurationUtil;
import org.jicarilla.container.util.Type1Util;
import org.jicarilla.container.util.Type2Util;
import org.jicarilla.container.util.Type3Util;
import org.jicarilla.framework.CascadingRuntimeException;
import org.jicarilla.framework.StringSelector;
import org.jicarilla.framework.TrueSelector;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.util.Properties;
/**
* @author <a href="lsimons at jicarilla dot org">Leo Simons</a>
* @version $Id: ClassUtilTestCase.java,v 1.1 2004/03/09 20:24:25 lsimons Exp $
*/
public class ClassUtilTestCase extends AbstractSimpson_test_case
{
public void setUp()
{
super.setUp();
}
// ----------------------------------------------------------------------
// Test: getGreediestConstructor() / sortConstructorsByGreediness()
// ----------------------------------------------------------------------
public void testGetGreediestSatisfiableConstructor()
{
assertEquals( emptyConstructor,
Type3Util.getGreediestSatisfiableConstructor(
ManyConstructors.class, r ) );
assertEquals( satisfiableConstructor,
Type3Util.getGreediestSatisfiableConstructor(
UnSatisfiableConstructors.class, r ) );
Throwable t = null;
try
{
Type3Util.getGreediestSatisfiableConstructor( null, r );
}
catch( AssertionError th )
{
t = th;
}
assertNotNull( t );
t = null;
try
{
Type3Util.getGreediestSatisfiableConstructor(
ProtectedConstructor.class, r );
}
catch( NoPublicConstructorAvailableException th )
{
t = th;
}
assertNotNull( t );
t = null;
try
{
Type3Util.getGreediestSatisfiableConstructor(
NoSatisfiableConstructors.class, r );
}
catch( NoSatisfiableConstructorAvailableException th )
{
t = th;
}
assertNotNull( t );
}
public void testGetGreediestConstructor()
{
assertEquals( longestConstructor,
Type3Util.getGreediestConstructor( ManyConstructors.class ) );
Throwable t = null;
try
{
Type3Util.getGreediestConstructor(null);
}
catch( AssertionError th )
{
t = th;
}
assertNotNull( t );
t = null;
try
{
Type3Util.getGreediestConstructor( ProtectedConstructor.class );
}
catch( NoPublicConstructorAvailableException th )
{
t = th;
}
assertNotNull( t );
}
public void testSortConstructorsByGreediness()
{
Constructor[] result = ManyConstructors.class.getDeclaredConstructors();
Type3Util.sortConstructorsByGreediness( result );
assertEquals( orderConstructors.length, result.length );
for( int i = 0; i < result.length; i++ )
{
assertEquals( orderConstructors[i], result[i] );
}
Throwable t = null;
try
{
Type3Util.sortConstructorsByGreediness( null );
}
catch( Throwable th )
{
t = th;
}
assertNotNull( t );
Type3Util.sortConstructorsByGreediness( new Constructor[0] );
// weird!
Constructor[] weird = new Constructor[] {
longestConstructor,
longestConstructor
};
Type3Util.sortConstructorsByGreediness(weird);
}
static Constructor longestConstructor = null;
static Constructor emptyConstructor = null;
static
{
try
{
longestConstructor = ManyConstructors.class.getDeclaredConstructor( new Class[] {
String.class,
Class.class,
Properties.class,
String.class,
String.class,
Integer.class,
Class.class,
TestCase.class
});
emptyConstructor = ManyConstructors.class.getDeclaredConstructor( new Class[0] );
}
catch( NoSuchMethodException e )
{
throw new CascadingRuntimeException( "Whaah!", e );
}
}
static Constructor[] orderConstructors = new Constructor[6];
static
{
orderConstructors[0] = longestConstructor;
try
{
orderConstructors[1] = ManyConstructors.class.getDeclaredConstructor( new Class[] {
String.class,
Class.class,
Properties.class,
String.class,
String.class,
Integer.class,
});
orderConstructors[2] = ManyConstructors.class.getDeclaredConstructor( new Class[] {
String.class,
Class.class,
Properties.class,
String.class,
});
orderConstructors[3] = ManyConstructors.class.getDeclaredConstructor( new Class[0] );
orderConstructors[4] = ManyConstructors.class.getDeclaredConstructor( new Class[] {
String.class,
Class.class,
});
orderConstructors[5] = ManyConstructors.class.getDeclaredConstructor( new Class[] {
String.class,
});
}
catch( NoSuchMethodException e )
{
}
}
static Constructor satisfiableConstructor = null;
static
{
try
{
satisfiableConstructor = UnSatisfiableConstructors.class.getDeclaredConstructor( new Class[] {
Homer.class,
Bart.class
});
}
catch( NoSuchMethodException e )
{
throw new CascadingRuntimeException( "Whaah!", e );
}
}
public static class ManyConstructors
{
public ManyConstructors() {}
protected ManyConstructors( String str, Class clz ) {}
private ManyConstructors( String str ) {}
public ManyConstructors( String str, Class clz, Properties props, String str2 ) {}
public ManyConstructors(
String str, Class clz, Properties props, String str2, String str3, Integer intg ) {}
public ManyConstructors(
String str, Class clz, Properties props, String str2, String str3, Integer intg,
Class clz2, TestCase someTest ) {}
}
public static class ProtectedConstructor
{
private ProtectedConstructor() {}
}
public static class NoSatisfiableConstructors
{
protected NoSatisfiableConstructors( String str, Class clz ) {}
private NoSatisfiableConstructors( String str ) {}
public NoSatisfiableConstructors( String str, Class clz, Properties props, String str2 ) {}
public NoSatisfiableConstructors(
String str, Class clz, Properties props, String str2, String str3, Integer intg ) {}
public NoSatisfiableConstructors(
String str, Class clz, Properties props, String str2, String str3, Integer intg,
Class clz2, TestCase someTest ) {}
}
public static class UnSatisfiableConstructors
{
public UnSatisfiableConstructors() {}
protected UnSatisfiableConstructors( String str, Class clz ) {}
private UnSatisfiableConstructors( String str ) {}
public UnSatisfiableConstructors( String str, Class clz, Properties props, String str2 ) {}
public UnSatisfiableConstructors(
String str, Class clz, Properties props, String str2, String str3, Integer intg ) {}
public UnSatisfiableConstructors(
String str, Class clz, Properties props, String str2, String str3, Integer intg,
Class clz2, TestCase someTest ) {}
// pick this
public UnSatisfiableConstructors( Homer homer , Bart bart ) {}
}
public static Method margeAware;
public static Method homerAware;
public static Method bartAware;
public static Method lisaAware;
public static Method maggieAware;
static
{
try
{
margeAware = MargeAware.class.getMethod( "setMarge", new Class[] { Marge.class } );
homerAware = HomerAware.class.getMethod( "setHomer", new Class[] { Homer.class } );
bartAware = BartAware.class.getMethod( "setBart", new Class[] { Bart.class } );
lisaAware = LisaAware.class.getMethod( "setLisa", new Class[] { Lisa.class } );
maggieAware = MaggieAware.class.getMethod( "setMaggie", new Class[] { Maggie.class } );
}
catch( NoSuchMethodException e )
{
throw new CascadingRuntimeException( "bug in testcase", e );
}
}
// ----------------------------------------------------------------------
// Test: getAwarenessMethods() / callAwarenessMethods()
// ----------------------------------------------------------------------
/* todo: fix TCK public void testGetAwarenessMethods() throws Exception
{
// basic usage
Method[] methods = Type2Util.getAwarenessMethods( HomerImpl.class );
assertEquals( 1, methods.length );
assertEquals( margeAware, methods[0] );
// finds superclass methods, too
methods = Type2Util.getAwarenessMethods( YoungHomer.class );
assertEquals( 1, methods.length );
assertEquals( margeAware, methods[0] );
// works on interfaces
methods = Type2Util.getAwarenessMethods( HomerAwareExtension.class );
assertEquals( 1, methods.length );
assertTrue( arrayContains( methods, homerAware ) );
// does not run through interface twice
methods = Type2Util.getAwarenessMethods( MaggieImpl.class );
assertEquals( 2, methods.length );
assertTrue( arrayContains( methods, homerAware ) );
assertTrue( arrayContains( methods, margeAware ) );
// complex usage is okay
methods = Type2Util.getAwarenessMethods( AllInTheFamilyScript.class );
assertEquals( 5, methods.length );
assertTrue( arrayContains( methods, margeAware ) );
assertTrue( arrayContains( methods, homerAware ) );
assertTrue( arrayContains( methods, bartAware ) );
assertTrue( arrayContains( methods, lisaAware ) );
assertTrue( arrayContains( methods, maggieAware ) );
// no awareness methods for Apu (has interface)
methods = Type2Util.getAwarenessMethods( ApuImpl.class );
assertEquals( 0, methods.length );
// no awareness methods for Object (has no interface)
methods = Type2Util.getAwarenessMethods( Object.class );
assertEquals( 0, methods.length );
}*/
public void testGetAwarenessMethodsThrowsExceptions()
{
// no null arg accepted
Throwable t = null;
try
{
Type2Util.getAwarenessMethods( null );
}
catch( AssertionError th )
{
t = th;
}
assertNotNull( t );
// two methods in **Aware violates XWork contract
t = null;
try
{
Type2Util.getAwarenessMethods( HomerAndMargeAwareImpl.class );
}
catch( AssertionError th )
{
t = th;
}
assertNotNull( t );
// no methods in **Aware violates XWork contract
t = null;
try
{
Type2Util.getAwarenessMethods( EmptyAwareImpl.class );
}
catch( AssertionError th )
{
t = th;
}
assertNotNull( t );
// non-void return type violates XWork contract
t = null;
try
{
Type2Util.getAwarenessMethods( NonVoidAwareImpl.class );
}
catch( AssertionError th )
{
t = th;
}
assertNotNull( t );
}
/* todo: fix TCK public void testCallAwarenessMethods() throws Exception
{
DefaultContainer container = new DefaultContainer();
Resolver resolver = container.getResolver();
container.registerAdapter(
new ClassSelector(Homer.class),
new SingletonAdapter(
new Type2ComponentFactory( resolver, HomerImpl.class.getName() )
)
);
container.registerAdapter(
new ClassSelector(Marge.class),
new SingletonAdapter(
new Type2ComponentFactory( resolver, MargeImpl.class.getName() )
)
);
LoggingHomer homer = new LoggingHomer();
Type2Util.callAwarenessMethods( homer, resolver );
assertTrue(homer.margeSet);
assertNotNull( homer.getMarge() );
Type2Util.callAwarenessMethods( new Object(), resolver );
Throwable t = null;
try
{
Type2Util.callAwarenessMethods( null, resolver );
}
catch( AssertionError th )
{
t = th;
}
assertNotNull( t );
t = null;
try
{
Type2Util.callAwarenessMethods( new Object(), null );
}
catch( AssertionError th )
{
t = th;
}
assertNotNull( t );
}*/
public final static class LoggingHomer extends HomerImpl
{
public boolean margeSet = false;
public void setMarge( Marge marge )
{
super.setMarge( marge );
margeSet = true;
}
public Marge getMarge()
{
return super.getMarge();
}
}
public boolean arrayContains( Object[] array, Object needle )
{
for( int i = 0; i < array.length; i++ )
{
Object o = array[i];
if( needle.equals(o) )
return true;
}
return false;
}
// ----------------------------------------------------------------------
// Test: startAvalonLifecycle()
// ----------------------------------------------------------------------
public void testStartAvalonLifecycle() throws Exception
{
Type1Util.startAvalonLifecycle( new Object(), r );
Type1Util.startAvalonLifecycle( new AvalonAllInTheFamilyScript(), r );
Type1Util.startAvalonLifecycle( new AvalonBart(), r );
Type1Util.startAvalonLifecycle( new AvalonMarge(), r );
}
public void testStartAvalonLifecycleServiceManager() throws Exception
{
AvalonHomer homer = new AvalonHomer();
Type1Util.startAvalonLifecycle( homer, r );
assertTrue( homer.getServiceManager().hasService( Bart.class.getName() ) );
assertFalse( homer.getServiceManager().hasService( "blah" ) );
Throwable t = null;
try
{
homer.getServiceManager().lookup("blah");
}
catch( ServiceException th )
{
t = th;
}
assertNotNull( t );
t = null;
try
{
homer.getServiceManager().lookup(null);
}
catch( ServiceException th )
{
t = th;
}
assertNotNull( t );
Bart bart = (Bart)homer.getServiceManager().lookup( Bart.class.getName() );
homer.getServiceManager().release(bart);
homer.getServiceManager().release(null);
homer.getServiceManager().release(new Object());
jc.registerAdapter( new TrueSelector(),
new DefaultContainerTestCase.ExceptionThrowingAdapter(
new IllegalAccessException() ) );
t = null;
try
{
homer.getServiceManager().lookup( "Jodalihiti" );
}
catch( ServiceException th )
{
t = th;
}
assertNotNull( t );
}
public void testStartAvalonLifecycleThrowsExceptions()
{
Throwable t = null;
try
{
Type1Util.startAvalonLifecycle( new AvalonBartWontLog(), r );
}
catch( InstantiationException re )
{
t = re;
}
assertNotNull( t );
jc.registerAdapter(
new ClassSelector( Logger.class ),
new SingletonAdapter(
new ManualComponentFactory( new ConsoleLogger(ConsoleLogger.LEVEL_DEBUG))
)
);
t = null;
try
{
Type1Util.startAvalonLifecycle( new AvalonBartWontLog(), r );
}
catch( InstantiationException re )
{
t = re;
}
assertNotNull( t );
}
public void testStartAvalonLifecycleContext() throws Exception
{
AvalonContextualizableHomer homer = new AvalonContextualizableHomer();
Type1Util.startAvalonLifecycle( homer, r );
assertNotNull( homer.getMarge() );
Throwable t = null;
try
{
homer.getContext().get( "Jodalihiti" );
}
catch( ContextException th )
{
t = th;
}
assertNotNull( t );
t = null;
try
{
homer.getContext().get( null );
}
catch( ContextException th )
{
t = th;
}
assertNotNull( t );
jc.registerAdapter(
new TrueSelector(),
new DefaultContainerTestCase.ExceptionThrowingAdapter(
new IllegalAccessException() ) );
t = null;
try
{
homer.getContext().get( "Jodalihiti" );
}
catch( ContextException th )
{
t = th;
}
assertNotNull( t );
}
public void testStartAvalonLifecycleBeanConfiguration() throws Exception
{
AvalonConfigurableHomerConfiguration conf = new AvalonConfigurableHomerConfiguration();
conf.setBurpMessage("burp!");
jc.registerAdapter(
new ClassSelector( AvalonConfigurableHomerConfiguration.class ),
new SingletonAdapter(
new ManualComponentFactory( conf )
)
);
AvalonConfigurableHomer homer = new AvalonConfigurableHomer();
Type1Util.startAvalonLifecycle( homer, r );
assertEquals( homer.getBurpMessage(), "burp!" );
}
public void testStartAvalonLifecycleDefaultConfiguration() throws Exception
{
DefaultConfiguration burp = new DefaultConfiguration("burpMessage");
burp.setValue("burp!");
DefaultConfiguration conf = new DefaultConfiguration("base");
conf.addChild( burp );
jc.registerAdapter(
new StringSelector(
AvalonConfigurableHomer.class.getName() +
ConfigurationUtil.CONFIGURATION_POSTFIX ),
new SingletonAdapter(
new ManualComponentFactory( conf )
)
);
AvalonConfigurableHomer homer = new AvalonConfigurableHomer();
Type1Util.startAvalonLifecycle( homer, r );
assertEquals( homer.getBurpMessage(), "burp!" );
}
}
-------------------------------------------------------
This SF.Net email is sponsored by: IBM Linux Tutorials
Free Linux tutorial presented by Daniel Robbins, President and CEO of
GenToo technologies. Learn everything from fundamentals to system
administration.http://ads.osdn.com/?ad_id=1470&alloc_id=3638&op=click
lmpx.com only provides a reader for public news (NNTP) servers. It is not
affiliated with the servers or forums shown here and is not responsible for
the content of articles, which is written by their respective authors.