1   /*
2    *
3    *  Copyright (c) 1998-2005, The University of Sheffield.
4    *
5    *  This file is part of GATE (see http://gate.ac.uk/), and is free
6    *  software, licenced under the GNU Library General Public License,
7    *  Version 2, June 1991 (in the distribution as file licence.html,
8    *  and also available at http://gate.ac.uk/gate/licence.html).
9    *
10   *  Valentin Tablan, 26/Feb/2002
11   *
12   *  $Id: TestReload.java,v 1.8 2005/01/11 13:51:37 ian Exp $
13   */
14  
15  package gate.util;
16  
17  import java.net.URL;
18  
19  import junit.framework.*;
20  
21  
22  public class TestReload extends TestCase{
23    /** Construction */
24    public TestReload(String name) { super(name); }
25  
26   /** Fixture set up */
27    public void setUp() {
28    } // setUp
29  
30    /** Test suite routine for the test runner */
31    public static Test suite() {
32      return new TestSuite(TestReload.class);
33    } // suite
34  
35   /** Reload */
36    public void testReload() throws Exception {
37      ReloadingClassLoader loader = new ReloadingClassLoader();
38      //load first version
39      //it looks that Java doesn't like the jar in jar situation so we'll have to
40      //load the jar archives directly from the website.
41      URL url = new URL("http://gate.ac.uk/tests/first.jar");
42      loader.load(url);
43      //try the class
44      Class c = loader.loadClass("loader.Scratch", true);
45      String firstResult = c.newInstance().toString();
46  
47      //unload first version
48      loader.unload(url);
49  
50      //try to get an error
51      try{
52        c = loader.loadClass("loader.Scratch", true);
53        Assert.assertTrue("Class was found after being unloaded!", false);
54      }catch(ClassNotFoundException cnfe){
55        if(DEBUG) System.out.println("OK: got exception");
56      }
57  
58      //load second version
59      url = new URL("http://gate.ac.uk/tests/second.jar");
60      loader.load(url);
61  
62      //try the class
63      c = loader.loadClass("loader.Scratch", true);
64      String secondResult = c.newInstance().toString();
65  
66      //check the results are different
67      Assert.assertTrue("Got same result from different versions of the class",
68                        !firstResult.equals(secondResult));
69    }
70  
71    public void testUnload() throws Exception {
72      ReloadingClassLoader loader = new ReloadingClassLoader();
73      //load first version
74  //    URL url = Gate.class.getResource(Files.getResourcePath() +
75  //                                     "/gate.ac.uk/tests/first.jar");
76      URL url = new URL("http://gate.ac.uk/tests/first.jar");
77  
78      loader.load(url);
79      //try the class
80      Class c = loader.loadClass("loader.Scratch", true);
81      String firstResult = c.newInstance().toString();
82  
83      //unload first version
84      loader.unload(url);
85  
86      //try to get an error
87      try{
88        c = loader.loadClass("loader.Scratch", true);
89        Assert.assertTrue("Class was found after being unloaded!", false);
90      }catch(ClassNotFoundException cnfe){
91        if(DEBUG) System.out.println("OK: got exception");
92      }
93    }
94  
95    public void testCache() throws Exception {
96      ReloadingClassLoader loader = new ReloadingClassLoader();
97      long timeFresh = 0;
98      long startTime;
99      long endTime;
100     //load fresh class 100 times
101     URL url = new URL("http://gate.ac.uk/tests/first.jar");
102     for(int i = 0; i< 100; i++){
103       loader.load(url);
104       startTime = System.currentTimeMillis();
105       //load the class
106       Class c = loader.loadClass("loader.Scratch", true);
107       endTime = System.currentTimeMillis();
108       timeFresh += endTime - startTime;
109       loader.unload(url);
110     }
111 
112     //load cached classes 100 times
113     loader.load(url);
114     //load the class
115     Class c = loader.loadClass("loader.Scratch", true);
116     long timeCache = 0;
117     for(int i = 0; i< 100; i++){
118       startTime = System.currentTimeMillis();
119       //load the class
120       c = loader.loadClass("loader.Scratch", true);
121       endTime = System.currentTimeMillis();
122       timeCache += endTime - startTime;
123     }
124     Assert.assertTrue("Cached classes load slower than fresh ones!",
125                       timeCache < timeFresh);
126   }
127 
128 
129   /** Debug flag */
130   private static final boolean DEBUG = false;
131 }