EventsBalancedTest.java revision 2599:50b448c5be54
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
30import java.io.*;
31import java.net.URI;
32import java.util.*;
33import java.util.Map.Entry;
34
35import javax.tools.*;
36
37import com.sun.source.util.*;
38import com.sun.source.util.TaskEvent.Kind;
39import com.sun.tools.javac.api.JavacTool;
40import com.sun.tools.javac.comp.CompileStates.CompileState;
41
42public 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(List<String> options, List<JavaFileObject> files) throws IOException {
70        System.err.println("testing: " + options + ", " + files);
71        TestListener listener = new TestListener();
72        JavacTask task = tool.getTask(null, fm, null, options, null, files);
73
74        task.setTaskListener(listener);
75
76        task.call();
77
78        for (Entry<Kind, Integer> e : listener.kind2Count.entrySet()) {
79            if (e.getValue() != null && e.getValue() != 0) {
80                throw new IllegalStateException("Not balanced event: " + e.getKey());
81            }
82        }
83    }
84
85    static class TestListener implements TaskListener {
86        final Map<Kind, Integer> kind2Count = new HashMap<>();
87
88        int get(Kind k) {
89            Integer count = kind2Count.get(k);
90
91            if (count == null)
92                kind2Count.put(k, count = 0);
93
94            return count;
95        }
96
97        @Override
98        public void started(TaskEvent e) {
99            kind2Count.put(e.getKind(), get(e.getKind()) + 1);
100        }
101
102        @Override
103        public void finished(TaskEvent e) {
104            int count = get(e.getKind());
105
106            if (count <= 0)
107                throw new IllegalStateException("count<=0 for: " + e.getKind());
108
109            kind2Count.put(e.getKind(), count - 1);
110        }
111
112    }
113    static class TestSource extends SimpleJavaFileObject {
114        final String content;
115        public TestSource(String fileName, String content) {
116            super(URI.create("myfo:/" + fileName + ".java"), JavaFileObject.Kind.SOURCE);
117            this.content = content;
118        }
119
120        @Override
121        public CharSequence getCharContent(boolean ignoreEncodingErrors) {
122            return content;
123        }
124    }
125
126}
127