jicarilla-sandbox/platform/container/impl/src/test/org/jicarilla/container/test/adapters AbstracAdapterTestCase.java,NONE,1.1 PoolingAdapterTestCase.java,NONE,1.1 SingleUseAdapterTestCase.java,NONE,1.1 SingletonAdapterTestCase.java,NONE,1.1 ThreadLocalAdapterTestCase.java,NONE,1.1 AbstracComponentAdapterTestCase.java,1.1,NONE PoolingComponentAdapterTestCase.java,1.4,NONE SingleUseComponentAdapterTestCase.java,1.5,NONE SingletonComponentAdapterTestCase.java,1.5,NONE

Leo Simons <[email protected]>
Newsgroups gmane.comp.java.jicarilla.cvs
Message-ID <[email protected]>
Update of /cvsroot/jicarilla/jicarilla-sandbox/platform/container/impl/src/test/org/jicarilla/container/test/adapters
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv16122/container/impl/src/test/org/jicarilla/container/test/adapters

Added Files:
	AbstracAdapterTestCase.java PoolingAdapterTestCase.java 
	SingleUseAdapterTestCase.java SingletonAdapterTestCase.java 
	ThreadLocalAdapterTestCase.java 
Removed Files:
	AbstracComponentAdapterTestCase.java 
	PoolingComponentAdapterTestCase.java 
	SingleUseComponentAdapterTestCase.java 
	SingletonComponentAdapterTestCase.java 
Log Message:
Get rid of a bunch of "Component" parts in classnames. And probably some other misc. updates.

--- NEW FILE: PoolingAdapterTestCase.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.adapters;

import junit.framework.TestCase;
import org.apache.commons.pool.ObjectPool;
import org.apache.commons.pool.impl.SoftReferenceObjectPool;
import org.jicarilla.container.adapters.PoolingAdapter;
import org.jicarilla.framework.RecyclingObjectFactory;

/**
 * @author <a href="lsimons at jicarilla dot org">Leo Simons</a>
 * @version $Id: PoolingAdapterTestCase.java,v 1.1 2004/03/17 14:14:17 lsimons Exp $
 */
public class PoolingAdapterTestCase extends TestCase
{
    public static class Factory extends RecyclingObjectFactory
    {
        public Object o = new Object();
        public boolean objectMade = false;
        public boolean objectPassivated = false;
        public boolean myObjectPassivated = false;

        public Object makeObject() throws Exception
        {
            objectMade = true;
            return o;
        }
        public void passivateObject( Object obj )
        {
            objectPassivated = true;
            if( o.equals( obj) )
                myObjectPassivated = true;
        }
    }
    public void testEverything() throws Exception
    {
        //TrueSelector selector = new TrueSelector();
        Factory factory = new Factory();

        ObjectPool pool = new SoftReferenceObjectPool( factory );

        PoolingAdapter pca = new PoolingAdapter( pool );

        Object instance = pca.getInstance();
        assertEquals( factory.o, instance );
        assertTrue( factory.objectMade );

        pca.releaseInstance(null);
        assertTrue(factory.objectPassivated);
        factory.objectPassivated = false;
        pca.releaseInstance(new Object());
        assertTrue(factory.objectPassivated);

        pca.releaseInstance( instance );
        assertTrue(factory.myObjectPassivated);

        //assertEquals( selector, pca.getSelector() );
    }
    public void testExceptions() throws Exception
    {
        //TrueSelector selector = new TrueSelector();
        Factory factory = new Factory();

        ObjectPool pool = new SoftReferenceObjectPool( factory );

        Throwable t = null;
        try
        {
            new PoolingAdapter( null );
        }
        catch( AssertionError th )
        {
            t = th;
        }
        assertNotNull( t );

        pool = new SoftReferenceObjectPool( factory )
        {
            public Object borrowObject() throws Exception
            {
                throw new Exception();
            }
        };
        t = null;
        PoolingAdapter adapter = new PoolingAdapter( pool );
        try
        {
            adapter.getInstance();
        }
        catch( Throwable th )
        {
            t = th;
        }
        assertNotNull( t );
    }
}

