jdk/test/tools/jar/ChangeDir.java
author katleman
Thu, 03 Sep 2015 14:24:43 -0700
changeset 32450 f036508e86e7
parent 30820 0d4717a011d3
child 41484 834b7539ada3
permissions -rw-r--r--
Added tag jdk9-b80 for changeset 56a580f0c008
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     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
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @test
20181
7dadfbe7c0d3 8023113: tools/jar/ChangeDir.java fails if /tmp/a exists
sherman
parents: 5506
diff changeset
    26
 * @bug 4806786 8023113
30820
0d4717a011d3 8081347: Add @modules to jdk_core tests
mchung
parents: 23010
diff changeset
    27
 * @modules jdk.jartool/sun.tools.jar
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * @summary jar -C doesn't ignore multiple // in path
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.io.*;
20181
7dadfbe7c0d3 8023113: tools/jar/ChangeDir.java fails if /tmp/a exists
sherman
parents: 5506
diff changeset
    32
import java.nio.file.*;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.util.jar.*;
20181
7dadfbe7c0d3 8023113: tools/jar/ChangeDir.java fails if /tmp/a exists
sherman
parents: 5506
diff changeset
    35
import java.util.stream.Stream;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import sun.tools.jar.Main;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
public class ChangeDir {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
    private final static String jarName = "test.jar";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    private final static String fileName = "hello.txt";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    /** Remove dirs & files needed for test. */
20181
7dadfbe7c0d3 8023113: tools/jar/ChangeDir.java fails if /tmp/a exists
sherman
parents: 5506
diff changeset
    43
    private static void cleanup(Path dir) {
7dadfbe7c0d3 8023113: tools/jar/ChangeDir.java fails if /tmp/a exists
sherman
parents: 5506
diff changeset
    44
        try {
7dadfbe7c0d3 8023113: tools/jar/ChangeDir.java fails if /tmp/a exists
sherman
parents: 5506
diff changeset
    45
            if (Files.isDirectory(dir)) {
7dadfbe7c0d3 8023113: tools/jar/ChangeDir.java fails if /tmp/a exists
sherman
parents: 5506
diff changeset
    46
                try (Stream<Path> s = Files.list(dir)) {
7dadfbe7c0d3 8023113: tools/jar/ChangeDir.java fails if /tmp/a exists
sherman
parents: 5506
diff changeset
    47
                    s.forEach( p -> cleanup(p));
7dadfbe7c0d3 8023113: tools/jar/ChangeDir.java fails if /tmp/a exists
sherman
parents: 5506
diff changeset
    48
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
            }
20181
7dadfbe7c0d3 8023113: tools/jar/ChangeDir.java fails if /tmp/a exists
sherman
parents: 5506
diff changeset
    50
            Files.delete(dir);
7dadfbe7c0d3 8023113: tools/jar/ChangeDir.java fails if /tmp/a exists
sherman
parents: 5506
diff changeset
    51
        } catch (IOException x) {
7dadfbe7c0d3 8023113: tools/jar/ChangeDir.java fails if /tmp/a exists
sherman
parents: 5506
diff changeset
    52
            fail(x.toString());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    public static void realMain(String[] args) throws Throwable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        doTest("/");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        doTest("//");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        doTest("///");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        doTest("////");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        if (System.getProperty("os.name").startsWith("Windows")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
            doTest("\\");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
            doTest("\\\\");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
            doTest("\\\\\\");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
            doTest("\\\\\\\\");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
            doTest("\\/");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    static void doTest(String sep) throws Throwable {
20181
7dadfbe7c0d3 8023113: tools/jar/ChangeDir.java fails if /tmp/a exists
sherman
parents: 5506
diff changeset
    71
        Path topDir = Files.createTempDirectory("delete");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        try {
20181
7dadfbe7c0d3 8023113: tools/jar/ChangeDir.java fails if /tmp/a exists
sherman
parents: 5506
diff changeset
    73
            Files.deleteIfExists(Paths.get(jarName));
7dadfbe7c0d3 8023113: tools/jar/ChangeDir.java fails if /tmp/a exists
sherman
parents: 5506
diff changeset
    74
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            // Create a subdirectory "a/b"
20181
7dadfbe7c0d3 8023113: tools/jar/ChangeDir.java fails if /tmp/a exists
sherman
parents: 5506
diff changeset
    76
            Path testDir = Files.createDirectories(topDir.resolve("a").resolve("b"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            // Create file in that subdirectory
20181
7dadfbe7c0d3 8023113: tools/jar/ChangeDir.java fails if /tmp/a exists
sherman
parents: 5506
diff changeset
    79
            Path testFile = testDir.resolve(fileName);
7dadfbe7c0d3 8023113: tools/jar/ChangeDir.java fails if /tmp/a exists
sherman
parents: 5506
diff changeset
    80
            Files.createFile(testFile);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            // Create a jar file from that subdirectory, but with a // in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            // path  name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            List<String> argList = new ArrayList<String>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            argList.add("cf");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            argList.add(jarName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            argList.add("-C");
20181
7dadfbe7c0d3 8023113: tools/jar/ChangeDir.java fails if /tmp/a exists
sherman
parents: 5506
diff changeset
    88
            argList.add(topDir.toString() + sep + "a" + sep + sep + "b"); // Note double 'sep' is intentional
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            argList.add(fileName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            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
    92
            if (!jarTool.run(argList.toArray(new String[argList.size()]))) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                fail("Could not create jar file.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            // 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
    97
            try (JarFile jf = new JarFile(jarName)) {
7dadfbe7c0d3 8023113: tools/jar/ChangeDir.java fails if /tmp/a exists
sherman
parents: 5506
diff changeset
    98
                for (Enumeration<JarEntry> i = jf.entries(); i.hasMoreElements();) {
7dadfbe7c0d3 8023113: tools/jar/ChangeDir.java fails if /tmp/a exists
sherman
parents: 5506
diff changeset
    99
                    JarEntry je = i.nextElement();
7dadfbe7c0d3 8023113: tools/jar/ChangeDir.java fails if /tmp/a exists
sherman
parents: 5506
diff changeset
   100
                    String name = je.getName();
7dadfbe7c0d3 8023113: tools/jar/ChangeDir.java fails if /tmp/a exists
sherman
parents: 5506
diff changeset
   101
                    if (name.indexOf(fileName) != -1) {
7dadfbe7c0d3 8023113: tools/jar/ChangeDir.java fails if /tmp/a exists
sherman
parents: 5506
diff changeset
   102
                        if (name.indexOf(fileName) != 0) {
7dadfbe7c0d3 8023113: tools/jar/ChangeDir.java fails if /tmp/a exists
sherman
parents: 5506
diff changeset
   103
                            fail(String.format(
7dadfbe7c0d3 8023113: tools/jar/ChangeDir.java fails if /tmp/a exists
sherman
parents: 5506
diff changeset
   104
                                     "Expected '%s' but got '%s'%n", fileName, name));
7dadfbe7c0d3 8023113: tools/jar/ChangeDir.java fails if /tmp/a exists
sherman
parents: 5506
diff changeset
   105
                        } else {
7dadfbe7c0d3 8023113: tools/jar/ChangeDir.java fails if /tmp/a exists
sherman
parents: 5506
diff changeset
   106
                            pass();
7dadfbe7c0d3 8023113: tools/jar/ChangeDir.java fails if /tmp/a exists
sherman
parents: 5506
diff changeset
   107
                        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        } finally {
20181
7dadfbe7c0d3 8023113: tools/jar/ChangeDir.java fails if /tmp/a exists
sherman
parents: 5506
diff changeset
   112
            cleanup(topDir);
7dadfbe7c0d3 8023113: tools/jar/ChangeDir.java fails if /tmp/a exists
sherman
parents: 5506
diff changeset
   113
            Files.deleteIfExists(Paths.get(jarName));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    //--------------------- Infrastructure ---------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    static volatile int passed = 0, failed = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    static void pass() {passed++;}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    static void fail() {failed++; Thread.dumpStack();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    static void fail(String msg) {System.out.println(msg); fail();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    static void unexpected(Throwable t) {failed++; t.printStackTrace();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    static void check(boolean cond) {if (cond) pass(); else fail();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    static void equal(Object x, Object y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        if (x == null ? y == null : x.equals(y)) pass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        else fail(x + " not equal to " + y);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    public static void main(String[] args) throws Throwable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        try {realMain(args);} catch (Throwable t) {unexpected(t);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        System.out.println("\nPassed = " + passed + " failed = " + failed);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        if (failed > 0) throw new AssertionError("Some tests failed");}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
}