EventsBalancedTest.java revision 2687:56f8be952a5c
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        EventsBalancedTest t = new EventsBalancedTest();
48        try {
49            t.test();
50        } finally {
51            t.fm.close();
52        }
53    }
54
55    void test() throws IOException {
56        TestSource a = new TestSource("B", "class B extends A { }");
57        TestSource b = new TestSource("A", "abstract class A { }");
58
59        test(null, Arrays.asList(a, b));
60        test(null, Arrays.asList(b, a));
61        test(Arrays.asList("-XD-relax"), Arrays.asList(a, b));
62        test(Arrays.asList("-XD-relax"), Arrays.asList(b, a));
63
64        for (CompileState stop : CompileState.values()) {
65            test(Arrays.asList("-XDshouldStopPolicyIfNoError=" + stop,
66                               "-XDshouldStopPolicyIfError=" + stop),
67                 Arrays.asList(a, b));
68            test(Arrays.asList("-XDshouldStopPolicyIfNoError=" + stop,
69                               "-XDshouldStopPolicyIfError=" + stop),
70                 Arrays.asList(b, a));
71        }
72    }
73
74    void test(List<String> options, List<JavaFileObject> files) throws IOException {
75        System.err.println("testing: " + options + ", " + files);
76        TestListener listener = new TestListener();
77        JavacTask task = tool.getTask(null, fm, null, options, null, files);
78
79        task.setTaskListener(listener);
80
81        task.call();
82
83        for (Entry<Kind, Integer> e : listener.kind2Count.entrySet()) {
84            if (e.getValue() != null && e.getValue() != 0) {
85                throw new IllegalStateException("Not balanced event: " + e.getKey());
86            }
87        }
88    }
89
90    static class TestListener implements TaskListener {
91        final Map<Kind, Integer> kind2Count = new HashMap<>();
92
93        int get(Kind k) {
94            Integer count = kind2Count.get(k);
95
96            if (count == null)
97                kind2Count.put(k, count = 0);
98
99            return count;
100        }
101
102        @Override
103        public void started(TaskEvent e) {
104            kind2Count.put(e.getKind(), get(e.getKind()) + 1);
105        }
106
107        @Override
108        public void finished(TaskEvent e) {
109            int count = get(e.getKind());
110
111            if (count <= 0)
112                throw new IllegalStateException("count<=0 for: " + e.getKind());
113
114            kind2Count.put(e.getKind(), count - 1);
115        }
116
117    }
118    static class TestSource extends SimpleJavaFileObject {
119        final String content;
120        public TestSource(String fileName, String content) {
121            super(URI.create("myfo:/" + fileName + ".java"), JavaFileObject.Kind.SOURCE);
122            this.content = content;
123        }
124
125        @Override
126        public CharSequence getCharContent(boolean ignoreEncodingErrors) {
127            return content;
128        }
129    }
130
131}
132