001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.netbeans;
042:
043: import java.io.DataInputStream;
044: import java.io.File;
045: import java.io.FileInputStream;
046: import java.io.FileOutputStream;
047: import java.io.IOException;
048: import java.io.InputStream;
049: import java.net.URL;
050: import java.util.Collections;
051: import java.util.Enumeration;
052: import java.util.List;
053: import java.util.Set;
054: import java.util.jar.JarFile;
055: import java.util.jar.Manifest;
056: import java.util.logging.Level;
057: import java.util.logging.Logger;
058: import junit.framework.AssertionFailedError;
059: import org.netbeans.junit.Log;
060: import org.netbeans.junit.NbTestCase;
061: import org.openide.filesystems.FileUtil;
062:
063: /** Tests that cover some basic aspects of a Proxy/JarClassLoader.
064: *
065: * @author Petr Nejedly
066: */
067: public class JarClassLoaderTest extends NbTestCase {
068:
069: private static Logger LOGGER = Logger
070: .getLogger(ProxyClassLoader.class.getName());
071:
072: public JarClassLoaderTest(String name) {
073: super (name);
074: }
075:
076: /** directory full of JAR files to test */
077: protected File jars;
078: /** directory full of testing roots */
079: protected File dirs;
080:
081: @Override
082: protected void setUp() throws Exception {
083: LOGGER.setUseParentHandlers(false);
084: LOGGER.setLevel(Level.OFF);
085: jars = new File(JarClassLoaderTest.class.getResource("jars")
086: .getFile());
087: dirs = new File(JarClassLoaderTest.class.getResource("dirs")
088: .getFile());
089: }
090:
091: public void testCanLoadFromDefaultPackage() throws Exception {
092: File jar = new File(jars, "default-package-resource.jar");
093: JarClassLoader jcl = new JarClassLoader(Collections
094: .singletonList(jar), new ProxyClassLoader[0]);
095:
096: assertStreamContent(jcl
097: .getResourceAsStream("package/resource.txt"), "content");
098: assertStreamContent(jcl
099: .getResourceAsStream("/package/resource.txt"),
100: "content");
101: assertStreamContent(jcl.getResourceAsStream("resource.txt"),
102: "content");
103: assertStreamContent(jcl.getResourceAsStream("/resource.txt"),
104: "content");
105:
106: assertURLsContent(jcl.getResources("package/resource.txt"),
107: "content");
108: assertURLsContent(jcl.getResources("/package/resource.txt"),
109: "content");
110: assertURLsContent(jcl.getResources("resource.txt"), "content");
111: assertURLsContent(jcl.getResources("/resource.txt"), "content");
112: }
113:
114: public void testCanLoadFromDefaultPackageCached() throws Exception {
115: final File jar = new File(jars,
116: "default-package-resource-cached.jar");
117:
118: Module fake = new Module(null, null, null, null) {
119: public List<File> getAllJars() {
120: throw new UnsupportedOperationException();
121: }
122:
123: public void setReloadable(boolean r) {
124: throw new UnsupportedOperationException();
125: }
126:
127: public void reload() throws IOException {
128: throw new UnsupportedOperationException();
129: }
130:
131: protected void classLoaderUp(Set<Module> parents)
132: throws IOException {
133: throw new UnsupportedOperationException();
134: }
135:
136: protected void classLoaderDown() {
137: throw new UnsupportedOperationException();
138: }
139:
140: protected void cleanup() {
141: throw new UnsupportedOperationException();
142: }
143:
144: protected void destroy() {
145: throw new UnsupportedOperationException(
146: "Not supported yet.");
147: }
148:
149: public boolean isFixed() {
150: throw new UnsupportedOperationException(
151: "Not supported yet.");
152: }
153:
154: public Object getLocalizedAttribute(String attr) {
155: throw new UnsupportedOperationException(
156: "Not supported yet.");
157: }
158:
159: public Manifest getManifest() {
160: try {
161: return new JarFile(jar, false).getManifest();
162: } catch (IOException ex) {
163: throw new AssertionFailedError(ex.getMessage());
164: }
165: }
166:
167: };
168:
169: JarClassLoader jcl = new JarClassLoader(Collections
170: .singletonList(jar), new ProxyClassLoader[0], false,
171: fake);
172:
173: assertStreamContent(jcl
174: .getResourceAsStream("package/resource.txt"), "content");
175: assertStreamContent(jcl
176: .getResourceAsStream("/package/resource.txt"),
177: "content");
178: assertStreamContent(jcl.getResourceAsStream("resource.txt"),
179: "content");
180: assertStreamContent(jcl.getResourceAsStream("/resource.txt"),
181: "content");
182:
183: assertURLsContent(jcl.getResources("package/resource.txt"),
184: "content");
185: assertURLsContent(jcl.getResources("/package/resource.txt"),
186: "content");
187: assertURLsContent(jcl.getResources("resource.txt"), "content");
188: assertURLsContent(jcl.getResources("/resource.txt"), "content");
189: }
190:
191: public void testCanLoadFromDefaultPackageDirs() throws Exception {
192: File dir = new File(dirs, "default-package-resource");
193: JarClassLoader jcl = new JarClassLoader(Collections
194: .singletonList(dir), new ProxyClassLoader[0]);
195:
196: assertStreamContent(jcl
197: .getResourceAsStream("package/resource.txt"), "content");
198: assertStreamContent(jcl
199: .getResourceAsStream("/package/resource.txt"),
200: "content");
201: assertStreamContent(jcl.getResourceAsStream("resource.txt"),
202: "content");
203: assertStreamContent(jcl.getResourceAsStream("/resource.txt"),
204: "content");
205: assertStreamContent(jcl
206: .getResourceAsStream("META-INF/services/resource.txt"),
207: "content");
208: assertStreamContent(
209: jcl
210: .getResourceAsStream("/META-INF/services/resource.txt"),
211: "content");
212:
213: assertURLsContent(jcl.getResources("package/resource.txt"),
214: "content");
215: assertURLsContent(jcl.getResources("/package/resource.txt"),
216: "content");
217: assertURLsContent(jcl.getResources("resource.txt"), "content");
218: assertURLsContent(jcl.getResources("/resource.txt"), "content");
219: }
220:
221: public void testFromNonExistentJAR() throws Exception {
222: File jar = new File(jars, "default-package-resource.jar");
223: File snd = new File(jars, "copy.jar");
224: FileInputStream is = new FileInputStream(jar);
225: FileOutputStream os = new FileOutputStream(snd);
226: FileUtil.copy(is, os);
227: is.close();
228: os.close();
229:
230: JarClassLoader jcl = new JarClassLoader(Collections
231: .singletonList(jar), new ProxyClassLoader[0]);
232: JarClassLoader.initializeCache();
233:
234: URL u = jcl.getResource("package/resource.txt");
235: //assertURLsContent(u, "content");
236:
237: URL n = new URL(u.toExternalForm().replaceAll(
238: "default-package-resource.jar", "copy.jar"));
239:
240: assertStreamContent(u.openStream(), "content");
241:
242: CharSequence log = Log.enable("org.netbeans.JarClassLoader",
243: Level.WARNING);
244: assertStreamContent(n.openStream(), "content");
245: if (log.toString().indexOf("Cannot find") == -1) {
246: fail("There should be a warning:\n" + log);
247: }
248:
249: CharSequence log2 = Log.enable("org.netbeans.JarClassLoader",
250: Level.WARNING);
251: assertStreamContent(n.openStream(), "content");
252: assertEquals("No second log:\n" + log2, -1, log2.toString()
253: .indexOf("Cannot find"));
254: }
255:
256: private void assertURLsContent(Enumeration<URL> urls,
257: String... contents) throws IOException {
258: for (String content : contents) {
259: assertTrue("Enough entries", urls.hasMoreElements());
260: assertStreamContent(urls.nextElement().openStream(),
261: content);
262: }
263: assertFalse("Too many entries", urls.hasMoreElements());
264: }
265:
266: private void assertStreamContent(InputStream str, String content)
267: throws IOException {
268: assertNotNull("Resource found", str);
269: byte[] data = new byte[content.length()];
270: DataInputStream dis = new DataInputStream(str);
271: try {
272: dis.readFully(data);
273: } finally {
274: dis.close();
275: }
276: assertEquals(new String(data), content);
277: }
278: }
|