1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
|
package model
import org.jetbrains.dokka.Platform
import org.jetbrains.dokka.base.transformers.documentables.InheritorsInfo
import org.jetbrains.dokka.links.DRI
import org.jetbrains.dokka.links.JavaClassReference
import org.jetbrains.dokka.links.PointingToDeclaration
import org.jetbrains.dokka.links.sureClassNames
import org.jetbrains.dokka.model.*
import org.jetbrains.dokka.model.doc.Param
import org.jetbrains.dokka.model.doc.Text
import org.junit.jupiter.api.Assertions.assertTrue
import org.junit.jupiter.api.Test
import utils.AbstractModelTest
import utils.assertNotNull
import utils.name
import kotlin.test.assertEquals
import org.jetbrains.dokka.links.Callable as DRICallable
class JavaTest : AbstractModelTest("/src/main/kotlin/java/Test.java", "java") {
val configuration = dokkaConfiguration {
sourceSets {
sourceSet {
sourceRoots = listOf("src/")
analysisPlatform = Platform.jvm.toString()
classpath += jvmStdlibPath!!
includeNonPublic = true
}
}
}
@Test
fun function() {
inlineModelTest(
"""
|class Test {
| /**
| * Summary for Function
| * @param name is String parameter
| * @param value is int parameter
| */
| public void fn(String name, int value) {}
|}
""", configuration = configuration
) {
with((this / "java" / "Test").cast<DClass>()) {
name equals "Test"
children counts 1
with((this / "fn").cast<DFunction>()) {
name equals "fn"
val params = parameters.map { it.documentation.values.first().children.first() as Param }
params.mapNotNull { it.firstMemberOfType<Text>()?.body } equals listOf("is String parameter", "is int parameter")
}
}
}
}
@Test fun allImplementedInterfacesInJava() {
inlineModelTest(
"""
|interface Highest { }
|interface Lower extends Highest { }
|class Extendable { }
|class Tested extends Extendable implements Lower { }
""", configuration = configuration){
with((this / "java" / "Tested").cast<DClass>()){
extra[ImplementedInterfaces]?.interfaces?.entries?.single()?.value?.map { it.dri.sureClassNames }?.sorted() equals listOf("Highest", "Lower").sorted()
}
}
}
@Test fun multipleClassInheritanceWithInterface() {
inlineModelTest(
"""
|interface Highest { }
|interface Lower extends Highest { }
|class Extendable { }
|class Tested extends Extendable implements Lower { }
""", configuration = configuration){
with((this / "java" / "Tested").cast<DClass>()) {
supertypes.entries.single().value.map { it.typeConstructor.dri.sureClassNames to it.kind }.sortedBy { it.first } equals listOf("Extendable" to JavaClassKindTypes.CLASS, "Lower" to JavaClassKindTypes.INTERFACE)
}
}
}
@Test
fun superClass() {
inlineModelTest(
"""
|public class Foo extends Exception implements Cloneable {}
""", configuration = configuration
) {
with((this / "java" / "Foo").cast<DClass>()) {
val sups = listOf("Exception", "Cloneable")
assertTrue(
sups.all { s -> supertypes.values.flatten().any { it.typeConstructor.dri.classNames == s } })
"Foo must extend ${sups.joinToString(", ")}"
}
}
}
@Test
fun arrayType() {
inlineModelTest(
"""
|class Test {
| public String[] arrayToString(int[] data) {
| return null;
| }
|}
""", configuration = configuration
) {
with((this / "java" / "Test").cast<DClass>()) {
name equals "Test"
children counts 1
with((this / "arrayToString").cast<DFunction>()) {
name equals "arrayToString"
type.name equals "Array"
with(parameters.firstOrNull().assertNotNull("parameters")) {
name equals "data"
type.name equals "Array"
}
}
}
}
}
@Test
fun typeParameter() {
inlineModelTest(
"""
|class Foo<T extends Comparable<T>> {
| public <E> E foo();
|}
""", configuration = configuration
) {
with((this / "java" / "Foo").cast<DClass>()) {
generics counts 1
}
}
}
@Test
fun constructors() {
inlineModelTest(
"""
|class Test {
| public Test() {}
|
| public Test(String s) {}
|}
""", configuration = configuration
) {
with((this / "java" / "Test").cast<DClass>()) {
name equals "Test"
constructors counts 2
constructors.forEach { it.name equals "Test" }
constructors.find { it.parameters.isNullOrEmpty() }.assertNotNull("Test()")
with(constructors.find { it.parameters.isNotEmpty() }.assertNotNull("Test(String)")) {
parameters.firstOrNull()?.type?.name equals "String"
}
}
}
}
@Test
fun innerClass() {
inlineModelTest(
"""
|class InnerClass {
| public class D {}
|}
""", configuration = configuration
) {
with((this / "java" / "InnerClass").cast<DClass>()) {
children counts 1
with((this / "D").cast<DClass>()) {
name equals "D"
children counts 0
}
}
}
}
@Test
fun varargs() {
inlineModelTest(
"""
|class Foo {
| public void bar(String... x);
|}
""", configuration = configuration
) {
with((this / "java" / "Foo").cast<DClass>()) {
name equals "Foo"
children counts 1
with((this / "bar").cast<DFunction>()) {
name equals "bar"
with(parameters.firstOrNull().assertNotNull("parameter")) {
name equals "x"
type.name equals "Array"
}
}
}
}
}
@Test
fun fields() {
inlineModelTest(
"""
|class Test {
| public int i;
| public static final String s;
|}
""", configuration = configuration
) {
with((this / "java" / "Test").cast<DClass>()) {
children counts 2
with((this / "i").cast<DProperty>()) {
getter equals null
setter equals null
}
with((this / "s").cast<DProperty>()) {
getter equals null
setter equals null
}
}
}
}
@Test
fun staticMethod() {
inlineModelTest(
"""
|class C {
| public static void foo() {}
|}
""", configuration = configuration
) {
with((this / "java" / "C" / "foo").cast<DFunction>()) {
with(extra[AdditionalModifiers]!!.content.entries.single().value.assertNotNull("AdditionalModifiers")) {
this counts 1
first() equals ExtraModifiers.JavaOnlyModifiers.Static
}
}
}
}
@Test
fun annotatedAnnotation() {
inlineModelTest(
"""
|import java.lang.annotation.*;
|
|@Target({ElementType.FIELD, ElementType.TYPE, ElementType.METHOD})
|public @interface Attribute {
| String value() default "";
|}
""", configuration = configuration
) {
with((this / "java" / "Attribute").cast<DAnnotation>()) {
with(extra[Annotations]!!.directAnnotations.entries.single().value.assertNotNull("Annotations")) {
with(single()) {
dri.classNames equals "Target"
(params["value"].assertNotNull("value") as ArrayValue).value equals listOf(
EnumValue("ElementType.FIELD", DRI("java.lang.annotation", "ElementType")),
EnumValue("ElementType.TYPE", DRI("java.lang.annotation", "ElementType")),
EnumValue("ElementType.METHOD", DRI("java.lang.annotation", "ElementType"))
)
}
}
}
}
}
@Test
fun javaLangObject() {
inlineModelTest(
"""
|class Test {
| public Object fn() { return null; }
|}
""", configuration = configuration
) {
with((this / "java" / "Test" / "fn").cast<DFunction>()) {
assertTrue(type is JavaObject)
}
}
}
@Test
fun enumValues() {
inlineModelTest(
"""
|enum E {
| Foo
|}
""", configuration = configuration
) {
with((this / "java" / "E").cast<DEnum>()) {
name equals "E"
entries counts 1
functions.sortedBy { it.name }.filter { it.name == "valueOf" || it.name == "values" }.map { it.dri } equals listOf(
DRI("java", "E", DRICallable("valueOf", null, listOf(JavaClassReference("java.lang.String"))), PointingToDeclaration),
DRI("java", "E", DRICallable("values", null, emptyList()), PointingToDeclaration),
)
with((this / "Foo").cast<DEnumEntry>()) {
name equals "Foo"
}
}
}
}
@Test
fun inheritorLinks() {
inlineModelTest(
"""
|public class InheritorLinks {
| public static class Foo {}
|
| public static class Bar extends Foo {}
|}
""", configuration = configuration
) {
with((this / "java" / "InheritorLinks").cast<DClass>()) {
val dri = (this / "Bar").assertNotNull("Foo dri").dri
with((this / "Foo").cast<DClass>()) {
with(extra[InheritorsInfo].assertNotNull("InheritorsInfo")) {
with(value.values.flatten().distinct()) {
this counts 1
first() equals dri
}
}
}
}
}
}
@Test
fun `retention should work with static import`() {
inlineModelTest(
"""
|import java.lang.annotation.Retention;
|import java.lang.annotation.RetentionPolicy;
|import static java.lang.annotation.RetentionPolicy.RUNTIME;
|
|@Retention(RUNTIME)
|public @interface JsonClass {
|};
""", configuration = configuration
) {
with((this / "java" / "JsonClass").cast<DAnnotation>()) {
val annotation = extra[Annotations]?.directAnnotations?.entries
?.firstOrNull()?.value //First sourceset
?.firstOrNull()
val expectedDri = DRI("java.lang.annotation", "Retention", null, PointingToDeclaration)
val expectedParams = "value" to EnumValue(
"RUNTIME",
DRI(
"java.lang.annotation",
"RetentionPolicy",
DRICallable("RUNTIME", null, emptyList()),
PointingToDeclaration
)
)
assertEquals(expectedDri, annotation?.dri)
assertEquals(expectedParams.first, annotation?.params?.entries?.first()?.key)
assertEquals(expectedParams.second, annotation?.params?.entries?.first()?.value)
}
}
}
}
|