EventsBalancedTest.java revision 3643:589ff4d43428
1/* 2 * Copyright (c) 2014, 2016, 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 8040822 27 * @summary Check that all TaskEvents are balanced. 28 * @modules jdk.compiler/com.sun.tools.javac.api 29 * jdk.compiler/com.sun.tools.javac.comp 30 * jdk.compiler/com.sun.tools.javac.file 31 */ 32 33import java.io.*; 34import java.net.URI; 35import java.util.*; 36import java.util.Map.Entry; 37 38import javax.tools.*; 39 40import com.sun.source.util.*; 41import com.sun.source.util.TaskEvent.Kind; 42import com.sun.tools.javac.api.JavacTool; 43import com.sun.tools.javac.comp.CompileStates.CompileState; 44 45public class EventsBalancedTest { 46 JavacTool tool = (JavacTool) ToolProvider.getSystemJavaCompiler(); 47 StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null); 48 49 public static void main(String... args) throws IOException { 50 EventsBalancedTest t = new EventsBalancedTest(); 51 try { 52 t.test(); 53 } finally { 54 t.fm.close(); 55 } 56 } 57 58 void test() throws IOException { 59 TestSource a = new TestSource("B", "class B extends A { }"); 60 TestSource b = new TestSource("A", "abstract class A { }"); 61 62 test(null, Arrays.asList(a, b)); 63 test(null, Arrays.asList(b, a)); 64 65 for (CompileState stop : CompileState.values()) { 66 test(Arrays.asList("--should-stop:ifNoError=" + stop, 67 "--should-stop:ifError=" + stop), 68 Arrays.asList(a, b)); 69 test(Arrays.asList("--should-stop:ifNoError=" + stop, 70 "--should-stop:ifError=" + stop), 71 Arrays.asList(b, a)); 72 } 73 } 74 75 void test(List<String> options, List<JavaFileObject> files) throws IOException { 76 System.err.println("testing: " + options + ", " + files); 77 TestListener listener = new TestListener(); 78 JavacTask task = tool.getTask(null, fm, null, options, null, files); 79 80 task.setTaskListener(listener); 81 82 task.call(); 83 84 for (Entry<Kind, Integer> e : listener.kind2Count.entrySet()) { 85 if (e.getValue() != null && e.getValue() != 0) { 86 throw new IllegalStateException("Not balanced event: " + e.getKey()); 87 } 88 } 89 } 90 91 static class TestListener implements TaskListener { 92 final Map<Kind, Integer> kind2Count = new HashMap<>(); 93 94 int get(Kind k) { 95 Integer count = kind2Count.get(k); 96 97 if (count == null) 98 kind2Count.put(k, count = 0); 99 100 return count; 101 } 102 103 @Override 104 public void started(TaskEvent e) { 105 kind2Count.put(e.getKind(), get(e.getKind()) + 1); 106 } 107 108 @Override 109 public void finished(TaskEvent e) { 110 int count = get(e.getKind()); 111 112 if (count <= 0) 113 throw new IllegalStateException("count<=0 for: " + e.getKind()); 114 115 kind2Count.put(e.getKind(), count - 1); 116 } 117 118 } 119 static class TestSource extends SimpleJavaFileObject { 120 final String content; 121 public TestSource(String fileName, String content) { 122 super(URI.create("myfo:/" + fileName + ".java"), JavaFileObject.Kind.SOURCE); 123 this.content = content; 124 } 125 126 @Override 127 public CharSequence getCharContent(boolean ignoreEncodingErrors) { 128 return content; 129 } 130 } 131 132} 133