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