PackageTest.java revision 3b5fe2883109da9470a8baf20e6700aba0d78193
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 dalvik.annotation.KnownFailure;
20import dalvik.annotation.TestTargets;
21import dalvik.annotation.TestLevel;
22import dalvik.annotation.TestTargetNew;
23import dalvik.annotation.TestTargetClass;
24
25import java.io.File;
26import java.net.URL;
27import java.net.URLClassLoader;
28import java.lang.annotation.Annotation;
29
30import java.lang.annotation.Annotation;
31
32import tests.support.Support_ClassLoader;
33import tests.support.resource.Support_Resources;
34
35@TestTargetClass(Package.class)
36public class PackageTest extends junit.framework.TestCase {
37
38    private File resources;
39
40    private String resPath;
41
42    Class clazz;
43
44    Package getTestPackage(String resourceJar, String className)
45            throws Exception {
46
47        if ("Dalvik".equals(System.getProperty("java.vm.name"))) {
48            resourceJar = resourceJar.substring(0, resourceJar.indexOf(".")) +
49                                                                    "_dex.jar";
50        }
51        Support_Resources.copyFile(resources, "Package", resourceJar);
52        URL resourceURL = new URL("file:/" + resPath + "/Package/"
53                + resourceJar);
54
55        ClassLoader cl = Support_ClassLoader.getInstance(resourceURL,
56                getClass().getClassLoader());
57
58       clazz = cl.loadClass(className);
59       return clazz.getPackage();
60    }
61
62    @Override
63    protected void setUp() {
64        resources = Support_Resources.createTempFolder();
65        resPath = resources.toString();
66        if (resPath.charAt(0) == '/' || resPath.charAt(0) == '\\')
67            resPath = resPath.substring(1);
68    }
69
70    /**
71     * There is a newer version of this class with some actual tests but since
72     * the class is not implemented they all fail. For now use the stub test
73     * methods.
74     */
75
76    /**
77     * @tests java.lang.Package#getImplementationVendor()
78     * @tests java.lang.Package#getImplementationVersion()
79     * @tests java.lang.Package#getSpecificationTitle()
80     * @tests java.lang.Package#getSpecificationVendor()
81     * @tests java.lang.Package#getSpecificationVersion()
82     * @tests java.lang.Package#getImplementationTitle()
83     */
84    @TestTargets({
85        @TestTargetNew(
86            level = TestLevel.COMPLETE,
87            notes = "",
88            method = "getImplementationTitle",
89            args = {}
90        ),
91        @TestTargetNew(
92            level = TestLevel.COMPLETE,
93            notes = "",
94            method = "getImplementationVendor",
95            args = {}
96        ),
97        @TestTargetNew(
98            level = TestLevel.COMPLETE,
99            notes = "",
100            method = "getImplementationVersion",
101            args = {}
102        ),
103        @TestTargetNew(
104            level = TestLevel.COMPLETE,
105            notes = "",
106            method = "getSpecificationTitle",
107            args = {}
108        ),
109        @TestTargetNew(
110            level = TestLevel.COMPLETE,
111            notes = "",
112            method = "getSpecificationVendor",
113            args = {}
114        ),
115        @TestTargetNew(
116            level = TestLevel.COMPLETE,
117            notes = "",
118            method = "getSpecificationVersion",
119            args = {}
120        )
121    })
122    @KnownFailure("get methods don't work.")
123    public void test_helper_Attributes() throws Exception {
124
125        Package p = getTestPackage("hyts_all_attributes.jar", "p.C");
126        assertEquals(
127                "Package getImplementationTitle returns a wrong string (1)",
128                "p Implementation-Title", p.getImplementationTitle());
129        assertEquals(
130                "Package getImplementationVendor returns a wrong string (1)",
131                "p Implementation-Vendor", p.getImplementationVendor());
132        assertEquals(
133                "Package getImplementationVersion returns a wrong string (1)",
134                "2.2.2", p.getImplementationVersion());
135        assertEquals(
136                "Package getSpecificationTitle returns a wrong string (1)",
137                "p Specification-Title", p.getSpecificationTitle());
138        assertEquals(
139                "Package getSpecificationVendor returns a wrong string (1)",
140                "p Specification-Vendor", p.getSpecificationVendor());
141        assertEquals(
142                "Package getSpecificationVersion returns a wrong string (1)",
143                "2.2.2", p.getSpecificationVersion());
144
145        // No entry for the package
146        Package p2 = getTestPackage("hyts_no_entry.jar", "p.C");
147        assertEquals(
148                "Package getImplementationTitle returns a wrong string (2)",
149                "MF Implementation-Title", p2.getImplementationTitle());
150        assertEquals(
151                "Package getImplementationVendor returns a wrong string (2)",
152                "MF Implementation-Vendor", p2.getImplementationVendor());
153        assertEquals(
154                "Package getImplementationVersion returns a wrong string (2)",
155                "5.3.b1", p2.getImplementationVersion());
156        assertEquals(
157                "Package getSpecificationTitle returns a wrong string (2)",
158                "MF Specification-Title", p2.getSpecificationTitle());
159        assertEquals(
160                "Package getSpecificationVendor returns a wrong string (2)",
161                "MF Specification-Vendor", p2.getSpecificationVendor());
162        assertEquals(
163                "Package getSpecificationVersion returns a wrong string (2)",
164                "1.2.3", p2.getSpecificationVersion());
165
166        // No attributes in the package entry
167        Package p3 = getTestPackage("hyts_no_attributes.jar", "p.C");
168        assertEquals(
169                "Package getImplementationTitle returns a wrong string (3)",
170                "MF Implementation-Title", p3.getImplementationTitle());
171        assertEquals(
172                "Package getImplementationVendor returns a wrong string (3)",
173                "MF Implementation-Vendor", p3.getImplementationVendor());
174        assertEquals(
175                "Package getImplementationVersion returns a wrong string (3)",
176                "5.3.b1", p3.getImplementationVersion());
177        assertEquals(
178                "Package getSpecificationTitle returns a wrong string (3)",
179                "MF Specification-Title", p3.getSpecificationTitle());
180        assertEquals(
181                "Package getSpecificationVendor returns a wrong string (3)",
182                "MF Specification-Vendor", p3.getSpecificationVendor());
183        assertEquals(
184                "Package getSpecificationVersion returns a wrong string (3)",
185                "1.2.3", p3.getSpecificationVersion());
186
187        // Some attributes in the package entry
188        Package p4 = getTestPackage("hyts_some_attributes.jar", "p.C");
189        assertEquals(
190                "Package getImplementationTitle returns a wrong string (4)",
191                "p Implementation-Title", p4.getImplementationTitle());
192        assertEquals(
193                "Package getImplementationVendor returns a wrong string (4)",
194                "MF Implementation-Vendor", p4.getImplementationVendor());
195        assertEquals(
196                "Package getImplementationVersion returns a wrong string (4)",
197                "2.2.2", p4.getImplementationVersion());
198        assertEquals(
199                "Package getSpecificationTitle returns a wrong string (4)",
200                "MF Specification-Title", p4.getSpecificationTitle());
201        assertEquals(
202                "Package getSpecificationVendor returns a wrong string (4)",
203                "p Specification-Vendor", p4.getSpecificationVendor());
204        assertEquals(
205                "Package getSpecificationVersion returns a wrong string (4)",
206                "2.2.2", p4.getSpecificationVersion());
207
208        // subdirectory Package
209        Package p5 = getTestPackage("hyts_pq.jar", "p.q.C");
210        assertEquals(
211                "Package getImplementationTitle returns a wrong string (5)",
212                "p Implementation-Title", p5.getImplementationTitle());
213        assertEquals(
214                "Package getImplementationVendor returns a wrong string (5)",
215                "p Implementation-Vendor", p5.getImplementationVendor());
216        assertEquals(
217                "Package getImplementationVersion returns a wrong string (5)",
218                "1.1.3", p5.getImplementationVersion());
219        assertEquals(
220                "Package getSpecificationTitle returns a wrong string (5)",
221                "p Specification-Title", p5.getSpecificationTitle());
222        assertEquals(
223                "Package getSpecificationVendor returns a wrong string (5)",
224                "p Specification-Vendor", p5.getSpecificationVendor());
225        assertEquals(
226                "Package getSpecificationVersion returns a wrong string (5)",
227                "2.2.0.0.0.0.0.0.0.0.0", p5.getSpecificationVersion());
228    }
229
230    /**
231     * @tests java.lang.Package#getName()
232     */
233    @TestTargetNew(
234        level = TestLevel.COMPLETE,
235        notes = "",
236        method = "getName",
237        args = {}
238    )
239    public void test_getName() throws Exception {
240        Package p = getTestPackage("hyts_pq.jar", "p.q.C");
241        assertEquals("Package getName returns a wrong string", "p.q", p
242                .getName());
243    }
244
245    /**
246     * @tests java.lang.Package#getPackage(java.lang.String)
247     */
248    @TestTargetNew(
249        level = TestLevel.COMPLETE,
250        notes = "",
251        method = "getPackage",
252        args = {java.lang.String.class}
253    )
254    @KnownFailure("Real package information missing on android.")
255    public void test_getPackageLjava_lang_String() throws Exception {
256        assertSame("Package getPackage failed for java.lang", Package
257                .getPackage("java.lang"), Package.getPackage("java.lang"));
258
259        assertSame("Package getPackage failed for java.lang", Package
260                .getPackage("java.lang"), Object.class.getPackage());
261
262        Package p = getTestPackage("hyts_package.jar", "C");
263        assertNull("getPackage should return null.", p);
264    }
265
266    /**
267     * @tests java.lang.Package#getPackages()
268     */
269    @TestTargetNew(
270        level = TestLevel.COMPLETE,
271        notes = "",
272        method = "getPackages",
273        args = {}
274    )
275    @KnownFailure("Package information missing on android")
276    public void test_getPackages() throws Exception {
277        Package[] pckgs = Package.getPackages();
278        boolean found = false;
279        for (int i = 0; i < pckgs.length; i++) {
280            if (pckgs[i].getName().equals("java.util")) {
281                found = true;
282                break;
283            }
284        }
285        assertTrue("Package getPackages failed to retrieve a package", found);
286    }
287
288    /**
289     * @tests java.lang.Package#hashCode()
290     */
291    @TestTargetNew(
292        level = TestLevel.COMPLETE,
293        notes = "",
294        method = "hashCode",
295        args = {}
296    )
297    public void test_hashCode() {
298        Package p1 = Package.getPackage("java.lang");
299        if (p1 != null) {
300            assertEquals(p1.hashCode(), "java.lang".hashCode());
301        }
302    }
303
304    /**
305     * @tests java.lang.Package#isCompatibleWith(java.lang.String)
306     */
307    @TestTargetNew(
308        level = TestLevel.COMPLETE,
309        notes = "",
310        method = "isCompatibleWith",
311        args = {java.lang.String.class}
312    )
313    @KnownFailure("isCompatibleWith returns incorrect value.")
314    public void test_isCompatibleWithLjava_lang_String() throws Exception {
315        Package p = getTestPackage("hyts_c.jar", "p.C");
316
317        assertTrue("Package isCompatibleWith fails with lower version", p
318                .isCompatibleWith("2.1.9.9"));
319        assertTrue("Package isCompatibleWith fails with same version (1)", p
320                .isCompatibleWith("2.2.0"));
321        assertTrue("Package isCompatibleWith fails with same version (2)", p
322                .isCompatibleWith("2.2"));
323        assertFalse("Package isCompatibleWith fails with higher version", p
324                .isCompatibleWith("2.2.0.0.1"));
325        try {
326            p.isCompatibleWith(null);
327            fail("Null version is illegal");
328        } catch (NumberFormatException ok) {
329        } catch (NullPointerException compatible) {
330            /*
331             * RI throws NPE instead of NFE...
332             */
333        }
334
335        try {
336            p.isCompatibleWith("");
337            fail("Empty version is illegal");
338        } catch (NumberFormatException ok) {}
339        try {
340            p.isCompatibleWith(".");
341            fail("'.' version is illegal");
342        } catch (NumberFormatException ok) {}
343        try {
344            p.isCompatibleWith("1.2.");
345            fail("'1.2.' version is illegal");
346        } catch (NumberFormatException ok) {}
347        try {
348            p.isCompatibleWith(".9");
349            fail("'.9' version is illegal");
350        } catch (NumberFormatException ok) {}
351        try {
352            p.isCompatibleWith("2.4..5");
353            fail("'2.4..5' version is illegal");
354        } catch (NumberFormatException ok) {}
355        try {
356            p.isCompatibleWith("20.-4");
357            fail("'20.-4' version is illegal");
358        } catch (NumberFormatException ok) {}
359    }
360
361    /**
362     * @tests java.lang.Package#isSealed()
363     */
364    @TestTargetNew(
365        level = TestLevel.COMPLETE,
366        notes = "",
367        method = "isSealed",
368        args = {}
369    )
370    @KnownFailure("isSealed method returns false for sealed package.")
371    public void test_isSealed() throws Exception {
372        Package p = getTestPackage("hyts_pq.jar", "p.q.C");
373        assertTrue("Package isSealed returns wrong boolean", p.isSealed());
374
375        p = String.class.getPackage();
376        assertFalse("Package isSealed returns wrong boolean", p.isSealed());
377    }
378
379    /**
380     * @tests java.lang.Package#isSealed(java.net.URL)
381     */
382    @TestTargetNew(
383        level = TestLevel.COMPLETE,
384        notes = "",
385        method = "isSealed",
386        args = {java.net.URL.class}
387    )
388    @KnownFailure("isSealed method returns false for sealed package.")
389    public void test_isSealedLjava_net_URL() throws Exception {
390        Package p = getTestPackage("hyts_c.jar", "p.C");
391        assertFalse("Package isSealed returns wrong boolean (1)", p
392                .isSealed(new URL("file:/" + resPath + "/")));
393        assertTrue("Package isSealed returns wrong boolean (2)", p
394                .isSealed(new URL("file:/" + resPath + "/Package/hyts_c.jar")));
395    }
396
397    /**
398     * @tests java.lang.Package#toString()
399     */
400    @TestTargetNew(
401        level = TestLevel.COMPLETE,
402        notes = "",
403        method = "toString",
404        args = {}
405    )
406    public void test_toString() throws Exception {
407        Package p = getTestPackage("hyts_c.jar", "p.C");
408        assertTrue("Package toString returns wrong string", p.toString()
409                .length() > 0);
410    }
411
412    @SuppressWarnings("unchecked")
413    @TestTargetNew(
414        level = TestLevel.COMPLETE,
415        notes = "",
416        method = "getAnnotation",
417        args = {java.lang.Class.class}
418    )
419    @KnownFailure("Class loader can't retrieve information about annotations.")
420    public void test_getAnnotation() throws Exception {
421        String annotationName = "a.b.PackageAnnotation";
422        Package p = getTestPackage("hyts_package.jar", annotationName);
423        assertEquals(annotationName,
424                p.getAnnotation(clazz).annotationType().getName());
425        assertNull(String.class.getPackage().getAnnotation(Deprecated.class));
426        assertNull(ExtendTestClass.class.getPackage().
427                getAnnotation(Deprecated.class));
428    }
429
430    @SuppressWarnings("unchecked")
431    @TestTargetNew(
432        level = TestLevel.COMPLETE,
433        notes = "",
434        method = "getAnnotations",
435        args = {}
436    )
437    @KnownFailure("Class loader can't retrieve information about annotations.")
438    public void test_getAnnotations() throws Exception {
439        String annotationName = "a.b.PackageAnnotation";
440        Package p = getTestPackage("hyts_package.jar", annotationName);
441
442        Annotation [] annotations = p.getAnnotations();
443        assertEquals(1, annotations.length);
444
445        p = String.class.getPackage();
446        assertEquals(0, p.getAnnotations().length);
447    }
448
449    @TestTargetNew(
450        level = TestLevel.COMPLETE,
451        notes = "",
452        method = "getDeclaredAnnotations",
453        args = {}
454    )
455    @KnownFailure("Class loader can't retrieve information about annotations.")
456    public void test_getDeclaredAnnotations() throws Exception {
457        String annotationName = "a.b.PackageAnnotation";
458        Package p = getTestPackage("hyts_package.jar", annotationName);
459
460        Annotation [] annotations = p.getDeclaredAnnotations();
461        assertEquals(1, annotations.length);
462
463        p = String.class.getPackage();
464        assertEquals(0, p.getDeclaredAnnotations().length);
465    }
466
467    @SuppressWarnings("unchecked")
468    @TestTargetNew(
469        level = TestLevel.COMPLETE,
470        notes = "",
471        method = "isAnnotationPresent",
472        args = {java.lang.Class.class}
473    )
474    @KnownFailure("Class loader can't retrieve information about annotations.")
475    public void test_isAnnotationPresent() throws Exception {
476        String annotationName = "a.b.PackageAnnotation";
477        Package p = getTestPackage("hyts_package.jar", annotationName);
478
479        assertTrue(p.isAnnotationPresent(clazz));
480        assertFalse(p.isAnnotationPresent(Deprecated.class));
481
482        p = String.class.getPackage();
483        assertFalse(p.isAnnotationPresent(clazz));
484        assertFalse(p.isAnnotationPresent(Deprecated.class));
485    }
486}
487