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      protected void setUp() throws Exception {
55          super.setUp();
56          
57          classloader = new ReloadingClassLoader(this.getClass().getClassLoader());
58          listener = new ReloadingListener();
59          
60          listener.addReloadNotificationListener(classloader);
61          
62          fam = new FilesystemAlterationMonitor();
63          fam.addListener(directory, listener);
64          fam.start();
65      }
66  
67      public void testCreate() throws Exception {
68          listener.waitForFirstCheck();
69  
70          log.debug("creating class");        
71          writeFile("jci/Simple.class", clazzSimple1);
72          listener.waitForCheck();
73          
74          final Object simple = classloader.loadClass("jci.Simple").newInstance();        
75          assertEquals("Simple1", simple.toString());        
76      }
77  
78      public void testChange() throws Exception {        
79          listener.waitForFirstCheck();
80  
81          log.debug("creating class");
82          writeFile("jci/Simple.class", clazzSimple1);
83          listener.waitForCheck();
84  
85          final Object simple1 = classloader.loadClass("jci.Simple").newInstance();        
86          assertEquals("Simple1", simple1.toString());
87          
88          log.debug("changing class");        
89          writeFile("jci/Simple.class", clazzSimple2);
90          listener.waitForEvent();
91      
92          final Object simple2 = classloader.loadClass("jci.Simple").newInstance();        
93          assertEquals("Simple2", simple2.toString());        
94      }
95  
96      public void testDelete() throws Exception {
97          listener.waitForFirstCheck();
98  
99          log.debug("creating class");
100         writeFile("jci/Simple.class", clazzSimple1);
101         listener.waitForCheck();
102 
103         final Object simple = classloader.loadClass("jci.Simple").newInstance();        
104         assertEquals("Simple1", simple.toString());
105 
106         log.debug("deleting class");        
107         assertTrue(new File(directory, "jci/Simple.class").delete());
108         listener.waitForEvent();
109 
110         try {
111             classloader.loadClass("jci.Simple").newInstance();        
112             fail();
113         } catch(final ClassNotFoundException e) {
114             assertEquals("jci.Simple", e.getMessage());
115         }        
116     }
117 
118     public void testDeleteDependency() throws Exception {        
119         listener.waitForFirstCheck();
120 
121         log.debug("creating classes");
122         writeFile("jci/Simple.class", clazzSimple1);
123         writeFile("jci/Extended.class", clazzExtended);
124         listener.waitForCheck();
125 
126         final Object simple = classloader.loadClass("jci.Simple").newInstance();        
127         assertEquals("Simple1", simple.toString());
128         
129         final Object extended = classloader.loadClass("jci.Extended").newInstance();        
130         assertEquals("Extended:Simple1", extended.toString());
131 
132         log.debug("deleting class dependency");        
133         assertTrue(new File(directory, "jci/Simple.class").delete());
134         listener.waitForEvent();
135 
136         try {
137             classloader.loadClass("jci.Extended").newInstance();
138             fail();
139         } catch(final NoClassDefFoundError e) {
140             assertEquals("jci/Simple", e.getMessage());
141         }
142     }
143 
144     public void testClassNotFound() {
145         try {
146             classloader.loadClass("bla");
147             fail();
148         } catch(final ClassNotFoundException e) {
149         }
150     }
151     
152     public void testDelegation() {
153         classloader.clearAssertionStatus();
154         classloader.setClassAssertionStatus("org.apache.commons.jci.ReloadingClassLoader", true);
155         classloader.setDefaultAssertionStatus(false);
156         classloader.setPackageAssertionStatus("org.apache.commons.jci", true);
157         // FIXME: compare with delegation
158     }
159     
160     protected void tearDown() throws Exception {
161         fam.removeListener(listener);
162         fam.stop();
163         super.tearDown();
164     }
165     
166 }