langtools/test/tools/javac/api/taskListeners/EventsBalancedTest.java
changeset 24392 aca305440fb9
child 26264 a09fedde76be
equal deleted inserted replaced
24301:1a30593dcb98 24392:aca305440fb9
       
     1 /*
       
     2  * Copyright (c) 2014, 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  */
       
    29 
       
    30 import java.io.*;
       
    31 import java.net.URI;
       
    32 import java.util.*;
       
    33 import java.util.Map.Entry;
       
    34 
       
    35 import javax.tools.*;
       
    36 
       
    37 import com.sun.source.util.*;
       
    38 import com.sun.source.util.TaskEvent.Kind;
       
    39 import com.sun.tools.javac.api.JavacTool;
       
    40 import com.sun.tools.javac.comp.CompileStates.CompileState;
       
    41 
       
    42 public class EventsBalancedTest {
       
    43     JavacTool tool = (JavacTool) ToolProvider.getSystemJavaCompiler();
       
    44     StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null);
       
    45 
       
    46     public static void main(String... args) throws IOException {
       
    47         new EventsBalancedTest().test();
       
    48     }
       
    49 
       
    50     void test() throws IOException {
       
    51         TestSource a = new TestSource("B", "class B extends A { }");
       
    52         TestSource b = new TestSource("A", "abstract class A { }");
       
    53 
       
    54         test(null, Arrays.asList(a, b));
       
    55         test(null, Arrays.asList(b, a));
       
    56         test(Arrays.asList("-XD-relax"), Arrays.asList(a, b));
       
    57         test(Arrays.asList("-XD-relax"), Arrays.asList(b, a));
       
    58 
       
    59         for (CompileState stop : CompileState.values()) {
       
    60             test(Arrays.asList("-XDshouldStopPolicyIfNoError=" + stop,
       
    61                                "-XDshouldStopPolicyIfError=" + stop),
       
    62                  Arrays.asList(a, b));
       
    63             test(Arrays.asList("-XDshouldStopPolicyIfNoError=" + stop,
       
    64                                "-XDshouldStopPolicyIfError=" + stop),
       
    65                  Arrays.asList(b, a));
       
    66         }
       
    67     }
       
    68 
       
    69     void test(Iterable<String> options, Iterable<JavaFileObject> files) throws IOException {
       
    70         StringWriter sw = new StringWriter();
       
    71         PrintWriter pw = new PrintWriter(sw);
       
    72         TestListener listener = new TestListener();
       
    73         JavacTask task = tool.getTask(pw, fm, null, options, null, files);
       
    74 
       
    75         task.setTaskListener(listener);
       
    76 
       
    77         task.call();
       
    78 
       
    79         for (Entry<Kind, Integer> e : listener.kind2Count.entrySet()) {
       
    80             if (e.getValue() != null && e.getValue() != 0) {
       
    81                 throw new IllegalStateException("Not balanced event: " + e.getKey());
       
    82             }
       
    83         }
       
    84     }
       
    85 
       
    86     static class TestListener implements TaskListener {
       
    87         final Map<Kind, Integer> kind2Count = new HashMap<>();
       
    88 
       
    89         int get(Kind k) {
       
    90             Integer count = kind2Count.get(k);
       
    91 
       
    92             if (count == null)
       
    93                 kind2Count.put(k, count = 0);
       
    94 
       
    95             return count;
       
    96         }
       
    97 
       
    98         @Override
       
    99         public void started(TaskEvent e) {
       
   100             kind2Count.put(e.getKind(), get(e.getKind()) + 1);
       
   101         }
       
   102 
       
   103         @Override
       
   104         public void finished(TaskEvent e) {
       
   105             int count = get(e.getKind());
       
   106 
       
   107             if (count <= 0)
       
   108                 throw new IllegalStateException("count<=0 for: " + e.getKind());
       
   109 
       
   110             kind2Count.put(e.getKind(), count - 1);
       
   111         }
       
   112 
       
   113     }
       
   114     static class TestSource extends SimpleJavaFileObject {
       
   115         final String content;
       
   116         public TestSource(String fileName, String content) {
       
   117             super(URI.create("myfo:/" + fileName + ".java"), JavaFileObject.Kind.SOURCE);
       
   118             this.content = content;
       
   119         }
       
   120 
       
   121         @Override
       
   122         public CharSequence getCharContent(boolean ignoreEncodingErrors) {
       
   123             return content;
       
   124         }
       
   125     }
       
   126 
       
   127 }