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 } 64 65 File createJar() throws Exception { 66 File testJar = new File(System.getProperty("test.classes"), "T8076104-test.jar"); 67 testJar.delete(); 68 try (OutputStream fileOut = new FileOutputStream(testJar); 69 JarOutputStream jarOut = new JarOutputStream(new BufferedOutputStream(fileOut))) { 70 jarOut.putNextEntry(new JarEntry("d1/A.java")); 71 jarOut.write("1".getBytes()); 72 jarOut.putNextEntry(new JarEntry("d2/A.java")); 73 jarOut.write("2".getBytes()); 74 } 75 76 return testJar; 77 } 78 79 JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); 80 81 void doTest(File testJar, String... additionalArgs) { 82 List<String> options = new ArrayList<>(); 83 options.add("-proc:only"); 84 options.add("-processor"); 85 options.add("T8076104"); 86 options.add("-classpath"); 87 options.add(System.getProperty("test.classes") + File.pathSeparator + testJar.getAbsolutePath()); 88 options.addAll(Arrays.asList(additionalArgs)); 89 options.add(System.getProperty("test.src") + File.separator + "T8076104.java"); 90 91 int res = Main.compile(options.toArray(new String[0])); 92 93 if (res != 0) { 94 throw new AssertionError("Unexpected error code: " + res); 95 } 96 } 97 98 @Override 99 public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) { 100 assertFileContent("d1/A.java", "1"); 101 assertFileContent("d2/A.java", "2"); 102 return false; 103 } 104 105 void assertFileContent(String relPath, 106 String expectedContent) { 107 try { 108 FileObject fo = processingEnv.getFiler() 109 .getResource(StandardLocation.CLASS_PATH, "", relPath); 110 String actualContent = fo.getCharContent(false).toString(); 111 112 if (!expectedContent.equals(actualContent)) { 113 throw new AssertionError("Actual content not matching the expected content: " + 114 actualContent); 115 } 116 } catch (IOException ex) { 117 throw new AssertionError("Unexpected exception: ", ex); 118 } 119 } 120 121 @Override 122 public SourceVersion getSupportedSourceVersion() { 123 return SourceVersion.latest(); 124 } 125} 126