001: /*
002: * Copyright 2005-2007 The Kuali Foundation.
003: *
004: *
005: * Licensed under the Educational Community License, Version 1.0 (the "License");
006: * you may not use this file except in compliance with the License.
007: * You may obtain a copy of the License at
008: *
009: * http://www.opensource.org/licenses/ecl1.php
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: package edu.iu.uis.eden.plugin;
018:
019: import java.io.BufferedReader;
020: import java.io.File;
021: import java.io.InputStream;
022: import java.io.InputStreamReader;
023: import java.net.URL;
024:
025: import junit.framework.TestCase;
026:
027: import org.junit.Test;
028:
029: public class EmbeddedPluginClassLoaderTest extends TestCase {
030:
031: @Test
032: public void testLoadEmbeddedResources() throws Exception {
033: // load the embedded-test.jar
034: File embeddedJar = new File(
035: "test/src/edu/iu/uis/eden/plugin/embedded-test.jar");
036: assertTrue(embeddedJar.exists());
037: assertTrue(embeddedJar.isFile());
038: URL embeddedURL = embeddedJar.toURL();
039: PluginClassLoader pluginClassLoader = new PluginClassLoader();
040: pluginClassLoader.addURL(embeddedURL);
041:
042: // first test that we can load the META-INF/embedded-workflow.xml file
043: URL embeddedWFXml = pluginClassLoader
044: .getResource("embedded/META-INF/embedded-workflow.xml");
045: assertNotNull("should be able to find embedded-workflow.xml",
046: embeddedWFXml);
047:
048: InputStream stream = embeddedWFXml.openStream();
049: int numBytes = 0;
050: while (stream.read() != -1) {
051: numBytes++;
052: }
053: assertTrue(
054: "There should be more than 0 bytes in the embedded-workflow.xml file.",
055: numBytes > 0);
056:
057: // test that we can load the classes-test.txt file
058: URL classesTestUrl = pluginClassLoader
059: .getResource("embedded/classes/classes-test.txt");
060: assertNotNull("Failed to load the classes-test.txt file.",
061: classesTestUrl);
062:
063: // now create the embedded classloader, it's important that the pluginClassLoader is that parent
064: // because that allows us to pull the embedded plugin out of the jar at
065: // test/src/edu/iu/uis/eden/plugin/embedded-test.jar
066: // This jar has some special files in it that we use to verify the jar in this test case.
067: EmbeddedPluginClassLoader embeddedCL = new EmbeddedPluginClassLoader(
068: pluginClassLoader, "embedded");
069:
070: // try to load the AxisClient class which should originate in a jar
071: String axisClientClassName = "org.apache.axis.client.AxisClient";
072: try {
073: Class axisClientClass = embeddedCL
074: .loadClass(axisClientClassName);
075: assertEquals("Wrong classloader.", embeddedCL,
076: axisClientClass.getClassLoader());
077: } catch (ClassNotFoundException e) {
078: fail("Could not load the axis client.");
079: }
080: // assert that, even after loading the class, it's still possible to access the .class file as a resource
081: String axisClientClassResourceName = axisClientClassName
082: .replace(".", "/").concat(".class");
083: URL axisClientClassURL = embeddedCL
084: .getResource(axisClientClassResourceName);
085: assertNotNull(axisClientClassURL);
086: // also test that we can load the resource, even if it has a / at the beginning
087: axisClientClassURL = embeddedCL.getResource("/"
088: + axisClientClassResourceName);
089: assertNotNull(axisClientClassURL);
090:
091: // try to load the WorkflowUtilityServiceEndpoint which should be in the embedded/classes directory
092: try {
093: Class endpointClass = embeddedCL
094: .loadClass("edu.iu.uis.eden.server.WorkflowUtilityServiceEndpoint");
095: assertEquals("Wrong classloader.", embeddedCL,
096: endpointClass.getClassLoader());
097: } catch (ClassNotFoundException e) {
098: fail("Could not load the workflow utility service endpoint.");
099: }
100:
101: // now try loading the same class as a resource
102: URL endpointUrl = embeddedCL
103: .getResource("edu/iu/uis/eden/server/WorkflowUtilityServiceEndpoint.class");
104: assertNotNull(
105: "Failed to locate the WorkflowUtilityServiceEndpoint class in the classloader.",
106: endpointUrl);
107:
108: // try to find a resource that shouldn't be in our embedded classloader (instead it's parent)
109: URL testSpringUrl = embeddedCL.findResource("TestSpring.xml");
110: assertNull("Shouldn't have been able to find TestSpring.xml",
111: testSpringUrl);
112: // however, if we call getResource it should look in the parent classloader as well
113: testSpringUrl = embeddedCL.getResource("TestSpring.xml");
114: assertNotNull("Should have been able to find TestSpring.xml",
115: testSpringUrl);
116:
117: // try to load the Spring.xml resource
118: URL springUrl = embeddedCL.getResource("Spring.xml");
119: assertNotNull("Could not locate Spring.xml", springUrl);
120: springUrl = embeddedCL.findResource("Spring.xml");
121: assertNotNull("Could not find Spring.xml", springUrl);
122:
123: // try to load the classes-test.txt resource
124: classesTestUrl = embeddedCL.getResource("classes-test.txt");
125: assertNotNull(
126: "Could not locate the classes-test.txt resource.",
127: classesTestUrl);
128: BufferedReader reader = new BufferedReader(
129: new InputStreamReader(classesTestUrl.openStream()));
130: String line = reader.readLine();
131: reader.close();
132: assertEquals("classes-test", line);
133:
134: // try to load the lib-test.txt resource
135: URL libTestUrl = embeddedCL.getResource("lib-test.txt");
136: assertNotNull("Could not locate the lib-test.txt resource.",
137: libTestUrl);
138: reader = new BufferedReader(new InputStreamReader(libTestUrl
139: .openStream()));
140: line = reader.readLine();
141: reader.close();
142: assertEquals("lib-test", line);
143:
144: // test with a slash at the beginning
145: libTestUrl = embeddedCL.getResource("/lib-test.txt");
146: assertNotNull("Could not locate the lib-test.txt resource.",
147: libTestUrl);
148: reader = new BufferedReader(new InputStreamReader(libTestUrl
149: .openStream()));
150: line = reader.readLine();
151: reader.close();
152: assertEquals("lib-test", line);
153:
154: }
155:
156: }
|