View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.jci;
19  
20  import java.io.File;
21  
22  import org.apache.commons.jci.classes.ExtendedDump;
23  import org.apache.commons.jci.classes.SimpleDump;
24  import org.apache.commons.jci.listeners.ReloadingListener;
25  import org.apache.commons.jci.monitor.FilesystemAlterationMonitor;
26  import org.apache.commons.logging.Log;
27  import org.apache.commons.logging.LogFactory;
28  
29  /**
30   * 
31   * @author tcurdt
32   */
33  public final class ReloadingClassLoaderTestCase extends AbstractTestCase {
34  
35      private final Log log = LogFactory.getLog(ReloadingClassLoaderTestCase.class);
36      
37      private ReloadingClassLoader classloader;
38      private ReloadingListener listener;
39      private FilesystemAlterationMonitor fam;
40  
41      private final byte[] clazzSimple1;
42      private final byte[] clazzSimple2;
43      private final byte[] clazzExtended;
44      
45      public ReloadingClassLoaderTestCase() throws Exception {
46          clazzSimple1 = SimpleDump.dump("Simple1");
47          clazzSimple2 = SimpleDump.dump("Simple2");
48          clazzExtended = ExtendedDump.dump(); 
49          assertTrue(clazzSimple1.length > 0);
50          assertTrue(clazzSimple2.length > 0);
51          assertTrue(clazzExtended.length > 0);
52      }
53      
54      @Override
55      protected void setUp() throws Exception {
56          super.setUp();
57          
58          classloader = new ReloadingClassLoader(this.getClass().getClassLoader());
59          listener = new ReloadingListener();
60          
61          listener.addReloadNotificationListener(classloader);
62          
63          fam = new FilesystemAlterationMonitor();
64          fam.addListener(directory, listener);
65          fam.start();
66      }
67  
68      public void testCreate() throws Exception {
69          listener.waitForFirstCheck();
70  
71          log.debug("creating class");        
72          writeFile("jci/Simple.class", clazzSimple1);
73          listener.waitForCheck();
74          
75          final Object simple = classloader.loadClass("jci.Simple").newInstance();        
76          assertEquals("Simple1", simple.toString());        
77      }
78  
79      public void testChange() throws Exception {        
80          listener.waitForFirstCheck();
81  
82          log.debug("creating class");
83          writeFile("jci/Simple.class", clazzSimple1);
84          listener.waitForCheck();
85  
86          final Object simple1 = classloader.loadClass("jci.Simple").newInstance();        
87          assertEquals("Simple1", simple1.toString());
88          
89          log.debug("changing class");        
90          writeFile("jci/Simple.class", clazzSimple2);
91          listener.waitForEvent();
92      
93          final Object simple2 = classloader.loadClass("jci.Simple").newInstance();        
94          assertEquals("Simple2", simple2.toString());        
95      }
96  
97      public void testDelete() throws Exception {
98          listener.waitForFirstCheck();
99  
100         log.debug("creating class");
101         writeFile("jci/Simple.class", clazzSimple1);
102         listener.waitForCheck();
103 
104         final Object simple = classloader.loadClass("jci.Simple").newInstance();        
105         assertEquals("Simple1", simple.toString());
106 
107         log.debug("deleting class");        
108         assertTrue(new File(directory, "jci/Simple.class").delete());
109         listener.waitForEvent();
110 
111         try {
112             classloader.loadClass("jci.Simple").newInstance();        
113             fail();
114         } catch(final ClassNotFoundException e) {
115             assertEquals("jci.Simple", e.getMessage());
116         }        
117     }
118 
119     public void testDeleteDependency() throws Exception {        
120         listener.waitForFirstCheck();
121 
122         log.debug("creating classes");
123         writeFile("jci/Simple.class", clazzSimple1);
124         writeFile("jci/Extended.class", clazzExtended);
125         listener.waitForCheck();
126 
127         final Object simple = classloader.loadClass("jci.Simple").newInstance();        
128         assertEquals("Simple1", simple.toString());
129         
130         final Object extended = classloader.loadClass("jci.Extended").newInstance();        
131         assertEquals("Extended:Simple1", extended.toString());
132 
133         log.debug("deleting class dependency");        
134         assertTrue(new File(directory, "jci/Simple.class").delete());
135         listener.waitForEvent();
136 
137         try {
138             classloader.loadClass("jci.Extended").newInstance();
139             fail();
140         } catch(final NoClassDefFoundError e) {
141             assertEquals("jci/Simple", e.getMessage());
142         }
143     }
144 
145     public void testClassNotFound() {
146         try {
147             classloader.loadClass("bla");
148             fail();
149         } catch(final ClassNotFoundException e) {
150         }
151     }
152     
153     public void testDelegation() {
154         classloader.clearAssertionStatus();
155         classloader.setClassAssertionStatus("org.apache.commons.jci.ReloadingClassLoader", true);
156         classloader.setDefaultAssertionStatus(false);
157         classloader.setPackageAssertionStatus("org.apache.commons.jci", true);
158         // FIXME: compare with delegation
159     }
160     
161     @Override
162     protected void tearDown() throws Exception {
163         fam.removeListener(listener);
164         fam.stop();
165         super.tearDown();
166     }
167     
168 }