langtools/test/tools/javac/file/zip/T8076104.java
author jjg
Mon, 07 Dec 2015 14:02:55 -0800
changeset 34560 b6a567b677f7
parent 30717 4ed55656acd7
permissions -rw-r--r--
8059976: Convert JavacFileManager to use java.nio.file internally Reviewed-by: jlahoda
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
30717
4ed55656acd7 8076104: Key collisions in ZipFileIndexFileObject content cache lead to wrong content
jlahoda
parents:
diff changeset
     1
/*
4ed55656acd7 8076104: Key collisions in ZipFileIndexFileObject content cache lead to wrong content
jlahoda
parents:
diff changeset
     2
 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
4ed55656acd7 8076104: Key collisions in ZipFileIndexFileObject content cache lead to wrong content
jlahoda
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4ed55656acd7 8076104: Key collisions in ZipFileIndexFileObject content cache lead to wrong content
jlahoda
parents:
diff changeset
     4
 *
4ed55656acd7 8076104: Key collisions in ZipFileIndexFileObject content cache lead to wrong content
jlahoda
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
4ed55656acd7 8076104: Key collisions in ZipFileIndexFileObject content cache lead to wrong content
jlahoda
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
4ed55656acd7 8076104: Key collisions in ZipFileIndexFileObject content cache lead to wrong content
jlahoda
parents:
diff changeset
     7
 * published by the Free Software Foundation.
4ed55656acd7 8076104: Key collisions in ZipFileIndexFileObject content cache lead to wrong content
jlahoda
parents:
diff changeset
     8
 *
4ed55656acd7 8076104: Key collisions in ZipFileIndexFileObject content cache lead to wrong content
jlahoda
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
4ed55656acd7 8076104: Key collisions in ZipFileIndexFileObject content cache lead to wrong content
jlahoda
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
4ed55656acd7 8076104: Key collisions in ZipFileIndexFileObject content cache lead to wrong content
jlahoda
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
4ed55656acd7 8076104: Key collisions in ZipFileIndexFileObject content cache lead to wrong content
jlahoda
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
4ed55656acd7 8076104: Key collisions in ZipFileIndexFileObject content cache lead to wrong content
jlahoda
parents:
diff changeset
    13
 * accompanied this code).
4ed55656acd7 8076104: Key collisions in ZipFileIndexFileObject content cache lead to wrong content
jlahoda
parents:
diff changeset
    14
 *
4ed55656acd7 8076104: Key collisions in ZipFileIndexFileObject content cache lead to wrong content
jlahoda
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
4ed55656acd7 8076104: Key collisions in ZipFileIndexFileObject content cache lead to wrong content
jlahoda
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
4ed55656acd7 8076104: Key collisions in ZipFileIndexFileObject content cache lead to wrong content
jlahoda
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
4ed55656acd7 8076104: Key collisions in ZipFileIndexFileObject content cache lead to wrong content
jlahoda
parents:
diff changeset
    18
 *
4ed55656acd7 8076104: Key collisions in ZipFileIndexFileObject content cache lead to wrong content
jlahoda
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
4ed55656acd7 8076104: Key collisions in ZipFileIndexFileObject content cache lead to wrong content
jlahoda
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
4ed55656acd7 8076104: Key collisions in ZipFileIndexFileObject content cache lead to wrong content
jlahoda
parents:
diff changeset
    21
 * questions.
4ed55656acd7 8076104: Key collisions in ZipFileIndexFileObject content cache lead to wrong content
jlahoda
parents:
diff changeset
    22
 */
4ed55656acd7 8076104: Key collisions in ZipFileIndexFileObject content cache lead to wrong content
jlahoda
parents:
diff changeset
    23
4ed55656acd7 8076104: Key collisions in ZipFileIndexFileObject content cache lead to wrong content
jlahoda
parents:
diff changeset
    24
/**
4ed55656acd7 8076104: Key collisions in ZipFileIndexFileObject content cache lead to wrong content
jlahoda
parents:
diff changeset
    25
 * @test
4ed55656acd7 8076104: Key collisions in ZipFileIndexFileObject content cache lead to wrong content
jlahoda
parents:
diff changeset
    26
 * @bug 8076104
4ed55656acd7 8076104: Key collisions in ZipFileIndexFileObject content cache lead to wrong content
jlahoda
parents:
diff changeset
    27
 * @summary Verify that ZipFileIndexFileObject and ZipFileObject's getCharContent method
4ed55656acd7 8076104: Key collisions in ZipFileIndexFileObject content cache lead to wrong content
jlahoda
parents:
diff changeset
    28
 *          do not return cached content for another file.
4ed55656acd7 8076104: Key collisions in ZipFileIndexFileObject content cache lead to wrong content
jlahoda
parents:
diff changeset
    29
 * @run main T8076104
4ed55656acd7 8076104: Key collisions in ZipFileIndexFileObject content cache lead to wrong content
jlahoda
parents:
diff changeset
    30
 */
