aboutsummaryrefslogtreecommitdiff
path: root/plugins/base/src/test/kotlin/model/annotations/JavaAnnotationsTest.kt
blob: 2b3637bb8cebd9fcf8d9de85d5ce1ba457b16575 (plain)
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
/*
 * Copyright 2014-2023 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
 */

package model.annotations

import org.jetbrains.dokka.base.testApi.testRunner.BaseAbstractTest
import org.jetbrains.dokka.model.*
import translators.findClasslike
import kotlin.test.*
import utils.JavaCode

@JavaCode
class JavaAnnotationsTest : BaseAbstractTest() {

    val configuration = dokkaConfiguration {
        sourceSets {
            sourceSet {
                sourceRoots = listOf("src/main/java")
            }
        }
    }

    @Test // see https://github.com/Kotlin/dokka/issues/2350
    fun `should hande array used as annotation param value`() {
        testInline(
            """
            |/src/main/java/annotation/TestClass.java
            |package annotation;
            |public class TestClass {
            |    @SimpleAnnotation(clazz = String[].class)
            |    public boolean simpleAnnotation() {
            |        return false;
            |    }
            |}
            |
            |/src/main/java/annotation/SimpleAnnotation.java
            |package annotation;
            |@Retention(RetentionPolicy.RUNTIME)
            |@Target(ElementType.METHOD)
            |public @interface SimpleAnnotation {
            |    Class<?> clazz();
            |}
            """.trimIndent(),
            configuration
        ) {
            documentablesTransformationStage = { module ->
                val testClass = module.findClasslike("annotation", "TestClass") as DClass
                assertNotNull(testClass)

                val annotatedFunction = testClass.functions.single { it.name == "simpleAnnotation" }
                val annotation =
                    annotatedFunction.extra[Annotations]?.directAnnotations?.entries?.single()?.value?.single()
                assertNotNull(annotation) { "Expected to find an annotation on simpleAnnotation function, found none" }
                assertEquals("annotation", annotation.dri.packageName)
                assertEquals("SimpleAnnotation", annotation.dri.classNames)
                assertEquals(1, annotation.params.size)

                val param = annotation.params.values.single()
                assertTrue(param is ClassValue)
                // should probably be Array instead
                // String matches parsing of Kotlin sources as of now
                assertEquals("String", param.className)
                assertEquals("java.lang", param.classDRI.packageName)
                assertEquals("String", param.classDRI.classNames)
            }
        }
    }

    @Test // see https://github.com/Kotlin/dokka/issues/2551
    fun `should hande annotation used within annotation params with class param value`() {
        testInline(
            """
            |/src/main/java/annotation/TestClass.java
            |package annotation;
            |public class TestClass {
            |    @XmlElementRefs({
            |            @XmlElementRef(name = "NotOffered", namespace = "http://www.gaeb.de/GAEB_DA_XML/DA86/3.3", type = JAXBElement.class, required = false)
            |    })
            |    public List<JAXBElement<Object>> content;
            |}
            |
            |/src/main/java/annotation/XmlElementRefs.java
            |package annotation;
            |public @interface XmlElementRefs {
            |    XmlElementRef[] value();
            |}
            |
            |/src/main/java/annotation/XmlElementRef.java
            |package annotation;
            |public @interface XmlElementRef {
            |    String name();
            |
            |    String namespace();
            |
            |    boolean required();
            |    
            |    Class<JAXBElement> type();
            |}
            |
            |/src/main/java/annotation/JAXBElement.java
            |package annotation;
            |public class JAXBElement<T> {
            |}
            """.trimIndent(),
            configuration
        ) {
            documentablesTransformationStage = { module ->
                val testClass = module.findClasslike("annotation", "TestClass") as DClass
                assertNotNull(testClass)

                val contentField = testClass.properties.find { it.name == "content" }
                assertNotNull(contentField)

                val annotation = contentField.extra[Annotations]?.directAnnotations?.entries?.single()?.value?.single()
                assertNotNull(annotation) { "Expected to find an annotation on content field, found none" }
                assertEquals("XmlElementRefs", annotation.dri.classNames)
                assertEquals(1, annotation.params.size)

                val arrayParam = annotation.params.values.single()
                assertTrue(arrayParam is ArrayValue, "Expected single annotation param to be array")
                assertEquals(1, arrayParam.value.size)

                val arrayParamValue = arrayParam.value.single()
                assertTrue(arrayParamValue is AnnotationValue)

                val arrayParamAnnotationValue = arrayParamValue.annotation
                assertEquals(4, arrayParamAnnotationValue.params.size)
                assertEquals("XmlElementRef", arrayParamAnnotationValue.dri.classNames)

                val annotationParams = arrayParamAnnotationValue.params.values.toList()

                val nameParam = annotationParams[0]
                assertTrue(nameParam is StringValue)
                assertEquals("NotOffered", nameParam.value)

                val namespaceParam = annotationParams[1]
                assertTrue(namespaceParam is StringValue)
                assertEquals("http://www.gaeb.de/GAEB_DA_XML/DA86/3.3", namespaceParam.value)

                val typeParam = annotationParams[2]
                assertTrue(typeParam is ClassValue)
                assertEquals("JAXBElement", typeParam.className)
                assertEquals("annotation", typeParam.classDRI.packageName)
                assertEquals("JAXBElement", typeParam.classDRI.classNames)

                val requiredParam = annotationParams[3]
                assertTrue(requiredParam is BooleanValue)
                assertFalse(requiredParam.value)
            }
        }
    }

    @Test // see https://github.com/Kotlin/dokka/issues/2509
    fun `should handle generic class in annotation`() {
        testInline(
            """
            |/src/main/java/annotation/Breaking.java
            |package annotation;
            |public class Breaking<Y> { 
            |}
            |
            |/src/main/java/annotation/TestAnnotate.java
            |package annotation;
            |public @interface TestAnnotate {
            |    Class<?> value();
            |}
            |
            |/src/main/java/annotation/TestClass.java
            |package annotation;
            |@TestAnnotate(Breaking.class)
            |public class TestClass {
            |}
            """.trimIndent(),
            configuration
        ) {
            documentablesTransformationStage = { module ->
                val testClass = module.findClasslike("annotation", "TestClass") as DClass
                assertNotNull(testClass)

                val annotation = testClass.extra[Annotations]?.directAnnotations?.entries?.single()?.value?.single()
                assertNotNull(annotation) { "Expected to find an annotation on TestClass, found none" }

                assertEquals("TestAnnotate", annotation.dri.classNames)
                assertEquals(1, annotation.params.size)

                val valueParameter = annotation.params.values.single()
                assertTrue(valueParameter is ClassValue)

                assertEquals("Breaking", valueParameter.className)

                assertEquals("annotation", valueParameter.classDRI.packageName)
                assertEquals("Breaking", valueParameter.classDRI.classNames)
            }
        }
    }
}