DeprecatedPackageTest.java revision 3314:97ec97671022
1/*
2 * Copyright (c) 2014, 2016, 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 * @build toolbox.ToolBox InMemoryFileManager TestResult TestBase
34 * @run main DeprecatedPackageTest
35 */
36
37import com.sun.tools.classfile.Attribute;
38import com.sun.tools.classfile.ClassFile;
39import com.sun.tools.classfile.Deprecated_attribute;
40
41public class DeprecatedPackageTest extends TestResult {
42
43    private static final String[] sourceTest = new String[]{
44        "package deprecated;\n"
45        + "public class notDeprecated{}",
46        "package deprecated;\n"
47        + "public interface notDeprecated{}",
48        "package deprecated;\n"
49        + "public @interface notDeprecated{}",
50        "package deprecated;\n"
51        + "public enum notDeprecated{}"
52    };
53
54    private static final String CLASS_NAME = "deprecated.notDeprecated";
55
56    private static final String PACKAGE_INFO =
57            "@Deprecated\n" +
58            "package deprecated;";
59
60    public static void main(String[] args) throws TestFailedException {
61        new DeprecatedPackageTest().test();
62    }
63
64    private void test() throws TestFailedException {
65        try {
66            for (String src : sourceTest) {
67                test(PACKAGE_INFO, src);
68                test(PACKAGE_INFO.replaceAll("@Deprecated", "/** @deprecated */"), src);
69            }
70        } catch (Exception e) {
71            addFailure(e);
72        } finally {
73            checkStatus();
74        }
75    }
76
77    private void test(String package_info, String src) {
78        addTestCase(src);
79        printf("Testing test case: \n%s\n", src);
80        try {
81            ClassFile cf = readClassFile(compile(
82                        new String[]{"package-info.java", package_info},
83                        new String[]{"notDeprecated.java", src})
84                    .getClasses().get(CLASS_NAME));
85            Deprecated_attribute attr =
86                    (Deprecated_attribute) cf.getAttribute(Attribute.Deprecated);
87            checkNull(attr, "Class can not have deprecated attribute : " + CLASS_NAME);
88        } catch (Exception e) {
89            addFailure(e);
90        }
91    }
92}
93