4ed55656acd7 8076104: Key collisions in ZipFileIndexFileObject content cache lead to wrong content
jlahoda
parents:
diff changeset
    31
import com.sun.tools.javac.Main;
4ed55656acd7 8076104: Key collisions in ZipFileIndexFileObject content cache lead to wrong content
jlahoda
parents:
diff changeset
    32
import java.io.BufferedOutputStream;
4ed55656acd7 8076104: Key collisions in ZipFileIndexFileObject content cache lead to wrong content
jlahoda
parents:
diff changeset
    33
import java.io.File;
4ed55656acd7 8076104: Key collisions in ZipFileIndexFileObject content cache lead to wrong content
jlahoda
parents:
diff changeset
    34
import java.io.FileOutputStream;
4ed55656acd7 8076104: Key collisions in ZipFileIndexFileObject content cache lead to wrong content
jlahoda
parents:
diff changeset
    35
import java.io.IOException;
4ed55656acd7 8076104: Key collisions in ZipFileIndexFileObject content cache lead to wrong content
jlahoda
parents:
diff changeset
    36
import java.io.OutputStream;
4ed55656acd7 8076104: Key collisions in ZipFileIndexFileObject content cache lead to wrong content
jlahoda
parents:
diff changeset
    37
import java.util.ArrayList;
4ed55656acd7 8076104: Key collisions in ZipFileIndexFileObject content cache lead to wrong content
jlahoda
parents:
diff changeset
    38
import java.util.Arrays;
4ed55656acd7 8076104: Key collisions in ZipFileIndexFileObject content cache lead to wrong content
jlahoda
parents:
diff changeset
    39
import java.util.List;
4ed55656acd7 8076104: Key collisions in ZipFileIndexFileObject content cache lead to wrong content
jlahoda
parents:
diff changeset
    40
import java.util.Set;
4ed55656acd7 8076104: Key collisions in ZipFileIndexFileObject content cache lead to wrong content
jlahoda
parents:
diff changeset
    41
import java.util.jar.JarEntry;
4ed55656acd7 8076104: Key collisions in ZipFileIndexFileObject content cache lead to wrong content
jlahoda
parents:
diff changeset
    42
import java.util.jar.JarOutputStream;
4ed55656acd7 8076104: Key collisions in ZipFileIndexFileObject content cache lead to wrong content
jlahoda
parents:
diff changeset
    43
import javax.annotation.processing.AbstractProcessor;
4ed55656acd7 8076104: Key collisions in ZipFileIndexFileObject content cache lead to wrong content
jlahoda
parents:
diff changeset
    44
import javax.annotation.processing.RoundEnvironment;
4ed55656acd7 8076104: Key collisions in ZipFileIndexFileObject content cache lead to wrong content
jlahoda
parents:
diff changeset
    45
import javax.annotation.processing.SupportedAnnotationTypes;
4ed55656acd7 8076104: Key collisions in ZipFileIndexFileObject content cache lead to wrong content
jlahoda
parents:
diff changeset
    46
import javax.lang.model.SourceVersion;
4ed55656acd7 8076104: Key collisions in ZipFileIndexFileObject content cache lead to wrong content
jlahoda
parents:
diff changeset
    47
import javax.lang.model.element.TypeElement;
4ed55656acd7 8076104: Key collisions in ZipFileIndexFileObject content cache lead to wrong content
jlahoda
parents:
diff changeset
    48
import javax.tools.FileObject;
4ed55656acd7 8076104: Key collisions in ZipFileIndexFileObject content cache lead to wrong content
jlahoda
parents:
diff changeset
    49
import javax.tools.JavaCompiler;
4ed55656acd7 8076104: Key collisions in ZipFileIndexFileObject content cache lead to wrong content
jlahoda
parents:
diff changeset
    50
import javax.tools.StandardLocation;
4ed55656acd7 8076104: Key collisions in ZipFileIndexFileObject content cache lead to wrong content
jlahoda
parents:
diff changeset
    51
