1 /*
   2  * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @bug 8149524 8131024 8165211 8080071 8130454 8167343 8129559 8114842 8182268 8223782
  27  * @summary Test SourceCodeAnalysis
  28  * @build KullaTesting TestingInputStream
  29  * @run testng CompletenessTest
  30  */
  31 
  32 import java.util.Map;
  33 import java.util.HashMap;
  34 
  35 import org.testng.annotations.Test;
  36 import jdk.jshell.SourceCodeAnalysis.Completeness;
  37 
  38 import static jdk.jshell.SourceCodeAnalysis.Completeness.*;
  39 
  40 @Test
  41 public class CompletenessTest extends KullaTesting {
  42 
  43     // Add complete units that end with semicolon to complete_with_semi (without
  44     // the semicolon).  Both cases will be tested.
  45     static final String[] complete = new String[] {
  46         "{ x= 4; }",
  47         "int mm(int x) {kll}",
  48         "if (t) { ddd; }",
  49         "for (int i = 0; i < lines.length(); ++i) { foo }",
  50         "while (ct == null) { switch (current.kind) { case EOF: { } } }",
  51         "if (match.kind == BRACES && (prevCT.kind == ARROW || prevCT.kind == NEW_MIDDLE)) { new CT(UNMATCHED, current, \"Unmatched \" + unmatched); }",
  52         "enum TK { EOF(TokenKind.EOF, 0), NEW_MIDDLE(XEXPR1|XTERM); }",
  53         "List<T> f() { return null; }",
  54         "List<?> f() { return null; }",
  55         "List<? extends Object> f() { return null; }",
  56         "Map<? extends Object, ? super Object> f() { return null; }",
  57         "class C { int z; }",
  58         "synchronized (r) { f(); }",
  59         "try { } catch (Exception ex) { }",
  60         "try { } catch (Exception ex) { } finally { }",
  61         "try { } finally { }",
  62         "try (java.util.zip.ZipFile zf = new java.util.zip.ZipFile(zipFileName)) { }",
  63         "foo: while (true) { printf(\"Innn\"); break foo; }",
  64         "class Case<E1 extends Enum<E1>, E2 extends Enum<E2>, E3 extends Enum<E3>> {}",
  65         ";",
  66         "enum Tt { FOO, BAR, BAZ,; }"
  67     };
  68 
  69     static final String[] expression = new String[] {
  70         "test",
  71         "x + y",
  72         "x + y ++",
  73         "p = 9",
  74         "match(BRACKETS, TokenKind.LBRACKET)",
  75         "new C()",
  76         "new C() { public String toString() { return \"Hi\"; } }",
  77         "new int[]",
  78         "new int[] {1, 2,3}",
  79         "new Foo() {}",
  80         "i >= 0 && Character.isWhitespace(s.charAt(i))",
  81         "int.class",
  82         "String.class",
  83     };
  84 
  85     static final String[] complete_with_semi = new String[] {
  86         "int mm",
  87         "if (t) ddd",
  88         "int p = 9",
  89         "int p",
  90         "Deque<Token> stack = new ArrayDeque<>()",
  91         "final Deque<Token> stack = new ArrayDeque<>()",
  92         "java.util.Scanner input = new java.util.Scanner(System.in)",
  93         "java.util.Scanner input = new java.util.Scanner(System.in) { }",
  94         "int j = -i",
  95         "String[] a = { \"AAA\" }",
  96         "assert true",
  97         "int path[]",
  98         "int path[][]",
  99         "int path[][] = new int[22][]",
 100         "int path[] = new int[22]",
 101         "int path[] = new int[] {1, 2, 3}",
 102         "int[] path",
 103         "int path[] = new int[22]",
 104         "int path[][] = new int[22][]",
 105         "for (Object o : a) System.out.println(\"Yep\")",
 106         "while (os == null) System.out.println(\"Yep\")",
 107         "do f(); while (t)",
 108         "if (os == null) System.out.println(\"Yep\")",
 109         "if (t) if (!t) System.out.println(123)",
 110         "for (int i = 0; i < 10; ++i) if (i < 5) System.out.println(i); else break",
 111         "for (int i = 0; i < 10; ++i) if (i < 5) System.out.println(i); else continue",
 112         "for (int i = 0; i < 10; ++i) if (i < 5) System.out.println(i); else return",
 113         "throw ex",
 114         "C c = new C()",
 115         "java.util.zip.ZipFile zf = new java.util.zip.ZipFile(zipFileName)",
 116         "BufferedReader br = new BufferedReader(new FileReader(path))",
 117         "bar: g()",
 118         "baz: while (true) if (t()) printf('-'); else break baz",
 119         "java.util.function.IntFunction<int[]> ggg = int[]::new",
 120         "List<? extends Object> l",
 121         "int[] m = {1, 2}",
 122         "int[] m = {1, 2}, n = null",
 123         "int[] m = {1, 2}, n",
 124         "int[] m = {1, 2}, n = {3, 4}",
 125         "record D(int i)",
 126         "static record D(int i)"
 127     };
 128 
 129     static final String[] considered_incomplete = new String[] {
 130         "if (t)",
 131         "if (t) { } else",
 132         "if (t) if (!t)",
 133         "if (match.kind == BRACES && (prevCT.kind == ARROW || prevCT.kind == NEW_MIDDLE))",
 134         "for (int i = 0; i < 10; ++i)",
 135         "while (os == null)",
 136     };
 137 
 138     static final String[] definitely_incomplete = new String[] {
 139         "int mm(",
 140         "int mm(int x",
 141         "int mm(int x)",
 142         "int mm(int x) {",
 143         "int mm(int x) {kll",
 144         "if",
 145         "if (",
 146         "if (t",
 147         "if (t) {",
 148         "if (t) { ddd",
 149         "if (t) { ddd;",
 150         "if (t) if (",
 151         "if (stack.isEmpty()) {",
 152         "if (match.kind == BRACES && (prevCT.kind == ARROW || prevCT.kind == NEW_MIDDLE)) {",
 153         "if (match.kind == BRACES && (prevCT.kind == ARROW || prevCT.kind == NEW_MIDDLE)) { new CT(UNMATCHED, current, \"Unmatched \" + unmatched);",
 154         "x +",
 155         "x *",
 156         "3 *",
 157         "int",
 158         "for (int i = 0; i < lines.length(); ++i) {",
 159         "new",
 160         "new C(",
 161         "new int[",
 162         "new int[] {1, 2,3",
 163         "new int[] {",
 164         "while (ct == null) {",
 165         "while (ct == null) { switch (current.kind) {",
 166         "while (ct == null) { switch (current.kind) { case EOF: {",
 167         "while (ct == null) { switch (current.kind) { case EOF: { } }",
 168         "enum TK {",
 169         "enum TK { EOF(TokenKind.EOF, 0),",
 170         "enum TK { EOF(TokenKind.EOF, 0), NEW_MIDDLE(XEXPR1|XTERM)",
 171         "enum TK { EOF(TokenKind.EOF, 0), NEW_MIDDLE(XEXPR1|XTERM); ",
 172         "enum Tt { FOO, BAR, BAZ,;",
 173         "class C",
 174         "class C extends D",
 175         "class C implements D",
 176         "class C implements D, E",
 177         "interface I extends D",
 178         "interface I extends D, E",
 179         "enum E",
 180         "enum E implements I1",
 181         "enum E implements I1, I2",
 182         "@interface Anno",
 183         "void f()",
 184         "void f() throws E",
 185         "@A(",
 186         "int n = 4,",
 187         "int n,",
 188         "int[] m = {1, 2},",
 189         "int[] m = {1, 2}, n = {3, 4},",
 190         "Map<String,",
 191         "switch (x) {",
 192         "var v = switch (x) {",
 193         "var v = switch (x) { case ",
 194         "var v = switch (x) { case 0:",
 195         "var v = switch (x) { case 0: break 12; ",
 196     };
 197 
 198     static final String[] unknown = new String[] {
 199         "new ;"
 200     };
 201 
 202     static final Map<Completeness, String[]> statusToCases = new HashMap<>();
 203     static {
 204         statusToCases.put(COMPLETE, complete);
 205         statusToCases.put(COMPLETE_WITH_SEMI, complete_with_semi);
 206         statusToCases.put(CONSIDERED_INCOMPLETE, considered_incomplete);
 207         statusToCases.put(DEFINITELY_INCOMPLETE, definitely_incomplete);
 208     }
 209 
 210     private void assertStatus(String input, Completeness status, String source) {
 211         String augSrc;
 212         switch (status) {
 213             case COMPLETE_WITH_SEMI:
 214                 augSrc = source + ";";
 215                 break;
 216 
 217             case DEFINITELY_INCOMPLETE:
 218                 augSrc = null;
 219                 break;
 220 
 221             case CONSIDERED_INCOMPLETE:
 222                 augSrc = source + ";";
 223                 break;
 224 
 225             case EMPTY:
 226             case COMPLETE:
 227             case UNKNOWN:
 228                 augSrc = source;
 229                 break;
 230 
 231             default:
 232                 throw new AssertionError();
 233         }
 234         assertAnalyze(input, status, augSrc);
 235     }
 236 
 237     private void assertStatus(String[] ins, Completeness status) {
 238         for (String input : ins) {
 239             assertStatus(input, status, input);
 240         }
 241     }
 242 
 243     public void test_complete() {
 244         assertStatus(complete, COMPLETE);
 245     }
 246 
 247     public void test_expression() {
 248         assertStatus(expression, COMPLETE);
 249     }
 250 
 251     public void test_complete_with_semi() {
 252         assertStatus(complete_with_semi, COMPLETE_WITH_SEMI);
 253     }
 254 
 255     public void test_considered_incomplete() {
 256         assertStatus(considered_incomplete, CONSIDERED_INCOMPLETE);
 257     }
 258 
 259     public void test_definitely_incomplete() {
 260         assertStatus(definitely_incomplete, DEFINITELY_INCOMPLETE);
 261     }
 262 
 263     public void test_unknown() {
 264         assertStatus(definitely_incomplete, DEFINITELY_INCOMPLETE);
 265     }
 266 
 267     public void testCompleted_complete_with_semi() {
 268         for (String in : complete_with_semi) {
 269             String input = in + ";";
 270             assertStatus(input, COMPLETE, input);
 271         }
 272     }
 273 
 274     public void testCompleted_expression_with_semi() {
 275         for (String in : expression) {
 276             String input = in + ";";
 277             assertStatus(input, COMPLETE, input);
 278         }
 279     }
 280 
 281     public void testCompleted_considered_incomplete() {
 282         for (String in : considered_incomplete) {
 283             String input = in + ";";
 284             assertStatus(input, COMPLETE, input);
 285         }
 286     }
 287 
 288     private void assertSourceByStatus(String first) {
 289         for (Map.Entry<Completeness, String[]> e : statusToCases.entrySet()) {
 290             for (String in : e.getValue()) {
 291                 String input = first + in;
 292                 assertAnalyze(input, COMPLETE, first, in, true);
 293             }
 294         }
 295     }
 296 
 297     public void testCompleteSource_complete() {
 298         for (String input : complete) {
 299             assertSourceByStatus(input);
 300         }
 301     }
 302 
 303     public void testCompleteSource_complete_with_semi() {
 304         for (String in : complete_with_semi) {
 305             String input = in + ";";
 306             assertSourceByStatus(input);
 307         }
 308     }
 309 
 310     public void testCompleteSource_expression() {
 311         for (String in : expression) {
 312             String input = in + ";";
 313             assertSourceByStatus(input);
 314         }
 315     }
 316 
 317     public void testCompleteSource_considered_incomplete() {
 318         for (String in : considered_incomplete) {
 319             String input = in + ";";
 320             assertSourceByStatus(input);
 321         }
 322     }
 323 
 324     public void testTrailingSlash() {
 325         assertStatus("\"abc\\", UNKNOWN, "\"abc\\");
 326     }
 327 
 328     public void testOpenComment() {
 329         assertStatus("int xx; /* hello", DEFINITELY_INCOMPLETE, null);
 330         assertStatus("/**  test", DEFINITELY_INCOMPLETE, null);
 331     }
 332 
 333     public void testTextBlocks() {
 334         assertStatus("\"\"\"", DEFINITELY_INCOMPLETE, null);
 335         assertStatus("\"\"\"broken", DEFINITELY_INCOMPLETE, null);
 336         assertStatus("\"\"\"\ntext", DEFINITELY_INCOMPLETE, null);
 337         assertStatus("\"\"\"\ntext\"\"", DEFINITELY_INCOMPLETE, "\"\"\"\ntext\"\"\"");
 338         assertStatus("\"\"\"\ntext\"\"\"", COMPLETE, "\"\"\"\ntext\"\"\"");
 339         assertStatus("\"\"\"\ntext\\\"\"\"\"", COMPLETE, "\"\"\"\ntext\\\"\"\"\"");
 340         assertStatus("\"\"\"\ntext\\\"\"\"", DEFINITELY_INCOMPLETE, null);
 341         assertStatus("\"\"\"\ntext\\\"\"\"\\\"\"\"", DEFINITELY_INCOMPLETE, null);
 342         assertStatus("\"\"\"\ntext\\\"\"\"\\\"\"\"\"\"\"", COMPLETE, "\"\"\"\ntext\\\"\"\"\\\"\"\"\"\"\"");
 343     }
 344 
 345     public void testMiscSource() {
 346         assertStatus("if (t) if ", DEFINITELY_INCOMPLETE, "if (t) if"); //Bug
 347         assertStatus("int m() {} dfd", COMPLETE, "int m() {}");
 348         assertStatus("int p = ", DEFINITELY_INCOMPLETE, "int p ="); //Bug
 349         assertStatus("int[] m = {1, 2}, n = new int[0];  int i;", COMPLETE,
 350                      "int[] m = {1, 2}, n = new int[0];");
 351     }
 352 }