DeprecatedPackageTest.java revision 3294:9adfb22ff08f
1/*
2 * Copyright (c) 2014, 2015, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24/*
25 * @test
26 * @bug 8042261
27 * @summary Checking that deprecated attribute does not apply to classes of deprecated package.
28 * @library /tools/lib /tools/javac/lib ../lib
29 * @modules jdk.compiler/com.sun.tools.javac.api
30 *          jdk.compiler/com.sun.tools.javac.main
31 *          jdk.compiler/com.sun.tools.javac.util
32 *          jdk.jdeps/com.sun.tools.classfile
33 *          jdk.jdeps/com.sun.tools.javap
34 * @build  ToolBox TestBase TestResult InMemoryFileManager
35 * @run main DeprecatedPackageTest
36 */
37
38import com.sun.tools.classfile.Attribute;
39import com.sun.tools.classfile.ClassFile;
40import com.sun.tools.classfile.Deprecated_attribute;
41
42public class DeprecatedPackageTest extends TestResult {
43
44    private static final String[] sourceTest = new String[]{
45        "package deprecated;\n"
46        + "public class notDeprecated{}",
47        "package deprecated;\n"
48        + "public interface notDeprecated{}",
49        "package deprecated;\n"
50        + "public @interface notDeprecated{}",
51        "package deprecated;\n"
52        + "public enum notDeprecated{}"
53    };
54
55    private static final String CLASS_NAME = "deprecated.notDeprecated";
56
57    private static final String PACKAGE_INFO =
58            "@Deprecated\n" +
59            "package deprecated;";
60
61    public static void main(String[] args) throws TestFailedException {
62        new DeprecatedPackageTest().test();
63    }
64
65    private void test() throws TestFailedException {
66        try {
67            for (String src : sourceTest) {
68                test(PACKAGE_INFO, src);
69                test(PACKAGE_INFO.replaceAll("@Deprecated", "/** @deprecated */"), src);
70            }
71        } catch (Exception e) {
72            addFailure(e);
73        } finally {
74            checkStatus();
75        }
76    }
77
78    private void test(String package_info, String src) {
79        addTestCase(src);
80        printf("Testing test case: \n%s\n", src);
81        try {
82            ClassFile cf = readClassFile(compile(
83                        new String[]{"package-info.java", package_info},
84                        new String[]{"notDeprecated.java", src})
85                    .getClasses().get(CLASS_NAME));
86            Deprecated_attribute attr =
87                    (Deprecated_attribute) cf.getAttribute(Attribute.Deprecated);
88            checkNull(attr, "Class can not have deprecated attribute : " + CLASS_NAME);
89        } catch (Exception e) {
90            addFailure(e);
91        }
92    }
93}
94