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 ClassTestGetResourceAsStream 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 ClassTestGetResourceAsStream class is used to test the Core API
035: * java.lang.Class.getResourceAsStream method
036: *
037: */
038: public class ClassTestGetResourceAsStream extends TestCase {
039:
040: static String vendor = System.getProperty("java.vm.vendor");
041:
042: /**
043: * The method throws NullPointerException if argument is null
044: */
045: public void test1() {
046: try {
047: Void.class.getResourceAsStream(null);
048: fail("Error1: NullPointerException is not thrown for null argument");
049: } catch (NullPointerException _) {
050: }
051: }
052:
053: /**
054: * prepending by package name.
055: */
056: public void test2() {
057: byte magic[] = new byte[4];
058: try {
059: Void.class.getResourceAsStream("Class.class").read(magic);
060: //System.out.println(Integer.toString(0xff&magic[0], 16));
061: assertTrue("Error1", new String(magic).equals(new String(
062: new byte[] { (byte) 0xCA, (byte) 0xFE, (byte) 0xBA,
063: (byte) 0xBE })));
064: } catch (java.io.IOException e) {
065: fail("Unexpected Exception " + e);
066: }
067: }
068:
069: /**
070: * unchanging.
071: */
072: public void test3() {
073: byte magic[] = new byte[4];
074: try {
075: Void.class
076: .getResourceAsStream(
077: "/"
078: + Class.class.getPackage()
079: .getName()
080: .replace('.', '/')
081: + "/Class.class").read(magic);
082: assertTrue("Error1", new String(magic).equals(new String(
083: new byte[] { (byte) 0xCA, (byte) 0xFE, (byte) 0xBA,
084: (byte) 0xBE })));
085: } catch (java.io.IOException e) {
086: fail("Unexpected Exception " + e);
087: }
088: }
089:
090: /**
091: * in java.ext.dirs.
092: */
093: public void test4() {
094: String as[] = System.getProperty("java.ext.dirs").split(
095: System.getProperty("path.separator"));
096: for (int i = 0; i < as.length; i++) {
097: File dir = new File(as[i]);
098: if (dir.exists() && dir.isDirectory()) {
099: String afn[] = dir.list();
100: File aff[] = dir.listFiles();
101: for (int j = 0; j < aff.length; j++) {
102: if (aff[j].isFile()
103: && aff[j].getName().endsWith(".jar")) {
104: try {
105: java.util.jar.JarFile jf = new java.util.jar.JarFile(
106: aff[j]);
107: for (java.util.Enumeration e = jf.entries(); e
108: .hasMoreElements();) {
109: String s = e.nextElement().toString();
110: if (s.endsWith(".class")) {
111: assertTrue("Error1", Void.class
112: .getResourceAsStream(
113: "/" + s)
114: .available() >= 0);
115: return;
116: }
117: }
118: } catch (java.io.IOException e) {
119: fail("Unexpected Exception " + e);
120: }
121: }
122: }
123: }
124: }
125: }
126:
127: /**
128: * in java.class.path.
129: */
130: public void test5() {
131: String as[] = System.getProperty("java.class.path").split(
132: System.getProperty("path.separator"));
133: for (int i = 0; i < as.length; i++) {
134: File f = new File(as[i]);
135: if (f.exists() && f.isDirectory()) {
136: String afn[] = f.list();
137: File aff[] = f.listFiles();
138: for (int j = 0; j < aff.length; j++) {
139: if (aff[j].isFile()) {
140: try {
141: assertTrue("Error1", Void.class
142: .getResourceAsStream("/" + afn[j])
143: .available() >= 0);
144: } catch (java.io.IOException e1) {
145: fail("Unexpected Exception 1 " + e1);
146: }
147: return;
148: }
149: }
150: } else if (f.exists() && f.isFile()
151: && f.getName().endsWith(".jar")) {
152: try {
153: java.util.jar.JarFile jf = new java.util.jar.JarFile(
154: f);
155: for (java.util.Enumeration e = jf.entries(); e
156: .hasMoreElements();) {
157: String s = e.nextElement().toString();
158: if (s.endsWith(".class")) {
159: byte magic[] = new byte[4];
160: try {
161: Void.class.getResourceAsStream("/" + s)
162: .read(magic);
163: assertTrue("Error2", new String(magic)
164: .equals(new String(new byte[] {
165: (byte) 0xCA,
166: (byte) 0xFE,
167: (byte) 0xBA,
168: (byte) 0xBE })));
169: } catch (java.io.IOException e2) {
170: fail("Unexpected Exception 2 " + e2);
171: }
172: return;
173: }
174: }
175: } catch (java.io.IOException e) {
176: fail("Unexpected Exception 3 " + e);
177: }
178: }
179: }
180: }
181:
182: /**
183: * via -Xbootclasspath (vm.boot.class.path).
184: */
185: public void test6() {
186: String as[] = System.getProperty(
187: (vendor.equals("Apache Software Foundation") ? "vm"
188: : "sun")
189: + ".boot.class.path").split(
190: System.getProperty("path.separator"));
191: for (int i = 0; i < as.length; i++) {
192: File f = new File(as[i]);
193: if (f.exists() && f.isFile()
194: && f.getName().endsWith(".jar")) {
195: try {
196: java.util.jar.JarFile jf = new java.util.jar.JarFile(
197: f);
198: for (java.util.Enumeration e = jf.entries(); e
199: .hasMoreElements();) {
200: String s = e.nextElement().toString();
201: if (s.endsWith(".class")) {
202: byte magic[] = new byte[4];
203: try {
204: Void.class.getResourceAsStream("/" + s)
205: .read(magic);
206: assertTrue("Error1", new String(magic)
207: .equals(new String(new byte[] {
208: (byte) 0xCA,
209: (byte) 0xFE,
210: (byte) 0xBA,
211: (byte) 0xBE })));
212: } catch (java.io.IOException e1) {
213: fail("Unexpected Exception 1 " + e1);
214: }
215: return;
216: }
217: }
218: } catch (java.io.IOException e2) {
219: fail("Unexpected Exception 2 " + e2);
220: }
221: } else if (f.exists() && f.isDirectory()) {
222: String afn[] = f.list();
223: File aff[] = f.listFiles();
224: for (int j = 0; j < aff.length; j++) {
225: if (aff[j].isFile()) {
226: try {
227: assertTrue("Error2", Void.class
228: .getResourceAsStream("/" + afn[j])
229: .available() >= 0);
230: } catch (java.io.IOException e) {
231: fail("Unexpected Exception 3 " + e);
232: }
233: return;
234: }
235: }
236: }
237: }
238: }
239: }
|