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