package reflection;

import java.net.URL;
import java.net.URLClassLoader;
import junit.UnitTestRunner;
import org.junit.Test;
import static org.junit.Assert.*;

public class ClassLoaderTest {
    
    public static void main(String[] args) throws Exception {
        //UnitTestRunner.run(ClassLoaderTest.class);
        
        ClassLoader appCl = ClassLoaderTest.class.getClassLoader();
        System.out.println(appCl.getClass());
        
        URL x = appCl.getResource("org/eclipse/jetty/http/mime.properties");
        System.out.println(x);
    }
    
    @Test
    public void sysClassLoader() {
        ClassLoader sysCl = ClassLoader.getSystemClassLoader();
        assertEquals("sun.misc.Launcher$AppClassLoader", sysCl.getClass().getName());
        
        ClassLoader extCl = sysCl.getParent();
        assertEquals("sun.misc.Launcher$ExtClassLoader", extCl.getClass().getName());
        
        ClassLoader bootCl = extCl.getParent();
        assertNull(bootCl);
    }
    
    @Test
    public void sysClassLoader2() {
        ClassLoader sysCl = ClassLoader.getSystemClassLoader();
        URLClassLoader urlCl = (URLClassLoader) sysCl;
        assertTrue(urlCl.getURLs().length > 0);
    }
    
    @Test
    public void getClassLoader() {
        ClassLoader bootCl = Object.class.getClassLoader();
        assertNull(bootCl);
        
        ClassLoader appCl = ClassLoaderTest.class.getClassLoader();
        ClassLoader sysCl = ClassLoader.getSystemClassLoader();
        assertSame(sysCl, appCl);
    }
    
    @Test
    public void loadClass() throws Exception {
        ClassLoader sysCl = ClassLoader.getSystemClassLoader();
        assertSame(Object.class, sysCl.loadClass("java.lang.Object"));
        assertSame(ClassLoaderTest.class, sysCl.loadClass("reflection.ClassLoaderTest"));
    }
    
    @Test
    public void classNotFound() throws Exception {
        try {
            ClassLoader sysCl = ClassLoader.getSystemClassLoader();
            sysCl.loadClass("foo.bar.XXX");
            fail();
        } catch (ClassNotFoundException e) {
            assertEquals("foo.bar.XXX", e.getMessage());
        }
    }
    
}

@@ -264,10 +264,8 @@
     
-//
 Unit
@@ -307,248 +307,8 @@
 s);%0A
-        %0A        ClassLoader appCl = ClassLoaderTest.class.getClassLoader();%0A        System.out.println(appCl.getClass());%0A        %0A        URL x = appCl.getResource(%22org/eclipse/jetty/http/mime.properties%22);%0A        System.out.println(x);%0A
     
@@ -1913,10 +1913,292 @@
  %7D%0A    %0A
+    @Test%0A    public void getResource() %7B%0A        ClassLoader appCl = ClassLoaderTest.class.getClassLoader();%0A        //URL url = appCl.getResource(%22org/eclipse/jetty/http/mime.properties%22);%0A        URL url = appCl.getResource(%22LICENSE.txt%22);%0A        assertNotNull(url);%0A    %7D%0A    %0A
 %7D%0A

test ClassLoader.getResource()