forked from AlchemistSimulator/Alchemist
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(alchemist-grid): add working directory and contextually refacto…
…r tests
- Loading branch information
1 parent
e895897
commit 359530e
Showing
8 changed files
with
133 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
alchemist-grid/src/main/kotlin/it/unibo/alchemist/boundary/grid/utils/WorkingDirectory.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/* | ||
* Copyright (C) 2010-2023, Danilo Pianini and contributors | ||
* listed, for each module, in the respective subproject's build.gradle.kts file. | ||
* | ||
* This file is part of Alchemist, and is distributed under the terms of the | ||
* GNU General Public License, with a linking exception, | ||
* as described in the file LICENSE in the Alchemist distribution's top directory. | ||
*/ | ||
|
||
package it.unibo.alchemist.boundary.grid.utils | ||
|
||
import org.apache.commons.io.FileUtils | ||
import java.io.File | ||
import java.net.URL | ||
import java.nio.charset.StandardCharsets | ||
import java.nio.file.Files | ||
|
||
/** | ||
* Manages a temporary working directory. | ||
*/ | ||
class WorkingDirectory : AutoCloseable { | ||
|
||
private val directory = Files.createTempDirectory("alchemist").toFile() | ||
|
||
/** | ||
* The temporary directory url. | ||
*/ | ||
val url: URL get() = directory.toURI().toURL() | ||
|
||
/** | ||
* Get the folder file content. | ||
*/ | ||
fun getFileContent(filename: String): String { | ||
val file = File(getFileAbsolutePath(filename)) | ||
return FileUtils.readFileToString(file, StandardCharsets.UTF_8) | ||
} | ||
|
||
/** | ||
* An absolute path for a given filename in this directory. | ||
*/ | ||
fun getFileAbsolutePath(filename: String): String = "${directory.absolutePath}${File.separator}$filename" | ||
|
||
/** | ||
* Writes multiple files inside the directory | ||
*/ | ||
fun writeFiles(files: Map<String, ByteArray>) { | ||
files.forEach { (name, content) -> | ||
val file = File(getFileAbsolutePath(name)) | ||
if (file.parentFile.exists() || file.parentFile.mkdirs()) { | ||
FileUtils.writeByteArrayToFile(file, content) | ||
} else { | ||
throw IllegalStateException("Could not create directory structure for $file") | ||
} | ||
} | ||
} | ||
|
||
override fun close() { | ||
FileUtils.deleteDirectory(directory) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
alchemist-grid/src/test/kotlin/it/unibo/alchemist/test/TestWorkingDirectory.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* | ||
* Copyright (C) 2010-2023, Danilo Pianini and contributors | ||
* listed, for each module, in the respective subproject's build.gradle.kts file. | ||
* | ||
* This file is part of Alchemist, and is distributed under the terms of the | ||
* GNU General Public License, with a linking exception, | ||
* as described in the file LICENSE in the Alchemist distribution's top directory. | ||
*/ | ||
|
||
package it.unibo.alchemist.test | ||
|
||
import io.kotest.assertions.withClue | ||
import io.kotest.core.spec.style.StringSpec | ||
import io.kotest.matchers.booleans.shouldBeFalse | ||
import io.kotest.matchers.booleans.shouldBeTrue | ||
import io.kotest.matchers.maps.shouldHaveSize | ||
import io.kotest.matchers.nulls.shouldNotBeNull | ||
import it.unibo.alchemist.boundary.LoadAlchemist | ||
import it.unibo.alchemist.boundary.Loader | ||
import it.unibo.alchemist.boundary.grid.simulation.SimulationConfigImpl | ||
import it.unibo.alchemist.boundary.grid.utils.WorkingDirectory | ||
import it.unibo.alchemist.model.Time | ||
import org.kaikikm.threadresloader.ResourceLoader | ||
import java.io.File | ||
import java.net.URL | ||
|
||
class TestWorkingDirectory : StringSpec({ | ||
"Files should be correctly written in working directory" { | ||
val resource = "config/00-dependencies.yml" | ||
val yaml = ResourceLoader.getResource(resource) | ||
withClue("Yaml configuration should exits") { | ||
yaml.shouldNotBeNull() | ||
} | ||
val loader: Loader = getLoader(yaml) | ||
val simulationConfiguration = SimulationConfigImpl(loader, 0, Time.INFINITY) | ||
simulationConfiguration.dependencies shouldHaveSize 2 | ||
val test: File | ||
WorkingDirectory().use { wd -> | ||
test = File(wd.getFileAbsolutePath("nothing")).parentFile | ||
test.exists().shouldBeTrue() | ||
wd.writeFiles(simulationConfiguration.dependencies) | ||
val newFile = File(wd.getFileAbsolutePath("test.txt")) | ||
if (newFile.exists() || newFile.createNewFile()) { | ||
ResourceLoader.addURL(wd.url) | ||
ResourceLoader.getResource("test.txt").shouldNotBeNull() | ||
} else { | ||
error("File was not written in working directory") | ||
} | ||
} | ||
test.exists().shouldBeFalse() | ||
} | ||
}) { | ||
companion object { | ||
private fun getLoader(yaml: URL) = LoadAlchemist.from(yaml) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters