langtools/test/tools/javac/api/taskListeners/EventsBalancedTest.java
author vromero
Mon, 11 Jul 2016 15:27:52 -0700
changeset 39601 5b37e511ae4b
parent 36153 ed5063b304be
child 40835 6ab9ed1abc46
permissions -rw-r--r--
8161020: javac, fold stop compilation options Reviewed-by: mcimadamore
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
24392
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
     1
/*
36153
ed5063b304be 8149600: javac, remove unused options, step 2
vromero
parents: 30730
diff changeset
     2
 * Copyright (c) 2014, 2016, Oracle and/or its affiliates. All rights reserved.
24392
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
     4
 *
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
     7
 * published by the Free Software Foundation.
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
     8
 *
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
    13
 * accompanied this code).
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
    14
 *
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
    18
 *
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
    21
 * questions.
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
    22
 */
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
    23
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
    24
/*
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
    25
 * @test
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
    26
 * @bug     8040822
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
    27
 * @summary Check that all TaskEvents are balanced.
30730
d3ce7619db2c 8076543: Add @modules as needed to the langtools tests
akulyakh
parents: 27319
diff changeset
    28
 * @modules jdk.compiler/com.sun.tools.javac.api
d3ce7619db2c 8076543: Add @modules as needed to the langtools tests
akulyakh
parents: 27319
diff changeset
    29
 *          jdk.compiler/com.sun.tools.javac.comp
d3ce7619db2c 8076543: Add @modules as needed to the langtools tests
akulyakh
parents: 27319
diff changeset
    30
 *          jdk.compiler/com.sun.tools.javac.file
24392
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
    31
 */
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
    32
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
    33
import java.io.*;
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
    34
import java.net.URI;
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
    35
import java.util.*;
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
    36
import java.util.Map.Entry;
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
    37
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
    38
import javax.tools.*;
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
    39
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
    40
import com.sun.source.util.*;
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
    41
import com.sun.source.util.TaskEvent.Kind;
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
    42
import com.sun.tools.javac.api.JavacTool;
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
    43
import com.sun.tools.javac.comp.CompileStates.CompileState;
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
    44
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
    45
public class EventsBalancedTest {
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
    46
    JavacTool tool = (JavacTool) ToolProvider.getSystemJavaCompiler();
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
    47
    StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null);
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
    48
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
    49
    public static void main(String... args) throws IOException {
27319
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 26264
diff changeset
    50
        EventsBalancedTest t = new EventsBalancedTest();
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 26264
diff changeset
    51
        try {
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 26264
diff changeset
    52
            t.test();
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 26264
diff changeset
    53
        } finally {
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 26264
diff changeset
    54
            t.fm.close();
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 26264
diff changeset
    55
        }
24392
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
    56
    }
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
    57
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
    58
    void test() throws IOException {
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
    59
        TestSource a = new TestSource("B", "class B extends A { }");
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
    60
        TestSource b = new TestSource("A", "abstract class A { }");
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
    61
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
    62
        test(null, Arrays.asList(a, b));
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
    63
        test(null, Arrays.asList(b, a));
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
    64
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
    65
        for (CompileState stop : CompileState.values()) {
39601
5b37e511ae4b 8161020: javac, fold stop compilation options
vromero
parents: 36153
diff changeset
    66
            test(Arrays.asList("-Xshouldstop:ifNoError=" + stop,
5b37e511ae4b 8161020: javac, fold stop compilation options
vromero
parents: 36153
diff changeset
    67
                               "-Xshouldstop:ifError=" + stop),
24392
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
    68
                 Arrays.asList(a, b));
39601
5b37e511ae4b 8161020: javac, fold stop compilation options
vromero
parents: 36153
diff changeset
    69
            test(Arrays.asList("-Xshouldstop:ifNoError=" + stop,
5b37e511ae4b 8161020: javac, fold stop compilation options
vromero
parents: 36153
diff changeset
    70
                               "-Xshouldstop:ifError=" + stop),
24392
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
    71
                 Arrays.asList(b, a));
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
    72
        }
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
    73
    }
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
    74
26264
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 24392
diff changeset
    75
    void test(List<String> options, List<JavaFileObject> files) throws IOException {
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 24392
diff changeset
    76
        System.err.println("testing: " + options + ", " + files);
24392
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
    77
        TestListener listener = new TestListener();
26264
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 24392
diff changeset
    78
        JavacTask task = tool.getTask(null, fm, null, options, null, files);
24392
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
    79
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
    80
        task.setTaskListener(listener);
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
    81
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
    82
        task.call();
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
    83
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
    84
        for (Entry<Kind, Integer> e : listener.kind2Count.entrySet()) {
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
    85
            if (e.getValue() != null && e.getValue() != 0) {
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
    86
                throw new IllegalStateException("Not balanced event: " + e.getKey());
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
    87
            }
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
    88
        }
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
    89
    }
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
    90
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
    91
    static class TestListener implements TaskListener {
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
    92
        final Map<Kind, Integer> kind2Count = new HashMap<>();
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
    93
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
    94
        int get(Kind k) {
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
    95
            Integer count = kind2Count.get(k);
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
    96
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
    97
            if (count == null)
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
    98
                kind2Count.put(k, count = 0);
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
    99
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
   100
            return count;
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
   101
        }
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
   102
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
   103
        @Override
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
   104
        public void started(TaskEvent e) {
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
   105
            kind2Count.put(e.getKind(), get(e.getKind()) + 1);
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
   106
        }
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
   107
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
   108
        @Override
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
   109
        public void finished(TaskEvent e) {
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
   110
            int count = get(e.getKind());
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
   111
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
   112
            if (count <= 0)
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
   113
                throw new IllegalStateException("count<=0 for: " + e.getKind());
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
   114
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
   115
            kind2Count.put(e.getKind(), count - 1);
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
   116
        }
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
   117
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
   118
    }
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
   119
    static class TestSource extends SimpleJavaFileObject {
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
   120
        final String content;
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
   121
        public TestSource(String fileName, String content) {
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
   122
            super(URI.create("myfo:/" + fileName + ".java"), JavaFileObject.Kind.SOURCE);
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
   123
            this.content = content;
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
   124
        }
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
   125
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
   126
        @Override
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
   127
        public CharSequence getCharContent(boolean ignoreEncodingErrors) {
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
   128
            return content;
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
   129
        }
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
   130
    }
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
   131
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
   132
}