PackageTest.java revision 2ad60cfc28e14ee8f0bb038720836a4696c478ad
1/* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17package org.apache.harmony.luni.tests.java.lang; 18 19import java.io.File; 20import java.lang.reflect.Method; 21import java.net.URL; 22import java.net.URLClassLoader; 23 24import tests.support.resource.Support_Resources; 25 26public class PackageTest extends junit.framework.TestCase { 27 28 private File resources; 29 30 private String resPath; 31 32 Package getTestPackage(String resourceJar, String className) 33 throws Exception { 34 Support_Resources.copyFile(resources, "Package", resourceJar); 35 URL resourceURL = new URL("file:/" + resPath + "/Package/" 36 + resourceJar); 37 38 URLClassLoader ucl = new URLClassLoader(new URL[] { resourceURL }, null); 39 return Class.forName(className, true, ucl).getPackage(); 40 } 41 42 @Override 43 protected void setUp() { 44 resources = Support_Resources.createTempFolder(); 45 resPath = resources.toString(); 46 if (resPath.charAt(0) == '/' || resPath.charAt(0) == '\\') 47 resPath = resPath.substring(1); 48 } 49 50 /** 51 * There is a newer version of this class with some actual tests but since 52 * the class is not implemented they all fail. For now use the stub test 53 * methods. 54 */ 55 56 /** 57 * @tests java.lang.Package#getImplementationVendor() 58 * @tests java.lang.Package#getImplementationVersion() 59 * @tests java.lang.Package#getSpecificationTitle() 60 * @tests java.lang.Package#getSpecificationVendor() 61 * @tests java.lang.Package#getSpecificationVersion() 62 * @tests java.lang.Package#getImplementationTitle() 63 */ 64 public void test_helper_Attributes() throws Exception { 65 66 Package p = getTestPackage("hyts_all_attributes.jar", "p.C"); 67 assertEquals( 68 "Package getImplementationTitle returns a wrong string (1)", 69 "p Implementation-Title", p.getImplementationTitle()); 70 assertEquals( 71 "Package getImplementationVendor returns a wrong string (1)", 72 "p Implementation-Vendor", p.getImplementationVendor()); 73 assertEquals( 74 "Package getImplementationVersion returns a wrong string (1)", 75 "2.2.2", p.getImplementationVersion()); 76 assertEquals( 77 "Package getSpecificationTitle returns a wrong string (1)", 78 "p Specification-Title", p.getSpecificationTitle()); 79 assertEquals( 80 "Package getSpecificationVendor returns a wrong string (1)", 81 "p Specification-Vendor", p.getSpecificationVendor()); 82 assertEquals( 83 "Package getSpecificationVersion returns a wrong string (1)", 84 "2.2.2", p.getSpecificationVersion()); 85 86 // No entry for the package 87 Package p2 = getTestPackage("hyts_no_entry.jar", "p.C"); 88 assertEquals( 89 "Package getImplementationTitle returns a wrong string (2)", 90 "MF Implementation-Title", p2.getImplementationTitle()); 91 assertEquals( 92 "Package getImplementationVendor returns a wrong string (2)", 93 "MF Implementation-Vendor", p2.getImplementationVendor()); 94 assertEquals( 95 "Package getImplementationVersion returns a wrong string (2)", 96 "5.3.b1", p2.getImplementationVersion()); 97 assertEquals( 98 "Package getSpecificationTitle returns a wrong string (2)", 99 "MF Specification-Title", p2.getSpecificationTitle()); 100 assertEquals( 101 "Package getSpecificationVendor returns a wrong string (2)", 102 "MF Specification-Vendor", p2.getSpecificationVendor()); 103 assertEquals( 104 "Package getSpecificationVersion returns a wrong string (2)", 105 "1.2.3", p2.getSpecificationVersion()); 106 107 // No attributes in the package entry 108 Package p3 = getTestPackage("hyts_no_attributes.jar", "p.C"); 109 assertEquals( 110 "Package getImplementationTitle returns a wrong string (3)", 111 "MF Implementation-Title", p3.getImplementationTitle()); 112 assertEquals( 113 "Package getImplementationVendor returns a wrong string (3)", 114 "MF Implementation-Vendor", p3.getImplementationVendor()); 115 assertEquals( 116 "Package getImplementationVersion returns a wrong string (3)", 117 "5.3.b1", p3.getImplementationVersion()); 118 assertEquals( 119 "Package getSpecificationTitle returns a wrong string (3)", 120 "MF Specification-Title", p3.getSpecificationTitle()); 121 assertEquals( 122 "Package getSpecificationVendor returns a wrong string (3)", 123 "MF Specification-Vendor", p3.getSpecificationVendor()); 124 assertEquals( 125 "Package getSpecificationVersion returns a wrong string (3)", 126 "1.2.3", p3.getSpecificationVersion()); 127 128 // Some attributes in the package entry 129 Package p4 = getTestPackage("hyts_some_attributes.jar", "p.C"); 130 assertEquals( 131 "Package getImplementationTitle returns a wrong string (4)", 132 "p Implementation-Title", p4.getImplementationTitle()); 133 assertEquals( 134 "Package getImplementationVendor returns a wrong string (4)", 135 "MF Implementation-Vendor", p4.getImplementationVendor()); 136 assertEquals( 137 "Package getImplementationVersion returns a wrong string (4)", 138 "2.2.2", p4.getImplementationVersion()); 139 assertEquals( 140 "Package getSpecificationTitle returns a wrong string (4)", 141 "MF Specification-Title", p4.getSpecificationTitle()); 142 assertEquals( 143 "Package getSpecificationVendor returns a wrong string (4)", 144 "p Specification-Vendor", p4.getSpecificationVendor()); 145 assertEquals( 146 "Package getSpecificationVersion returns a wrong string (4)", 147 "2.2.2", p4.getSpecificationVersion()); 148 149 // subdirectory Package 150 Package p5 = getTestPackage("hyts_pq.jar", "p.q.C"); 151 assertEquals( 152 "Package getImplementationTitle returns a wrong string (5)", 153 "p Implementation-Title", p5.getImplementationTitle()); 154 assertEquals( 155 "Package getImplementationVendor returns a wrong string (5)", 156 "p Implementation-Vendor", p5.getImplementationVendor()); 157 assertEquals( 158 "Package getImplementationVersion returns a wrong string (5)", 159 "1.1.3", p5.getImplementationVersion()); 160 assertEquals( 161 "Package getSpecificationTitle returns a wrong string (5)", 162 "p Specification-Title", p5.getSpecificationTitle()); 163 assertEquals( 164 "Package getSpecificationVendor returns a wrong string (5)", 165 "p Specification-Vendor", p5.getSpecificationVendor()); 166 assertEquals( 167 "Package getSpecificationVersion returns a wrong string (5)", 168 "2.2.0.0.0.0.0.0.0.0.0", p5.getSpecificationVersion()); 169 } 170 171 /** 172 * @tests java.lang.Package#getName() 173 */ 174 public void test_getName() throws Exception { 175 Package p = getTestPackage("hyts_pq.jar", "p.q.C"); 176 assertEquals("Package getName returns a wrong string", "p.q", p 177 .getName()); 178 } 179 180 /** 181 * @tests java.lang.Package#getPackage(java.lang.String) 182 */ 183 public void test_getPackageLjava_lang_String() { 184 assertSame("Package getPackage failed for java.lang", Package 185 .getPackage("java.lang"), Package.getPackage("java.lang")); 186 187 assertSame("Package getPackage failed for java.lang", Package 188 .getPackage("java.lang"), Object.class.getPackage()); 189 } 190 191 /** 192 * @tests java.lang.Package#getPackages() 193 */ 194 public void test_getPackages() throws Exception { 195 Package[] pckgs = Package.getPackages(); 196 boolean found = false; 197 for (int i = 0; i < pckgs.length; i++) { 198 if (pckgs[i].getName().equals("java.util")) { 199 found = true; 200 break; 201 } 202 } 203 assertTrue("Package getPackages failed to retrieve a package", found); 204 } 205 206 /** 207 * @tests java.lang.Package#hashCode() 208 */ 209 public void test_hashCode() { 210 Package p1 = Package.getPackage("java.lang"); 211 if (p1 != null) { 212 assertEquals(p1.hashCode(), "java.lang".hashCode()); 213 } 214 } 215 216 /** 217 * @tests java.lang.Package#isCompatibleWith(java.lang.String) 218 */ 219 public void test_isCompatibleWithLjava_lang_String() throws Exception { 220 Package p = getTestPackage("hyts_c.jar", "p.C"); 221 222 assertTrue("Package isCompatibleWith fails with lower version", p 223 .isCompatibleWith("2.1.9.9")); 224 assertTrue("Package isCompatibleWith fails with same version (1)", p 225 .isCompatibleWith("2.2.0")); 226 assertTrue("Package isCompatibleWith fails with same version (2)", p 227 .isCompatibleWith("2.2")); 228 assertFalse("Package isCompatibleWith fails with higher version", p 229 .isCompatibleWith("2.2.0.0.1")); 230 try { 231 p.isCompatibleWith(null); 232 fail("Null version is illegal"); 233 } catch (NumberFormatException ok) { 234 } catch (NullPointerException compatible) { 235 /* 236 * RI throws NPE instead of NFE... 237 */ 238 } 239 240 try { 241 p.isCompatibleWith(""); 242 fail("Empty version is illegal"); 243 } catch (NumberFormatException ok) {} 244 try { 245 p.isCompatibleWith("."); 246 fail("'.' version is illegal"); 247 } catch (NumberFormatException ok) {} 248 try { 249 p.isCompatibleWith("1.2."); 250 fail("'1.2.' version is illegal"); 251 } catch (NumberFormatException ok) {} 252 try { 253 p.isCompatibleWith(".9"); 254 fail("'.9' version is illegal"); 255 } catch (NumberFormatException ok) {} 256 try { 257 p.isCompatibleWith("2.4..5"); 258 fail("'2.4..5' version is illegal"); 259 } catch (NumberFormatException ok) {} 260 try { 261 p.isCompatibleWith("20.-4"); 262 fail("'20.-4' version is illegal"); 263 } catch (NumberFormatException ok) {} 264 } 265 266 /** 267 * @tests java.lang.Package#isSealed() 268 */ 269 public void test_isSealed() throws Exception { 270 Package p = getTestPackage("hyts_pq.jar", "p.q.C"); 271 assertTrue("Package isSealed returns wrong boolean", p.isSealed()); 272 } 273 274 /** 275 * @tests java.lang.Package#isSealed(java.net.URL) 276 */ 277 public void test_isSealedLjava_net_URL() throws Exception { 278 Package p = getTestPackage("hyts_c.jar", "p.C"); 279 assertFalse("Package isSealed returns wrong boolean (1)", p 280 .isSealed(new URL("file:/" + resPath + "/"))); 281 assertTrue("Package isSealed returns wrong boolean (2)", p 282 .isSealed(new URL("file:/" + resPath + "/Package/hyts_c.jar"))); 283 } 284 285 /** 286 * @tests java.lang.Package#toString() 287 */ 288 public void test_toString() throws Exception { 289 Package p = getTestPackage("hyts_c.jar", "p.C"); 290 assertTrue("Package toString returns wrong string", p.toString() 291 .length() > 0); 292 } 293} 294