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: /**
019: * @author Serguei S.Zapreyev
020: * @version $Revision$
021: *
022: * This ClassTestGetResource class ("Software") is furnished under license and
023: * may only be used or copied in accordance with the terms of that license.
024: *
025: */package java.lang;
026:
027: import java.io.File;
028:
029: import junit.framework.TestCase;
030:
031: /*
032: * Created on 03.02.2006
033: *
034: * This ClassTestGetResource class is used to test the Core API
035: * java.lang.Class.getResource method
036: *
037: */
038: public class ClassTestGetResource extends TestCase {
039:
040: static String vendor = System.getProperty("java.vm.vendor");
041:
042: /**
043: * prepending by package name.
044: */
045: public void test1() {
046: assertTrue("Error1: unexpected:"
047: + Void.class.getResource("Class.class").toString(),
048: Void.class.getResource("Class.class").toString()
049: .indexOf("Class.class") != -1);
050: }
051:
052: /**
053: * unchanging.
054: */
055: public void test2() {
056: assertTrue("Error1", Void.class.getResource(
057: "/"
058: + Class.class.getPackage().getName().replace(
059: '.', '/') + "/Class.class").toString()
060: .indexOf("Class.class") != -1);
061: }
062:
063: /**
064: * in java.ext.dirs.
065: */
066: public void test3() {
067: String as[] = System.getProperty("java.ext.dirs").split(
068: System.getProperty("path.separator"));
069: for (int i = 0; i < as.length; i++) {
070: File dir = new File(as[i]);
071: if (dir.exists() && dir.isDirectory()) {
072: String afn[] = dir.list();
073: File aff[] = dir.listFiles();
074: for (int j = 0; j < aff.length; j++) {
075: if (aff[j].isFile()
076: && aff[j].getName().endsWith(".jar")) {
077: aff[j].getName();
078: try {
079: java.util.jar.JarFile jf = new java.util.jar.JarFile(
080: aff[j]);
081: for (java.util.Enumeration e = jf.entries(); e
082: .hasMoreElements();) {
083: String s = e.nextElement().toString();
084: if (s.endsWith(".class")) {
085: String resource = Void.class
086: .getResource("/" + s)
087: .toString();
088: assertTrue(
089: "Error1",
090: (resource.indexOf(s) != -1)
091: && (resource
092: .indexOf("jar:file:") != -1));
093: return;
094: }
095: }
096: } catch (java.io.IOException e) {
097: fail("Unexpected Exception " + e);
098: }
099: }
100: }
101: }
102: }
103: }
104:
105: /**
106: * in java.class.path.
107: */
108: public void test4() {
109: String as[] = System.getProperty("java.class.path").split(
110: System.getProperty("path.separator"));
111: for (int i = 0; i < as.length; i++) {
112: File f = new File(as[i]);
113: if (f.exists() && f.isDirectory()) {
114: String afn[] = f.list();
115: File aff[] = f.listFiles();
116: for (int j = 0; j < aff.length; j++) {
117: if (aff[j].isFile()) {
118: String resource = Void.class.getResource(
119: "/" + afn[j]).toString();
120: assertTrue("Error1", (resource.toString()
121: .indexOf(afn[j]) != -1)
122: && (resource.toString()
123: .indexOf("file:") != -1));
124: return;
125: }
126: }
127: } else if (f.exists() && f.isFile()
128: && f.getName().endsWith(".jar")) {
129: try {
130: java.util.jar.JarFile jf = new java.util.jar.JarFile(
131: f);
132: for (java.util.Enumeration e = jf.entries(); e
133: .hasMoreElements();) {
134: String s = e.nextElement().toString();
135: if (s.endsWith(".class")) {
136: String resource = Void.class.getResource(
137: "/" + s).toString();
138: assertTrue(
139: "Error2",
140: (resource.indexOf(s) != -1)
141: && (resource
142: .indexOf("jar:file:") != -1));
143: return;
144: }
145: }
146: } catch (java.io.IOException e) {
147: fail("Unexpected Exception " + e);
148: }
149: }
150: }
151: }
152:
153: /**
154: * via -Xbootclasspath (vm.boot.class.path).
155: */
156: public void test5() {
157: String as[] = System.getProperty(
158: (vendor.equals("Apache Software Foundation") ? "vm"
159: : "sun")
160: + ".boot.class.path").split(
161: System.getProperty("path.separator"));
162: for (int i = 0; i < as.length; i++) {
163: File f = new File(as[i]);
164: if (f.exists() && f.isFile()
165: && f.getName().endsWith(".jar")) {
166: try {
167: java.util.jar.JarFile jf = new java.util.jar.JarFile(
168: f);
169: for (java.util.Enumeration e = jf.entries(); e
170: .hasMoreElements();) {
171: String s = e.nextElement().toString();
172: if (s.endsWith(".class")) {
173: String resource = Void.class.getResource(
174: "/" + s).toString();
175: assertTrue(
176: "Error1",
177: (resource.indexOf(s) != -1)
178: && (resource
179: .indexOf("jar:file:") != -1));
180: return;
181: }
182: }
183: } catch (java.io.IOException e) {
184: fail("Unexpected Exception " + e);
185: }
186: } else if (f.exists() && f.isDirectory()) {
187: String afn[] = f.list();
188: File aff[] = f.listFiles();
189: for (int j = 0; j < aff.length; j++) {
190: if (aff[j].isFile()) {
191: String resource = Void.class.getResource(
192: "/" + afn[j]).toString();
193: assertTrue(
194: "Error2",
195: (resource.indexOf(afn[j]) != -1)
196: && (resource.indexOf("file") != -1));
197: return;
198: }
199: }
200: }
201: }
202: }
203:
204: /**
205: * The method throws NullPointerException if argument is null
206: */
207: public void test6() {
208: try {
209: Void.class.getResource(null);
210: fail("Error1: NullPointerException is not thrown for null argument");
211: } catch (NullPointerException _) {
212: }
213: }
214: }
|