DefaultContainerTestCase.java :  » Web-Server » JicarillaHTTP » org » jicarilla » container » test » Java Open Source

Java Open Source » Web Server » JicarillaHTTP 
JicarillaHTTP » org » jicarilla » container » test » DefaultContainerTestCase.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;

import junit.framework.TestCase;
import org.jicarilla.container.Adapter;
import org.jicarilla.container.Container;
import org.jicarilla.container.DefaultContainer;
import org.jicarilla.container.InitializationException;
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.ResolutionException;
import org.jicarilla.container.Resolver;
import org.jicarilla.container.selectors.ClassSelector;
import org.jicarilla.lang.FalseSelector;
import org.jicarilla.lang.Selector;
import org.jicarilla.lang.SelectorSwitch;
import org.jicarilla.lang.TrueSelector;

import java.lang.reflect.InvocationTargetException;

/**
 * @author <a href="lsimons at jicarilla dot org">Leo Simons</a>
 * @version $Id: DefaultContainerTestCase.java,v 1.9 2004/03/23 13:37:52 lsimons Exp $
 */
public class DefaultContainerTestCase
        extends TestCase
{
    protected Container m_container;
    protected NoopComponentAdapter m_adapter;
    protected NoopComponentAdapter m_falseAdapter;
    protected NoopComponentAdapter m_classAdapter;
    protected ExceptionThrowingAdapter m_exceptionAdapter;

    protected Selector m_falseSelector;
    protected Selector m_trueSelector;
    protected Selector m_classSelector;

    public void setUp()
    {
        m_container = new DefaultContainer();
        m_adapter = new NoopComponentAdapter();
        m_falseAdapter = new NoopComponentAdapter();
        m_classAdapter = new NoopComponentAdapter();
        m_exceptionAdapter = new ExceptionThrowingAdapter(
                new JicarillaException("some problem") );

        m_falseSelector = new FalseSelector();
        m_trueSelector = new TrueSelector();
        m_classSelector = new ClassSelector(
            DefaultContainerTestCase.class );
    }

    public void testConstructor()
    {
        new DefaultContainer();
        new DefaultContainer( new MockResolver() );
        new DefaultContainer( null );

        new DefaultContainer(
                new MockResolver(), new SelectorSwitch() );
        new DefaultContainer( null, new SelectorSwitch() );
        new DefaultContainer( new MockResolver(), null );
        new DefaultContainer( null, null );
    }

    public void testContains()
    {
        assertFalse( m_container.getResolver().contains("blah") );
        assertFalse( m_container.getResolver().contains(null) );

        // still 'empty'
        m_container.registerAdapter( m_falseSelector, m_falseAdapter );
        assertFalse( m_container.getResolver().contains("blah"));
        assertFalse( m_container.getResolver().contains(null) );

        // gotcha!
        m_container.registerAdapter( m_trueSelector, m_adapter );
        assertTrue( m_container.getResolver().contains("blah") );
        assertFalse( m_container.getResolver().contains(null) );
    }

    public void testRegisterSelectorBasedAdapter()
    {
        m_container.registerAdapter( m_trueSelector, m_adapter );

        AssertionError ex = null;
        try
        {
            m_container.registerAdapter( m_trueSelector, null );
        }
        catch( AssertionError ae )
        {
            ex = ae;
        }
        assertNotNull( ex );

        ex = null;
        try
        {
            m_container.registerAdapter( (Selector)null, m_adapter );
        }
        catch( AssertionError ae )
        {
            ex = ae;
        }
        assertNotNull( ex );
    }

    public void testRegisterKeyBasedAdapter()
    {
        m_container.registerAdapter( (Object)m_trueSelector, m_adapter );
        m_container.registerAdapter( "blah", m_adapter );

        m_container.registerAdapter( this.getClass(), m_adapter );
        m_container.registerAdapter( this.getClass().getName(), m_adapter );

        m_container.registerAdapter( new Object(), m_adapter );
        AssertionError ex = null;
        try
        {
            m_container.registerAdapter( "blah", null );
        }
        catch( AssertionError ae )
        {
            ex = ae;
        }
        assertNotNull( ex );

        ex = null;
        try
        {
            m_container.registerAdapter( (Object)null, m_adapter );
        }
        catch( AssertionError ae )
        {
            ex = ae;
        }
        assertNotNull( ex );
    }

    public void testGet() throws IllegalAccessException, InvocationTargetException, ClassNotFoundException, InstantiationException
    {
        // empty at first
        assertNull( m_container.getResolver().get("blah"));

        // still 'empty'
        m_container.registerAdapter( m_falseSelector, m_falseAdapter );
        assertNull( m_container.getResolver().get("blah"));

        // no match possible
        m_container.registerAdapter( m_classSelector, m_classAdapter );
        assertNull( m_container.getResolver().get("blah"));

        // but this works
        assertEquals( m_classAdapter.m_object, m_container.getResolver().get(this.getClass()) );

        // now we will always get a component
        m_container.registerAdapter( m_trueSelector, m_adapter );

        Object obj = m_container.getResolver().get("blah");
        assertEquals( m_adapter.m_object, obj );

        // and the TestCase is still resolved by the classselector
        assertEquals( m_classAdapter.m_object,
                m_container.getResolver().get(this.getClass()) );

        // get the second component
        AssertionError ex = null;
        try
        {
            m_container.getResolver().get( null );
        }
        catch( AssertionError ae )
        {
            ex = ae;
        }
        assertNotNull( ex );
    }

    public void testGetThrowsExceptions() throws IllegalAccessException, InvocationTargetException, ClassNotFoundException, InstantiationException
    {
        m_container.registerAdapter( m_trueSelector, m_exceptionAdapter );
        Throwable t = null;
        try
        {
            m_container.getResolver().get("blah");
        }
        catch( Throwable th )
        {
            t = th;
        }
        assertEquals( m_exceptionAdapter.t, t );

        m_container = new DefaultContainer();
        m_exceptionAdapter = new ExceptionThrowingAdapter(
                new JicarillaInvocationTargetException(new Exception()) );
        m_container.registerAdapter( m_trueSelector, m_exceptionAdapter );
        t = null;
        try
        {
            m_container.getResolver().get("blah");
        }
        catch( Throwable th )
        {
            t = th;
        }
        assertEquals( m_exceptionAdapter.t, t );

        m_container = new DefaultContainer();
        m_exceptionAdapter = new ExceptionThrowingAdapter(
                new JicarillaInstantiationException() );
        m_container.registerAdapter( m_trueSelector, m_exceptionAdapter );
        t = null;
        try
        {
            m_container.getResolver().get("blah");
        }
        catch( Throwable th )
        {
            t = th;
        }
        assertEquals( m_exceptionAdapter.t, t );

        m_container = new DefaultContainer();
        m_exceptionAdapter = new ExceptionThrowingAdapter(
                new JicarillaClassNotFoundException() );
        m_container.registerAdapter( m_trueSelector, m_exceptionAdapter );
        t = null;
        try
        {
            m_container.getResolver().get("blah");
        }
        catch( Throwable th )
        {
            t = th;
        }
        assertEquals( m_exceptionAdapter.t, t );

        // bad!
        m_container = new DefaultContainer();
        m_exceptionAdapter = new ExceptionThrowingAdapter(
                new RuntimeException() );
        m_container.registerAdapter( m_trueSelector, m_exceptionAdapter );
        t = null;
        try
        {
            m_container.getResolver().get("blah");
        }
        catch( Throwable th )
        {
            t = th;
        }
        assertNotNull( t );

        /*
        // bad!: will return true in contains(), then false on the next calls
        m_container = new DefaultContainer();
        m_container.addAdapter(
                new SelectOnlyOnceSelector(),
                new NoopComponentAdapter() );
        t = null;
        try
        {
            m_container.getResolver().get("blah");
        }
        catch( Throwable th )
        {
            t = th;
        }
        assertTrue( t instanceof RuntimeException ); // don't care much what kind of exception this is
        */
    }

    public void testGetAll() throws Exception
    {
        Throwable t = null;
        try
        {
            m_container.getResolver().getAll(null);
        }
        catch( AssertionError th )
        {
            t = th;
        }
        assertNotNull( t );

        assertEquals( 0, m_container.getResolver().getAll("blah").length );

        m_container.registerAdapter( m_trueSelector, m_adapter );

        assertEquals( 1, m_container.getResolver().getAll("blah").length );
        assertEquals( m_adapter.m_object, m_container.getResolver().getAll("blah")[0] );

        m_container.registerAdapter( m_trueSelector, m_adapter );
        assertEquals( 2, m_container.getResolver().getAll("blah").length );

        m_container.registerAdapter( m_falseSelector,
                new NoopComponentAdapter() );
        assertEquals( 2, m_container.getResolver().getAll("blah").length );

        m_container.registerAdapter(
                m_trueSelector, new NoopComponentAdapter() );
        assertEquals( 3, m_container.getResolver().getAll("Yahoo!").length );
    }

    public void testGetAllThrowsExceptions() throws IllegalAccessException, InvocationTargetException, ClassNotFoundException, InstantiationException
    {
        m_container.registerAdapter( m_trueSelector, m_exceptionAdapter );
        Throwable t = null;
        try
        {
            m_container.getResolver().getAll("blah");
        }
        catch( Throwable th )
        {
            t = th;
        }
        assertEquals( m_exceptionAdapter.t, t );

        m_container = new DefaultContainer();
        m_exceptionAdapter = new ExceptionThrowingAdapter(
                new JicarillaInvocationTargetException(new Exception()) );
        m_container.registerAdapter( m_trueSelector, m_exceptionAdapter );
        t = null;
        try
        {
            m_container.getResolver().getAll("blah");
        }
        catch( Throwable th )
        {
            t = th;
        }
        assertEquals( m_exceptionAdapter.t, t );

        m_container = new DefaultContainer();
        m_exceptionAdapter = new ExceptionThrowingAdapter(
                new JicarillaInstantiationException() );
        m_container.registerAdapter( m_trueSelector, m_exceptionAdapter );
        t = null;
        try
        {
            m_container.getResolver().getAll("blah");
        }
        catch( Throwable th )
        {
            t = th;
        }
        assertEquals( m_exceptionAdapter.t, t );

        m_container = new DefaultContainer();
        m_exceptionAdapter = new ExceptionThrowingAdapter(
                new JicarillaClassNotFoundException() );
        m_container.registerAdapter( m_trueSelector, m_exceptionAdapter );
        t = null;
        try
        {
            m_container.getResolver().getAll("blah");
        }
        catch( Throwable th )
        {
            t = th;
        }
        assertEquals( m_exceptionAdapter.t, t );

        // bad!
        m_container = new DefaultContainer();
        m_exceptionAdapter = new ExceptionThrowingAdapter(
                new RuntimeException() );
        m_container.registerAdapter( m_trueSelector, m_exceptionAdapter );
        t = null;
        try
        {
            m_container.getResolver().getAll("blah");
        }
        catch( Throwable th )
        {
            t = th;
        }
        assertNotNull( t );

        // bad!: will return true in contains(), then false on the next calls
        m_container = new DefaultContainer();
        m_container.registerAdapter(
                new SelectOnlyOnceSelector(), new NoopComponentAdapter() );
        Object[] instances = m_container.getResolver().getAll("blah");
        assertEquals( 0, instances.length );
    }

    public void testRelease() throws Exception
    {
        m_container.getResolver().releaseInstance( null );


        m_container.registerAdapter( m_trueSelector, m_adapter );
        Object obj = m_container.getResolver().get("blah");
        m_container.getResolver().releaseInstance( obj );

        m_container.getResolver().releaseInstance( new Object() );
        m_container.getResolver().releaseInstance( this );
    }

    public static class NoopComponentAdapter implements Adapter
    {
        public Object m_object;

        public NoopComponentAdapter()
        {
            m_object = new Object();
        }

        public Object getInstance()
        {
            return m_object;
        }

        public void releaseInstance( Object component ) throws Exception
        {
        }
    }
    public static class ExceptionThrowingAdapter extends NoopComponentAdapter
    {
        public Throwable t;

        public ExceptionThrowingAdapter( Throwable throwable )
        {
            t = throwable;
        }

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

            throw new JicarillaException( "blah", t );
        }
    }
    public static class SelectOnlyOnceSelector implements Selector
    {
        public boolean didSelect = false;

        public boolean select( Object object )
        {
            if(!didSelect)
            {
                didSelect = true;
                return true;
            }
            return false;
        }
    }

    public static class MockResolver implements Resolver
    {
        public Object get( Object key ) throws ResolutionException,
                JicarillaIllegalAccessException,
                JicarillaInvocationTargetException,
                JicarillaInstantiationException, JicarillaClassNotFoundException,
                InitializationException, JicarillaException
        {
            return null;
        }

        public Object[] getAll( Object key )
                throws JicarillaIllegalAccessException,
                JicarillaInvocationTargetException,
                JicarillaInstantiationException, JicarillaClassNotFoundException,
                InitializationException, JicarillaException
        {
            return new Object[0];
        }

        public boolean contains( Object key )
        {
            return false;
        }

        public void releaseInstance( Object instance ) throws Exception
        {
        }

    }

    /*public static class Exposer extends DefaultContainer
    {
        protected void setInstanceToAdapterMap( final Map instanceToAdapterMap )
        {
            m_instanceToAdapterMap = instanceToAdapterMap;
        }
        protected void setSwitch( final Switch switcher )
        {
            if( switcher != null )
                m_switch = switcher;
            else
                m_switch = new SelectorSwitch();
        }
        protected void setResolver( final Resolver resolver )
        {
            if( resolver != null )
                m_resolver = resolver;
            else
                m_resolver = new DefaultResolver( new DefaultResolverCallback() );
        }
    }*/
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.