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