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