--- NEW FILE: SingleUseAdapterTestCase.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.adapters;

import junit.framework.TestCase;
import org.jicarilla.container.adapters.SingleUseAdapter;

import java.lang.reflect.InvocationTargetException;

/**
 * @author <a href="lsimons at jicarilla dot org">Leo Simons</a>
 * @version $Id: SingleUseAdapterTestCase.java,v 1.1 2004/03/17 14:14:17 lsimons Exp $
 */
public class SingleUseAdapterTestCase extends TestCase
{
    public void testEverything() throws IllegalAccessException, InvocationTargetException, ClassNotFoundException, InstantiationException
    {
        //TrueSelector selector = new TrueSelector();
        AbstracAdapterTestCase.LoggingFactory factory = new AbstracAdapterTestCase.LoggingFactory();

        SingleUseAdapter adapter = new SingleUseAdapter(
                factory );

        Object instance = adapter.getInstance();
        factory.newInstanceCalled = false;

        assertNotSame( instance, adapter.getInstance() );
        assertTrue( factory.newInstanceCalled );
        assertNotSame( adapter.getInstance(), adapter.getInstance() );
        assertTrue( factory.newInstanceCalled );
    }
}

--- PoolingComponentAdapterTestCase.java DELETED ---

--- SingletonComponentAdapterTestCase.java DELETED ---

--- NEW FILE: AbstracAdapterTestCase.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.adapters;

import junit.framework.TestCase;
import org.jicarilla.container.Factory;
import org.jicarilla.container.JicarillaException;
import org.jicarilla.container.JicarillaInstantiationException;
import org.jicarilla.container.adapters.AbstractAdapter;

/**
 * @author <a href="lsimons at jicarilla dot org">Leo Simons</a>
 * @version $Id: AbstracAdapterTestCase.java,v 1.1 2004/03/17 14:14:17 lsimons Exp $
 */
public class AbstracAdapterTestCase extends TestCase
{
    public static class Adapter extends AbstractAdapter
    {
        public Adapter( Factory factory )
        {
            super( factory );
        }

        public Object getInstance()
        {
            return m_factory.newInstance();
        }
    }

    public static class LoggingFactory implements Factory
    {
        public boolean newInstanceCalled = false;
        public boolean releaseInstanceCalled = false;

        public Object newInstance()
        {
            newInstanceCalled = true;
            return new Object();
        }

        public void releaseInstance( Object o ) throws Exception
        {
            releaseInstanceCalled = true;
        }
    }

    public void testEverything() throws Exception
    {
        LoggingFactory factory = new LoggingFactory();
        Adapter adapter = new Adapter( factory );

        Object instance = adapter.getInstance();
        adapter.releaseInstance( instance );

        assertTrue( factory.newInstanceCalled );
        assertTrue( factory.releaseInstanceCalled );

        factory = new LoggingFactory();
        adapter = new Adapter( factory );
        adapter.releaseInstance( null );
        assertTrue( factory.releaseInstanceCalled );
    }

    public void testExceptions()
    {
        Throwable t = null;
        try
        {
            new Adapter( null );
        }
        catch( AssertionError th )
        {
            t = th;
        }
        assertNotNull( t );
    }

    public void testHandleUnExpectedException()
    {
        JicarillaException ex = new JicarillaException();
        try
        {
            AbstractAdapter.handleUnexpectedException( ex );
            fail( "Expected an exception!" );
        }
        catch( Throwable th )
        {
            assertEquals( ex, th );
        }

        Error e = new Error();
        try
        {
            AbstractAdapter.handleUnexpectedException( e );
            fail( "Expected an exception!" );
        }
        catch( Throwable th )
        {
            assertEquals( e, th );
        }

        Exception exc = new Exception();
        try
        {
            AbstractAdapter.handleUnexpectedException( exc );
            fail( "Expected an exception!" );
        }
        catch( Throwable th )
        {
            assertTrue( th instanceof JicarillaInstantiationException );
            assertEquals( exc, th.getCause() );
        }

    }

