jdk/test/tools/jar/index/MetaInf.java
author duke
Wed, 05 Jul 2017 21:06:18 +0200
changeset 34465 41a1258588da
parent 30820 0d4717a011d3
child 41484 834b7539ada3
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
7668
d4a77089c587 6962318: Update copyright year
ohair
parents: 5813
diff changeset
     2
 * Copyright (c) 2003, 2010, 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: 3288
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3288
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3288
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
3218
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 2
diff changeset
    26
 * @bug 4408526 6854795
30820
0d4717a011d3 8081347: Add @modules to jdk_core tests
mchung
parents: 7668
diff changeset
    27
 * @modules jdk.jartool/sun.tools.jar
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * @summary Index the non-meta files in META-INF, such as META-INF/services.
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.*;
3218
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 2
diff changeset
    32
import java.util.Arrays;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.util.jar.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import sun.tools.jar.Main;
3218
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 2
diff changeset
    35
import java.util.zip.ZipFile;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
public class MetaInf {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
    static String jarName = "a.jar";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    static String INDEX = "META-INF/INDEX.LIST";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    static String SERVICES = "META-INF/services";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    static String contents =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
        System.getProperty("test.src") + File.separatorChar + "jarcontents";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
3218
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 2
diff changeset
    45
    static void run(String ... args) {
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 2
diff changeset
    46
        if (! new Main(System.out, System.err, "jar").run(args))
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 2
diff changeset
    47
            throw new Error("jar failed: args=" + Arrays.toString(args));
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 2
diff changeset
    48
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
3218
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 2
diff changeset
    50
    static void copy(File from, File to) throws IOException {
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 2
diff changeset
    51
        FileInputStream in = new FileInputStream(from);
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 2
diff changeset
    52
        FileOutputStream out = new FileOutputStream(to);
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 2
diff changeset
    53
        try {
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 2
diff changeset
    54
            byte[] buf = new byte[8192];
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 2
diff changeset
    55
            int n;
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 2
diff changeset
    56
            while ((n = in.read(buf)) != -1)
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 2
diff changeset
    57
                out.write(buf, 0, n);
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 2
diff changeset
    58
        } finally {
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 2
diff changeset
    59
            in.close();
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 2
diff changeset
    60
            out.close();
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 2
diff changeset
    61
        }
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 2
diff changeset
    62
    }
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 2
diff changeset
    63
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 2
diff changeset
    64
    static boolean contains(File jarFile, String entryName)
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 2
diff changeset
    65
        throws IOException {
5813
8e455cd3e807 6962617: Testcase changes, cleanup of problem list for jdk_tools targets
ohair
parents: 5506
diff changeset
    66
        ZipFile zf = new ZipFile(jarFile);
8e455cd3e807 6962617: Testcase changes, cleanup of problem list for jdk_tools targets
ohair
parents: 5506
diff changeset
    67
        if ( zf != null ) {
8e455cd3e807 6962617: Testcase changes, cleanup of problem list for jdk_tools targets
ohair
parents: 5506
diff changeset
    68
            boolean result = zf.getEntry(entryName) != null;
8e455cd3e807 6962617: Testcase changes, cleanup of problem list for jdk_tools targets
ohair
parents: 5506
diff changeset
    69
            zf.close();
8e455cd3e807 6962617: Testcase changes, cleanup of problem list for jdk_tools targets
ohair
parents: 5506
diff changeset
    70
            return result;
8e455cd3e807 6962617: Testcase changes, cleanup of problem list for jdk_tools targets
ohair
parents: 5506
diff changeset
    71
        }
8e455cd3e807 6962617: Testcase changes, cleanup of problem list for jdk_tools targets
ohair
parents: 5506
diff changeset
    72
        return false;
3218
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 2
diff changeset
    73
    }
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 2
diff changeset
    74
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 2
diff changeset
    75
    static void checkContains(File jarFile, String entryName)
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 2
diff changeset
    76
        throws IOException {
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 2
diff changeset
    77
        if (! contains(jarFile, entryName))
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 2
diff changeset
    78
            throw new Error(String.format("expected jar %s to contain %s",
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 2
diff changeset
    79
                                          jarFile, entryName));
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 2
diff changeset
    80
    }
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 2
diff changeset
    81
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 2
diff changeset
    82
    static void testIndex(String jarName) throws IOException {
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 2
diff changeset
    83
        System.err.printf("jarName=%s%n", jarName);
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 2
diff changeset
    84
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 2
diff changeset
    85
        File jar = new File(jarName);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        // Create a jar to be indexed.
3218
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 2
diff changeset
    88
        run("cf", jarName, "-C", contents, SERVICES);
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 2
diff changeset
    89
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 2
diff changeset
    90
        for (int i = 0; i < 2; i++) {
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 2
diff changeset
    91
            run("i", jarName);
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 2
diff changeset
    92
            checkContains(jar, INDEX);
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 2
diff changeset
    93
            checkContains(jar, SERVICES);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        JarFile f = new JarFile(jarName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        BufferedReader index =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            new BufferedReader(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                    new InputStreamReader(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                            f.getInputStream(f.getJarEntry(INDEX))));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        String line;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        while ((line = index.readLine()) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            if (line.equals(SERVICES)) {
5813
8e455cd3e807 6962617: Testcase changes, cleanup of problem list for jdk_tools targets
ohair
parents: 5506
diff changeset
   104
                index.close();
8e455cd3e807 6962617: Testcase changes, cleanup of problem list for jdk_tools targets
ohair
parents: 5506
diff changeset
   105
                f.close();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        }
5813
8e455cd3e807 6962617: Testcase changes, cleanup of problem list for jdk_tools targets
ohair
parents: 5506
diff changeset
   109
        index.close();
8e455cd3e807 6962617: Testcase changes, cleanup of problem list for jdk_tools targets
ohair
parents: 5506
diff changeset
   110
        f.close();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        throw new Error(SERVICES + " not indexed.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    }
3218
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 2
diff changeset
   113
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 2
diff changeset
   114
    public static void main(String[] args) throws IOException {
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 2
diff changeset
   115
        testIndex("a.jar");             // a path with parent == null
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 2
diff changeset
   116
        testIndex("./a.zip");           // a path with parent != null
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 2
diff changeset
   117
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 2
diff changeset
   118
        // Try indexing a jar in the default temp directory.
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 2
diff changeset
   119
        File tmpFile = File.createTempFile("MetaInf", null, null);
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 2
diff changeset
   120
        try {
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 2
diff changeset
   121
            testIndex(tmpFile.getPath());
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 2
diff changeset
   122
        } finally {
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 2
diff changeset
   123
            tmpFile.delete();
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 2
diff changeset
   124
        }
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 2
diff changeset
   125
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
}