/*
 * @(#)IUrlClassLoaderUTestI.java      0.9.0 23-May-2001 - 14:40
 *
 * Copyright (C) 2001,2002-2003 Matt Albrecht
 * groboclown@users.sourceforge.net
 * http://groboutils.sourceforge.net
 *
 *  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 net.sourceforge.groboutils.util.classes.v1;

import net.sourceforge.groboutils.junit.v1.iftc.*;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;

import java.net.URL;


/**
 * Offline tests.
 *
 * @author  Matt Albrecht <a href="mailto:groboclown@users.sourceforge.net">groboclown@users.sourceforge.net</a>
 * @version $Date: 2003/02/10 22:52:39 $
 * @since   May 23, 2001 (GroboUtils Alpha 0.9.0)
 */
public class IUrlClassLoaderUTestI extends InterfaceTestCase
{
    private static final Class THIS_CLASS = IUrlClassLoaderUTestI.class;
    
    public IUrlClassLoaderUTestI( String name, ImplFactory f )
    {
        super( name, IUrlClassLoader.class, f );
    }

    
    public IUrlClassLoader createLoader()
    {
        return (IUrlClassLoader)createImplObject();
    }
    
    
    public static InterfaceTestSuite suite()
    {
        InterfaceTestSuite suite = new InterfaceTestSuite( THIS_CLASS );
        
        return suite;
    }
    
    //-------------------------------------------------------------------------
    
    protected void setUp() throws Exception
    {
        super.setUp();
        
        // set ourself up
    }
    
    
    protected void tearDown() throws Exception
    {
        // tear ourself down
        
        super.tearDown();
    }

    
    public void testInstantiate()
    {
        assertNotNull( "Creation should not return null.",
            createLoader() );
    }
    
    
    
    
    public static class MyInner
    {
        // default constructor implied
    }
    public static final String MY_INNER_RES =
        "IUrlClassLoaderUTestI$MyInner.class";
    public static final String MY_INNER_CLASS = MyInner.class.getName();
    public static final String MY_INNER_FULL_RES =
        '/' + MY_INNER_CLASS.replace( '.', '/' ) + ".class";
    
    public static class InnerClass
    {
        public InnerClass()
        {
            // do nothing
        }
    }
    
    
    public static final String BELIEF_CLASS = "BeliefOfTheDay";
    
    
    public void testGetClass1()
    {
        IUrlClassLoader loader = createLoader();
        
        Class c = loader.loadClass( MY_INNER_CLASS, null );
        assertNotNull( "loadClass( "+MY_INNER_CLASS+
            " ) returned null.",
            c );
        assertEquals( "Did not load the class from the default classloader.",
            MyInner.class, c );
    }
    
    
    public void testGetClass2()
    {
        IUrlClassLoader loader = createLoader();
        
        Class c = loader.loadClass( BELIEF_CLASS, null );
        assertEquals(
            "getClass( Belief ) was found in the default classloader.",
            null, c );
    }
    
    
    public void testGetClass3()
    {
        IUrlClassLoader loader = createLoader();
        
        URL url = this.getClass().getResource( MY_INNER_RES );
        assertNotNull( "Did not find resource for inner class "+
            MY_INNER_RES, url );
        System.out.println("Found class in URL "+url);
        String urlStr = url.toString();
        if (urlStr.endsWith( MY_INNER_FULL_RES ))
        {
            urlStr = urlStr.substring( 0, urlStr.length() -
                MY_INNER_FULL_RES.length() );
        }
        
        Class c = loader.loadClass( MY_INNER_CLASS, urlStr );
        assertNotNull( "getClass( "+MY_INNER_CLASS+" ) returned null.", c );
        assertEquals( "getClass( "+MY_INNER_CLASS+" ) returned the wrong class.",
            MY_INNER_CLASS, c.getName() );
    }
    
    
    public void testFlush1()
    {
        IUrlClassLoader loader = createLoader();
        
        Class c = loader.loadClass( this.getClass().getName(), null );
        loader.flush();
        
        // Can't really test this for correctness, just for error throwing.
        // Testing for GC size is really inaccurate.
    }
    
}
