33362
|
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 null test
|
|
27 |
* @build KullaTesting TestingInputStream
|
|
28 |
* @run testng TypeNameTest
|
|
29 |
*/
|
|
30 |
|
|
31 |
import jdk.jshell.Snippet;
|
|
32 |
import jdk.jshell.VarSnippet;
|
|
33 |
import org.testng.annotations.Test;
|
|
34 |
|
|
35 |
import static jdk.jshell.Snippet.Status.VALID;
|
|
36 |
import static org.testng.Assert.assertEquals;
|
|
37 |
import static jdk.jshell.Snippet.Status.OVERWRITTEN;
|
|
38 |
|
|
39 |
@Test
|
|
40 |
public class TypeNameTest extends KullaTesting {
|
|
41 |
|
|
42 |
public void testReplClassName() {
|
|
43 |
assertEval("class C {}");
|
|
44 |
VarSnippet sn = (VarSnippet) varKey(assertEval("new C();"));
|
|
45 |
assertEquals(sn.typeName(), "C");
|
|
46 |
}
|
|
47 |
|
|
48 |
public void testReplNestedClassName() {
|
|
49 |
assertEval("class D { static class E {} }");
|
|
50 |
VarSnippet sn = (VarSnippet) varKey(assertEval("new D.E();"));
|
|
51 |
assertEquals(sn.typeName(), "D.E");
|
|
52 |
}
|
|
53 |
|
|
54 |
public void testAnonymousClassName() {
|
|
55 |
assertEval("class C {}");
|
|
56 |
VarSnippet sn = (VarSnippet) varKey(assertEval("new C() { int x; };"));
|
|
57 |
assertEquals(sn.typeName(), "C");
|
|
58 |
}
|
|
59 |
|
|
60 |
public void testCapturedTypeName() {
|
|
61 |
VarSnippet sn = (VarSnippet) varKey(assertEval("\"\".getClass();"));
|
|
62 |
assertEquals(sn.typeName(), "Class<? extends String>");
|
|
63 |
}
|
|
64 |
|
|
65 |
public void testJavaLang() {
|
|
66 |
VarSnippet sn = (VarSnippet) varKey(assertEval("\"\";"));
|
|
67 |
assertEquals(sn.typeName(), "String");
|
|
68 |
}
|
|
69 |
|
|
70 |
public void testNotOverEagerPackageEating() {
|
|
71 |
VarSnippet sn = (VarSnippet) varKey(assertEval("\"\".getClass().getDeclaredMethod(\"hashCode\");"));
|
|
72 |
assertEquals(sn.typeName(), "java.lang.reflect.Method");
|
|
73 |
}
|
|
74 |
|
|
75 |
public void testBounds() {
|
|
76 |
assertEval("java.util.List<? extends String> list1 = java.util.Arrays.asList(\"\");");
|
|
77 |
VarSnippet sn1 = (VarSnippet) varKey(assertEval("list1.iterator().next()"));
|
|
78 |
assertEquals(sn1.typeName(), "String");
|
|
79 |
assertEval("java.util.List<? super String> list2 = java.util.Arrays.asList(\"\");");
|
|
80 |
VarSnippet sn2 = (VarSnippet) varKey(assertEval("list2.iterator().next()"));
|
|
81 |
assertEquals(sn2.typeName(), "Object");
|
|
82 |
assertEval("java.util.List<?> list3 = java.util.Arrays.asList(\"\");");
|
|
83 |
VarSnippet sn3 = (VarSnippet) varKey(assertEval("list3.iterator().next()"));
|
|
84 |
assertEquals(sn3.typeName(), "Object");
|
|
85 |
assertEval("class Test1<X extends CharSequence> { public X get() { return null; } }");
|
|
86 |
Snippet x = varKey(assertEval("Test1<?> x = new Test1<>();"));
|
|
87 |
VarSnippet sn4 = (VarSnippet) varKey(assertEval("x.get()"));
|
|
88 |
assertEquals(sn4.typeName(), "CharSequence");
|
|
89 |
assertEval("class Foo<X extends Number & CharSequence> { public X get() { return null; } }");
|
|
90 |
assertEval("Foo<?> x = new Foo<>();",
|
|
91 |
ste(MAIN_SNIPPET, VALID, VALID, true, null),
|
|
92 |
ste(x, VALID, OVERWRITTEN, false, MAIN_SNIPPET));
|
|
93 |
VarSnippet sn5 = (VarSnippet) varKey(assertEval("x.get()"));
|
|
94 |
assertEquals(sn5.typeName(), "Object");
|
|
95 |
}
|
|
96 |
}
|