From 2798b1e42739d01e6682b9020ea0853033209342 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Paul=20Maximilian=20Kr=C3=BCger?= Date: Tue, 6 Apr 2021 13:41:38 +0200 Subject: [PATCH] improved testing for TaskStorageService.kt #2 #10 --- src/services/TaskStorageService.kt | 12 +++--------- test/services/TaskServiceTest.kt | 27 +++++++++++++++++++++++---- 2 files changed, 26 insertions(+), 13 deletions(-) diff --git a/src/services/TaskStorageService.kt b/src/services/TaskStorageService.kt index 79f3a1d..0fbba69 100644 --- a/src/services/TaskStorageService.kt +++ b/src/services/TaskStorageService.kt @@ -7,7 +7,7 @@ import com.adclock.util.createOrEmptyFile import com.adclock.util.readLinesIndexed import java.io.File -class TaskStorageService { +class TaskStorageService(private val taskFolder: File) { fun loadTask(name: String): Task { val file = getTaskFile(name) @@ -45,20 +45,14 @@ class TaskStorageService { fun exists(name: String) = getTaskFile(name).exists() - fun taskNames(): Array = TASK_FOLDER.list { _, name -> name.endsWith(FILE_EXTENSION) } + fun taskNames(): Array = taskFolder.list { _, name -> name.endsWith(FILE_EXTENSION) } ?: throw IllegalStateException("Error loading task names.") - private fun getTaskFile(name: String) = File(TASK_FOLDER, "$name$FILE_EXTENSION") + private fun getTaskFile(name: String) = File(taskFolder, "$name$FILE_EXTENSION") companion object { - private val TASK_FOLDER = File("tasks/") private const val FILE_EXTENSION = ".adc" - - init { - if (!TASK_FOLDER.exists()) - TASK_FOLDER.mkdirs() - } } } \ No newline at end of file diff --git a/test/services/TaskServiceTest.kt b/test/services/TaskServiceTest.kt index 23bf16e..c8409e3 100644 --- a/test/services/TaskServiceTest.kt +++ b/test/services/TaskServiceTest.kt @@ -1,27 +1,46 @@ package com.adclock.services import com.adclock.instructionset.instructions.wall.RunInstruction +import org.junit.After +import org.junit.Before +import org.junit.rules.TemporaryFolder import kotlin.test.Test import kotlin.test.assertEquals class TaskServiceTest { + val tempFolder = TemporaryFolder() + + @Before + fun prepareTempFolder() { + tempFolder.create() + } + + @After + fun cleanupTempFolder() { + tempFolder.create() + } + + private fun getTaskStorageService(): TaskStorageService { + return TaskStorageService(tempFolder.newFolder("tasks/")) + } + @Test fun shouldReturnEmptyTaskNameList() { - val service = TaskService(TaskStorageService()) + val service = TaskService(getTaskStorageService()) assertEquals(emptyList(), service.getTaskNameList(), "Task list") } @Test fun shouldReturnOneTaskNameList() { - val service = TaskService(TaskStorageService()) + val service = TaskService(getTaskStorageService()) service.createTask("Foo") assertEquals(listOf("Foo"), service.getTaskNameList(), "Task list") } @Test fun addTaskWithNoInstructions() { - val service = TaskService(TaskStorageService()) + val service = TaskService(getTaskStorageService()) service.createTask("Foo") val task = service.getTask("Foo") assertEquals("Foo", task.name, "Task name") @@ -31,7 +50,7 @@ class TaskServiceTest { @Test fun addTaskWithSingleInstruction() { - val service = TaskService(TaskStorageService()) + val service = TaskService(getTaskStorageService()) service.createTask("Simple", listOf("RUN")) val task = service.getTask("Simple") assertEquals(1, task.instructions.size, "Task instructions list size")