Skip to content

Commit

Permalink
Fix resource loading when run from a JAR
Browse files Browse the repository at this point in the history
  • Loading branch information
Anael Ollier committed Jun 4, 2018
1 parent aa6b92d commit d374a3d
Show file tree
Hide file tree
Showing 10 changed files with 31 additions and 25 deletions.
10 changes: 9 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.github.dataanon</groupId>
<artifactId>data-anon</artifactId>
<version>0.9.5</version>
<version>0.9.5-ca1</version>
<packaging>jar</packaging>

<name>data-anon</name>
Expand Down Expand Up @@ -197,6 +197,14 @@
<goals>deploy</goals>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
</plugins>
</build>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import com.github.dataanon.strategy.AnonymizationStrategy
import com.github.dataanon.strategy.list.PickFromFile
import com.github.dataanon.strategy.name.RandomFirstName

class RandomEmail(sourceFilePath: String = RandomFirstName::class.java.getResource("/data/first_names.dat").path,
class RandomEmail(sourceFilePath: String = "/data/first_names.dat",
private val host: String = "data-anonymization", private val tld: String = "com") : AnonymizationStrategy<String> {

init {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import com.github.dataanon.model.Record
import com.github.dataanon.strategy.AnonymizationStrategy
import com.github.dataanon.strategy.list.PickFromFile

class RandomFirstName(sourceFilePath: String = RandomFirstName::class.java.getResource("/data/first_names.dat").path) : AnonymizationStrategy<String> {
class RandomFirstName(sourceFilePath: String = "/data/first_names.dat") : AnonymizationStrategy<String> {

init {
require(sourceFilePath.isNotBlank(), {"sourceFilePath can not be empty while using RandomFirstName"})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import com.github.dataanon.model.Record
import com.github.dataanon.strategy.AnonymizationStrategy
import com.github.dataanon.strategy.list.PickFromFile

class RandomFullName(firstNameSourceFilePath: String = RandomFirstName::class.java.getResource("/data/first_names.dat").path,
lastNameSourceFilePath: String = RandomFirstName::class.java.getResource("/data/last_names.dat").path) : AnonymizationStrategy<String> {
class RandomFullName(firstNameSourceFilePath: String = "/data/first_names.dat",
lastNameSourceFilePath: String = "/data/last_names.dat") : AnonymizationStrategy<String> {

init {
require(firstNameSourceFilePath.isNotBlank(), {"firstNameSourceFilePath can not be empty while using RandomFullName"})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import com.github.dataanon.model.Record
import com.github.dataanon.strategy.AnonymizationStrategy
import com.github.dataanon.strategy.list.PickFromFile

class RandomLastName(sourceFilePath: String = RandomFirstName::class.java.getResource("/data/last_names.dat").path) : AnonymizationStrategy<String> {
class RandomLastName(sourceFilePath: String = "/data/last_names.dat") : AnonymizationStrategy<String> {

init {
require(sourceFilePath.isNotBlank(), {"sourceFilePath can not be empty while using RandomLastName"})
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
package com.github.dataanon.utils

import java.io.File

object FlatFileContentStore {

private var fileContent: HashMap<String, List<String>> = File(this::class.java.getResource("/data/").path)
.walk()
.filter { it.extension.equals("dat") }
.associateByTo (HashMap(), { it.path }, { it.readLines() } )
private var fileContent: HashMap<String, List<String>> = HashMap()

fun getFileContentByPath(path: String): List<String> = if ( !fileContent.containsKey(path) ) getFileContentPostRead(path) else fileContent[path] as List<String>


private fun getFileContentPostRead(path: String): List<String> {
fileContent[path] = File(path).readLines()
val lineList = mutableListOf<String>()
this::class.java.getResourceAsStream(path).bufferedReader().useLines { lines -> lines.forEach { lineList.add(it)} }

fileContent[path] = lineList
return fileContent[path] as List<String>
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ class PickFromFileUnitTest : FunSpec(), Matchers {

init {
test("should pick a random value from a file containing string") {
val filePath = this::class.java.getResource("/test_countries.txt").path
val values = File(filePath).readLines()
val filePath = "/test_countries.txt"
val values = File(this::class.java.getResource(filePath).path).readLines()
val field = Field("country", "India")
val pickFromFile = PickFromFile<String>(filePath = filePath)

Expand All @@ -25,8 +25,8 @@ class PickFromFileUnitTest : FunSpec(), Matchers {
}

test("should pick a random value from a file containing string using PickStringFromFile") {
val filePath = this::class.java.getResource("/test_countries.txt").path
val values = File(filePath).readLines()
val filePath = "/test_countries.txt"
val values = File(this::class.java.getResource(filePath).path).readLines()
val field = Field("country", "India")
val pickFromFile = PickStringFromFile(filePath = filePath)

Expand All @@ -35,8 +35,8 @@ class PickFromFileUnitTest : FunSpec(), Matchers {
}

test("should pick a random value from a file containing double") {
val filePath = this::class.java.getResource("/test_stock-prices.txt").path
val values = File(filePath).readLines().map { it.toDouble() }
val filePath = "/test_stock-prices.txt"
val values = File(this::class.java.getResource(filePath).path).readLines().map { it.toDouble() }
val field = Field("stock-price", 12.90)
val pickFromFile = PickFromFile<Double>(filePath = filePath)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class RandomFirstNameUnitTest : FunSpec(), Matchers {

test("should return random First name from file provided") {
val firstNames = File(this::class.java.getResource("/test_first_names.txt").path).readLines()
val randomFirstName = RandomFirstName(sourceFilePath = this::class.java.getResource("/test_first_names.txt").path)
val randomFirstName = RandomFirstName(sourceFilePath = "/test_first_names.txt")
val field = Field("first_name", "John")

val anonymized = randomFirstName.anonymize(field, emptyRecord)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ class RandomFullNameUnitTest : FunSpec(), Matchers {
val firstNames = File(this::class.java.getResource("/test_first_names.txt").path).readLines()
val lastNames = File(this::class.java.getResource("/test_last_names.txt").path).readLines()
val field = Field("name", "John Smith")
val randomFullName = RandomFullName(firstNameSourceFilePath = this::class.java.getResource("/test_first_names.txt").path,
lastNameSourceFilePath = this::class.java.getResource("/test_last_names.txt").path
val randomFullName = RandomFullName(firstNameSourceFilePath = "/test_first_names.txt",
lastNameSourceFilePath = "/test_last_names.txt"
)

val anonymized = randomFullName.anonymize(field, emptyRecord)
Expand All @@ -53,8 +53,8 @@ class RandomFullNameUnitTest : FunSpec(), Matchers {

test("should return random full name consisting of first name and last name separated by space from provided sources") {
val field = Field("name", "John Smith")
val randomFullName = RandomFullName(firstNameSourceFilePath = this::class.java.getResource("/test_first_names.txt").path,
lastNameSourceFilePath = this::class.java.getResource("/test_last_names.txt").path
val randomFullName = RandomFullName(firstNameSourceFilePath = "/test_first_names.txt",
lastNameSourceFilePath = "/test_last_names.txt"
)

val anonymized = randomFullName.anonymize(field, emptyRecord)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class RandomLastNameUnitTest : FunSpec(), Matchers {

test("should return random Last name from file provided") {
val firstNames = File(this::class.java.getResource("/test_last_names.txt").path).readLines()
val randomLastName = RandomLastName(sourceFilePath = this::class.java.getResource("/test_last_names.txt").path)
val randomLastName = RandomLastName(sourceFilePath = "/test_last_names.txt")
val field = Field("last_name", "John")

val anonymized = randomLastName.anonymize(field, emptyRecord)
Expand Down

0 comments on commit d374a3d

Please sign in to comment.