langtools/test/tools/javac/api/taskListeners/EventsBalancedTest.java
author jjg
Wed, 29 Oct 2014 17:25:23 -0700
changeset 27319 030080f03e4f
parent 26264 a09fedde76be
child 30730 d3ce7619db2c
permissions -rw-r--r--
8062348: langtools tests should close file manager (group 1) Reviewed-by: darcy
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
/*
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
     2
 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
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.
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
    28
 */
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
    29
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
    30
import java.io.*;
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
    31
import java.net.URI;
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
    32
import java.util.*;
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
    33
import java.util.Map.Entry;
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
    34
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
    35
import javax.tools.*;
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
    36
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
    37
import com.sun.source.util.*;
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
    38
import com.sun.source.util.TaskEvent.Kind;
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
    39
import com.sun.tools.javac.api.JavacTool;
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
    40
import com.sun.tools.javac.comp.CompileStates.CompileState;
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
    41
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
    42
public class EventsBalancedTest {
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
    43
    JavacTool tool = (JavacTool) ToolProvider.getSystemJavaCompiler();
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
    44
    StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null);
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
    45
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
    46
    public static void main(String... args) throws IOException {
27319
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 26264
diff changeset
    47
        EventsBalancedTest t = new EventsBalancedTest();
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 26264
diff changeset
    48
        try {
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 26264
diff changeset
    49
            t.test();
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 26264
diff changeset
    50
        } finally {
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 26264
diff changeset
    51
            t.fm.close();
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 26264
diff changeset
    52
        }
24392
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
    53
    }
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
    54
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
    55
    void test() throws IOException {
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
    56
        TestSource a = new TestSource("B", "class B extends A { }");
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
    57
        TestSource b = new TestSource("A", "abstract class A { }");
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
    58
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
    59
        test(null, Arrays.asList(a, b));
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
    60
        test(null, Arrays.asList(b, a));
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
    61
        test(Arrays.asList("-XD-relax"), Arrays.asList(a, b));
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
    62
        test(Arrays.asList("-XD-relax"), Arrays.asList(b, a));
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
    63
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
    64
        for (CompileState stop : CompileState.values()) {
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
    65
            test(Arrays.asList("-XDshouldStopPolicyIfNoError=" + stop,
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
    66
                               "-XDshouldStopPolicyIfError=" + stop),
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
    67
                 Arrays.asList(a, b));
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
    68
            test(Arrays.asList("-XDshouldStopPolicyIfNoError=" + stop,
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
    69
                               "-XDshouldStopPolicyIfError=" + stop),
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
    70
                 Arrays.asList(b, a));
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
    71
        }
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
26264
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 24392
diff changeset
    74
    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
    75
        System.err.println("testing: " + options + ", " + files);
24392
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
    76
        TestListener listener = new TestListener();
26264
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 24392
diff changeset
    77
        JavacTask task = tool.getTask(null, fm, null, options, null, files);
24392
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
    78
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
    79
        task.setTaskListener(listener);
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
    80
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
    81
        task.call();
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
    82
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
    83
        for (Entry<Kind, Integer> e : listener.kind2Count.entrySet()) {
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
    84
            if (e.getValue() != null && e.getValue() != 0) {
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
    85
                throw new IllegalStateException("Not balanced event: " + e.getKey());
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
    86
            }
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
    static class TestListener implements TaskListener {
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
    91
        final Map<Kind, Integer> kind2Count = new HashMap<>();
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
    92
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
    93
        int get(Kind k) {
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
    94
            Integer count = kind2Count.get(k);
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
    95
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
    96
            if (count == null)
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
    97
                kind2Count.put(k, count = 0);
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
    98
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
    99
            return count;
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
   100
        }
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
        @Override
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
   103
        public void started(TaskEvent e) {
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
   104
            kind2Count.put(e.getKind(), get(e.getKind()) + 1);
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
   105
        }
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
        @Override
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
   108
        public void finished(TaskEvent e) {
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
   109
            int count = get(e.getKind());
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
   110
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
   111
            if (count <= 0)
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
   112
                throw new IllegalStateException("count<=0 for: " + e.getKind());
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
   113
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
   114
            kind2Count.put(e.getKind(), count - 1);
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
   115
        }
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
    static class TestSource extends SimpleJavaFileObject {
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
   119
        final String content;
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
   120
        public TestSource(String fileName, String content) {
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
   121
            super(URI.create("myfo:/" + fileName + ".java"), JavaFileObject.Kind.SOURCE);
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
   122
            this.content = content;
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
   123
        }
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
        @Override
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
   126
        public CharSequence getCharContent(boolean ignoreEncodingErrors) {
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
   127
            return content;
aca305440fb9 8040822: Duplicated notifications can be sent to TaskListener
jlahoda
parents:
diff changeset
   128
        }
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
}