7160072: (javac) JavacParserTests needs cleanup

Mon, 11 Jun 2012 15:33:28 -0700

author
ksrini
date
Mon, 11 Jun 2012 15:33:28 -0700
changeset 1277
9cafabb5e576
parent 1271
252f8b7473e1
child 1278
e534aa747b22

7160072: (javac) JavacParserTests needs cleanup
Reviewed-by: jjg

test/tools/javac/parser/JavacParserTest.java file | annotate | diff | comparison | revisions
     1.1 --- a/test/tools/javac/parser/JavacParserTest.java	Tue Jun 05 17:40:33 2012 -0700
     1.2 +++ b/test/tools/javac/parser/JavacParserTest.java	Mon Jun 11 15:33:28 2012 -0700
     1.3 @@ -23,7 +23,7 @@
     1.4  
     1.5  /*
     1.6   * @test
     1.7 - * @bug 7073631 7159445
     1.8 + * @bug 7073631 7159445 7156633
     1.9   * @summary tests error and diagnostics positions
    1.10   * @author  Jan Lahoda
    1.11   */
    1.12 @@ -49,11 +49,17 @@
    1.13  import com.sun.tools.javac.api.JavacTaskImpl;
    1.14  import com.sun.tools.javac.tree.JCTree;
    1.15  import java.io.IOException;
    1.16 +import java.lang.annotation.ElementType;
    1.17 +import java.lang.annotation.Retention;
    1.18 +import java.lang.annotation.RetentionPolicy;
    1.19 +import java.lang.annotation.Target;
    1.20 +import java.lang.reflect.Method;
    1.21  import java.net.URI;
    1.22  import java.util.ArrayList;
    1.23  import java.util.Arrays;
    1.24  import java.util.LinkedList;
    1.25  import java.util.List;
    1.26 +import java.util.regex.Pattern;
    1.27  import javax.tools.Diagnostic;
    1.28  import javax.tools.DiagnosticCollector;
    1.29  import javax.tools.DiagnosticListener;
    1.30 @@ -63,13 +69,15 @@
    1.31  import javax.tools.ToolProvider;
    1.32  
    1.33  public class JavacParserTest extends TestCase {
    1.34 -    final JavaCompiler tool;
    1.35 -    public JavacParserTest(String testName) {
    1.36 -        tool = ToolProvider.getSystemJavaCompiler();
    1.37 -        System.out.println("java.home=" + System.getProperty("java.home"));
    1.38 +    static final JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
    1.39 +
    1.40 +    private JavacParserTest(){}
    1.41 +
    1.42 +    public static void main(String... args) throws Exception {
    1.43 +        new JavacParserTest().run(args);
    1.44      }
    1.45  
    1.46 -    static class MyFileObject extends SimpleJavaFileObject {
    1.47 +    class MyFileObject extends SimpleJavaFileObject {
    1.48  
    1.49          private String text;
    1.50  
    1.51 @@ -86,11 +94,11 @@
    1.52      /*
    1.53       * converts Windows to Unix style LFs for comparing strings
    1.54       */
    1.55 -    private String normalize(String in) {
    1.56 +    String normalize(String in) {
    1.57          return in.replace(System.getProperty("line.separator"), "\n");
    1.58      }
    1.59  
    1.60 -    public CompilationUnitTree getCompilationUnitTree(String code) throws IOException {
    1.61 +    CompilationUnitTree getCompilationUnitTree(String code) throws IOException {
    1.62  
    1.63          JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null, null,
    1.64                  null, Arrays.asList(new MyFileObject(code)));
    1.65 @@ -98,7 +106,7 @@
    1.66          return cut;
    1.67      }
    1.68  
    1.69 -    public List<String> getErroneousTreeValues(ErroneousTree node) {
    1.70 +    List<String> getErroneousTreeValues(ErroneousTree node) {
    1.71  
    1.72          List<String> values = new ArrayList<>();
    1.73          if (node.getErrorTrees() != null) {
    1.74 @@ -112,7 +120,8 @@
    1.75          return values;
    1.76      }
    1.77  
    1.78 -    public void testPositionForSuperConstructorCalls() throws IOException {
    1.79 +    @Test
    1.80 +    void testPositionForSuperConstructorCalls() throws IOException {
    1.81          assert tool != null;
    1.82  
    1.83          String code = "package test; public class Test {public Test() {super();}}";
    1.84 @@ -149,12 +158,12 @@
    1.85                  methodStartPos, pos.getStartPosition(cut, mit.getMethodSelect()));
    1.86          assertEquals("testPositionForSuperConstructorCalls",
    1.87                  methodEndPos, pos.getEndPosition(cut, mit.getMethodSelect()));
    1.88 -
    1.89      }
    1.90  
    1.91 -    public void testPositionForEnumModifiers() throws IOException {
    1.92 -
    1.93 -        String code = "package test; public enum Test {A;}";
    1.94 +    @Test
    1.95 +    void testPositionForEnumModifiers() throws IOException {
    1.96 +        final String theString = "public";
    1.97 +        String code = "package test; " + theString + " enum Test {A;}";
    1.98  
    1.99          JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null, null,
   1.100                  null, Arrays.asList(new MyFileObject(code)));
   1.101 @@ -163,19 +172,21 @@
   1.102  
   1.103          ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0);
   1.104          ModifiersTree mt = clazz.getModifiers();
   1.105 -
   1.106 +        int spos = code.indexOf(theString);
   1.107 +        int epos = spos + theString.length();
   1.108          assertEquals("testPositionForEnumModifiers",
   1.109 -                38 - 24, pos.getStartPosition(cut, mt));
   1.110 +                spos, pos.getStartPosition(cut, mt));
   1.111          assertEquals("testPositionForEnumModifiers",
   1.112 -                44 - 24, pos.getEndPosition(cut, mt));
   1.113 +                epos, pos.getEndPosition(cut, mt));
   1.114      }
   1.115  
   1.116 -    public void testNewClassWithEnclosing() throws IOException {
   1.117 +    @Test
   1.118 +    void testNewClassWithEnclosing() throws IOException {
   1.119  
   1.120 -
   1.121 +        final String theString = "Test.this.new d()";
   1.122          String code = "package test; class Test { " +
   1.123                  "class d {} private void method() { " +
   1.124 -                "Object o = Test.this.new d(); } }";
   1.125 +                "Object o = " + theString + "; } }";
   1.126  
   1.127          JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null, null,
   1.128                  null, Arrays.asList(new MyFileObject(code)));
   1.129 @@ -186,13 +197,16 @@
   1.130          ExpressionTree est =
   1.131                  ((VariableTree) ((MethodTree) clazz.getMembers().get(1)).getBody().getStatements().get(0)).getInitializer();
   1.132  
   1.133 +        final int spos = code.indexOf(theString);
   1.134 +        final int epos = spos + theString.length();
   1.135          assertEquals("testNewClassWithEnclosing",
   1.136 -                97 - 24, pos.getStartPosition(cut, est));
   1.137 +                spos, pos.getStartPosition(cut, est));
   1.138          assertEquals("testNewClassWithEnclosing",
   1.139 -                114 - 24, pos.getEndPosition(cut, est));
   1.140 +                epos, pos.getEndPosition(cut, est));
   1.141      }
   1.142  
   1.143 -    public void testPreferredPositionForBinaryOp() throws IOException {
   1.144 +    @Test
   1.145 +    void testPreferredPositionForBinaryOp() throws IOException {
   1.146  
   1.147          String code = "package test; public class Test {"
   1.148                  + "private void test() {"
   1.149 @@ -211,7 +225,581 @@
   1.150                  condStartPos, condJC.pos);
   1.151      }
   1.152  
   1.153 -    public void testPositionBrokenSource126732a() throws IOException {
   1.154 +    @Test
   1.155 +    void testErrorRecoveryForEnhancedForLoop142381() throws IOException {
   1.156 +
   1.157 +        String code = "package test; class Test { " +
   1.158 +                "private void method() { " +
   1.159 +                "java.util.Set<String> s = null; for (a : s) {} } }";
   1.160 +
   1.161 +        final List<Diagnostic<? extends JavaFileObject>> errors =
   1.162 +                new LinkedList<Diagnostic<? extends JavaFileObject>>();
   1.163 +
   1.164 +        JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null,
   1.165 +                new DiagnosticListener<JavaFileObject>() {
   1.166 +            public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
   1.167 +                errors.add(diagnostic);
   1.168 +            }
   1.169 +        }, null, null, Arrays.asList(new MyFileObject(code)));
   1.170 +
   1.171 +        CompilationUnitTree cut = ct.parse().iterator().next();
   1.172 +
   1.173 +        ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0);
   1.174 +        StatementTree forStatement =
   1.175 +                ((MethodTree) clazz.getMembers().get(0)).getBody().getStatements().get(1);
   1.176 +
   1.177 +        assertEquals("testErrorRecoveryForEnhancedForLoop142381",
   1.178 +                Kind.ENHANCED_FOR_LOOP, forStatement.getKind());
   1.179 +        assertFalse("testErrorRecoveryForEnhancedForLoop142381", errors.isEmpty());
   1.180 +    }
   1.181 +
   1.182 +    @Test
   1.183 +    void testPositionAnnotationNoPackage187551() throws IOException {
   1.184 +
   1.185 +        String code = "\n@interface Test {}";
   1.186 +
   1.187 +        JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null, null,
   1.188 +                null, Arrays.asList(new MyFileObject(code)));
   1.189 +
   1.190 +        CompilationUnitTree cut = ct.parse().iterator().next();
   1.191 +        ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0);
   1.192 +        Trees t = Trees.instance(ct);
   1.193 +
   1.194 +        assertEquals("testPositionAnnotationNoPackage187551",
   1.195 +                1, t.getSourcePositions().getStartPosition(cut, clazz));
   1.196 +    }
   1.197 +
   1.198 +    @Test
   1.199 +    void testPositionsSane1() throws IOException {
   1.200 +        performPositionsSanityTest("package test; class Test { " +
   1.201 +                "private void method() { " +
   1.202 +                "java.util.List<? extends java.util.List<? extends String>> l; " +
   1.203 +                "} }");
   1.204 +    }
   1.205 +
   1.206 +    @Test
   1.207 +    void testPositionsSane2() throws IOException {
   1.208 +        performPositionsSanityTest("package test; class Test { " +
   1.209 +                "private void method() { " +
   1.210 +                "java.util.List<? super java.util.List<? super String>> l; " +
   1.211 +                "} }");
   1.212 +    }
   1.213 +
   1.214 +    @Test
   1.215 +    void testPositionsSane3() throws IOException {
   1.216 +        performPositionsSanityTest("package test; class Test { " +
   1.217 +                "private void method() { " +
   1.218 +                "java.util.List<? super java.util.List<?>> l; } }");
   1.219 +    }
   1.220 +
   1.221 +    private void performPositionsSanityTest(String code) throws IOException {
   1.222 +
   1.223 +        final List<Diagnostic<? extends JavaFileObject>> errors =
   1.224 +                new LinkedList<Diagnostic<? extends JavaFileObject>>();
   1.225 +
   1.226 +        JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null,
   1.227 +                new DiagnosticListener<JavaFileObject>() {
   1.228 +
   1.229 +            public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
   1.230 +                errors.add(diagnostic);
   1.231 +            }
   1.232 +        }, null, null, Arrays.asList(new MyFileObject(code)));
   1.233 +
   1.234 +        final CompilationUnitTree cut = ct.parse().iterator().next();
   1.235 +        final Trees trees = Trees.instance(ct);
   1.236 +
   1.237 +        new TreeScanner<Void, Void>() {
   1.238 +
   1.239 +            private long parentStart = 0;
   1.240 +            private long parentEnd = Integer.MAX_VALUE;
   1.241 +
   1.242 +            @Override
   1.243 +            public Void scan(Tree node, Void p) {
   1.244 +                if (node == null) {
   1.245 +                    return null;
   1.246 +                }
   1.247 +
   1.248 +                long start = trees.getSourcePositions().getStartPosition(cut, node);
   1.249 +
   1.250 +                if (start == (-1)) {
   1.251 +                    return null; // synthetic tree
   1.252 +                }
   1.253 +                assertTrue(node.toString() + ":" + start + "/" + parentStart,
   1.254 +                        parentStart <= start);
   1.255 +
   1.256 +                long prevParentStart = parentStart;
   1.257 +
   1.258 +                parentStart = start;
   1.259 +
   1.260 +                long end = trees.getSourcePositions().getEndPosition(cut, node);
   1.261 +
   1.262 +                assertTrue(node.toString() + ":" + end + "/" + parentEnd,
   1.263 +                        end <= parentEnd);
   1.264 +
   1.265 +                long prevParentEnd = parentEnd;
   1.266 +
   1.267 +                parentEnd = end;
   1.268 +
   1.269 +                super.scan(node, p);
   1.270 +
   1.271 +                parentStart = prevParentStart;
   1.272 +                parentEnd = prevParentEnd;
   1.273 +
   1.274 +                return null;
   1.275 +            }
   1.276 +
   1.277 +            private void assertTrue(String message, boolean b) {
   1.278 +                if (!b) fail(message);
   1.279 +            }
   1.280 +        }.scan(cut, null);
   1.281 +    }
   1.282 +
   1.283 +    @Test
   1.284 +    void testCorrectWilcardPositions1() throws IOException {
   1.285 +        performWildcardPositionsTest("package test; import java.util.List; " +
   1.286 +                "class Test { private void method() { List<? extends List<? extends String>> l; } }",
   1.287 +
   1.288 +                Arrays.asList("List<? extends List<? extends String>> l;",
   1.289 +                "List<? extends List<? extends String>>",
   1.290 +                "List",
   1.291 +                "? extends List<? extends String>",
   1.292 +                "List<? extends String>",
   1.293 +                "List",
   1.294 +                "? extends String",
   1.295 +                "String"));
   1.296 +    }
   1.297 +
   1.298 +    @Test
   1.299 +    void testCorrectWilcardPositions2() throws IOException {
   1.300 +        performWildcardPositionsTest("package test; import java.util.List; "
   1.301 +                + "class Test { private void method() { List<? super List<? super String>> l; } }",
   1.302 +                Arrays.asList("List<? super List<? super String>> l;",
   1.303 +                "List<? super List<? super String>>",
   1.304 +                "List",
   1.305 +                "? super List<? super String>",
   1.306 +                "List<? super String>",
   1.307 +                "List",
   1.308 +                "? super String",
   1.309 +                "String"));
   1.310 +    }
   1.311 +
   1.312 +    @Test
   1.313 +    void testCorrectWilcardPositions3() throws IOException {
   1.314 +        performWildcardPositionsTest("package test; import java.util.List; " +
   1.315 +                "class Test { private void method() { List<? super List<?>> l; } }",
   1.316 +
   1.317 +                Arrays.asList("List<? super List<?>> l;",
   1.318 +                "List<? super List<?>>",
   1.319 +                "List",
   1.320 +                "? super List<?>",
   1.321 +                "List<?>",
   1.322 +                "List",
   1.323 +                "?"));
   1.324 +    }
   1.325 +
   1.326 +    @Test
   1.327 +    void testCorrectWilcardPositions4() throws IOException {
   1.328 +        performWildcardPositionsTest("package test; import java.util.List; " +
   1.329 +                "class Test { private void method() { " +
   1.330 +                "List<? extends List<? extends List<? extends String>>> l; } }",
   1.331 +
   1.332 +                Arrays.asList("List<? extends List<? extends List<? extends String>>> l;",
   1.333 +                "List<? extends List<? extends List<? extends String>>>",
   1.334 +                "List",
   1.335 +                "? extends List<? extends List<? extends String>>",
   1.336 +                "List<? extends List<? extends String>>",
   1.337 +                "List",
   1.338 +                "? extends List<? extends String>",
   1.339 +                "List<? extends String>",
   1.340 +                "List",
   1.341 +                "? extends String",
   1.342 +                "String"));
   1.343 +    }
   1.344 +
   1.345 +    @Test
   1.346 +    void testCorrectWilcardPositions5() throws IOException {
   1.347 +        performWildcardPositionsTest("package test; import java.util.List; " +
   1.348 +                "class Test { private void method() { " +
   1.349 +                "List<? extends List<? extends List<? extends String   >>> l; } }",
   1.350 +                Arrays.asList("List<? extends List<? extends List<? extends String   >>> l;",
   1.351 +                "List<? extends List<? extends List<? extends String   >>>",
   1.352 +                "List",
   1.353 +                "? extends List<? extends List<? extends String   >>",
   1.354 +                "List<? extends List<? extends String   >>",
   1.355 +                "List",
   1.356 +                "? extends List<? extends String   >",
   1.357 +                "List<? extends String   >",
   1.358 +                "List",
   1.359 +                "? extends String",
   1.360 +                "String"));
   1.361 +    }
   1.362 +
   1.363 +    void performWildcardPositionsTest(final String code,
   1.364 +            List<String> golden) throws IOException {
   1.365 +
   1.366 +        final List<Diagnostic<? extends JavaFileObject>> errors =
   1.367 +                new LinkedList<Diagnostic<? extends JavaFileObject>>();
   1.368 +
   1.369 +        JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null,
   1.370 +                new DiagnosticListener<JavaFileObject>() {
   1.371 +                    public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
   1.372 +                        errors.add(diagnostic);
   1.373 +                    }
   1.374 +                }, null, null, Arrays.asList(new MyFileObject(code)));
   1.375 +
   1.376 +        final CompilationUnitTree cut = ct.parse().iterator().next();
   1.377 +        final List<String> content = new LinkedList<String>();
   1.378 +        final Trees trees = Trees.instance(ct);
   1.379 +
   1.380 +        new TreeScanner<Void, Void>() {
   1.381 +            @Override
   1.382 +            public Void scan(Tree node, Void p) {
   1.383 +                if (node == null) {
   1.384 +                    return null;
   1.385 +                }
   1.386 +                long start = trees.getSourcePositions().getStartPosition(cut, node);
   1.387 +
   1.388 +                if (start == (-1)) {
   1.389 +                    return null; // synthetic tree
   1.390 +                }
   1.391 +                long end = trees.getSourcePositions().getEndPosition(cut, node);
   1.392 +                String s = code.substring((int) start, (int) end);
   1.393 +                content.add(s);
   1.394 +
   1.395 +                return super.scan(node, p);
   1.396 +            }
   1.397 +        }.scan(((MethodTree) ((ClassTree) cut.getTypeDecls().get(0)).getMembers().get(0)).getBody().getStatements().get(0), null);
   1.398 +
   1.399 +        assertEquals("performWildcardPositionsTest",golden.toString(),
   1.400 +                content.toString());
   1.401 +    }
   1.402 +
   1.403 +    @Test
   1.404 +    void testStartPositionForMethodWithoutModifiers() throws IOException {
   1.405 +
   1.406 +        String code = "package t; class Test { <T> void t() {} }";
   1.407 +
   1.408 +        JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null, null,
   1.409 +                null, Arrays.asList(new MyFileObject(code)));
   1.410 +        CompilationUnitTree cut = ct.parse().iterator().next();
   1.411 +        ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0);
   1.412 +        MethodTree mt = (MethodTree) clazz.getMembers().get(0);
   1.413 +        Trees t = Trees.instance(ct);
   1.414 +        int start = (int) t.getSourcePositions().getStartPosition(cut, mt);
   1.415 +        int end = (int) t.getSourcePositions().getEndPosition(cut, mt);
   1.416 +
   1.417 +        assertEquals("testStartPositionForMethodWithoutModifiers",
   1.418 +                "<T> void t() {}", code.substring(start, end));
   1.419 +    }
   1.420 +
   1.421 +    @Test
   1.422 +    void testVariableInIfThen1() throws IOException {
   1.423 +
   1.424 +        String code = "package t; class Test { " +
   1.425 +                "private static void t(String name) { " +
   1.426 +                "if (name != null) String nn = name.trim(); } }";
   1.427 +
   1.428 +        DiagnosticCollector<JavaFileObject> coll =
   1.429 +                new DiagnosticCollector<JavaFileObject>();
   1.430 +
   1.431 +        JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, coll, null,
   1.432 +                null, Arrays.asList(new MyFileObject(code)));
   1.433 +
   1.434 +        ct.parse();
   1.435 +
   1.436 +        List<String> codes = new LinkedList<String>();
   1.437 +
   1.438 +        for (Diagnostic<? extends JavaFileObject> d : coll.getDiagnostics()) {
   1.439 +            codes.add(d.getCode());
   1.440 +        }
   1.441 +
   1.442 +        assertEquals("testVariableInIfThen1",
   1.443 +                Arrays.<String>asList("compiler.err.variable.not.allowed"),
   1.444 +                codes);
   1.445 +    }
   1.446 +
   1.447 +    @Test
   1.448 +   void testVariableInIfThen2() throws IOException {
   1.449 +
   1.450 +        String code = "package t; class Test { " +
   1.451 +                "private static void t(String name) { " +
   1.452 +                "if (name != null) class X {} } }";
   1.453 +        DiagnosticCollector<JavaFileObject> coll =
   1.454 +                new DiagnosticCollector<JavaFileObject>();
   1.455 +        JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, coll, null,
   1.456 +                null, Arrays.asList(new MyFileObject(code)));
   1.457 +
   1.458 +        ct.parse();
   1.459 +
   1.460 +        List<String> codes = new LinkedList<String>();
   1.461 +
   1.462 +        for (Diagnostic<? extends JavaFileObject> d : coll.getDiagnostics()) {
   1.463 +            codes.add(d.getCode());
   1.464 +        }
   1.465 +
   1.466 +        assertEquals("testVariableInIfThen2",
   1.467 +                Arrays.<String>asList("compiler.err.class.not.allowed"), codes);
   1.468 +    }
   1.469 +
   1.470 +    @Test
   1.471 +    void testVariableInIfThen3() throws IOException {
   1.472 +
   1.473 +        String code = "package t; class Test { "+
   1.474 +                "private static void t() { " +
   1.475 +                "if (true) abstract class F {} }}";
   1.476 +        DiagnosticCollector<JavaFileObject> coll =
   1.477 +                new DiagnosticCollector<JavaFileObject>();
   1.478 +        JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, coll, null,
   1.479 +                null, Arrays.asList(new MyFileObject(code)));
   1.480 +
   1.481 +        ct.parse();
   1.482 +
   1.483 +        List<String> codes = new LinkedList<String>();
   1.484 +
   1.485 +        for (Diagnostic<? extends JavaFileObject> d : coll.getDiagnostics()) {
   1.486 +            codes.add(d.getCode());
   1.487 +        }
   1.488 +
   1.489 +        assertEquals("testVariableInIfThen3",
   1.490 +                Arrays.<String>asList("compiler.err.class.not.allowed"), codes);
   1.491 +    }
   1.492 +
   1.493 +    @Test
   1.494 +    void testVariableInIfThen4() throws IOException {
   1.495 +
   1.496 +        String code = "package t; class Test { "+
   1.497 +                "private static void t(String name) { " +
   1.498 +                "if (name != null) interface X {} } }";
   1.499 +        DiagnosticCollector<JavaFileObject> coll =
   1.500 +                new DiagnosticCollector<JavaFileObject>();
   1.501 +        JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, coll, null,
   1.502 +                null, Arrays.asList(new MyFileObject(code)));
   1.503 +
   1.504 +        ct.parse();
   1.505 +
   1.506 +        List<String> codes = new LinkedList<String>();
   1.507 +
   1.508 +        for (Diagnostic<? extends JavaFileObject> d : coll.getDiagnostics()) {
   1.509 +            codes.add(d.getCode());
   1.510 +        }
   1.511 +
   1.512 +        assertEquals("testVariableInIfThen4",
   1.513 +                Arrays.<String>asList("compiler.err.class.not.allowed"), codes);
   1.514 +    }
   1.515 +
   1.516 +    @Test
   1.517 +    void testVariableInIfThen5() throws IOException {
   1.518 +
   1.519 +        String code = "package t; class Test { "+
   1.520 +                "private static void t() { " +
   1.521 +                "if (true) } }";
   1.522 +        DiagnosticCollector<JavaFileObject> coll =
   1.523 +                new DiagnosticCollector<JavaFileObject>();
   1.524 +        JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, coll, null,
   1.525 +                null, Arrays.asList(new MyFileObject(code)));
   1.526 +
   1.527 +        ct.parse();
   1.528 +
   1.529 +        List<String> codes = new LinkedList<String>();
   1.530 +
   1.531 +        for (Diagnostic<? extends JavaFileObject> d : coll.getDiagnostics()) {
   1.532 +            codes.add(d.getCode());
   1.533 +        }
   1.534 +
   1.535 +        assertEquals("testVariableInIfThen5",
   1.536 +                Arrays.<String>asList("compiler.err.illegal.start.of.stmt"),
   1.537 +                codes);
   1.538 +    }
   1.539 +
   1.540 +    // see javac bug #6882235, NB bug #98234:
   1.541 +    @Test
   1.542 +    void testMissingExponent() throws IOException {
   1.543 +
   1.544 +        String code = "\nclass Test { { System.err.println(0e); } }";
   1.545 +
   1.546 +        JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null, null,
   1.547 +                null, Arrays.asList(new MyFileObject(code)));
   1.548 +
   1.549 +        assertNotNull(ct.parse().iterator().next());
   1.550 +    }
   1.551 +
   1.552 +    @Test
   1.553 +    void testTryResourcePos() throws IOException {
   1.554 +
   1.555 +        final String code = "package t; class Test { " +
   1.556 +                "{ try (java.io.InputStream in = null) { } } }";
   1.557 +
   1.558 +        CompilationUnitTree cut = getCompilationUnitTree(code);
   1.559 +
   1.560 +        new TreeScanner<Void, Void>() {
   1.561 +            @Override
   1.562 +            public Void visitVariable(VariableTree node, Void p) {
   1.563 +                if ("in".contentEquals(node.getName())) {
   1.564 +                    JCTree.JCVariableDecl var = (JCTree.JCVariableDecl) node;
   1.565 +                    assertEquals("testTryResourcePos", "in = null) { } } }",
   1.566 +                            code.substring(var.pos));
   1.567 +                }
   1.568 +                return super.visitVariable(node, p);
   1.569 +            }
   1.570 +        }.scan(cut, null);
   1.571 +    }
   1.572 +
   1.573 +    @Test
   1.574 +    void testVarPos() throws IOException {
   1.575 +
   1.576 +        final String code = "package t; class Test { " +
   1.577 +                "{ java.io.InputStream in = null; } }";
   1.578 +
   1.579 +        CompilationUnitTree cut = getCompilationUnitTree(code);
   1.580 +
   1.581 +        new TreeScanner<Void, Void>() {
   1.582 +
   1.583 +            @Override
   1.584 +            public Void visitVariable(VariableTree node, Void p) {
   1.585 +                if ("in".contentEquals(node.getName())) {
   1.586 +                    JCTree.JCVariableDecl var = (JCTree.JCVariableDecl) node;
   1.587 +                    assertEquals("testVarPos","in = null; } }",
   1.588 +                            code.substring(var.pos));
   1.589 +                }
   1.590 +                return super.visitVariable(node, p);
   1.591 +            }
   1.592 +        }.scan(cut, null);
   1.593 +    }
   1.594 +
   1.595 +    // expected erroneous tree: int x = y;(ERROR);
   1.596 +    @Test
   1.597 +    void testOperatorMissingError() throws IOException {
   1.598 +
   1.599 +        String code = "package test; public class ErrorTest { "
   1.600 +                + "void method() { int x = y  z } }";
   1.601 +        CompilationUnitTree cut = getCompilationUnitTree(code);
   1.602 +        final List<String> values = new ArrayList<>();
   1.603 +        final List<String> expectedValues =
   1.604 +                new ArrayList<>(Arrays.asList("[z]"));
   1.605 +
   1.606 +        new TreeScanner<Void, Void>() {
   1.607 +            @Override
   1.608 +            public Void visitErroneous(ErroneousTree node, Void p) {
   1.609 +                values.add(getErroneousTreeValues(node).toString());
   1.610 +                return null;
   1.611 +
   1.612 +            }
   1.613 +        }.scan(cut, null);
   1.614 +
   1.615 +        assertEquals("testSwitchError: The Erroneous tree "
   1.616 +                + "error values: " + values
   1.617 +                + " do not match expected error values: "
   1.618 +                + expectedValues, values, expectedValues);
   1.619 +    }
   1.620 +
   1.621 +    // expected erroneous tree:  String s = (ERROR);
   1.622 +    @Test
   1.623 +    void testMissingParenthesisError() throws IOException {
   1.624 +
   1.625 +        String code = "package test; public class ErrorTest { "
   1.626 +                + "void f() {String s = new String; } }";
   1.627 +        CompilationUnitTree cut = getCompilationUnitTree(code);
   1.628 +        final List<String> values = new ArrayList<>();
   1.629 +        final List<String> expectedValues =
   1.630 +                new ArrayList<>(Arrays.asList("[new String()]"));
   1.631 +
   1.632 +        new TreeScanner<Void, Void>() {
   1.633 +            @Override
   1.634 +            public Void visitErroneous(ErroneousTree node, Void p) {
   1.635 +                values.add(getErroneousTreeValues(node).toString());
   1.636 +                return null;
   1.637 +            }
   1.638 +        }.scan(cut, null);
   1.639 +
   1.640 +        assertEquals("testSwitchError: The Erroneous tree "
   1.641 +                + "error values: " + values
   1.642 +                + " do not match expected error values: "
   1.643 +                + expectedValues, values, expectedValues);
   1.644 +    }
   1.645 +
   1.646 +    // expected erroneous tree: package test; (ERROR)(ERROR)
   1.647 +    @Test
   1.648 +    void testMissingClassError() throws IOException {
   1.649 +
   1.650 +        String code = "package Test; clas ErrorTest {  "
   1.651 +                + "void f() {String s = new String(); } }";
   1.652 +        CompilationUnitTree cut = getCompilationUnitTree(code);
   1.653 +        final List<String> values = new ArrayList<>();
   1.654 +        final List<String> expectedValues =
   1.655 +                new ArrayList<>(Arrays.asList("[, clas]", "[]"));
   1.656 +
   1.657 +        new TreeScanner<Void, Void>() {
   1.658 +            @Override
   1.659 +            public Void visitErroneous(ErroneousTree node, Void p) {
   1.660 +                values.add(getErroneousTreeValues(node).toString());
   1.661 +                return null;
   1.662 +            }
   1.663 +        }.scan(cut, null);
   1.664 +
   1.665 +        assertEquals("testSwitchError: The Erroneous tree "
   1.666 +                + "error values: " + values
   1.667 +                + " do not match expected error values: "
   1.668 +                + expectedValues, values, expectedValues);
   1.669 +    }
   1.670 +
   1.671 +    // expected erroneous tree: void m1(int i) {(ERROR);{(ERROR);}
   1.672 +    @Test
   1.673 +    void testSwitchError() throws IOException {
   1.674 +
   1.675 +        String code = "package test; public class ErrorTest { "
   1.676 +                + "int numDays; void m1(int i) { switchh {i} { case 1: "
   1.677 +                + "numDays = 31; break; } } }";
   1.678 +        CompilationUnitTree cut = getCompilationUnitTree(code);
   1.679 +        final List<String> values = new ArrayList<>();
   1.680 +        final List<String> expectedValues =
   1.681 +                new ArrayList<>(Arrays.asList("[switchh]", "[i]"));
   1.682 +
   1.683 +        new TreeScanner<Void, Void>() {
   1.684 +            @Override
   1.685 +            public Void visitErroneous(ErroneousTree node, Void p) {
   1.686 +                values.add(getErroneousTreeValues(node).toString());
   1.687 +                return null;
   1.688 +            }
   1.689 +        }.scan(cut, null);
   1.690 +
   1.691 +        assertEquals("testSwitchError: The Erroneous tree "
   1.692 +                + "error values: " + values
   1.693 +                + " do not match expected error values: "
   1.694 +                + expectedValues, values, expectedValues);
   1.695 +    }
   1.696 +
   1.697 +    // expected erroneous tree: class ErrorTest {(ERROR)
   1.698 +    @Test
   1.699 +    void testMethodError() throws IOException {
   1.700 +
   1.701 +        String code = "package Test; class ErrorTest {  "
   1.702 +                + "static final void f) {String s = new String(); } }";
   1.703 +        CompilationUnitTree cut = cut = getCompilationUnitTree(code);
   1.704 +
   1.705 +        final List<String> values = new ArrayList<>();
   1.706 +        final List<String> expectedValues =
   1.707 +                new ArrayList<>(Arrays.asList("[\nstatic final void f();]"));
   1.708 +
   1.709 +        new TreeScanner<Void, Void>() {
   1.710 +            @Override
   1.711 +            public Void visitErroneous(ErroneousTree node, Void p) {
   1.712 +                values.add(normalize(getErroneousTreeValues(node).toString()));
   1.713 +                return null;
   1.714 +            }
   1.715 +        }.scan(cut, null);
   1.716 +
   1.717 +        assertEquals("testMethodError: The Erroneous tree "
   1.718 +                + "error value: " + values
   1.719 +                + " does not match expected error values: "
   1.720 +                + expectedValues, values, expectedValues);
   1.721 +    }
   1.722 +
   1.723 +    /*
   1.724 +     * The following tests do not work just yet with nb-javac nor javac,
   1.725 +     * they need further investigation, see CR: 7167356
   1.726 +     */
   1.727 +
   1.728 +    void testPositionBrokenSource126732a() throws IOException {
   1.729          String[] commands = new String[]{
   1.730              "return Runnable()",
   1.731              "do { } while (true)",
   1.732 @@ -250,7 +838,7 @@
   1.733          }
   1.734      }
   1.735  
   1.736 -    public void testPositionBrokenSource126732b() throws IOException {
   1.737 +    void testPositionBrokenSource126732b() throws IOException {
   1.738          String[] commands = new String[]{
   1.739              "break",
   1.740              "break A",
   1.741 @@ -291,246 +879,7 @@
   1.742          }
   1.743      }
   1.744  
   1.745 -    public void testErrorRecoveryForEnhancedForLoop142381() throws IOException {
   1.746 -
   1.747 -        String code = "package test; class Test { " +
   1.748 -                "private void method() { " +
   1.749 -                "java.util.Set<String> s = null; for (a : s) {} } }";
   1.750 -
   1.751 -        final List<Diagnostic<? extends JavaFileObject>> errors =
   1.752 -                new LinkedList<Diagnostic<? extends JavaFileObject>>();
   1.753 -
   1.754 -        JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null,
   1.755 -                new DiagnosticListener<JavaFileObject>() {
   1.756 -            public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
   1.757 -                errors.add(diagnostic);
   1.758 -            }
   1.759 -        }, null, null, Arrays.asList(new MyFileObject(code)));
   1.760 -
   1.761 -        CompilationUnitTree cut = ct.parse().iterator().next();
   1.762 -
   1.763 -        ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0);
   1.764 -        StatementTree forStatement =
   1.765 -                ((MethodTree) clazz.getMembers().get(0)).getBody().getStatements().get(1);
   1.766 -
   1.767 -        assertEquals("testErrorRecoveryForEnhancedForLoop142381",
   1.768 -                Kind.ENHANCED_FOR_LOOP, forStatement.getKind());
   1.769 -        assertFalse("testErrorRecoveryForEnhancedForLoop142381", errors.isEmpty());
   1.770 -    }
   1.771 -
   1.772 -    public void testPositionAnnotationNoPackage187551() throws IOException {
   1.773 -
   1.774 -        String code = "\n@interface Test {}";
   1.775 -
   1.776 -        JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null, null,
   1.777 -                null, Arrays.asList(new MyFileObject(code)));
   1.778 -
   1.779 -        CompilationUnitTree cut = ct.parse().iterator().next();
   1.780 -        ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0);
   1.781 -        Trees t = Trees.instance(ct);
   1.782 -
   1.783 -        assertEquals("testPositionAnnotationNoPackage187551",
   1.784 -                1, t.getSourcePositions().getStartPosition(cut, clazz));
   1.785 -    }
   1.786 -
   1.787 -    public void testPositionsSane() throws IOException {
   1.788 -        performPositionsSanityTest("package test; class Test { " +
   1.789 -                "private void method() { " +
   1.790 -                "java.util.List<? extends java.util.List<? extends String>> l; " +
   1.791 -                "} }");
   1.792 -        performPositionsSanityTest("package test; class Test { " +
   1.793 -                "private void method() { " +
   1.794 -                "java.util.List<? super java.util.List<? super String>> l; " +
   1.795 -                "} }");
   1.796 -        performPositionsSanityTest("package test; class Test { " +
   1.797 -                "private void method() { " +
   1.798 -                "java.util.List<? super java.util.List<?>> l; } }");
   1.799 -    }
   1.800 -
   1.801 -    private void performPositionsSanityTest(String code) throws IOException {
   1.802 -
   1.803 -        final List<Diagnostic<? extends JavaFileObject>> errors =
   1.804 -                new LinkedList<Diagnostic<? extends JavaFileObject>>();
   1.805 -
   1.806 -        JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null,
   1.807 -                new DiagnosticListener<JavaFileObject>() {
   1.808 -
   1.809 -            public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
   1.810 -                errors.add(diagnostic);
   1.811 -            }
   1.812 -        }, null, null, Arrays.asList(new MyFileObject(code)));
   1.813 -
   1.814 -        final CompilationUnitTree cut = ct.parse().iterator().next();
   1.815 -        final Trees trees = Trees.instance(ct);
   1.816 -
   1.817 -        new TreeScanner<Void, Void>() {
   1.818 -
   1.819 -            private long parentStart = 0;
   1.820 -            private long parentEnd = Integer.MAX_VALUE;
   1.821 -
   1.822 -            @Override
   1.823 -            public Void scan(Tree node, Void p) {
   1.824 -                if (node == null) {
   1.825 -                    return null;
   1.826 -                }
   1.827 -
   1.828 -                long start = trees.getSourcePositions().getStartPosition(cut, node);
   1.829 -
   1.830 -                if (start == (-1)) {
   1.831 -                    return null; //synthetic tree
   1.832 -                }
   1.833 -                assertTrue(node.toString() + ":" + start + "/" + parentStart,
   1.834 -                        parentStart <= start);
   1.835 -
   1.836 -                long prevParentStart = parentStart;
   1.837 -
   1.838 -                parentStart = start;
   1.839 -
   1.840 -                long end = trees.getSourcePositions().getEndPosition(cut, node);
   1.841 -
   1.842 -                assertTrue(node.toString() + ":" + end + "/" + parentEnd,
   1.843 -                        end <= parentEnd);
   1.844 -
   1.845 -                long prevParentEnd = parentEnd;
   1.846 -
   1.847 -                parentEnd = end;
   1.848 -
   1.849 -                super.scan(node, p);
   1.850 -
   1.851 -                parentStart = prevParentStart;
   1.852 -                parentEnd = prevParentEnd;
   1.853 -
   1.854 -                return null;
   1.855 -            }
   1.856 -
   1.857 -            private void assertTrue(String message, boolean b) {
   1.858 -                if (!b) fail(message);
   1.859 -            }
   1.860 -        }.scan(cut, null);
   1.861 -    }
   1.862 -
   1.863 -    public void testCorrectWilcardPositions() throws IOException {
   1.864 -        performWildcardPositionsTest("package test; import java.util.List; " +
   1.865 -                "class Test { private void method() { List<? extends List<? extends String>> l; } }",
   1.866 -
   1.867 -                Arrays.asList("List<? extends List<? extends String>> l;",
   1.868 -                "List<? extends List<? extends String>>",
   1.869 -                "List",
   1.870 -                "? extends List<? extends String>",
   1.871 -                "List<? extends String>",
   1.872 -                "List",
   1.873 -                "? extends String",
   1.874 -                "String"));
   1.875 -        performWildcardPositionsTest("package test; import java.util.List; " +
   1.876 -                "class Test { private void method() { List<? super List<? super String>> l; } }",
   1.877 -
   1.878 -                Arrays.asList("List<? super List<? super String>> l;",
   1.879 -                "List<? super List<? super String>>",
   1.880 -                "List",
   1.881 -                "? super List<? super String>",
   1.882 -                "List<? super String>",
   1.883 -                "List",
   1.884 -                "? super String",
   1.885 -                "String"));
   1.886 -        performWildcardPositionsTest("package test; import java.util.List; " +
   1.887 -                "class Test { private void method() { List<? super List<?>> l; } }",
   1.888 -
   1.889 -                Arrays.asList("List<? super List<?>> l;",
   1.890 -                "List<? super List<?>>",
   1.891 -                "List",
   1.892 -                "? super List<?>",
   1.893 -                "List<?>",
   1.894 -                "List",
   1.895 -                "?"));
   1.896 -        performWildcardPositionsTest("package test; import java.util.List; " +
   1.897 -                "class Test { private void method() { " +
   1.898 -                "List<? extends List<? extends List<? extends String>>> l; } }",
   1.899 -
   1.900 -                Arrays.asList("List<? extends List<? extends List<? extends String>>> l;",
   1.901 -                "List<? extends List<? extends List<? extends String>>>",
   1.902 -                "List",
   1.903 -                "? extends List<? extends List<? extends String>>",
   1.904 -                "List<? extends List<? extends String>>",
   1.905 -                "List",
   1.906 -                "? extends List<? extends String>",
   1.907 -                "List<? extends String>",
   1.908 -                "List",
   1.909 -                "? extends String",
   1.910 -                "String"));
   1.911 -        performWildcardPositionsTest("package test; import java.util.List; " +
   1.912 -                "class Test { private void method() { " +
   1.913 -                "List<? extends List<? extends List<? extends String   >>> l; } }",
   1.914 -                Arrays.asList("List<? extends List<? extends List<? extends String   >>> l;",
   1.915 -                "List<? extends List<? extends List<? extends String   >>>",
   1.916 -                "List",
   1.917 -                "? extends List<? extends List<? extends String   >>",
   1.918 -                "List<? extends List<? extends String   >>",
   1.919 -                "List",
   1.920 -                "? extends List<? extends String   >",
   1.921 -                "List<? extends String   >",
   1.922 -                "List",
   1.923 -                "? extends String",
   1.924 -                "String"));
   1.925 -    }
   1.926 -
   1.927 -    public void performWildcardPositionsTest(final String code,
   1.928 -            List<String> golden) throws IOException {
   1.929 -
   1.930 -        final List<Diagnostic<? extends JavaFileObject>> errors =
   1.931 -                new LinkedList<Diagnostic<? extends JavaFileObject>>();
   1.932 -
   1.933 -        JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null,
   1.934 -                new DiagnosticListener<JavaFileObject>() {
   1.935 -                    public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
   1.936 -                        errors.add(diagnostic);
   1.937 -                    }
   1.938 -                }, null, null, Arrays.asList(new MyFileObject(code)));
   1.939 -
   1.940 -        final CompilationUnitTree cut = ct.parse().iterator().next();
   1.941 -        final List<String> content = new LinkedList<String>();
   1.942 -        final Trees trees = Trees.instance(ct);
   1.943 -
   1.944 -        new TreeScanner<Void, Void>() {
   1.945 -            @Override
   1.946 -            public Void scan(Tree node, Void p) {
   1.947 -                if (node == null) {
   1.948 -                    return null;
   1.949 -                }
   1.950 -                long start = trees.getSourcePositions().getStartPosition(cut, node);
   1.951 -
   1.952 -                if (start == (-1)) {
   1.953 -                    return null; //synthetic tree
   1.954 -                }
   1.955 -                long end = trees.getSourcePositions().getEndPosition(cut, node);
   1.956 -                String s = code.substring((int) start, (int) end);
   1.957 -                content.add(s);
   1.958 -
   1.959 -                return super.scan(node, p);
   1.960 -            }
   1.961 -        }.scan(((MethodTree) ((ClassTree) cut.getTypeDecls().get(0)).getMembers().get(0)).getBody().getStatements().get(0), null);
   1.962 -
   1.963 -        assertEquals("performWildcardPositionsTest",golden.toString(),
   1.964 -                content.toString());
   1.965 -    }
   1.966 -
   1.967 -    public void testStartPositionForMethodWithoutModifiers() throws IOException {
   1.968 -
   1.969 -        String code = "package t; class Test { <T> void t() {} }";
   1.970 -
   1.971 -        JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null, null,
   1.972 -                null, Arrays.asList(new MyFileObject(code)));
   1.973 -        CompilationUnitTree cut = ct.parse().iterator().next();
   1.974 -        ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0);
   1.975 -        MethodTree mt = (MethodTree) clazz.getMembers().get(0);
   1.976 -        Trees t = Trees.instance(ct);
   1.977 -        int start = (int) t.getSourcePositions().getStartPosition(cut, mt);
   1.978 -        int end = (int) t.getSourcePositions().getEndPosition(cut, mt);
   1.979 -
   1.980 -        assertEquals("testStartPositionForMethodWithoutModifiers",
   1.981 -                "<T> void t() {}", code.substring(start, end));
   1.982 -    }
   1.983 -
   1.984 -    public void testStartPositionEnumConstantInit() throws IOException {
   1.985 +    void testStartPositionEnumConstantInit() throws IOException {
   1.986  
   1.987          String code = "package t; enum Test { AAA; }";
   1.988  
   1.989 @@ -546,342 +895,34 @@
   1.990          assertEquals("testStartPositionEnumConstantInit", -1, start);
   1.991      }
   1.992  
   1.993 -    public void testVariableInIfThen1() throws IOException {
   1.994 -
   1.995 -        String code = "package t; class Test { " +
   1.996 -                "private static void t(String name) { " +
   1.997 -                "if (name != null) String nn = name.trim(); } }";
   1.998 -
   1.999 -        DiagnosticCollector<JavaFileObject> coll =
  1.1000 -                new DiagnosticCollector<JavaFileObject>();
  1.1001 -
  1.1002 -        JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, coll, null,
  1.1003 -                null, Arrays.asList(new MyFileObject(code)));
  1.1004 -
  1.1005 -        ct.parse();
  1.1006 -
  1.1007 -        List<String> codes = new LinkedList<String>();
  1.1008 -
  1.1009 -        for (Diagnostic<? extends JavaFileObject> d : coll.getDiagnostics()) {
  1.1010 -            codes.add(d.getCode());
  1.1011 +    void run(String[] args) throws Exception {
  1.1012 +        int passed = 0, failed = 0;
  1.1013 +        final Pattern p = (args != null && args.length > 0)
  1.1014 +                ? Pattern.compile(args[0])
  1.1015 +                : null;
  1.1016 +        for (Method m : this.getClass().getDeclaredMethods()) {
  1.1017 +            boolean selected = (p == null)
  1.1018 +                    ? m.isAnnotationPresent(Test.class)
  1.1019 +                    : p.matcher(m.getName()).matches();
  1.1020 +            if (selected) {
  1.1021 +                try {
  1.1022 +                    m.invoke(this, (Object[]) null);
  1.1023 +                    System.out.println(m.getName() + ": OK");
  1.1024 +                    passed++;
  1.1025 +                } catch (Throwable ex) {
  1.1026 +                    System.out.printf("Test %s failed: %s %n", m, ex.getCause());
  1.1027 +                    failed++;
  1.1028 +                }
  1.1029 +            }
  1.1030          }
  1.1031 -
  1.1032 -        assertEquals("testVariableInIfThen1",
  1.1033 -                Arrays.<String>asList("compiler.err.variable.not.allowed"),
  1.1034 -                codes);
  1.1035 -    }
  1.1036 -
  1.1037 -    public void testVariableInIfThen2() throws IOException {
  1.1038 -
  1.1039 -        String code = "package t; class Test { " +
  1.1040 -                "private static void t(String name) { " +
  1.1041 -                "if (name != null) class X {} } }";
  1.1042 -        DiagnosticCollector<JavaFileObject> coll =
  1.1043 -                new DiagnosticCollector<JavaFileObject>();
  1.1044 -        JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, coll, null,
  1.1045 -                null, Arrays.asList(new MyFileObject(code)));
  1.1046 -
  1.1047 -        ct.parse();
  1.1048 -
  1.1049 -        List<String> codes = new LinkedList<String>();
  1.1050 -
  1.1051 -        for (Diagnostic<? extends JavaFileObject> d : coll.getDiagnostics()) {
  1.1052 -            codes.add(d.getCode());
  1.1053 +        System.out.printf("Passed: %d, Failed %d%n", passed, failed);
  1.1054 +        if (failed > 0) {
  1.1055 +            throw new RuntimeException("Tests failed: " + failed);
  1.1056          }
  1.1057 -
  1.1058 -        assertEquals("testVariableInIfThen2",
  1.1059 -                Arrays.<String>asList("compiler.err.class.not.allowed"), codes);
  1.1060 -    }
  1.1061 -
  1.1062 -    public void testVariableInIfThen3() throws IOException {
  1.1063 -
  1.1064 -        String code = "package t; class Test { "+
  1.1065 -                "private static void t() { " +
  1.1066 -                "if (true) abstract class F {} }}";
  1.1067 -        DiagnosticCollector<JavaFileObject> coll =
  1.1068 -                new DiagnosticCollector<JavaFileObject>();
  1.1069 -        JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, coll, null,
  1.1070 -                null, Arrays.asList(new MyFileObject(code)));
  1.1071 -
  1.1072 -        ct.parse();
  1.1073 -
  1.1074 -        List<String> codes = new LinkedList<String>();
  1.1075 -
  1.1076 -        for (Diagnostic<? extends JavaFileObject> d : coll.getDiagnostics()) {
  1.1077 -            codes.add(d.getCode());
  1.1078 +        if (passed == 0 && failed == 0) {
  1.1079 +            throw new AssertionError("No test(s) selected: passed = " +
  1.1080 +                    passed + ", failed = " + failed + " ??????????");
  1.1081          }
  1.1082 -
  1.1083 -        assertEquals("testVariableInIfThen3",
  1.1084 -                Arrays.<String>asList("compiler.err.class.not.allowed"), codes);
  1.1085 -    }
  1.1086 -
  1.1087 -    public void testVariableInIfThen4() throws IOException {
  1.1088 -
  1.1089 -        String code = "package t; class Test { "+
  1.1090 -                "private static void t(String name) { " +
  1.1091 -                "if (name != null) interface X {} } }";
  1.1092 -        DiagnosticCollector<JavaFileObject> coll =
  1.1093 -                new DiagnosticCollector<JavaFileObject>();
  1.1094 -        JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, coll, null,
  1.1095 -                null, Arrays.asList(new MyFileObject(code)));
  1.1096 -
  1.1097 -        ct.parse();
  1.1098 -
  1.1099 -        List<String> codes = new LinkedList<String>();
  1.1100 -
  1.1101 -        for (Diagnostic<? extends JavaFileObject> d : coll.getDiagnostics()) {
  1.1102 -            codes.add(d.getCode());
  1.1103 -        }
  1.1104 -
  1.1105 -        assertEquals("testVariableInIfThen4",
  1.1106 -                Arrays.<String>asList("compiler.err.class.not.allowed"), codes);
  1.1107 -    }
  1.1108 -
  1.1109 -    public void testVariableInIfThen5() throws IOException {
  1.1110 -
  1.1111 -        String code = "package t; class Test { "+
  1.1112 -                "private static void t() { " +
  1.1113 -                "if (true) } }";
  1.1114 -        DiagnosticCollector<JavaFileObject> coll =
  1.1115 -                new DiagnosticCollector<JavaFileObject>();
  1.1116 -        JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, coll, null,
  1.1117 -                null, Arrays.asList(new MyFileObject(code)));
  1.1118 -
  1.1119 -        ct.parse();
  1.1120 -
  1.1121 -        List<String> codes = new LinkedList<String>();
  1.1122 -
  1.1123 -        for (Diagnostic<? extends JavaFileObject> d : coll.getDiagnostics()) {
  1.1124 -            codes.add(d.getCode());
  1.1125 -        }
  1.1126 -
  1.1127 -        assertEquals("testVariableInIfThen5",
  1.1128 -                Arrays.<String>asList("compiler.err.illegal.start.of.stmt"),
  1.1129 -                codes);
  1.1130 -    }
  1.1131 -
  1.1132 -    //see javac bug #6882235, NB bug #98234:
  1.1133 -    public void testMissingExponent() throws IOException {
  1.1134 -
  1.1135 -        String code = "\nclass Test { { System.err.println(0e); } }";
  1.1136 -
  1.1137 -        JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null, null,
  1.1138 -                null, Arrays.asList(new MyFileObject(code)));
  1.1139 -
  1.1140 -        assertNotNull(ct.parse().iterator().next());
  1.1141 -    }
  1.1142 -
  1.1143 -    public void testTryResourcePos() throws IOException {
  1.1144 -
  1.1145 -        final String code = "package t; class Test { " +
  1.1146 -                "{ try (java.io.InputStream in = null) { } } }";
  1.1147 -
  1.1148 -        CompilationUnitTree cut = getCompilationUnitTree(code);
  1.1149 -
  1.1150 -        new TreeScanner<Void, Void>() {
  1.1151 -            @Override
  1.1152 -            public Void visitVariable(VariableTree node, Void p) {
  1.1153 -                if ("in".contentEquals(node.getName())) {
  1.1154 -                    JCTree.JCVariableDecl var = (JCTree.JCVariableDecl) node;
  1.1155 -                    System.out.println(node.getName() + "," + var.pos);
  1.1156 -                    assertEquals("testTryResourcePos", "in = null) { } } }",
  1.1157 -                            code.substring(var.pos));
  1.1158 -                }
  1.1159 -                return super.visitVariable(node, p);
  1.1160 -            }
  1.1161 -        }.scan(cut, null);
  1.1162 -    }
  1.1163 -
  1.1164 -    public void testVarPos() throws IOException {
  1.1165 -
  1.1166 -        final String code = "package t; class Test { " +
  1.1167 -                "{ java.io.InputStream in = null; } }";
  1.1168 -
  1.1169 -        CompilationUnitTree cut = getCompilationUnitTree(code);
  1.1170 -
  1.1171 -        new TreeScanner<Void, Void>() {
  1.1172 -
  1.1173 -            @Override
  1.1174 -            public Void visitVariable(VariableTree node, Void p) {
  1.1175 -                if ("in".contentEquals(node.getName())) {
  1.1176 -                    JCTree.JCVariableDecl var = (JCTree.JCVariableDecl) node;
  1.1177 -                    assertEquals("testVarPos","in = null; } }",
  1.1178 -                            code.substring(var.pos));
  1.1179 -                }
  1.1180 -                return super.visitVariable(node, p);
  1.1181 -            }
  1.1182 -        }.scan(cut, null);
  1.1183 -    }
  1.1184 -
  1.1185 -    // expected erroneous tree: int x = y;(ERROR);
  1.1186 -    public void testOperatorMissingError() throws IOException {
  1.1187 -
  1.1188 -        String code = "package test; public class ErrorTest { "
  1.1189 -                + "void method() { int x = y  z } }";
  1.1190 -        CompilationUnitTree cut = getCompilationUnitTree(code);
  1.1191 -        final List<String> values = new ArrayList<>();
  1.1192 -        final List<String> expectedValues =
  1.1193 -                new ArrayList<>(Arrays.asList("[z]"));
  1.1194 -
  1.1195 -        new TreeScanner<Void, Void>() {
  1.1196 -
  1.1197 -            @Override
  1.1198 -            public Void visitErroneous(ErroneousTree node, Void p) {
  1.1199 -
  1.1200 -                values.add(getErroneousTreeValues(node).toString());
  1.1201 -                return null;
  1.1202 -
  1.1203 -            }
  1.1204 -        }.scan(cut, null);
  1.1205 -
  1.1206 -        assertEquals("testSwitchError: The Erroneous tree "
  1.1207 -                + "error values: " + values
  1.1208 -                + " do not match expected error values: "
  1.1209 -                + expectedValues, values, expectedValues);
  1.1210 -    }
  1.1211 -
  1.1212 -    //expected erroneous tree:  String s = (ERROR);
  1.1213 -    public void testMissingParenthesisError() throws IOException {
  1.1214 -
  1.1215 -        String code = "package test; public class ErrorTest { "
  1.1216 -                + "void f() {String s = new String; } }";
  1.1217 -        CompilationUnitTree cut = getCompilationUnitTree(code);
  1.1218 -        final List<String> values = new ArrayList<>();
  1.1219 -        final List<String> expectedValues =
  1.1220 -                new ArrayList<>(Arrays.asList("[new String()]"));
  1.1221 -
  1.1222 -        new TreeScanner<Void, Void>() {
  1.1223 -
  1.1224 -            @Override
  1.1225 -            public Void visitErroneous(ErroneousTree node, Void p) {
  1.1226 -
  1.1227 -                values.add(getErroneousTreeValues(node).toString());
  1.1228 -                return null;
  1.1229 -            }
  1.1230 -        }.scan(cut, null);
  1.1231 -
  1.1232 -        assertEquals("testSwitchError: The Erroneous tree "
  1.1233 -                + "error values: " + values
  1.1234 -                + " do not match expected error values: "
  1.1235 -                + expectedValues, values, expectedValues);
  1.1236 -    }
  1.1237 -
  1.1238 -    //expected erroneous tree: package test; (ERROR)(ERROR)
  1.1239 -    public void testMissingClassError() throws IOException {
  1.1240 -
  1.1241 -        String code = "package Test; clas ErrorTest {  "
  1.1242 -                + "void f() {String s = new String(); } }";
  1.1243 -        CompilationUnitTree cut = getCompilationUnitTree(code);
  1.1244 -        final List<String> values = new ArrayList<>();
  1.1245 -        final List<String> expectedValues =
  1.1246 -                new ArrayList<>(Arrays.asList("[, clas]", "[]"));
  1.1247 -
  1.1248 -        new TreeScanner<Void, Void>() {
  1.1249 -
  1.1250 -            @Override
  1.1251 -            public Void visitErroneous(ErroneousTree node, Void p) {
  1.1252 -
  1.1253 -                values.add(getErroneousTreeValues(node).toString());
  1.1254 -                return null;
  1.1255 -            }
  1.1256 -        }.scan(cut, null);
  1.1257 -
  1.1258 -        assertEquals("testSwitchError: The Erroneous tree "
  1.1259 -                + "error values: " + values
  1.1260 -                + " do not match expected error values: "
  1.1261 -                + expectedValues, values, expectedValues);
  1.1262 -    }
  1.1263 -
  1.1264 -    //expected erroneous tree: void m1(int i) {(ERROR);{(ERROR);}
  1.1265 -    public void testSwitchError() throws IOException {
  1.1266 -
  1.1267 -        String code = "package test; public class ErrorTest { "
  1.1268 -                + "int numDays; void m1(int i) { switchh {i} { case 1: "
  1.1269 -                + "numDays = 31; break; } } }";
  1.1270 -        CompilationUnitTree cut = getCompilationUnitTree(code);
  1.1271 -        final List<String> values = new ArrayList<>();
  1.1272 -        final List<String> expectedValues =
  1.1273 -                new ArrayList<>(Arrays.asList("[switchh]", "[i]"));
  1.1274 -
  1.1275 -        new TreeScanner<Void, Void>() {
  1.1276 -
  1.1277 -            @Override
  1.1278 -            public Void visitErroneous(ErroneousTree node, Void p) {
  1.1279 -
  1.1280 -                values.add(getErroneousTreeValues(node).toString());
  1.1281 -                return null;
  1.1282 -            }
  1.1283 -        }.scan(cut, null);
  1.1284 -
  1.1285 -        assertEquals("testSwitchError: The Erroneous tree "
  1.1286 -                + "error values: " + values
  1.1287 -                + " do not match expected error values: "
  1.1288 -                + expectedValues, values, expectedValues);
  1.1289 -    }
  1.1290 -
  1.1291 -    //expected erroneous tree: class ErrorTest {(ERROR)
  1.1292 -    public void testMethodError() throws IOException {
  1.1293 -
  1.1294 -        String code = "package Test; class ErrorTest {  "
  1.1295 -                + "static final void f) {String s = new String(); } }";
  1.1296 -        CompilationUnitTree cut = getCompilationUnitTree(code);
  1.1297 -        final List<String> values = new ArrayList<>();
  1.1298 -        final List<String> expectedValues =
  1.1299 -                new ArrayList<>(Arrays.asList("[\nstatic final void f();]"));
  1.1300 -
  1.1301 -        new TreeScanner<Void, Void>() {
  1.1302 -
  1.1303 -            @Override
  1.1304 -            public Void visitErroneous(ErroneousTree node, Void p) {
  1.1305 -
  1.1306 -                values.add(normalize(getErroneousTreeValues(node).toString()));
  1.1307 -                return null;
  1.1308 -            }
  1.1309 -        }.scan(cut, null);
  1.1310 -
  1.1311 -        assertEquals("testMethodError: The Erroneous tree "
  1.1312 -                + "error value: " + values
  1.1313 -                + " does not match expected error values: "
  1.1314 -                + expectedValues, values, expectedValues);
  1.1315 -    }
  1.1316 -
  1.1317 -    void testsNotWorking() throws IOException {
  1.1318 -
  1.1319 -        // Fails with nb-javac, needs further investigation
  1.1320 -        testPositionBrokenSource126732a();
  1.1321 -        testPositionBrokenSource126732b();
  1.1322 -
  1.1323 -        // Fails, these tests yet to be addressed
  1.1324 -        testPositionForEnumModifiers();
  1.1325 -        testStartPositionEnumConstantInit();
  1.1326 -    }
  1.1327 -    void testPositions() throws IOException {
  1.1328 -        testPositionsSane();
  1.1329 -        testCorrectWilcardPositions();
  1.1330 -        testPositionAnnotationNoPackage187551();
  1.1331 -        testPositionForSuperConstructorCalls();
  1.1332 -        testPreferredPositionForBinaryOp();
  1.1333 -        testStartPositionForMethodWithoutModifiers();
  1.1334 -        testVarPos();
  1.1335 -        testVariableInIfThen1();
  1.1336 -        testVariableInIfThen2();
  1.1337 -        testVariableInIfThen3();
  1.1338 -        testVariableInIfThen4();
  1.1339 -        testVariableInIfThen5();
  1.1340 -        testMissingExponent();
  1.1341 -        testTryResourcePos();
  1.1342 -        testOperatorMissingError();
  1.1343 -        testMissingParenthesisError();
  1.1344 -        testMissingClassError();
  1.1345 -        testSwitchError();
  1.1346 -        testMethodError();
  1.1347 -        testErrorRecoveryForEnhancedForLoop142381();
  1.1348 -    }
  1.1349 -
  1.1350 -    public static void main(String... args) throws IOException {
  1.1351 -        JavacParserTest jpt = new JavacParserTest("JavacParserTest");
  1.1352 -        jpt.testPositions();
  1.1353 -        System.out.println("PASS");
  1.1354      }
  1.1355  }
  1.1356  
  1.1357 @@ -906,8 +947,6 @@
  1.1358      }
  1.1359  
  1.1360      void assertEquals(String message, Object o1, Object o2) {
  1.1361 -        System.out.println(o1);
  1.1362 -        System.out.println(o2);
  1.1363          if (o1 != null && o2 != null && !o1.equals(o2)) {
  1.1364              fail(message);
  1.1365          }
  1.1366 @@ -929,4 +968,11 @@
  1.1367      void fail(String message) {
  1.1368          throw new RuntimeException(message);
  1.1369      }
  1.1370 +
  1.1371 +    /**
  1.1372 +     * Indicates that the annotated method is a test method.
  1.1373 +     */
  1.1374 +    @Retention(RetentionPolicy.RUNTIME)
  1.1375 +    @Target(ElementType.METHOD)
  1.1376 +    public @interface Test {}
  1.1377  }

mercurial