jdk/test/tools/jar/ChangeDir.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2007 Sun Microsystems, Inc.  All Rights Reserved.
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * have any questions.
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @bug 4806786
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary jar -C doesn't ignore multiple // in path
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.util.jar.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import sun.tools.jar.Main;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
public class ChangeDir {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
    private final static String jarName = "test.jar";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
    private final static String fileName = "hello.txt";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
    /** Remove dirs & files needed for test. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    private static void cleanup(File dir) throws Throwable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
        if (dir != null && dir.exists()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
            for (File ff : dir.listFiles()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
                check(ff.delete());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
            check(dir.delete());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
            check(new File(jarName).delete());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    public static void realMain(String[] args) throws Throwable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        doTest("/");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        doTest("//");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        doTest("///");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        doTest("////");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        if (System.getProperty("os.name").startsWith("Windows")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
            doTest("\\");
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
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    static void doTest(String sep) throws Throwable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        File testDir = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        JarFile jf = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
            // Create a subdirectory "a/b"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            File f = File.createTempFile("delete", ".me");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            String dirName = f.getParent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            testDir = new File(dirName + sep + "a" + sep + "b");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            cleanup(testDir);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            check(testDir.mkdirs());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            // Create file in that subdirectory
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            File testFile = new File(testDir, fileName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            check(testFile.createNewFile());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            // Create a jar file from that subdirectory, but with a // in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            // path  name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            List<String> argList = new ArrayList<String>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            argList.add("cf");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            argList.add(jarName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            argList.add("-C");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            argList.add(dirName + sep + "a" + sep + sep + "b"); // Note double 'sep' is intentional
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            argList.add(fileName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            String jarArgs[] = new String[argList.size()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            jarArgs = argList.toArray(jarArgs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            Main jarTool = new Main(System.out, System.err, "jar");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            if (!jarTool.run(jarArgs)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                fail("Could not create jar file.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            // Check that the entry for hello.txt does *not* have a pathname.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            jf = new JarFile(jarName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            for (Enumeration<JarEntry> i = jf.entries(); i.hasMoreElements();) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                JarEntry je = i.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                String name = je.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                if (name.indexOf(fileName) != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                    if (name.indexOf(fileName) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                        fail(String.format(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                                 "Expected '%s' but got '%s'%n", fileName, name));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            if (jf != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                jf.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            cleanup(testDir);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    //--------------------- Infrastructure ---------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    static volatile int passed = 0, failed = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    static void pass() {passed++;}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    static void fail() {failed++; Thread.dumpStack();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    static void fail(String msg) {System.out.println(msg); fail();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    static void unexpected(Throwable t) {failed++; t.printStackTrace();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    static void check(boolean cond) {if (cond) pass(); else fail();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    static void equal(Object x, Object y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        if (x == null ? y == null : x.equals(y)) pass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        else fail(x + " not equal to " + y);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    public static void main(String[] args) throws Throwable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        try {realMain(args);} catch (Throwable t) {unexpected(t);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        System.out.println("\nPassed = " + passed + " failed = " + failed);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        if (failed > 0) throw new AssertionError("Some tests failed");}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
}