Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/tools/jar/normalize/TestNormal.java
38840 views
/*1* Copyright (c) 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*/222324/*25* @test26* @run main/timeout=600 TestNormal27* @bug 802080228* @summary Need an ability to create jar files that are invariant to the pack200 packing/unpacking29* @author Alexander Zuev30*/3132import java.io.*;33import java.util.Collections;34import java.util.Properties;35import java.util.jar.JarEntry;36import java.util.jar.JarFile;3738public class TestNormal {39private static String FS = File.separator;4041public static void main(String args[]) throws Exception {42Properties p = System.getProperties();43String java_home = p.getProperty("test.jdk");44String dtjar = java_home + File.separator + "lib"45+ File.separator + "dt.jar";4647File folder = new File("dt");48if (folder.exists()) {49delete(folder);50}51folder.mkdir();5253try {54extractJar(new JarFile(dtjar), folder);55execJavaCommand(java_home, "jar cnf normalized.jar -C dt .");56execJavaCommand(java_home, "jar cf original.jar -C dt .");57execJavaCommand(java_home, "pack200 -r repacked.jar original.jar");58compareJars(new JarFile("normalized.jar"), new JarFile("repacked.jar"));59} finally {60String[] cleanupList = {"dt", "normalized.jar", "original.jar", "repacked.jar"};61for (String s : cleanupList) {62delete(new File(s));63}64}65}6667public static void execJavaCommand(String java_home, String cmd) throws Exception {68Process proc = Runtime.getRuntime().exec(java_home + FS + "bin" + FS + cmd);69String s;70BufferedReader stdInput =71new BufferedReader(new InputStreamReader(proc.getInputStream()));72BufferedReader stdError =73new BufferedReader(new InputStreamReader(proc.getErrorStream()));74while ((s = stdInput.readLine()) != null) {75System.out.println(s);76}77while ((s = stdError.readLine()) != null) {78System.err.println(s);79}80}8182public static void compareJars(JarFile jf1, JarFile jf2) throws Exception {83try {84if (jf1.size() != jf2.size()) {85throw new Exception("Jars " + jf1.getName() + " and " + jf2.getName()86+ " have different number of entries");87}88for (JarEntry elem1 : Collections.list(jf1.entries())) {89JarEntry elem2 = jf2.getJarEntry(elem1.getName());90if (elem2 == null) {91throw new Exception("Element " + elem1.getName() + " is missing from " + jf2.getName());92}93if (!elem1.isDirectory() && elem1.getCrc() != elem2.getCrc()) {94throw new Exception("The crc of " + elem1.getName() + " is different.");95}96}97} finally {98jf1.close();99jf2.close();100}101}102103public static void extractJar(JarFile jf, File where) throws Exception {104for (JarEntry file : Collections.list(jf.entries())) {105File out = new File(where, file.getName());106if (file.isDirectory()) {107out.mkdirs();108continue;109}110File parent = out.getParentFile();111if (parent != null && !parent.exists()) {112parent.mkdirs();113}114InputStream is = null;115OutputStream os = null;116try {117is = jf.getInputStream(file);118os = new FileOutputStream(out);119while (is.available() > 0) {120os.write(is.read());121}122} finally {123if (is != null) {124is.close();125}126if (os != null) {127os.close();128}129}130}131}132133static void delete(File f) throws IOException {134if (!f.exists()) {135return;136}137if (f.isDirectory()) {138for (File c : f.listFiles()) {139delete(c);140}141}142if (!f.delete()) {143throw new FileNotFoundException("Failed to delete file: " + f);144}145}146}147148149