import javax.tools.ToolProvider;
4ed55656acd7 8076104: Key collisions in ZipFileIndexFileObject content cache lead to wrong content
jlahoda
parents:
diff changeset
    52
4ed55656acd7 8076104: Key collisions in ZipFileIndexFileObject content cache lead to wrong content
jlahoda
parents:
diff changeset
    53
@SupportedAnnotationTypes("*")
4ed55656acd7 8076104: Key collisions in ZipFileIndexFileObject content cache lead to wrong content
jlahoda
parents:
diff changeset
    54
public class T8076104 extends AbstractProcessor {
4ed55656acd7 8076104: Key collisions in ZipFileIndexFileObject content cache lead to wrong content
jlahoda
parents:
diff changeset
    55
4ed55656acd7 8076104: Key collisions in ZipFileIndexFileObject content cache lead to wrong content
jlahoda
parents:
diff changeset
    56
    public static void main(String [] args) throws Exception {
4ed55656acd7 8076104: Key collisions in ZipFileIndexFileObject content cache lead to wrong content
jlahoda
parents:
diff changeset
    57
        new T8076104().run();
4ed55656acd7 8076104: Key collisions in ZipFileIndexFileObject content cache lead to wrong content
jlahoda
parents:
diff changeset
    58
    }
4ed55656acd7 8076104: Key collisions in ZipFileIndexFileObject content cache lead to wrong content
jlahoda
parents:
diff changeset
    59
4ed55656acd7 8076104: Key collisions in ZipFileIndexFileObject content cache lead to wrong content
jlahoda
parents:
diff changeset
    60
    void run() throws Exception {
4ed55656acd7 8076104: Key collisions in ZipFileIndexFileObject content cache lead to wrong content
jlahoda
parents:
diff changeset
    61
        File testJar = createJar();
4ed55656acd7 8076104: Key collisions in ZipFileIndexFileObject content cache lead to wrong content
jlahoda
parents:
diff changeset
    62
        doTest(testJar);
4ed55656acd7 8076104: Key collisions in ZipFileIndexFileObject content cache lead to wrong content
jlahoda
parents:
diff changeset
    63
    }
4ed55656acd7 8076104: Key collisions in ZipFileIndexFileObject content cache lead to wrong content
jlahoda
parents:
diff changeset
    64
4ed55656acd7 8076104: Key collisions in ZipFileIndexFileObject content cache lead to wrong content
jlahoda
parents:
diff changeset
    65
    File createJar() throws Exception {
4ed55656acd7 8076104: Key collisions in ZipFileIndexFileObject content cache lead to wrong content
jlahoda
parents:
diff changeset
    66
        File testJar = new File(System.getProperty("test.classes"), "T8076104-test.jar");
4ed55656acd7 8076104: Key collisions in ZipFileIndexFileObject content cache lead to wrong content
jlahoda
parents:
diff changeset
    67
        testJar.delete();
4ed55656acd7 8076104: Key collisions in ZipFileIndexFileObject content cache lead to wrong content
jlahoda
parents:
diff changeset
    68
        try (OutputStream fileOut = new FileOutputStream(testJar);
4ed55656acd7 8076104: Key collisions in ZipFileIndexFileObject content cache lead to wrong content
jlahoda
parents:
diff changeset
    69
             JarOutputStream jarOut = new JarOutputStream(new BufferedOutputStream(fileOut))) {
4ed55656acd7 8076104: Key collisions in ZipFileIndexFileObject content cache lead to wrong content
jlahoda
parents:
diff changeset
    70
            jarOut.putNextEntry(new JarEntry("d1/A.java"));
4ed55656acd7 8076104: Key collisions in ZipFileIndexFileObject content cache lead to wrong content
jlahoda
parents:
diff changeset
    71
            jarOut.write("1".getBytes());
4ed55656acd7 8076104: Key collisions in ZipFileIndexFileObject content cache lead to wrong content
jlahoda
parents:
diff changeset
    72
            jarOut.putNextEntry(new JarEntry("d2/A.java"));
4ed55656acd7 8076104: Key collisions in ZipFileIndexFileObject content cache lead to wrong content
jlahoda
parents:
diff changeset
    73
            jarOut.write("2".getBytes());
4ed55656acd7 8076104: Key collisions in ZipFileIndexFileObject content cache lead to wrong content
jlahoda
parents:
diff changeset
    74
        }
4ed55656acd7 8076104: Key collisions in ZipFileIndexFileObject content cache lead to wrong content
jlahoda
parents:
diff changeset
    75
4ed55656acd7 8076104: Key collisions in ZipFileIndexFileObject content cache lead to wrong content
jlahoda
parents:
diff changeset
    76
        return testJar;
4ed55656acd7 8076104: Key collisions in ZipFileIndexFileObject content cache lead to wrong content
jlahoda
parents:
diff changeset
    77
    }
4ed55656acd7 8076104: Key collisions in ZipFileIndexFileObject content cache lead to wrong content
jlahoda
parents:
diff changeset
    78
4ed55656acd7 8076104: Key collisions in ZipFileIndexFileObject content cache lead to wrong content
jlahoda
parents:
diff changeset
    79
    JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
4ed55656acd7 8076104: Key collisions in ZipFileIndexFileObject content cache lead to wrong content
jlahoda
parents:
diff changeset
    80
4ed55656acd7 8076104: Key collisions in ZipFileIndexFileObject content cache lead to wrong content
jlahoda
parents:
diff changeset
    81
    void doTest(File testJar, String... additionalArgs) {
4ed55656acd7 8076104: Key collisions in ZipFileIndexFileObject content cache lead to wrong content
jlahoda
parents:
diff changeset
    82
        List<String> options = new ArrayList<>();
4ed55656acd7 8076104: Key collisions in ZipFileIndexFileObject content cache lead to wrong content
jlahoda
parents:
diff changeset
    83
        options.add("-proc:only");
4ed55656acd7 8076104: Key collisions in ZipFileIndexFileObject content cache lead to wrong content
jlahoda
parents:
diff changeset
    84
        options.add("-processor");
4ed55656acd7 8076104: Key collisions in ZipFileIndexFileObject content cache lead to wrong content
jlahoda
parents:
diff changeset
    85
        options.add("T8076104");
4ed55656acd7 8076104: Key collisions in ZipFileIndexFileObject content cache lead to wrong content
jlahoda
parents:
diff changeset
    86
        options.add("-classpath");
4ed55656acd7 8076104: Key collisions in ZipFileIndexFileObject content cache lead to wrong content
jlahoda
parents:
diff changeset
    87
        options.add(System.getProperty("test.classes") + File.pathSeparator + testJar.getAbsolutePath());
4ed55656acd7 8076104: Key collisions in ZipFileIndexFileObject content cache lead to wrong content
jlahoda
parents:
diff changeset
    88
        options.addAll(Arrays.asList(additionalArgs));
4ed55656acd7 8076104: Key collisions in ZipFileIndexFileObject content cache lead to wrong content
jlahoda
parents:
diff changeset
    89
        options.add(System.getProperty("test.src") + File.separator + "T8076104.java");
4ed55656acd7 8076104: Key collisions in ZipFileIndexFileObject content cache lead to wrong content
jlahoda
parents:
diff changeset
    90
4ed55656acd7 8076104: Key collisions in ZipFileIndexFileObject content cache lead to wrong content
jlahoda
parents:
diff changeset
    91
        int res = Main.compile(options.toArray(new String[0]));
4ed55656acd7 8076104: Key collisions in ZipFileIndexFileObject content cache lead to wrong content
jlahoda
parents:
diff changeset
    92
4ed55656acd7 8076104: Key collisions in ZipFileIndexFileObject content cache lead to wrong content
jlahoda
parents:
diff changeset
    93
        if (res != 0) {
4ed55656acd7 8076104: Key collisions in ZipFileIndexFileObject content cache lead to wrong content
jlahoda
parents:
diff changeset
    94
            throw new AssertionError("Unexpected error code: " + res);
4ed55656acd7 8076104: Key collisions in ZipFileIndexFileObject content cache lead to wrong content
jlahoda
parents:
diff changeset
    95
        }
4ed55656acd7 8076104: Key collisions in ZipFileIndexFileObject content cache lead to wrong content
jlahoda
parents:
diff changeset
    96
    }
4ed55656acd7 8076104: Key collisions in ZipFileIndexFileObject content cache lead to wrong content
jlahoda
parents:
diff changeset
    97
4ed55656acd7 8076104: Key collisions in ZipFileIndexFileObject content cache lead to wrong content
jlahoda
parents:
diff changeset
    98
    @Override
4ed55656acd7 8076104: Key collisions in ZipFileIndexFileObject content cache lead to wrong content
jlahoda
parents:
diff changeset
    99
    public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
4ed55656acd7 8076104: Key collisions in ZipFileIndexFileObject content cache lead to wrong content
jlahoda
parents:
diff changeset
   100
        assertFileContent("d1/A.java", "1");
4ed55656acd7 8076104: Key collisions in ZipFileIndexFileObject content cache lead to wrong content
jlahoda
parents:
diff changeset
   101
        assertFileContent("d2/A.java", "2");
4ed55656acd7 8076104: Key collisions in ZipFileIndexFileObject content cache lead to wrong content
jlahoda
parents:
diff changeset
   102
        return false;
4ed55656acd7 8076104: Key collisions in ZipFileIndexFileObject content cache lead to wrong content
jlahoda
parents:
diff changeset
   103
    }
