source: trunk/openjdk/jdk/test/tools/jar/ChangeDir.java

Last change on this file was 278, checked in by dmik, 14 years ago

trunk: Merged in openjdk6 b22 from branches/vendor/oracle.

File size: 4.8 KB
Line 
1/*
2 * Copyright (c) 2007, 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 4806786
27 * @summary jar -C doesn't ignore multiple // in path
28 */
29
30import java.io.*;
31import java.util.*;
32import java.util.jar.*;
33import sun.tools.jar.Main;
34
35public class ChangeDir {
36 private final static String jarName = "test.jar";
37 private final static String fileName = "hello.txt";
38
39 /** Remove dirs & files needed for test. */
40 private static void cleanup(File dir) throws Throwable {
41 if (dir != null && dir.exists()) {
42 for (File ff : dir.listFiles()) {
43 check(ff.delete());
44 }
45 check(dir.delete());
46 check(new File(jarName).delete());
47 }
48 }
49
50 public static void realMain(String[] args) throws Throwable {
51 doTest("/");
52 doTest("//");
53 doTest("///");
54 doTest("////");
55 if (System.getProperty("os.name").startsWith("Windows")) {
56 doTest("\\");
57 doTest("\\\\");
58 doTest("\\\\\\");
59 doTest("\\\\\\\\");
60 doTest("\\/");
61 }
62 }
63
64 static void doTest(String sep) throws Throwable {
65 File testDir = null;
66 JarFile jf = null;
67 try {
68 // Create a subdirectory "a/b"
69 File f = File.createTempFile("delete", ".me");
70 String dirName = f.getParent();
71 testDir = new File(dirName + sep + "a" + sep + "b");
72 cleanup(testDir);
73 check(testDir.mkdirs());
74
75 // Create file in that subdirectory
76 File testFile = new File(testDir, fileName);
77 check(testFile.createNewFile());
78
79 // Create a jar file from that subdirectory, but with a // in the
80 // path name.
81 List<String> argList = new ArrayList<String>();
82 argList.add("cf");
83 argList.add(jarName);
84 argList.add("-C");
85 argList.add(dirName + sep + "a" + sep + sep + "b"); // Note double 'sep' is intentional
86 argList.add(fileName);
87 String jarArgs[] = new String[argList.size()];
88 jarArgs = argList.toArray(jarArgs);
89
90 Main jarTool = new Main(System.out, System.err, "jar");
91 if (!jarTool.run(jarArgs)) {
92 fail("Could not create jar file.");
93 }
94
95 // Check that the entry for hello.txt does *not* have a pathname.
96 jf = new JarFile(jarName);
97 for (Enumeration<JarEntry> i = jf.entries(); i.hasMoreElements();) {
98 JarEntry je = i.nextElement();
99 String name = je.getName();
100 if (name.indexOf(fileName) != -1) {
101 if (name.indexOf(fileName) != 0) {
102 fail(String.format(
103 "Expected '%s' but got '%s'%n", fileName, name));
104 }
105 }
106 }
107 } finally {
108 if (jf != null) {
109 jf.close();
110 }
111 cleanup(testDir);
112 }
113 }
114
115 //--------------------- Infrastructure ---------------------------
116 static volatile int passed = 0, failed = 0;
117 static void pass() {passed++;}
118 static void fail() {failed++; Thread.dumpStack();}
119 static void fail(String msg) {System.out.println(msg); fail();}
120 static void unexpected(Throwable t) {failed++; t.printStackTrace();}
121 static void check(boolean cond) {if (cond) pass(); else fail();}
122 static void equal(Object x, Object y) {
123 if (x == null ? y == null : x.equals(y)) pass();
124 else fail(x + " not equal to " + y);}
125 public static void main(String[] args) throws Throwable {
126 try {realMain(args);} catch (Throwable t) {unexpected(t);}
127 System.out.println("\nPassed = " + passed + " failed = " + failed);
128 if (failed > 0) throw new AssertionError("Some tests failed");}
129}
Note: See TracBrowser for help on using the repository browser.