1 /* 2 * Copyright (c) 2016, 2017, 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 8167237 27 * @summary test that both old style command line options and new gnu style 28 * command line options work with the --release option whether or 29 * not the --release option is preceded by a file name. 30 * @library /test/lib 31 * @modules jdk.jartool/sun.tools.jar 32 * @build jdk.test.lib.util.FileUtils 33 * jdk.test.lib.Utils 34 * jdk.test.lib.Asserts 35 * jdk.test.lib.JDKToolFinder 36 * jdk.test.lib.JDKToolLauncher 37 * jdk.test.lib.Platform 38 * jdk.test.lib.process.* 39 * @run testng ReleaseBeforeFiles 40 */ 41 42 import org.testng.Assert; 43 import org.testng.annotations.AfterMethod; 44 import org.testng.annotations.BeforeMethod; 45 import org.testng.annotations.Test; 46 47 import java.io.IOException; 48 import java.io.UncheckedIOException; 49 import java.nio.file.Files; 50 import java.nio.file.Path; 51 import java.nio.file.Paths; 52 import java.util.Arrays; 53 import java.util.stream.Stream; 54 55 import jdk.test.lib.util.FileUtils; 56 57 public class ReleaseBeforeFiles { 58 private Runnable onCompletion; 59 60 @BeforeMethod 61 public void reset() { 62 onCompletion = null; 63 } 64 65 @AfterMethod 66 public void run() { 67 if (onCompletion != null) { 68 onCompletion.run(); 69 } 70 } 71 72 @Test // passes before bug fix 73 public void test1() throws IOException { 74 mkdir("test1"); 75 touch("test1/testfile1"); 76 jar("cf test.jar --release 9 test1"); 77 jar("tf test.jar"); 78 rm("test.jar test1"); 79 } 80 81 @Test // fails before bug fix 82 public void test2() throws IOException { 83 System.out.println("====="); 84 mkdir("test1"); 85 touch("test1/testfile1"); 86 onCompletion = () -> rm("test.jar test1"); 87 jar("--create --file=test.jar --release 9 test1"); 88 jar("tf test.jar"); 89 } 90 91 @Test // passes before bug fix 92 public void test3() throws IOException { 93 System.out.println("====="); 94 mkdir("test1"); 95 touch("test1/testfile1"); 96 jar("-cf test.jar -C test1 ."); 97 jar("-uf test.jar --release 9 -C test1 ."); 98 jar("tf test.jar"); 99 rm("test.jar test1"); 100 } 101 102 @Test // fails before bug fix 103 public void test4() throws IOException { 104 System.out.println("====="); 105 mkdir("test1"); 106 touch("test1/testfile1"); 107 onCompletion = () -> rm("test.jar test1"); 108 jar("--create --file=test.jar -C test1 ."); 109 jar("--update --file=test.jar --release 9 -C test1 ."); 110 jar("tf test.jar"); 111 } 112 113 @Test // passes before bug fix since test2 precedes --release 9 114 public void test5() throws IOException { 115 System.out.println("====="); 116 mkdir("test1 test2"); 117 touch("test1/testfile1 test2/testfile2"); 118 jar("--create --file=test.jar -C test1 ."); 119 jar("--update --file=test.jar test2 --release 9 -C test1 ."); 120 jar("tf test.jar"); 121 rm("test.jar test1 test2"); 122 } 123 124 private Stream<Path> mkpath(String... args) { 125 return Arrays.stream(args).map(d -> Paths.get(".", d.split("/"))); 126 } 127 128 private void mkdir(String cmdline) { 129 System.out.println("mkdir -p " + cmdline); 130 mkpath(cmdline.split(" +")).forEach(p -> { 131 try { 132 Files.createDirectories(p); 133 } catch (IOException x) { 134 throw new UncheckedIOException(x); 135 } 136 }); 137 } 138 139 private void touch(String cmdline) { 140 System.out.println("touch " + cmdline); 141 mkpath(cmdline.split(" +")).forEach(p -> { 142 try { 143 Files.createFile(p); 144 } catch (IOException x) { 145 throw new UncheckedIOException(x); 146 } 147 }); 148 } 149 150 private void rm(String cmdline) { 151 System.out.println("rm -rf " + cmdline); 152 mkpath(cmdline.split(" +")).forEach(p -> { 153 try { 154 if (Files.isDirectory(p)) { 155 FileUtils.deleteFileTreeWithRetry(p); 156 } else { 157 FileUtils.deleteFileIfExistsWithRetry(p); 158 } 159 } catch (IOException x) { 160 throw new UncheckedIOException(x); 161 } 162 }); 163 } 164 165 private void jar(String cmdline) throws IOException { 166 System.out.println("jar " + cmdline); 167 boolean ok = new sun.tools.jar.Main(System.out, System.err, "jar") 168 .run(cmdline.split(" +")); 169 Assert.assertTrue(ok); 170 } 171 }