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: package org.apache.harmony.luni.tests.java.lang;
018:
019: import java.io.File;
020: import java.lang.reflect.Method;
021: import java.net.URL;
022: import java.net.URLClassLoader;
023:
024: import tests.support.resource.Support_Resources;
025:
026: public class PackageTest extends junit.framework.TestCase {
027:
028: private File resources;
029:
030: private String resPath;
031:
032: Package getTestPackage(String resourceJar, String className)
033: throws Exception {
034: Support_Resources.copyFile(resources, "Package", resourceJar);
035: URL resourceURL = new URL("file:/" + resPath + "/Package/"
036: + resourceJar);
037:
038: URLClassLoader ucl = new URLClassLoader(
039: new URL[] { resourceURL }, null);
040: return Class.forName(className, true, ucl).getPackage();
041: }
042:
043: @Override
044: protected void setUp() {
045: resources = Support_Resources.createTempFolder();
046: resPath = resources.toString();
047: if (resPath.charAt(0) == '/' || resPath.charAt(0) == '\\')
048: resPath = resPath.substring(1);
049: }
050:
051: /**
052: * There is a newer version of this class with some actual tests but since
053: * the class is not implemented they all fail. For now use the stub test
054: * methods.
055: */
056:
057: /**
058: * @tests java.lang.Package#getImplementationVendor()
059: * @tests java.lang.Package#getImplementationVersion()
060: * @tests java.lang.Package#getSpecificationTitle()
061: * @tests java.lang.Package#getSpecificationVendor()
062: * @tests java.lang.Package#getSpecificationVersion()
063: * @tests java.lang.Package#getImplementationTitle()
064: */
065: public void test_helper_Attributes() throws Exception {
066:
067: Package p = getTestPackage("hyts_all_attributes.jar", "p.C");
068: assertEquals(
069: "Package getImplementationTitle returns a wrong string (1)",
070: "p Implementation-Title", p.getImplementationTitle());
071: assertEquals(
072: "Package getImplementationVendor returns a wrong string (1)",
073: "p Implementation-Vendor", p.getImplementationVendor());
074: assertEquals(
075: "Package getImplementationVersion returns a wrong string (1)",
076: "2.2.2", p.getImplementationVersion());
077: assertEquals(
078: "Package getSpecificationTitle returns a wrong string (1)",
079: "p Specification-Title", p.getSpecificationTitle());
080: assertEquals(
081: "Package getSpecificationVendor returns a wrong string (1)",
082: "p Specification-Vendor", p.getSpecificationVendor());
083: assertEquals(
084: "Package getSpecificationVersion returns a wrong string (1)",
085: "2.2.2", p.getSpecificationVersion());
086:
087: // No entry for the package
088: Package p2 = getTestPackage("hyts_no_entry.jar", "p.C");
089: assertEquals(
090: "Package getImplementationTitle returns a wrong string (2)",
091: "MF Implementation-Title", p2.getImplementationTitle());
092: assertEquals(
093: "Package getImplementationVendor returns a wrong string (2)",
094: "MF Implementation-Vendor", p2
095: .getImplementationVendor());
096: assertEquals(
097: "Package getImplementationVersion returns a wrong string (2)",
098: "5.3.b1", p2.getImplementationVersion());
099: assertEquals(
100: "Package getSpecificationTitle returns a wrong string (2)",
101: "MF Specification-Title", p2.getSpecificationTitle());
102: assertEquals(
103: "Package getSpecificationVendor returns a wrong string (2)",
104: "MF Specification-Vendor", p2.getSpecificationVendor());
105: assertEquals(
106: "Package getSpecificationVersion returns a wrong string (2)",
107: "1.2.3", p2.getSpecificationVersion());
108:
109: // No attributes in the package entry
110: Package p3 = getTestPackage("hyts_no_attributes.jar", "p.C");
111: assertEquals(
112: "Package getImplementationTitle returns a wrong string (3)",
113: "MF Implementation-Title", p3.getImplementationTitle());
114: assertEquals(
115: "Package getImplementationVendor returns a wrong string (3)",
116: "MF Implementation-Vendor", p3
117: .getImplementationVendor());
118: assertEquals(
119: "Package getImplementationVersion returns a wrong string (3)",
120: "5.3.b1", p3.getImplementationVersion());
121: assertEquals(
122: "Package getSpecificationTitle returns a wrong string (3)",
123: "MF Specification-Title", p3.getSpecificationTitle());
124: assertEquals(
125: "Package getSpecificationVendor returns a wrong string (3)",
126: "MF Specification-Vendor", p3.getSpecificationVendor());
127: assertEquals(
128: "Package getSpecificationVersion returns a wrong string (3)",
129: "1.2.3", p3.getSpecificationVersion());
130:
131: // Some attributes in the package entry
132: Package p4 = getTestPackage("hyts_some_attributes.jar", "p.C");
133: assertEquals(
134: "Package getImplementationTitle returns a wrong string (4)",
135: "p Implementation-Title", p4.getImplementationTitle());
136: assertEquals(
137: "Package getImplementationVendor returns a wrong string (4)",
138: "MF Implementation-Vendor", p4
139: .getImplementationVendor());
140: assertEquals(
141: "Package getImplementationVersion returns a wrong string (4)",
142: "2.2.2", p4.getImplementationVersion());
143: assertEquals(
144: "Package getSpecificationTitle returns a wrong string (4)",
145: "MF Specification-Title", p4.getSpecificationTitle());
146: assertEquals(
147: "Package getSpecificationVendor returns a wrong string (4)",
148: "p Specification-Vendor", p4.getSpecificationVendor());
149: assertEquals(
150: "Package getSpecificationVersion returns a wrong string (4)",
151: "2.2.2", p4.getSpecificationVersion());
152:
153: // subdirectory Package
154: Package p5 = getTestPackage("hyts_pq.jar", "p.q.C");
155: assertEquals(
156: "Package getImplementationTitle returns a wrong string (5)",
157: "p Implementation-Title", p5.getImplementationTitle());
158: assertEquals(
159: "Package getImplementationVendor returns a wrong string (5)",
160: "p Implementation-Vendor", p5.getImplementationVendor());
161: assertEquals(
162: "Package getImplementationVersion returns a wrong string (5)",
163: "1.1.3", p5.getImplementationVersion());
164: assertEquals(
165: "Package getSpecificationTitle returns a wrong string (5)",
166: "p Specification-Title", p5.getSpecificationTitle());
167: assertEquals(
168: "Package getSpecificationVendor returns a wrong string (5)",
169: "p Specification-Vendor", p5.getSpecificationVendor());
170: assertEquals(
171: "Package getSpecificationVersion returns a wrong string (5)",
172: "2.2.0.0.0.0.0.0.0.0.0", p5.getSpecificationVersion());
173: }
174:
175: /**
176: * @tests java.lang.Package#getName()
177: */
178: public void test_getName() throws Exception {
179: Package p = getTestPackage("hyts_pq.jar", "p.q.C");
180: assertEquals("Package getName returns a wrong string", "p.q", p
181: .getName());
182: }
183:
184: /**
185: * @tests java.lang.Package#getPackage(java.lang.String)
186: */
187: public void test_getPackageLjava_lang_String() {
188: assertSame("Package getPackage failed for java.lang", Package
189: .getPackage("java.lang"), Package
190: .getPackage("java.lang"));
191:
192: assertSame("Package getPackage failed for java.lang", Package
193: .getPackage("java.lang"), Object.class.getPackage());
194: }
195:
196: /**
197: * @tests java.lang.Package#getPackages()
198: */
199: public void test_getPackages() throws Exception {
200: Package[] pckgs = Package.getPackages();
201: boolean found = false;
202: for (int i = 0; i < pckgs.length; i++) {
203: if (pckgs[i].getName().equals("java.util")) {
204: found = true;
205: break;
206: }
207: }
208: assertTrue("Package getPackages failed to retrieve a package",
209: found);
210: }
211:
212: /**
213: * @tests java.lang.Package#hashCode()
214: */
215: public void test_hashCode() {
216: Package p1 = Package.getPackage("java.lang");
217: if (p1 != null) {
218: assertEquals(p1.hashCode(), "java.lang".hashCode());
219: }
220: }
221:
222: /**
223: * @tests java.lang.Package#isCompatibleWith(java.lang.String)
224: */
225: public void test_isCompatibleWithLjava_lang_String()
226: throws Exception {
227: Package p = getTestPackage("hyts_c.jar", "p.C");
228:
229: assertTrue("Package isCompatibleWith fails with lower version",
230: p.isCompatibleWith("2.1.9.9"));
231: assertTrue(
232: "Package isCompatibleWith fails with same version (1)",
233: p.isCompatibleWith("2.2.0"));
234: assertTrue(
235: "Package isCompatibleWith fails with same version (2)",
236: p.isCompatibleWith("2.2"));
237: assertFalse(
238: "Package isCompatibleWith fails with higher version", p
239: .isCompatibleWith("2.2.0.0.1"));
240: try {
241: p.isCompatibleWith(null);
242: fail("Null version is illegal");
243: } catch (NumberFormatException ok) {
244: } catch (NullPointerException compatible) {
245: /*
246: * RI throws NPE instead of NFE...
247: */
248: }
249:
250: try {
251: p.isCompatibleWith("");
252: fail("Empty version is illegal");
253: } catch (NumberFormatException ok) {
254: }
255: try {
256: p.isCompatibleWith(".");
257: fail("'.' version is illegal");
258: } catch (NumberFormatException ok) {
259: }
260: try {
261: p.isCompatibleWith("1.2.");
262: fail("'1.2.' version is illegal");
263: } catch (NumberFormatException ok) {
264: }
265: try {
266: p.isCompatibleWith(".9");
267: fail("'.9' version is illegal");
268: } catch (NumberFormatException ok) {
269: }
270: try {
271: p.isCompatibleWith("2.4..5");
272: fail("'2.4..5' version is illegal");
273: } catch (NumberFormatException ok) {
274: }
275: try {
276: p.isCompatibleWith("20.-4");
277: fail("'20.-4' version is illegal");
278: } catch (NumberFormatException ok) {
279: }
280: }
281:
282: /**
283: * @tests java.lang.Package#isSealed()
284: */
285: public void test_isSealed() throws Exception {
286: Package p = getTestPackage("hyts_pq.jar", "p.q.C");
287: assertTrue("Package isSealed returns wrong boolean", p
288: .isSealed());
289: }
290:
291: /**
292: * @tests java.lang.Package#isSealed(java.net.URL)
293: */
294: public void test_isSealedLjava_net_URL() throws Exception {
295: Package p = getTestPackage("hyts_c.jar", "p.C");
296: assertFalse("Package isSealed returns wrong boolean (1)", p
297: .isSealed(new URL("file:/" + resPath + "/")));
298: assertTrue("Package isSealed returns wrong boolean (2)", p
299: .isSealed(new URL("file:/" + resPath
300: + "/Package/hyts_c.jar")));
301: }
302:
303: /**
304: * @tests java.lang.Package#toString()
305: */
306: public void test_toString() throws Exception {
307: Package p = getTestPackage("hyts_c.jar", "p.C");
308: assertTrue("Package toString returns wrong string", p
309: .toString().length() > 0);
310: }
311: }
|