TestReload.java
001 /*
002  *
003  *  Copyright (c) 1995-2010, The University of Sheffield. See the file
004  *  COPYRIGHT.txt in the software or at http://gate.ac.uk/gate/COPYRIGHT.txt
005  *
006  *  This file is part of GATE (see http://gate.ac.uk/), and is free
007  *  software, licenced under the GNU Library General Public License,
008  *  Version 2, June 1991 (in the distribution as file licence.html,
009  *  and also available at http://gate.ac.uk/gate/licence.html).
010  *
011  *  Valentin Tablan, 26/Feb/2002
012  *
013  *  $Id: TestReload.java 12388 2010-03-22 12:39:06Z valyt $
014  */
015 
016 package gate.util;
017 
018 import java.net.URL;
019 
020 import junit.framework.*;
021 
022 
023 public class TestReload extends TestCase{
024   /** Construction */
025   public TestReload(String name) { super(name)}
026 
027  /** Fixture set up */
028   public void setUp() {
029   // setUp
030 
031   /** Test suite routine for the test runner */
032   public static Test suite() {
033     return new TestSuite(TestReload.class);
034   // suite
035 
036  /** Reload */
037   public void testReload() throws Exception {
038     ReloadingClassLoader loader = new ReloadingClassLoader();
039     //load first version
040     //it looks that Java doesn't like the jar in jar situation so we'll have to
041     //load the jar archives directly from the website.
042     URL url = new URL("http://gate.ac.uk/tests/first.jar");
043     loader.load(url);
044     //try the class
045     Class c = loader.loadClass("loader.Scratch"true);
046     String firstResult = c.newInstance().toString();
047 
048     //unload first version
049     loader.unload(url);
050 
051     //try to get an error
052     try{
053       c = loader.loadClass("loader.Scratch"true);
054       Assert.assertTrue("Class was found after being unloaded!"false);
055     }catch(ClassNotFoundException cnfe){
056       if(DEBUGSystem.out.println("OK: got exception");
057     }
058 
059     //load second version
060     url = new URL("http://gate.ac.uk/tests/second.jar");
061     loader.load(url);
062 
063     //try the class
064     c = loader.loadClass("loader.Scratch"true);
065     String secondResult = c.newInstance().toString();
066 
067     //check the results are different
068     Assert.assertTrue("Got same result from different versions of the class",
069                       !firstResult.equals(secondResult));
070   }
071 
072   public void testUnload() throws Exception {
073     ReloadingClassLoader loader = new ReloadingClassLoader();
074     //load first version
075 //    URL url = Gate.class.getResource(Files.getResourcePath() +
076 //                                     "/gate.ac.uk/tests/first.jar");
077     URL url = new URL("http://gate.ac.uk/tests/first.jar");
078 
079     loader.load(url);
080     //try the class
081     Class c = loader.loadClass("loader.Scratch"true);
082     String firstResult = c.newInstance().toString();
083 
084     //unload first version
085     loader.unload(url);
086 
087     //try to get an error
088     try{
089       c = loader.loadClass("loader.Scratch"true);
090       Assert.assertTrue("Class was found after being unloaded!"false);
091     }catch(ClassNotFoundException cnfe){
092       if(DEBUGSystem.out.println("OK: got exception");
093     }
094   }
095 
096   public void doNottestCache() throws Exception {
097     ReloadingClassLoader loader = new ReloadingClassLoader();
098     long timeFresh = 0;
099     long startTime;
100     long endTime;
101     //load fresh class 100 times
102     URL url = new URL("http://gate.ac.uk/tests/first.jar");
103     for(int i = 0; i< 100; i++){
104       loader.load(url);
105       startTime = System.currentTimeMillis();
106       //load the class
107       Class c = loader.loadClass("loader.Scratch"true);
108       endTime = System.currentTimeMillis();
109       timeFresh += endTime - startTime;
110       loader.unload(url);
111     }
112 
113     //load cached classes 100 times
114     loader.load(url);
115     //load the class
116     Class c = loader.loadClass("loader.Scratch"true);
117     long timeCache = 0;
118     for(int i = 0; i< 100; i++){
119       startTime = System.currentTimeMillis();
120       //load the class
121       c = loader.loadClass("loader.Scratch"true);
122       endTime = System.currentTimeMillis();
123       timeCache += endTime - startTime;
124     }
125     Assert.assertTrue("Cached classes load slower than fresh ones!",
126                       timeCache < timeFresh);
127   }
128 
129 
130   /** Debug flag */
131   private static final boolean DEBUG = false;
132 }