T8076104.java revision 2920:8a2e31525a95
1/* 2 * Copyright (c) 2015, 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 8076104 27 * @summary Verify that ZipFileIndexFileObject and ZipFileObject's getCharContent method 28 * do not return cached content for another file. 29 * @run main T8076104 30 */ 31import com.sun.tools.javac.Main; 32import java.io.BufferedOutputStream; 33import java.io.File; 34import java.io.FileOutputStream; 35import java.io.IOException; 36import java.io.OutputStream; 37import java.util.ArrayList; 38import java.util.Arrays; 39import java.util.List; 40import java.util.Set; 41import java.util.jar.JarEntry; 42import java.util.jar.JarOutputStream; 43import javax.annotation.processing.AbstractProcessor; 44import javax.annotation.processing.RoundEnvironment; 45import javax.annotation.processing.SupportedAnnotationTypes; 46import javax.lang.model.SourceVersion; 47import javax.lang.model.element.TypeElement; 48import javax.tools.FileObject; 49import javax.tools.JavaCompiler; 50import javax.tools.StandardLocation; 51import javax.tools.ToolProvider; 52 53@SupportedAnnotationTypes("*") 54public class T8076104 extends AbstractProcessor { 55 56 public static void main(String [] args) throws Exception { 57 new T8076104().run(); 58 } 59 60 void run() throws Exception { 61 File testJar = createJar(); 62 doTest(testJar); 63 doTest(testJar, "-XDuseOptimizedZip=false"); 64 } 65 66 File createJar() throws Exception { 67 File testJar = new File(System.getProperty("test.classes"), "T8076104-test.jar"); 68 testJar.delete(); 69 try (OutputStream fileOut = new FileOutputStream(testJar); 70 JarOutputStream jarOut = new JarOutputStream(new BufferedOutputStream(fileOut))) { 71 jarOut.putNextEntry(new JarEntry("d1/A.java")); 72 jarOut.write("1".getBytes()); 73 jarOut.putNextEntry(new JarEntry("d2/A.java")); 74 jarOut.write("2".getBytes()); 75 } 76 77 return testJar; 78 } 79 80 JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); 81 82 void doTest(File testJar, String... additionalArgs) { 83 List<String> options = new ArrayList<>(); 84 options.add("-proc:only"); 85 options.add("-processor"); 86 options.add("T8076104"); 87 options.add("-classpath"); 88 options.add(System.getProperty("test.classes") + File.pathSeparator + testJar.getAbsolutePath()); 89 options.addAll(Arrays.asList(additionalArgs)); 90 options.add(System.getProperty("test.src") + File.separator + "T8076104.java"); 91 92 int res = Main.compile(options.toArray(new String[0])); 93 94 if (res != 0) { 95 throw new AssertionError("Unexpected error code: " + res); 96 } 97 } 98 99 @Override 100 public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) { 101 assertFileContent("d1/A.java", "1"); 102 assertFileContent("d2/A.java", "2"); 103 return false; 104 } 105 106 void assertFileContent(String relPath, 107 String expectedContent) { 108 try { 109 FileObject fo = processingEnv.getFiler() 110 .getResource(StandardLocation.CLASS_PATH, "", relPath); 111 String actualContent = fo.getCharContent(false).toString(); 112 113 if (!expectedContent.equals(actualContent)) { 114 throw new AssertionError("Actual content not matching the expected content: " + 115 actualContent); 116 } 117 } catch (IOException ex) { 118 throw new AssertionError("Unexpected exception: ", ex); 119 } 120 } 121 122 @Override 123 public SourceVersion getSupportedSourceVersion() { 124 return SourceVersion.latest(); 125 } 126} 127