001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.commons.jci;
019:
020: import java.io.File;
021:
022: import org.apache.commons.jci.classes.ExtendedDump;
023: import org.apache.commons.jci.classes.SimpleDump;
024: import org.apache.commons.jci.listeners.ReloadingListener;
025: import org.apache.commons.jci.monitor.FilesystemAlterationMonitor;
026: import org.apache.commons.logging.Log;
027: import org.apache.commons.logging.LogFactory;
028:
029: /**
030: *
031: * @author tcurdt
032: */
033: public final class ReloadingClassLoaderTestCase extends
034: AbstractTestCase {
035:
036: private final Log log = LogFactory
037: .getLog(ReloadingClassLoaderTestCase.class);
038:
039: private ReloadingClassLoader classloader;
040: private ReloadingListener listener;
041: private FilesystemAlterationMonitor fam;
042:
043: private final byte[] clazzSimple1;
044: private final byte[] clazzSimple2;
045: private final byte[] clazzExtended;
046:
047: public ReloadingClassLoaderTestCase() throws Exception {
048: clazzSimple1 = SimpleDump.dump("Simple1");
049: clazzSimple2 = SimpleDump.dump("Simple2");
050: clazzExtended = ExtendedDump.dump();
051: assertTrue(clazzSimple1.length > 0);
052: assertTrue(clazzSimple2.length > 0);
053: assertTrue(clazzExtended.length > 0);
054: }
055:
056: protected void setUp() throws Exception {
057: super .setUp();
058:
059: classloader = new ReloadingClassLoader(this .getClass()
060: .getClassLoader());
061: listener = new ReloadingListener();
062:
063: listener.addReloadNotificationListener(classloader);
064:
065: fam = new FilesystemAlterationMonitor();
066: fam.addListener(directory, listener);
067: fam.start();
068: }
069:
070: public void testCreate() throws Exception {
071: listener.waitForFirstCheck();
072:
073: log.debug("creating class");
074: writeFile("jci/Simple.class", clazzSimple1);
075: listener.waitForCheck();
076:
077: final Object simple = classloader.loadClass("jci.Simple")
078: .newInstance();
079: assertEquals("Simple1", simple.toString());
080: }
081:
082: public void testChange() throws Exception {
083: listener.waitForFirstCheck();
084:
085: log.debug("creating class");
086: writeFile("jci/Simple.class", clazzSimple1);
087: listener.waitForCheck();
088:
089: final Object simple1 = classloader.loadClass("jci.Simple")
090: .newInstance();
091: assertEquals("Simple1", simple1.toString());
092:
093: log.debug("changing class");
094: writeFile("jci/Simple.class", clazzSimple2);
095: listener.waitForEvent();
096:
097: final Object simple2 = classloader.loadClass("jci.Simple")
098: .newInstance();
099: assertEquals("Simple2", simple2.toString());
100: }
101:
102: public void testDelete() throws Exception {
103: listener.waitForFirstCheck();
104:
105: log.debug("creating class");
106: writeFile("jci/Simple.class", clazzSimple1);
107: listener.waitForCheck();
108:
109: final Object simple = classloader.loadClass("jci.Simple")
110: .newInstance();
111: assertEquals("Simple1", simple.toString());
112:
113: log.debug("deleting class");
114: assertTrue(new File(directory, "jci/Simple.class").delete());
115: listener.waitForEvent();
116:
117: try {
118: classloader.loadClass("jci.Simple").newInstance();
119: fail();
120: } catch (final ClassNotFoundException e) {
121: assertEquals("jci.Simple", e.getMessage());
122: }
123: }
124:
125: public void testDeleteDependency() throws Exception {
126: listener.waitForFirstCheck();
127:
128: log.debug("creating classes");
129: writeFile("jci/Simple.class", clazzSimple1);
130: writeFile("jci/Extended.class", clazzExtended);
131: listener.waitForCheck();
132:
133: final Object simple = classloader.loadClass("jci.Simple")
134: .newInstance();
135: assertEquals("Simple1", simple.toString());
136:
137: final Object extended = classloader.loadClass("jci.Extended")
138: .newInstance();
139: assertEquals("Extended:Simple1", extended.toString());
140:
141: log.debug("deleting class dependency");
142: assertTrue(new File(directory, "jci/Simple.class").delete());
143: listener.waitForEvent();
144:
145: try {
146: classloader.loadClass("jci.Extended").newInstance();
147: fail();
148: } catch (final NoClassDefFoundError e) {
149: assertEquals("jci/Simple", e.getMessage());
150: }
151: }
152:
153: public void testClassNotFound() {
154: try {
155: classloader.loadClass("bla");
156: fail();
157: } catch (final ClassNotFoundException e) {
158: }
159: }
160:
161: public void testDelegation() {
162: classloader.clearAssertionStatus();
163: classloader.setClassAssertionStatus(
164: "org.apache.commons.jci.ReloadingClassLoader", true);
165: classloader.setDefaultAssertionStatus(false);
166: classloader.setPackageAssertionStatus("org.apache.commons.jci",
167: true);
168: // FIXME: compare with delegation
169: }
170:
171: protected void tearDown() throws Exception {
172: fam.removeListener(listener);
173: fam.stop();
174: super.tearDown();
175: }
176:
177: }
|