@@ -7,55 +7,61 @@ import org.gradle.api.Project
7
7
import org.gradle.api.internal.project.DefaultProject
8
8
import org.gradle.api.tasks.TaskExecutionException
9
9
import org.gradle.testfixtures.ProjectBuilder
10
- import org.junit.jupiter.api.Assertions.assertThrows
11
- import org.junit.jupiter.api.Assertions.assertTrue
12
- import org.junit.jupiter.api.BeforeEach
13
- import org.junit.jupiter.api.Test
14
- import org.junit.jupiter.api.io.TempDir
10
+ import kotlin.io.path.createTempDirectory
11
+ import kotlin.test.BeforeTest
12
+ import kotlin.test.AfterTest
13
+ import kotlin.test.Test
14
+ import kotlin.test.assertTrue
15
+ import kotlin.test.assertFailsWith
15
16
import java.io.File
16
17
17
18
class ZipModuleTests : BaseTest () {
18
19
19
- @TempDir
20
- lateinit var testProjectDir: File
20
+ private lateinit var tempDir: File
21
21
private lateinit var project: Project
22
22
private lateinit var task: ZipModule
23
23
24
- @BeforeEach
24
+ @BeforeTest
25
25
fun setup () {
26
- project = ProjectBuilder .builder().withProjectDir(testProjectDir).build() as DefaultProject
26
+ tempDir = createTempDirectory().toFile()
27
+ project = ProjectBuilder .builder().withProjectDir(tempDir).build() as DefaultProject
27
28
task = project.tasks.create(" testTask" , ZipModule ::class .java)
28
29
29
- // Set up the task properties with the test directory
30
- task.content.set(project.objects.directoryProperty().fileValue(File (testProjectDir, " content" )))
31
- task.unsignedModule.set(project.objects.fileProperty().fileValue(File (testProjectDir, " output.modl" )))
30
+ // Set up the task properties with the temp directory
31
+ task.content.set(project.objects.directoryProperty().fileValue(File (tempDir, " content" )))
32
+ task.unsignedModule.set(project.objects.fileProperty().fileValue(File (tempDir, " output.modl" )))
33
+ }
34
+
35
+ @AfterTest
36
+ fun cleanup () {
37
+ tempDir.deleteRecursively()
32
38
}
33
39
34
40
@Test
35
41
fun `task succeeds when no duplicate jars exist` () {
36
42
// Arrange
37
- val contentDir = File (testProjectDir, " content" ).apply { mkdirs() }
43
+ val contentDir = task. content.asFile.get( ).apply { mkdirs() }
38
44
File (contentDir, " my-lib-1.0.jar" ).createNewFile()
39
45
File (contentDir, " another-lib-2.0.jar" ).createNewFile()
40
46
41
- // Act & Assert : The task should not throw an exception
47
+ // Act: The task should not throw an exception
42
48
task.execute()
43
49
}
44
50
45
51
@Test
46
52
fun `task fails when duplicate jars with different versions exist` () {
47
53
// Arrange
48
- val contentDir = File (testProjectDir, " content" ).apply { mkdirs() }
54
+ val contentDir = task. content.asFile.get( ).apply { mkdirs() }
49
55
File (contentDir, " my-lib-1.0.jar" ).createNewFile()
50
56
File (contentDir, " my-lib-2.0.jar" ).createNewFile() // This is the duplicate
51
57
52
- // Act & Assert: The task should throw a TaskExecutionException
53
- val exception = assertThrows( TaskExecutionException :: class .java) {
58
+ // Act & Assert: The task should throw a IllegalArgumentException
59
+ val exception = assertFailsWith< IllegalArgumentException > {
54
60
task.execute()
55
61
}
56
62
57
63
// Verify the exception message
58
- val expectedMessage = " Jar with 'my-lib' has multiple versions presented "
64
+ val expectedMessage = " Library 'my-lib' exists in multiple versions"
59
65
assertTrue(exception.message!! .contains(expectedMessage))
60
66
}
61
67
0 commit comments