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