1 /*
2  * Copyright (c) 2015, 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 8080352
26  * @summary Tests for hard errors, like syntax errors
27  * @build KullaTesting
28  * @run testng RejectedFailedTest
29  */
30 
31 import java.util.List;
32 
33 import jdk.jshell.Snippet.SubKind;
34 import org.testng.annotations.Test;
35 
36 import jdk.jshell.Diag;
37 import jdk.jshell.Snippet;
38 import jdk.jshell.Snippet.Kind;
39 import jdk.jshell.Snippet.Status;
40 
41 import jdk.jshell.SnippetEvent;
42 import static java.util.stream.Collectors.toList;
43 import static org.testng.Assert.assertEquals;
44 import static org.testng.Assert.assertTrue;
45 
46 @Test
47 public class RejectedFailedTest extends KullaTesting {
48 
bad(String input, Kind kind, String prevId)49     private String bad(String input, Kind kind, String prevId) {
50         List<SnippetEvent> events = assertEvalFail(input);
51         assertEquals(events.size(), 1, "Expected one event, got: " + events.size());
52         SnippetEvent e = events.get(0);
53         List<Diag> diagnostics = getState().diagnostics(e.snippet()).collect(toList());
54         assertTrue(diagnostics.size() > 0, "Expected diagnostics, got none");
55         assertEquals(e.exception(), null, "Expected exception to be null.");
56         assertEquals(e.value(), null, "Expected value to be null.");
57 
58         Snippet key = e.snippet();
59         assertTrue(key != null, "key must be non-null, but was null.");
60         assertEquals(key.kind(), kind, "Expected kind: " + kind + ", got: " + key.kind());
61         SubKind expectedSubKind = kind == Kind.ERRONEOUS ? SubKind.UNKNOWN_SUBKIND : SubKind.METHOD_SUBKIND;
62         assertEquals(key.subKind(), expectedSubKind, "SubKind: ");
63         assertTrue(key.id().compareTo(prevId) > 0, "Current id: " + key.id() + ", previous: " + prevId);
64         assertEquals(getState().diagnostics(key).collect(toList()), diagnostics, "Expected retrieved diagnostics to match, but didn't.");
65         assertEquals(key.source(), input, "Expected retrieved source: " +
66                 key.source() + " to match input: " + input);
67         assertEquals(getState().status(key), Status.REJECTED, "Expected status of REJECTED, got: " + getState().status(key));
68         return key.id();
69     }
70 
checkByKind(String[] inputs, Kind kind)71     private void checkByKind(String[] inputs, Kind kind) {
72         String prevId = "";
73         for (String in : inputs) {
74             prevId = bad(in, kind, prevId);
75         }
76     }
77 
testErroneous()78     public void testErroneous() {
79         String[] inputsErroneous = {
80                 "%&^%&",
81                 " a b c",
82                 ")",
83                 "class interface A",
84                 "package foo;"
85         };
86         checkByKind(inputsErroneous, Kind.ERRONEOUS);
87     }
88 
testBadMethod()89     public void testBadMethod() {
90         String[] inputsMethod = {
91                 "transient int m() { return x; }",
92                 "int h() { }",
93                 "void l() { return 4; }",
94                 "int vv(void x) { return 2; }",
95         };
96         checkByKind(inputsMethod, Kind.METHOD);
97     }
98 }
99