From 3c9f53969abf296f133cb46b05914c4954bd481f Mon Sep 17 00:00:00 2001 From: Kumar Srinivasan Date: Sat, 14 Jul 2012 18:00:42 -0700 Subject: [PATCH] 7184145: (pack200) pack200 --repack throws NullPointerException when JAR file specified without path Reviewed-by: alanb, sherman --- .../com/sun/java/util/jar/pack/Driver.java | 12 ++- jdk/test/tools/pack200/RepackTest.java | 74 +++++++++++++++++++ 2 files changed, 82 insertions(+), 4 deletions(-) create mode 100644 jdk/test/tools/pack200/RepackTest.java diff --git a/jdk/src/share/classes/com/sun/java/util/jar/pack/Driver.java b/jdk/src/share/classes/com/sun/java/util/jar/pack/Driver.java index 960e2d40432..7ecbca05ef2 100644 --- a/jdk/src/share/classes/com/sun/java/util/jar/pack/Driver.java +++ b/jdk/src/share/classes/com/sun/java/util/jar/pack/Driver.java @@ -36,6 +36,7 @@ import java.io.OutputStream; import java.io.PrintStream; import java.text.MessageFormat; import java.nio.file.Files; +import java.nio.file.Path; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; @@ -381,12 +382,15 @@ class Driver { String prefix = base.getName(); if (prefix.length() < 3) prefix += "tmp"; - File where = base.getParentFile(); + File where = (base.getParentFile() == null && suffix.equals(".bak")) + ? new File(".").getAbsoluteFile() + : base.getParentFile(); - if ( base.getParentFile() == null && suffix.equals(".bak")) - where = new File(".").getAbsoluteFile(); + Path tmpfile = (where == null) + ? Files.createTempFile(prefix, suffix) + : Files.createTempFile(where.toPath(), prefix, suffix); - return Files.createTempFile(where.toPath(), prefix, suffix).toFile(); + return tmpfile.toFile(); } static private diff --git a/jdk/test/tools/pack200/RepackTest.java b/jdk/test/tools/pack200/RepackTest.java new file mode 100644 index 00000000000..774e0d66eb3 --- /dev/null +++ b/jdk/test/tools/pack200/RepackTest.java @@ -0,0 +1,74 @@ +/* + * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ +import java.io.File; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; +/* + * @test + * @bug 7184145 + * @summary tests repacking of a simple named jarfile. + * @compile -XDignore.symbol.file Utils.java RepackTest.java + * @run main RepackTest + * @author ksrini + */ +public class RepackTest { + + public static void main(String... args) throws Exception { + testRepack(); + } + + /* + * there are two cases we need to test, where the file in question is + * orpaned, ie. without a parent ie. not qualified by a parent path + * relative nor absolute + * case 1: src and dest are the same + * case 2: src and dest are different + */ + static void testRepack() throws IOException { + + // make a copy of the test specimen to local directory + File testFile = new File("src_tools.jar"); + Utils.copyFile(Utils.locateJar("golden.jar"), testFile); + List cmdsList = new ArrayList<>(); + + // case 1: + cmdsList.add(Utils.getPack200Cmd()); + cmdsList.add("--repack"); + cmdsList.add(testFile.getName()); + Utils.runExec(cmdsList); + + // case 2: + File dstFile = new File("dst_tools.jar"); + cmdsList.clear(); + cmdsList.add(Utils.getPack200Cmd()); + cmdsList.add("--repack"); + cmdsList.add(dstFile.getName()); + cmdsList.add(testFile.getName()); + Utils.runExec(cmdsList); + + // tidy up + testFile.delete(); + dstFile.delete(); + } +}