author | bpb |
Fri, 06 Feb 2015 08:05:44 -0800 | |
changeset 28847 | 5667388e3a79 |
parent 23010 | 6dadb192ad81 |
child 30820 | 0d4717a011d3 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
23010
6dadb192ad81
8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents:
20181
diff
changeset
|
2 |
* Copyright (c) 2007, 2013, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 22 |
*/ |
23 |
||
24 |
/** |
|
25 |
* @test |
|
20181
7dadfbe7c0d3
8023113: tools/jar/ChangeDir.java fails if /tmp/a exists
sherman
parents:
5506
diff
changeset
|
26 |
* @bug 4806786 8023113 |
2 | 27 |
* @summary jar -C doesn't ignore multiple // in path |
28 |
*/ |
|
29 |
||
30 |
import java.io.*; |
|
20181
7dadfbe7c0d3
8023113: tools/jar/ChangeDir.java fails if /tmp/a exists
sherman
parents:
5506
diff
changeset
|
31 |
import java.nio.file.*; |
2 | 32 |
import java.util.*; |
33 |
import java.util.jar.*; |
|
20181
7dadfbe7c0d3
8023113: tools/jar/ChangeDir.java fails if /tmp/a exists
sherman
parents:
5506
diff
changeset
|
34 |
import java.util.stream.Stream; |
2 | 35 |
import sun.tools.jar.Main; |
36 |
||
37 |
public class ChangeDir { |
|
38 |
private final static String jarName = "test.jar"; |
|
39 |
private final static String fileName = "hello.txt"; |
|
40 |
||
41 |
/** Remove dirs & files needed for test. */ |
|
20181
7dadfbe7c0d3
8023113: tools/jar/ChangeDir.java fails if /tmp/a exists
sherman
parents:
5506
diff
changeset
|
42 |
private static void cleanup(Path dir) { |
7dadfbe7c0d3
8023113: tools/jar/ChangeDir.java fails if /tmp/a exists
sherman
parents:
5506
diff
changeset
|
43 |
try { |
7dadfbe7c0d3
8023113: tools/jar/ChangeDir.java fails if /tmp/a exists
sherman
parents:
5506
diff
changeset
|
44 |
if (Files.isDirectory(dir)) { |
7dadfbe7c0d3
8023113: tools/jar/ChangeDir.java fails if /tmp/a exists
sherman
parents:
5506
diff
changeset
|
45 |
try (Stream<Path> s = Files.list(dir)) { |
7dadfbe7c0d3
8023113: tools/jar/ChangeDir.java fails if /tmp/a exists
sherman
parents:
5506
diff
changeset
|
46 |
s.forEach( p -> cleanup(p)); |
7dadfbe7c0d3
8023113: tools/jar/ChangeDir.java fails if /tmp/a exists
sherman
parents:
5506
diff
changeset
|
47 |
} |
2 | 48 |
} |
20181
7dadfbe7c0d3
8023113: tools/jar/ChangeDir.java fails if /tmp/a exists
sherman
parents:
5506
diff
changeset
|
49 |
Files.delete(dir); |
7dadfbe7c0d3
8023113: tools/jar/ChangeDir.java fails if /tmp/a exists
sherman
parents:
5506
diff
changeset
|
50 |
} catch (IOException x) { |
7dadfbe7c0d3
8023113: tools/jar/ChangeDir.java fails if /tmp/a exists
sherman
parents:
5506
diff
changeset
|
51 |
fail(x.toString()); |
2 | 52 |
} |
53 |
} |
|
54 |
||
55 |
public static void realMain(String[] args) throws Throwable { |
|
56 |
doTest("/"); |
|
57 |
doTest("//"); |
|
58 |
doTest("///"); |
|
59 |
doTest("////"); |
|
60 |
if (System.getProperty("os.name").startsWith("Windows")) { |
|
61 |
doTest("\\"); |
|
62 |
doTest("\\\\"); |
|
63 |
doTest("\\\\\\"); |
|
64 |
doTest("\\\\\\\\"); |
|
65 |
doTest("\\/"); |
|
66 |
} |
|
67 |
} |
|
68 |
||
69 |
static void doTest(String sep) throws Throwable { |
|
20181
7dadfbe7c0d3
8023113: tools/jar/ChangeDir.java fails if /tmp/a exists
sherman
parents:
5506
diff
changeset
|
70 |
Path topDir = Files.createTempDirectory("delete"); |
2 | 71 |
try { |
20181
7dadfbe7c0d3
8023113: tools/jar/ChangeDir.java fails if /tmp/a exists
sherman
parents:
5506
diff
changeset
|
72 |
Files.deleteIfExists(Paths.get(jarName)); |
7dadfbe7c0d3
8023113: tools/jar/ChangeDir.java fails if /tmp/a exists
sherman
parents:
5506
diff
changeset
|
73 |
|
2 | 74 |
// Create a subdirectory "a/b" |
20181
7dadfbe7c0d3
8023113: tools/jar/ChangeDir.java fails if /tmp/a exists
sherman
parents:
5506
diff
changeset
|
75 |
Path testDir = Files.createDirectories(topDir.resolve("a").resolve("b")); |
2 | 76 |
|
77 |
// Create file in that subdirectory |
|
20181
7dadfbe7c0d3
8023113: tools/jar/ChangeDir.java fails if /tmp/a exists
sherman
parents:
5506
diff
changeset
|
78 |
Path testFile = testDir.resolve(fileName); |
7dadfbe7c0d3
8023113: tools/jar/ChangeDir.java fails if /tmp/a exists
sherman
parents:
5506
diff
changeset
|
79 |
Files.createFile(testFile); |
2 | 80 |
|
81 |
// Create a jar file from that subdirectory, but with a // in the |
|
82 |
// path name. |
|
83 |
List<String> argList = new ArrayList<String>(); |
|
84 |
argList.add("cf"); |
|
85 |
argList.add(jarName); |
|
86 |
argList.add("-C"); |
|
20181
7dadfbe7c0d3
8023113: tools/jar/ChangeDir.java fails if /tmp/a exists
sherman
parents:
5506
diff
changeset
|
87 |
argList.add(topDir.toString() + sep + "a" + sep + sep + "b"); // Note double 'sep' is intentional |
2 | 88 |
argList.add(fileName); |
89 |
||
90 |
Main jarTool = new Main(System.out, System.err, "jar"); |
|
20181
7dadfbe7c0d3
8023113: tools/jar/ChangeDir.java fails if /tmp/a exists
sherman
parents:
5506
diff
changeset
|
91 |
if (!jarTool.run(argList.toArray(new String[argList.size()]))) { |
2 | 92 |
fail("Could not create jar file."); |
93 |
} |
|
94 |
||
95 |
// Check that the entry for hello.txt does *not* have a pathname. |
|
20181
7dadfbe7c0d3
8023113: tools/jar/ChangeDir.java fails if /tmp/a exists
sherman
parents:
5506
diff
changeset
|
96 |
try (JarFile jf = new JarFile(jarName)) { |
7dadfbe7c0d3
8023113: tools/jar/ChangeDir.java fails if /tmp/a exists
sherman
parents:
5506
diff
changeset
|
97 |
for (Enumeration<JarEntry> i = jf.entries(); i.hasMoreElements();) { |
7dadfbe7c0d3
8023113: tools/jar/ChangeDir.java fails if /tmp/a exists
sherman
parents:
5506
diff
changeset
|
98 |
JarEntry je = i.nextElement(); |
7dadfbe7c0d3
8023113: tools/jar/ChangeDir.java fails if /tmp/a exists
sherman
parents:
5506
diff
changeset
|
99 |
String name = je.getName(); |
7dadfbe7c0d3
8023113: tools/jar/ChangeDir.java fails if /tmp/a exists
sherman
parents:
5506
diff
changeset
|
100 |
if (name.indexOf(fileName) != -1) { |
7dadfbe7c0d3
8023113: tools/jar/ChangeDir.java fails if /tmp/a exists
sherman
parents:
5506
diff
changeset
|
101 |
if (name.indexOf(fileName) != 0) { |
7dadfbe7c0d3
8023113: tools/jar/ChangeDir.java fails if /tmp/a exists
sherman
parents:
5506
diff
changeset
|
102 |
fail(String.format( |
7dadfbe7c0d3
8023113: tools/jar/ChangeDir.java fails if /tmp/a exists
sherman
parents:
5506
diff
changeset
|
103 |
"Expected '%s' but got '%s'%n", fileName, name)); |
7dadfbe7c0d3
8023113: tools/jar/ChangeDir.java fails if /tmp/a exists
sherman
parents:
5506
diff
changeset
|
104 |
} else { |
7dadfbe7c0d3
8023113: tools/jar/ChangeDir.java fails if /tmp/a exists
sherman
parents:
5506
diff
changeset
|
105 |
pass(); |
7dadfbe7c0d3
8023113: tools/jar/ChangeDir.java fails if /tmp/a exists
sherman
parents:
5506
diff
changeset
|
106 |
} |
2 | 107 |
} |
108 |
} |
|
109 |
} |
|
110 |
} finally { |
|
20181
7dadfbe7c0d3
8023113: tools/jar/ChangeDir.java fails if /tmp/a exists
sherman
parents:
5506
diff
changeset
|
111 |
cleanup(topDir); |
7dadfbe7c0d3
8023113: tools/jar/ChangeDir.java fails if /tmp/a exists
sherman
parents:
5506
diff
changeset
|
112 |
Files.deleteIfExists(Paths.get(jarName)); |
2 | 113 |
} |
114 |
} |
|
115 |
||
116 |
//--------------------- Infrastructure --------------------------- |
|
117 |
static volatile int passed = 0, failed = 0; |
|
118 |
static void pass() {passed++;} |
|
119 |
static void fail() {failed++; Thread.dumpStack();} |
|
120 |
static void fail(String msg) {System.out.println(msg); fail();} |
|
121 |
static void unexpected(Throwable t) {failed++; t.printStackTrace();} |
|
122 |
static void check(boolean cond) {if (cond) pass(); else fail();} |
|
123 |
static void equal(Object x, Object y) { |
|
124 |
if (x == null ? y == null : x.equals(y)) pass(); |
|
125 |
else fail(x + " not equal to " + y);} |
|
126 |
public static void main(String[] args) throws Throwable { |
|
127 |
try {realMain(args);} catch (Throwable t) {unexpected(t);} |
|
128 |
System.out.println("\nPassed = " + passed + " failed = " + failed); |
|
129 |
if (failed > 0) throw new AssertionError("Some tests failed");} |
|
130 |
} |