4ed55656acd7 8076104: Key collisions in ZipFileIndexFileObject content cache lead to wrong content
jlahoda
parents:
diff changeset
   104
4ed55656acd7 8076104: Key collisions in ZipFileIndexFileObject content cache lead to wrong content
jlahoda
parents:
diff changeset
   105
    void assertFileContent(String relPath,
4ed55656acd7 8076104: Key collisions in ZipFileIndexFileObject content cache lead to wrong content
jlahoda
parents:
diff changeset
   106
                           String expectedContent) {
4ed55656acd7 8076104: Key collisions in ZipFileIndexFileObject content cache lead to wrong content
jlahoda
parents:
diff changeset
   107
        try {
4ed55656acd7 8076104: Key collisions in ZipFileIndexFileObject content cache lead to wrong content
jlahoda
parents:
diff changeset
   108
            FileObject fo = processingEnv.getFiler()
4ed55656acd7 8076104: Key collisions in ZipFileIndexFileObject content cache lead to wrong content
jlahoda
parents:
diff changeset
   109
                                         .getResource(StandardLocation.CLASS_PATH, "", relPath);
4ed55656acd7 8076104: Key collisions in ZipFileIndexFileObject content cache lead to wrong content
jlahoda
parents:
diff changeset
   110
            String actualContent = fo.getCharContent(false).toString();
4ed55656acd7 8076104: Key collisions in ZipFileIndexFileObject content cache lead to wrong content
jlahoda
parents:
diff changeset
   111
4ed55656acd7 8076104: Key collisions in ZipFileIndexFileObject content cache lead to wrong content
jlahoda
parents:
diff changeset
   112
            if (!expectedContent.equals(actualContent)) {
4ed55656acd7 8076104: Key collisions in ZipFileIndexFileObject content cache lead to wrong content
jlahoda
parents:
diff changeset
   113
                throw new AssertionError("Actual content not matching the expected content: " +
4ed55656acd7 8076104: Key collisions in ZipFileIndexFileObject content cache lead to wrong content
jlahoda
parents:
diff changeset
   114
                                         actualContent);
4ed55656acd7 8076104: Key collisions in ZipFileIndexFileObject content cache lead to wrong content
jlahoda
parents:
diff changeset
   115
            }
4ed55656acd7 8076104: Key collisions in ZipFileIndexFileObject content cache lead to wrong content
jlahoda
parents:
diff changeset
   116
        } catch (IOException ex) {
4ed55656acd7 8076104: Key collisions in ZipFileIndexFileObject content cache lead to wrong content
jlahoda
parents:
diff changeset
   117
            throw new AssertionError("Unexpected exception: ", ex);
4ed55656acd7 8076104: Key collisions in ZipFileIndexFileObject content cache lead to wrong content
jlahoda
parents:
diff changeset
   118
        }
4ed55656acd7 8076104: Key collisions in ZipFileIndexFileObject content cache lead to wrong content
jlahoda
parents:
diff changeset
   119
    }
4ed55656acd7 8076104: Key collisions in ZipFileIndexFileObject content cache lead to wrong content
jlahoda
parents:
diff changeset
   120
4ed55656acd7 8076104: Key collisions in ZipFileIndexFileObject content cache lead to wrong content
jlahoda
parents:
diff changeset
   121
    @Override
4ed55656acd7 8076104: Key collisions in ZipFileIndexFileObject content cache lead to wrong content
jlahoda
parents:
diff changeset
   122
    public SourceVersion getSupportedSourceVersion() {
4ed55656acd7 8076104: Key collisions in ZipFileIndexFileObject content cache lead to wrong content
jlahoda
parents:
diff changeset
   123
        return SourceVersion.latest();
4ed55656acd7 8076104: Key collisions in ZipFileIndexFileObject content cache lead to wrong content
jlahoda
parents:
diff changeset
   124
    }
4ed55656acd7 8076104: Key collisions in ZipFileIndexFileObject content cache lead to wrong content
jlahoda
parents:
diff changeset
   125
}