    public void testHandleUnExpectedExceptionFromSubClass()
    {
        LoggingFactory factory = new LoggingFactory();
        Adapter adapter = new Adapter( factory );

        JicarillaException ex = new JicarillaException();
        try
        {
            adapter.handleUnexpectedException( ex );
            fail( "Expected an exception!" );
        }
        catch( Throwable th )
        {
            assertEquals( ex, th );
        }

        Error e = new Error();
        try
        {
            adapter.handleUnexpectedException( e );
            fail( "Expected an exception!" );
        }
        catch( Throwable th )
        {
            assertEquals( e, th );
        }

        Exception exc = new Exception();
        try
        {
            adapter.handleUnexpectedException( exc );
            fail( "Expected an exception!" );
        }
        catch( Throwable th )
        {
            assertTrue( th instanceof JicarillaInstantiationException );
            assertEquals( exc, th.getCause() );
        }

    }
}

--- NEW FILE: SingletonAdapterTestCase.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.adapters;

import junit.framework.TestCase;
import org.jicarilla.container.adapters.SingletonAdapter;

import java.lang.reflect.InvocationTargetException;

/**
 * @author <a href="lsimons at jicarilla dot org">Leo Simons</a>
 * @version $Id: SingletonAdapterTestCase.java,v 1.1 2004/03/17 14:14:17 lsimons Exp $
 */
public class SingletonAdapterTestCase extends TestCase
{
    public void testEverything() throws IllegalAccessException, InvocationTargetException, ClassNotFoundException, InstantiationException
    {
        //TrueSelector selector = new TrueSelector();
        AbstracAdapterTestCase.LoggingFactory factory = new AbstracAdapterTestCase.LoggingFactory();

        SingletonAdapter adapter = new SingletonAdapter(
                factory );

        Object instance = adapter.getInstance();
        factory.newInstanceCalled = false;

        assertSame( instance, adapter.getInstance() );
        assertSame( instance, adapter.getInstance() );
        assertSame( instance, adapter.getInstance() );

        assertFalse( factory.newInstanceCalled );
    }
}

--- SingleUseComponentAdapterTestCase.java DELETED ---

--- AbstracComponentAdapterTestCase.java DELETED ---

--- NEW FILE: ThreadLocalAdapterTestCase.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.adapters;

import junit.framework.TestCase;
import org.jicarilla.container.Adapter;
import org.jicarilla.container.Factory;
import org.jicarilla.container.JicarillaClassNotFoundException;
import org.jicarilla.container.JicarillaException;
import org.jicarilla.container.JicarillaIllegalAccessException;
import org.jicarilla.container.JicarillaInstantiationException;
import org.jicarilla.container.JicarillaInvocationTargetException;
import org.jicarilla.container.SynchronizationUtil;
import org.jicarilla.container.adapters.ThreadLocalAdapter;
import org.jicarilla.framework.CascadingRuntimeException;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author <a href="lsimons at jicarilla dot org">Leo Simons</a>
 * @version $Id: ThreadLocalAdapterTestCase.java,v 1.1 2004/03/17 14:14:17 lsimons Exp $
 */
public class ThreadLocalAdapterTestCase extends TestCase
{
    public static class FactoryImpl implements Factory
    {
        public int newInstanceCalled = 0;

        public Object newInstance()
        {
            increment();
            return new Object();
        }

        public synchronized void increment()
        {
            newInstanceCalled++;
        }

        public void releaseInstance( Object o ) throws Exception {}
    }

    public static class ExceptionFactory implements Factory
    {
        protected Throwable t;

        public ExceptionFactory( Throwable exception )
        {
            t = exception;
        }

        public Object newInstance()
        {
            if( t instanceof RuntimeException )
                throw (RuntimeException)t;

            throw new JicarillaException( "blah", t );
        }

        public void releaseInstance( Object o ) throws Exception {}
    }

    public static class Holder
    {
        public Object obj = null;
    }
    
