Clover coverage report - clover
Coverage timestamp: Sat Oct 8 2005 22:54:17 EDT
file stats: LOC: 243   Methods: 11
NCLOC: 152   Classes: 2
 
 Source file Conditionals Statements Methods TOTAL
AppClassLoaderTest.java - 94.2% 90.9% 93.8%
coverage coverage
 1   
 package abbot.script;
 2   
 
 3   
 import javax.swing.SwingUtilities;
 4   
 
 5   
 import junit.extensions.abbot.TestHelper;
 6   
 import junit.framework.*;
 7   
 import abbot.util.NonDelegatingClassLoader;
 8   
 import java.lang.ref.WeakReference;
 9   
 
 10   
 /** 
 11   
  * Verify AppClassLoader operation.
 12   
  */
 13   
 
 14   
 // Additional tests:
 15   
 // A class loaded by both system and app loader should differ
 16   
 // load from different class path
 17   
 // load resource from different class path
 18   
 // load from manifest class path in jar in class path
 19   
 public class AppClassLoaderTest extends TestCase {
 20   
 
 21   
     private AppClassLoader acl;
 22   
 
 23   
     /** Ensure the class loader is GC'd after uninstall. */
 24  1
     public void testGC() throws Exception {
 25  1
         acl = new AppClassLoader();
 26  1
         WeakReference ref = new WeakReference(acl);
 27   
         class Flag { volatile boolean flag; }
 28  1
         final Flag flag = new Flag();
 29   
 
 30  1
         acl.install();
 31   
 
 32  1
         SwingUtilities.invokeAndWait(new Runnable() {
 33  1
             public void run() {
 34  1
                 flag.flag = acl.isEventDispatchThread();
 35   
             }
 36   
         });
 37   
 
 38  1
         assertTrue("Custom event queue not installed", flag.flag);
 39   
 
 40  1
         acl.uninstall();
 41  1
         acl = null;
 42   
 
 43  1
         System.gc();
 44  1
         acl = (AppClassLoader)ref.get();
 45  1
         assertTrue("Class loader reference should be GC'd", acl == null);
 46   
     }
 47   
 
 48   
     /** Ensure the bootstrap classes are always loaded by the bootstrap
 49   
      * loader, except in cases where we specifically want preloading.
 50   
      */
 51  1
     public void testAppletViewerExclusion() throws Throwable {
 52   
         // applet viewer should get the custom loader
 53  1
         AppClassLoader dcl = new AppClassLoader();
 54   
 
 55  1
         Class appletClass = Class.forName("sun.applet.AppletViewer", true, dcl);
 56  1
         assertTrue("AppletViewer class loading should *not* be delegated",
 57   
                    appletClass.getClassLoader() instanceof NonDelegatingClassLoader);
 58   
 
 59  1
         Class orbClass = Class.forName("org.omg.CORBA.ORB", true, dcl);
 60  1
         assertTrue("org.omg class loading should always be delegated",
 61   
                    !(orbClass.getClassLoader() instanceof NonDelegatingClassLoader));
 62   
 
 63  1
         Class sliderClass = Class.forName("javax.swing.JSlider", true, dcl);
 64  1
         assertTrue("javax.swing class loading should always be delegated",
 65   
                    !(sliderClass.getClassLoader() instanceof NonDelegatingClassLoader));
 66   
     }
 67   
 
 68   
     private ClassLoader eventClassLoader = null;
 69  1
     public void testContextInstallUninstall() throws Throwable {
 70  1
         final AppClassLoader dcl = new AppClassLoader();
 71  1
         ClassLoader original = getClass().getClassLoader();
 72  1
         ClassLoader origContext =
 73   
             Thread.currentThread().getContextClassLoader();
 74  1
         try {
 75  1
             dcl.install();
 76  1
             eventClassLoader = null;
 77  1
             SwingUtilities.invokeAndWait(new Runnable() {
 78  1
                 public void run() {
 79  1
                     eventClassLoader =
 80   
                         Thread.currentThread().getContextClassLoader();
 81   
                 }
 82   
             });
 83  1
             assertEquals("Context not installed on event thread",
 84   
                          dcl, eventClassLoader);
 85  1
             dcl.uninstall();
 86  1
             eventClassLoader = null;
 87  1
             SwingUtilities.invokeAndWait(new Runnable() {
 88  1
                 public void run() {
 89  1
                     eventClassLoader =
 90   
                         Thread.currentThread().getContextClassLoader();
 91   
                 }
 92   
             });
 93  1
             assertTrue("Context not uninstalled from event thread",
 94   
                        eventClassLoader != dcl);
 95  1
             assertEquals("Original class loader not restored", 
 96   
                          getClass().getClassLoader(), original);
 97  1
             assertEquals("Original context not restored", 
 98   
                          Thread.currentThread().getContextClassLoader(), origContext);
 99   
         }
 100   
         finally {
 101   
             // Make sure it's uninstalled in case something bad happened
 102  1
             dcl.uninstall();
 103   
         }
 104   
     }
 105   
 
 106  1
     public void testLoadAppletViewer() throws Throwable {
 107  1
         String pathName = "lib/example.jar";
 108  1
         String className = "sun.applet.AppletViewer";
 109  1
         ClassLoader cl1 = new AppClassLoader(pathName);
 110  1
         ClassLoader cl2 = new AppClassLoader(pathName);
 111  1
         try {
 112  1
             Class cls1 = Class.forName(className, true, cl1);
 113  1
             Class cls2 = Class.forName(className, true, cl2);
 114  1
             assertTrue("Class loader should define the class package",
 115   
                        cls1.getPackage() != null);
 116  1
             assertEquals("Class loader defined the wrong package",
 117   
                          "sun.applet", cls1.getPackage().getName());
 118   
 
 119  1
             Class cls3 = Class.forName(className);
 120  1
             assertTrue("Core classes should be different when "
 121   
                        + "loaded dynamically", !cls1.equals(cls2));
 122  1
             assertTrue("Class loader should never delegate AppletViewer class",
 123   
                        !cls3.equals(cls1) && !cls3.equals(cls2));
 124   
         }
 125   
         catch(ClassNotFoundException cnf) {
 126  0
             fail("App class loader failed to load " + className
 127   
                  + " from path " + pathName);
 128   
         }
 129   
     }
 130   
 
 131   
     // this behavior results in too many complications; it's inevitably
 132   
     // impossible for the framework ensure it loads from the right class
 133   
     // loader at the right time, since the exact same classes are being loaded.
 134   
     /*
 135   
     public void testDirectBaseTesterLoading() throws Throwable {
 136   
         AppClassLoader dcl =
 137   
             new AppClassLoader("lib/abbot.jar");
 138   
         Class testerClass = 
 139   
             Class.forName("abbot.tester.ComponentTester", true, dcl);
 140   
         assertEquals("Custom class loader should be used to load a framework class when the framework is in the custom loader's class path",
 141   
                      dcl, testerClass.getClassLoader());
 142   
         Class compClass = Class.forName("javax.swing.JSlider", true, dcl);
 143   
         assertTrue("Component class should not be loaded by custom loader, "
 144   
                    + "not by " + dcl,
 145   
                    !dcl.equals(compClass.getClass().getClassLoader()));
 146   
 
 147   
         // A new reloading class loader should still get its own copy
 148   
         AppClassLoader dcl2 =
 149   
             new AppClassLoader("lib/abbot.jar");
 150   
         Class testerClass2 = 
 151   
             Class.forName("abbot.tester.ComponentTester", true, dcl2);
 152   
         assertEquals("Basic tester should be reloadable",
 153   
                      dcl2, testerClass2.getClassLoader());
 154   
         compClass = Class.forName("javax.swing.JSlider", true, dcl);
 155   
         assertTrue("Component class should not be loaded by custom loader, "
 156   
                    + "not by " + dcl2,
 157   
                    !dcl2.equals(compClass.getClass().getClassLoader()));
 158   
 
 159   
         // If the framework is *not* in the classpath, should always get the
 160   
         // same class.
 161   
         AppClassLoader dcl3 = new AppClassLoader("classes");
 162   
         Class testerClass3 =
 163   
             Class.forName("abbot.tester.ComponentTester", true, dcl3);
 164   
         assertTrue("Basic tester should be loaded from framework path, not by "
 165   
                    + dcl3,
 166   
                    !dcl3.equals(testerClass3.getClassLoader()));
 167   
     }
 168   
     */
 169   
 
 170   
     /** Load a custom tester and a tester and ensure the class hierarchy is
 171   
      * appropriate.
 172   
      */
 173  1
     public void testExtensionTesterLoading() throws Throwable {
 174  1
         AppClassLoader dcl =
 175   
             new AppClassLoader("lib/example.jar");
 176  1
         Class customTesterClass = 
 177   
             Class.forName("abbot.tester.extensions.ArrowButtonTester",
 178   
                           true, dcl);
 179  1
         ClassLoader ccl = customTesterClass.getClassLoader();
 180  1
         ClassLoader fcl = getClass().getClassLoader();
 181  1
         assertTrue("Class loader for extension tester should allow reloading: "
 182   
                    + ccl, ccl instanceof NonDelegatingClassLoader);
 183   
 
 184  1
         assertTrue("Class loader for extension tester should differ from the "
 185   
                    + "framework class loader " + fcl,
 186   
                    !ccl.equals(fcl));
 187   
 
 188  1
         assertEquals("Class loader for base tester class "
 189   
                      + "ancestor of the extension tester "
 190   
                      + "should match that of the test environment",
 191   
                      getClass().getClassLoader(),
 192   
                      customTesterClass.getSuperclass().getClassLoader());
 193   
 
 194  1
         assertEquals("Actual base tester class ancestor "
 195   
                      + "of the extension should match that of "
 196   
                      + "the framework environment", 
 197   
                      abbot.tester.ComponentTester.class,
 198   
                      customTesterClass.getSuperclass());
 199   
     }
 200   
 
 201   
     /** Extensions not in the app loader class path but in the framework
 202   
      * class path should still be loaded by the AppClassLoader, not the system
 203   
      * class loader.
 204   
      */
 205  1
     public void testExtensionsNotInTestCodeClassPath() throws Throwable {
 206  1
         AppClassLoader dcl =
 207   
             new AppClassLoader("lib/example.jar");
 208  1
         try {
 209  1
             Class customTesterClass = 
 210   
                 Class.forName("abbot.tester.extensions.DummyTester",
 211   
                               true, dcl);
 212  1
             ClassLoader ccl = customTesterClass.getClassLoader();
 213  1
             ClassLoader fcl = getClass().getClassLoader();
 214   
 
 215  1
             assertTrue("Wrong class loader for custom tester",
 216   
                        ccl instanceof NonDelegatingClassLoader);
 217  1
             assertTrue("Extension should not be found in the test code path",
 218   
                        !dcl.equals(ccl));
 219   
 
 220   
             // The parent class of the custom class should not come from the
 221   
             // app class loader
 222  1
             assertEquals("Tester superclass should match the test environment",
 223   
                          fcl, 
 224   
                          customTesterClass.getSuperclass().getClassLoader());
 225   
         }
 226   
         catch(ClassNotFoundException cnf) {
 227  0
             String msg = "The class loader should revert to java.class.path "
 228   
                 + "if the extension is not found in the app class loader "
 229   
                 + "classpath.  The extension was not found.";
 230  0
             throw new AssertionFailedError(msg);
 231   
         }
 232   
     }
 233   
 
 234  6
     public AppClassLoaderTest(String name) {
 235  6
         super(name);
 236   
     }
 237   
 
 238  0
     public static void main(String[] args) {
 239  0
         TestHelper.runTests(args, AppClassLoaderTest.class);
 240   
     }
 241   
 }
 242   
 
 243