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