    public static class Runner implements Runnable
    {
        private Adapter m_adapter;
        private List m_components;
        private Holder m_counter;
        private Holder m_exception;

        public Runner( Adapter adapter, List components,
                Holder counter, Holder exception )
        {
            m_adapter = adapter;
            m_components = components;
            m_counter = counter;
            m_exception = exception;
        }

        public void run()
        {
            try
            {
                Object instance = m_adapter.getInstance();
                m_components.add( instance );
                m_counter.obj = new Integer(((Integer)m_counter.obj).intValue()+1);
            }
            catch( Exception e )
            {
                m_exception.obj = e;
                throw new CascadingRuntimeException( e.getMessage(), e );
            }
        }
    }

    public void testSingleThreaded() throws Exception
    {
        //TrueSelector selector = new TrueSelector();
        AbstracAdapterTestCase.LoggingFactory factory = new AbstracAdapterTestCase.LoggingFactory();

        ThreadLocalAdapter adapter = new ThreadLocalAdapter( factory );

        Object instance = adapter.getInstance();
        factory.newInstanceCalled = false;

        assertSame( instance, adapter.getInstance() );
        assertSame( instance, adapter.getInstance() );
        assertSame( instance, adapter.getInstance() );

        assertFalse( factory.newInstanceCalled );
    }

    public void testMultiThreaded() throws Exception
    {
        //TrueSelector selector = new TrueSelector();
        FactoryImpl factory = new FactoryImpl();

        final Adapter adapter = SynchronizationUtil.synchronizedAdapter( new ThreadLocalAdapter( factory ) );

        final List components = Collections.synchronizedList( new ArrayList() );
        final Holder exceptionHolder = new Holder();
        final Holder countHolder = new Holder();
        countHolder.obj = new Integer(0);

        int size = 5;
        Thread[] threads = new Thread[size];
        for( int i = 0; i < size; i++ )
            threads[i] = new Thread(
                    new Runner( adapter, components, countHolder, exceptionHolder ) );

        for( int i = 0; i < size; i++ )
            threads[i].start();

        for( int i = 0; i < size; i++ )
            threads[i].join();

        assertNull( exceptionHolder.obj );
        assertEquals( size, components.size() );
        assertEquals( size, ((Integer)countHolder.obj).intValue() );

        assertEquals( size, factory.newInstanceCalled );

        for( int i = 0; i < components.size()-1; i++ )
        {
            Object o = components.get( i );
            Object o2 = components.get( i+1 );
            assertNotSame( o, o2 );
        }
    }

    public void testExceptions() throws InvocationTargetException, ClassNotFoundException,
            InstantiationException, IllegalAccessException
    {
        //TrueSelector selector = new TrueSelector();
        ExceptionFactory factory = new ExceptionFactory( new JicarillaIllegalAccessException() );
        ThreadLocalAdapter adapter = new ThreadLocalAdapter( factory );
        Throwable t = null;
        try
        {
            adapter.getInstance();
        }
        catch( JicarillaIllegalAccessException th )
        {
            t = th;
        }
        assertNotNull( t );

        factory = new ExceptionFactory( new JicarillaInvocationTargetException(new Exception()) );
        adapter = new ThreadLocalAdapter( factory );
        t = null;
        try
        {
            adapter.getInstance();
        }
        catch( JicarillaInvocationTargetException th )
        {
            t = th;
        }
        assertNotNull( t );

        factory = new ExceptionFactory( new JicarillaClassNotFoundException() );
        adapter = new ThreadLocalAdapter( factory );
        t = null;
        try
        {
            adapter.getInstance();
        }
        catch( JicarillaClassNotFoundException th )
        {
            t = th;
        }
        assertNotNull( t );

        factory = new ExceptionFactory( new JicarillaInstantiationException() );
        adapter = new ThreadLocalAdapter( factory );
        t = null;
        try
        {
            adapter.getInstance();
        }
        catch( JicarillaInstantiationException th )
        {
            t = th;
        }
        assertNotNull( t );
    }
}



-------------------------------------------------------
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.