Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/tools/pack200/AttributeTests.java
38833 views
/*1* Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/22import java.io.File;23import java.util.ArrayList;24import java.util.Arrays;25import java.util.List;26/*27* @test28* @bug 6746111 8005252 800826229* @summary tests various classfile format and attribute handling by pack20030* @compile -XDignore.symbol.file Utils.java AttributeTests.java31* @run main AttributeTests32* @author ksrini33*/34public class AttributeTests {3536public static void main(String... args) throws Exception {37test6746111();38testMethodParameters();39Utils.cleanup();40}4142/*43* this tests ensure that MethodParameters produces by javac is packed44* correctly. Usually this is not the case as new attributes are available45* in the sdk jars, since MethodParameters happens to be an optional46* attribute, thus this test.47*/48static void testMethodParameters() throws Exception {49List<String> scratch = new ArrayList<>();50final String fname = "MP";51String javaFileName = fname + Utils.JAVA_FILE_EXT;52String javaClassName = fname + Utils.CLASS_FILE_EXT;53scratch.add("class " + fname + " {");54scratch.add("void foo2(int j, final int k){}");55scratch.add("}");56File cwd = new File(".");57File javaFile = new File(cwd, javaFileName);58Utils.createFile(javaFile, scratch);5960Utils.compiler(javaFile.getName(), "-parameters");6162// jar the file up63File testjarFile = new File(cwd, "test" + Utils.JAR_FILE_EXT);64Utils.jar("cvf", testjarFile.getName(), javaClassName);6566Utils.testWithRepack(testjarFile, "--unknown-attribute=error");67}68/*69* this test checks to see if we get the expected strings for output70*/71static void test6746111() throws Exception {72String pack200Cmd = Utils.getPack200Cmd();73File badAttrJar = new File(".", "badattr.jar");74Utils.copyFile(new File(Utils.TEST_SRC_DIR, "badattr.jar"), badAttrJar);75File testJar = new File(".", "test.jar");76List<String> cmds = new ArrayList<String>();77cmds.add(pack200Cmd);78cmds.add("--repack");79cmds.add("-v");80cmds.add(testJar.getAbsolutePath());81cmds.add(badAttrJar.getAbsolutePath());82List<String> output = Utils.runExec(cmds);83/*84* compare the repacked jar bit-wise, as all the files85* should be transmitted "as-is".86*/87Utils.doCompareBitWise(badAttrJar.getAbsoluteFile(), testJar.getAbsoluteFile());88String[] expectedStrings = {89"WARNING: Passing class file uncompressed due to unrecognized" +90" attribute: Foo.class",91"INFO: com.sun.java.util.jar.pack.Attribute$FormatException: " +92"class attribute \"XourceFile\": is unknown attribute " +93"in class Foo",94"INFO: com.sun.java.util.jar.pack.ClassReader$ClassFormatException: " +95"AnnotationDefault: attribute length cannot be zero, in Test.message()",96"WARNING: Passing class file uncompressed due to unknown class format: Test.class"97};98List<String> notfoundList = new ArrayList<String>();99notfoundList.addAll(Arrays.asList(expectedStrings));100// make sure the expected messages are emitted101for (String x : output) {102findString(x, notfoundList, expectedStrings);103}104if (!notfoundList.isEmpty()) {105System.out.println("Not found:");106for (String x : notfoundList) {107System.out.println(x);108}109throw new Exception("Test fails: " + notfoundList.size() +110" expected strings not found");111}112testJar.delete();113badAttrJar.delete();114}115116private static void findString(String outputStr, List<String> notfoundList,117String[] expectedStrings) {118for (String y : expectedStrings) {119if (outputStr.contains(y)) {120notfoundList.remove(y);121return;122}123}124}125}126127128