Skip to content

Commit a601ba5

Browse files
committed
Cleanups
1 parent 7e37156 commit a601ba5

1 file changed

Lines changed: 48 additions & 63 deletions

File tree

src/test/kotlin/com/github/jengelman/gradle/plugins/shadow/internal/RelocatorRemapperTest.kt renamed to src/test/kotlin/com/github/jengelman/gradle/plugins/shadow/internal/BytecodeRemappingTest.kt

Lines changed: 48 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import org.junit.jupiter.api.io.TempDir
2424
* bytecode level to verify that the remapper correctly transforms class names in all relevant
2525
* bytecode structures.
2626
*/
27-
class RelocatorRemapperTest {
27+
class BytecodeRemappingTest {
2828
@TempDir lateinit var tempDir: Path
2929

3030
// Relocator used across all relocation tests: moves the test package to a distinct target.
@@ -37,11 +37,14 @@ class RelocatorRemapperTest {
3737
)
3838

3939
// Internal name of the relocated FixtureBase for use in assertions.
40-
private val relocatedFixtureBase = $$"com/example/relocated/RelocatorRemapperTest$FixtureBase"
40+
private val relocatedFixtureBase = $$"com/example/relocated/BytecodeRemappingTest$FixtureBase"
41+
42+
private val fixtureSubjectDetails
43+
get() = FixtureSubject::class.toFileCopyDetails()
4144

4245
@Test
43-
fun remapClassNotModified() {
44-
val details = FixtureSubject::class.toFileCopyDetails()
46+
fun classNotModified() {
47+
val details = fixtureSubjectDetails
4548
// Relocator pattern does not match – original bytes must be returned as-is.
4649
val noMatchRelocators = setOf(SimpleRelocator("org.unrelated", "org.other"))
4750

@@ -51,118 +54,100 @@ class RelocatorRemapperTest {
5154
}
5255

5356
@Test
54-
fun remapClassNameIsRelocated() {
55-
val details = FixtureSubject::class.toFileCopyDetails()
56-
57-
val result = details.remapClass(relocators)
57+
fun classNameIsRelocated() {
58+
val result = fixtureSubjectDetails.remapClass(relocators)
5859

5960
val classModel = ClassFile.of().parse(result)
6061
assertThat(classModel.thisClass().asInternalName())
61-
.isEqualTo($$"com/example/relocated/RelocatorRemapperTest$FixtureSubject")
62+
.isEqualTo($$"com/example/relocated/BytecodeRemappingTest$FixtureSubject")
6263
}
6364

6465
@Test
65-
fun remapSuperclassIsRelocated() {
66-
val details = FixtureSubject::class.toFileCopyDetails()
67-
68-
val result = details.remapClass(relocators)
66+
fun annotationIsRelocated() {
67+
val result = fixtureSubjectDetails.remapClass(relocators)
6968

7069
val classModel = ClassFile.of().parse(result)
71-
assertThat(classModel.superclass().get().asInternalName()).isEqualTo(relocatedFixtureBase)
70+
val annotationsAttr = classModel.findAttribute(Attributes.runtimeVisibleAnnotations())
71+
assertThat(annotationsAttr.isPresent).isTrue()
72+
val annotationDescriptors =
73+
annotationsAttr.get().annotations().map { it.className().stringValue() }
74+
assertThat(annotationDescriptors)
75+
.contains($$"Lcom/example/relocated/BytecodeRemappingTest$FixtureAnnotation;")
7276
}
7377

7478
@Test
75-
fun remapFieldDescriptorIsRelocated() {
76-
val details = FixtureSubject::class.toFileCopyDetails()
79+
fun baseClassNameIsRelocated() {
80+
// Verify relocation also works on a simple class (FixtureBase has no fields/methods
81+
// referencing the target package beyond its own class name).
82+
val details = FixtureBase::class.toFileCopyDetails()
7783

7884
val result = details.remapClass(relocators)
7985

8086
val classModel = ClassFile.of().parse(result)
81-
val fieldDescriptors = classModel.fields().map { it.fieldType().stringValue() }
82-
assertThat(fieldDescriptors).contains("L$relocatedFixtureBase;")
87+
assertThat(classModel.thisClass().asInternalName()).isEqualTo(relocatedFixtureBase)
8388
}
8489

8590
@Test
86-
fun remapMethodDescriptorIsRelocated() {
87-
val details = FixtureSubject::class.toFileCopyDetails()
88-
89-
val result = details.remapClass(relocators)
91+
fun superclassIsRelocated() {
92+
val result = fixtureSubjectDetails.remapClass(relocators)
9093

9194
val classModel = ClassFile.of().parse(result)
92-
val methodDescriptors = classModel.methods().map { it.methodType().stringValue() }
93-
assertThat(methodDescriptors).contains("(L$relocatedFixtureBase;)L$relocatedFixtureBase;")
95+
assertThat(classModel.superclass().get().asInternalName()).isEqualTo(relocatedFixtureBase)
9496
}
9597

9698
@Test
97-
fun remapAnnotationIsRelocated() {
98-
val details = FixtureSubject::class.toFileCopyDetails()
99-
100-
val result = details.remapClass(relocators)
99+
fun fieldDescriptorIsRelocated() {
100+
val result = fixtureSubjectDetails.remapClass(relocators)
101101

102102
val classModel = ClassFile.of().parse(result)
103-
val annotationsAttr = classModel.findAttribute(Attributes.runtimeVisibleAnnotations())
104-
assertThat(annotationsAttr.isPresent).isTrue()
105-
val annotationDescriptors =
106-
annotationsAttr.get().annotations().map { it.className().stringValue() }
107-
assertThat(annotationDescriptors)
108-
.contains($$"Lcom/example/relocated/RelocatorRemapperTest$FixtureAnnotation;")
103+
val fieldDescriptors = classModel.fields().map { it.fieldType().stringValue() }
104+
assertThat(fieldDescriptors).contains("L$relocatedFixtureBase;")
109105
}
110106

111107
@Test
112-
fun remapArrayFieldDescriptorIsRelocated() {
113-
val details = FixtureSubject::class.toFileCopyDetails()
114-
115-
val result = details.remapClass(relocators)
108+
fun arrayFieldDescriptorIsRelocated() {
109+
val result = fixtureSubjectDetails.remapClass(relocators)
116110

117111
val classModel = ClassFile.of().parse(result)
118112
val fieldDescriptors = classModel.fields().map { it.fieldType().stringValue() }
119113
assertThat(fieldDescriptors).contains("[L$relocatedFixtureBase;")
120114
}
121115

122116
@Test
123-
fun remapArray2dFieldDescriptorIsRelocated() {
124-
val details = FixtureSubject::class.toFileCopyDetails()
125-
126-
val result = details.remapClass(relocators)
117+
fun array2dFieldDescriptorIsRelocated() {
118+
val result = fixtureSubjectDetails.remapClass(relocators)
127119

128120
val classModel = ClassFile.of().parse(result)
129121
val fieldDescriptors = classModel.fields().map { it.fieldType().stringValue() }
130122
assertThat(fieldDescriptors).contains("[[L$relocatedFixtureBase;")
131123
}
132124

133125
@Test
134-
fun remapMethodMultipleArgsIsRelocated() {
135-
val details = FixtureSubject::class.toFileCopyDetails()
136-
137-
val result = details.remapClass(relocators)
126+
fun methodDescriptorIsRelocated() {
127+
val result = fixtureSubjectDetails.remapClass(relocators)
138128

139129
val classModel = ClassFile.of().parse(result)
140130
val methodDescriptors = classModel.methods().map { it.methodType().stringValue() }
141-
assertThat(methodDescriptors)
142-
.contains("(L$relocatedFixtureBase;L$relocatedFixtureBase;)L$relocatedFixtureBase;")
131+
assertThat(methodDescriptors).contains("(L$relocatedFixtureBase;)L$relocatedFixtureBase;")
143132
}
144133

145134
@Test
146-
fun remapMethodPrimitivePlusClassIsRelocated() {
147-
val details = FixtureSubject::class.toFileCopyDetails()
148-
149-
val result = details.remapClass(relocators)
135+
fun methodMultipleArgsIsRelocated() {
136+
val result = fixtureSubjectDetails.remapClass(relocators)
150137

151138
val classModel = ClassFile.of().parse(result)
152139
val methodDescriptors = classModel.methods().map { it.methodType().stringValue() }
153-
assertThat(methodDescriptors).contains("(BL$relocatedFixtureBase;)L$relocatedFixtureBase;")
140+
assertThat(methodDescriptors)
141+
.contains("(L$relocatedFixtureBase;L$relocatedFixtureBase;)L$relocatedFixtureBase;")
154142
}
155143

156144
@Test
157-
fun remapBaseClassNameIsRelocated() {
158-
// Verify relocation also works on a simple class (FixtureBase has no fields/methods
159-
// referencing the target package beyond its own class name).
160-
val details = FixtureBase::class.toFileCopyDetails()
161-
162-
val result = details.remapClass(relocators)
145+
fun methodPrimitivePlusClassIsRelocated() {
146+
val result = fixtureSubjectDetails.remapClass(relocators)
163147

164148
val classModel = ClassFile.of().parse(result)
165-
assertThat(classModel.thisClass().asInternalName()).isEqualTo(relocatedFixtureBase)
149+
val methodDescriptors = classModel.methods().map { it.methodType().stringValue() }
150+
assertThat(methodDescriptors).contains("(BL$relocatedFixtureBase;)L$relocatedFixtureBase;")
166151
}
167152

168153
private fun KClass<*>.toFileCopyDetails() =
@@ -187,13 +172,13 @@ class RelocatorRemapperTest {
187172

188173
@Retention(AnnotationRetention.RUNTIME)
189174
@Target(AnnotationTarget.CLASS)
190-
annotation class FixtureAnnotation
175+
private annotation class FixtureAnnotation
191176

192-
open class FixtureBase
177+
private open class FixtureBase
193178

194179
@Suppress("unused") // Used by parsing bytecode.
195180
@FixtureAnnotation
196-
class FixtureSubject : FixtureBase() {
181+
private class FixtureSubject : FixtureBase() {
197182
val field: FixtureBase = FixtureBase()
198183
val arrayField: Array<FixtureBase> = emptyArray()
199184
val array2dField: Array<Array<FixtureBase>> = emptyArray()

0 commit comments

Comments
 (0)