36511
|
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 |
* @compile Basic.java
|
|
27 |
* @run testng p.Basic
|
|
28 |
* @summary Basic test for java.lang.Class::getPackageName
|
|
29 |
*/
|
|
30 |
|
|
31 |
package p;
|
|
32 |
|
|
33 |
import org.testng.annotations.Test;
|
|
34 |
import org.testng.annotations.DataProvider;
|
|
35 |
import static org.testng.Assert.*;
|
|
36 |
|
|
37 |
public class Basic {
|
|
38 |
|
|
39 |
|
|
40 |
// -- member classes --
|
|
41 |
|
|
42 |
static class Nested {
|
|
43 |
static class Foo { }
|
|
44 |
}
|
|
45 |
|
|
46 |
Class<?> getNestedClass1() {
|
|
47 |
return Nested.class;
|
|
48 |
}
|
|
49 |
Class<?> getNestedClass2() {
|
|
50 |
return Nested.Foo.class;
|
|
51 |
}
|
|
52 |
|
|
53 |
class Inner {
|
|
54 |
class Foo { }
|
|
55 |
}
|
|
56 |
|
|
57 |
Class<?> getInnerClass1() {
|
|
58 |
return Inner.class;
|
|
59 |
}
|
|
60 |
Class<?> getInnerClass2() {
|
|
61 |
return Inner.Foo.class;
|
|
62 |
}
|
|
63 |
|
|
64 |
// -- local and anonymous classes --
|
|
65 |
|
|
66 |
Class<?> getLocalClass1() {
|
|
67 |
class Local { }
|
|
68 |
return Local.class;
|
|
69 |
}
|
|
70 |
|
|
71 |
Class<?> getLocalClass2() {
|
|
72 |
class Local {
|
|
73 |
class Foo { }
|
|
74 |
}
|
|
75 |
return Local.Foo.class;
|
|
76 |
}
|
|
77 |
|
|
78 |
Class<?> getLocalClass3() {
|
|
79 |
class Local {
|
|
80 |
final Class<?> c;
|
|
81 |
Local() {
|
|
82 |
class Foo { }
|
|
83 |
this.c = Foo.class;
|
|
84 |
}
|
|
85 |
Class<?> get() {
|
|
86 |
return c;
|
|
87 |
}
|
|
88 |
}
|
|
89 |
return new Local().get();
|
|
90 |
}
|
|
91 |
|
|
92 |
Class<?> getAnonymousClass1() {
|
|
93 |
Runnable r = new Runnable() { public void run() { } };
|
|
94 |
return r.getClass();
|
|
95 |
}
|
|
96 |
|
|
97 |
Class<?> getAnonymousClass2() {
|
|
98 |
class Local {
|
|
99 |
Class<?> get() {
|
|
100 |
Runnable r = new Runnable() { public void run() { } };
|
|
101 |
return r.getClass();
|
|
102 |
}
|
|
103 |
}
|
|
104 |
return new Local().get();
|
|
105 |
}
|
|
106 |
|
|
107 |
Class<?> getAnonymousClass3() {
|
|
108 |
Runnable r = () -> { };
|
|
109 |
return r.getClass();
|
|
110 |
}
|
|
111 |
|
|
112 |
Class<?> getAnonymousClass4() {
|
|
113 |
class Local {
|
|
114 |
Class<?> get() {
|
|
115 |
Runnable r = () -> { };
|
|
116 |
return r.getClass();
|
|
117 |
}
|
|
118 |
}
|
|
119 |
return new Local().get();
|
|
120 |
}
|
|
121 |
|
|
122 |
Class<?> getAnonymousClass5() {
|
|
123 |
class Local {
|
|
124 |
final Class<?> c;
|
|
125 |
Local() {
|
|
126 |
Runnable r = new Runnable() { public void run() { } };
|
|
127 |
this.c = r.getClass();
|
|
128 |
}
|
|
129 |
Class<?> get() {
|
|
130 |
return c;
|
|
131 |
}
|
|
132 |
}
|
|
133 |
return new Local().get();
|
|
134 |
}
|
|
135 |
|
|
136 |
Class<?> getAnonymousClass6() {
|
|
137 |
class Local {
|
|
138 |
final Class<?> c;
|
|
139 |
Local() {
|
|
140 |
Runnable r = () -> { };
|
|
141 |
this.c = r.getClass();
|
|
142 |
}
|
|
143 |
Class<?> get() {
|
|
144 |
return c;
|
|
145 |
}
|
|
146 |
}
|
|
147 |
return new Local().get();
|
|
148 |
}
|
|
149 |
|
|
150 |
static final String TEST_PACKAGE = Basic.class.getPackage().getName();
|
|
151 |
|
|
152 |
@DataProvider(name = "classes")
|
|
153 |
public Object[][] classes() {
|
|
154 |
return new Object[][] {
|
|
155 |
|
|
156 |
{ Basic.class, TEST_PACKAGE },
|
|
157 |
{ Basic[].class, null },
|
|
158 |
{ Basic[][].class, null },
|
|
159 |
|
|
160 |
{ getNestedClass1(), TEST_PACKAGE },
|
|
161 |
{ getNestedClass2(), TEST_PACKAGE },
|
|
162 |
{ getInnerClass1(), TEST_PACKAGE },
|
|
163 |
{ getInnerClass2(), TEST_PACKAGE },
|
|
164 |
|
|
165 |
{ getLocalClass1(), TEST_PACKAGE },
|
|
166 |
{ getLocalClass2(), TEST_PACKAGE },
|
|
167 |
{ getLocalClass3(), TEST_PACKAGE },
|
|
168 |
|
|
169 |
{ getAnonymousClass1(), TEST_PACKAGE },
|
|
170 |
{ getAnonymousClass2(), TEST_PACKAGE },
|
|
171 |
{ getAnonymousClass3(), TEST_PACKAGE },
|
|
172 |
{ getAnonymousClass4(), TEST_PACKAGE },
|
|
173 |
{ getAnonymousClass5(), TEST_PACKAGE },
|
|
174 |
{ getAnonymousClass6(), TEST_PACKAGE },
|
|
175 |
|
|
176 |
{ Object.class, "java.lang" },
|
|
177 |
{ Object[].class, null },
|
|
178 |
{ Object[][].class, null },
|
|
179 |
|
|
180 |
{ int.class, null },
|
|
181 |
{ int[].class, null },
|
|
182 |
{ int[][].class, null },
|
|
183 |
|
|
184 |
{ void.class, null },
|
|
185 |
|
|
186 |
};
|
|
187 |
}
|
|
188 |
|
|
189 |
@Test(dataProvider = "classes")
|
|
190 |
public void testPackageName(Class<?> type, String expected) {
|
|
191 |
assertEquals(type.getPackageName(), expected);
|
|
192 |
}
|
|
193 |
|
|
194 |
}
|