Skip to content

Commit

Permalink
more tests and better java operability
Browse files Browse the repository at this point in the history
  • Loading branch information
pietrotull committed Dec 18, 2018
1 parent cab8031 commit ced4f72
Show file tree
Hide file tree
Showing 12 changed files with 93 additions and 33 deletions.
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
organization := "io.sqooba"
scalaVersion := "2.12.7"
version := "0.3.6.1"
version := "0.3.7"
name := "sq-conf"

crossScalaVersions := Seq("2.12.7", "2.11.12")
Expand Down
2 changes: 1 addition & 1 deletion java-test-app/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<dependency>
<groupId>io.sqooba</groupId>
<artifactId>sq-conf_2.12</artifactId>
<version>0.3.6</version>
<version>0.3.6.3</version>
</dependency>

<dependency>
Expand Down
4 changes: 3 additions & 1 deletion java-test-app/src/main/resources/another.conf
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
some.int = 16
some.string = "perses"
some.intList = [15, 16, 17]
some.intList = [15, 16, 17]
some.stringList = ["string1", "string2", "string3"]
some.durationList = [10s, 10m, 10h]
4 changes: 3 additions & 1 deletion java-test-app/src/main/resources/application.conf
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
some.int = 15
some.string = "perse"
some.boolean = false
some.intList = [15, 16, 17]
some.intList = [15, 16, 17]
some.stringList = ["string1", "string2", "string3"]
some.durationList = [10s, 10m, 10h]
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

import io.sqooba.conf.JavaSqConf;
import io.sqooba.conf.SqConf;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.junit.Test;

Expand All @@ -22,12 +20,7 @@ public void getIntCorrectly() {
@Test
public void getIntListCorrectly() {
Iterable<Integer> someInts = conf.getIterable("some.intList");
List<Integer> ints = new ArrayList<>();
Iterator<Integer> iter = someInts.iterator();

while (iter.hasNext()) {
ints.add(iter.next());
}
List<Integer> ints = Util.fromIterToList(someInts);
assertEquals(3, ints.size());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import io.sqooba.conf.JavaSqConf;
import io.sqooba.conf.SqConf;
import java.util.HashMap;
import java.util.Map;
import java.time.Duration;
import java.util.*;
import org.junit.Test;

import static org.junit.Assert.assertEquals;
Expand Down Expand Up @@ -33,4 +33,37 @@ public void useOverWrites() {
String newVal = newConf.getString("some.string");
assertEquals("some.other.string", newVal);
}

@Test
public void getIterableOfStrings() {

Iterable<String> someStrings = conf.getIterable("some.stringList");

List<String> asList = Util.fromIterToList(someStrings);

assertEquals(3, asList.size());
assertEquals(asList.get(0), "string1");
assertEquals(asList.get(1), "string2");
assertEquals(asList.get(2), "string3");
}

@Test
public void getListOfStrings() {

List<String> someStrings = conf.getListOf("some.stringList");

assertEquals(3, someStrings.size());
assertEquals(someStrings.get(0), "string1");
assertEquals(someStrings.get(1), "string2");
assertEquals(someStrings.get(2), "string3");
}

@Test
public void getListOfDuration() {

Iterable<Duration> someStrings = conf.getIterable("some.durationList");
List<Duration> asList = Util.fromIterToList(someStrings);

assertEquals(3, asList.size());
}
}
18 changes: 18 additions & 0 deletions java-test-app/src/test/java/io/sqooba/sqconf/someapp/Util.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package io.sqooba.sqconf.someapp;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class Util {

public static <T> List<T> fromIterToList(Iterable<T> src) {
List<T> res = new ArrayList<>();
Iterator<T> iter = src.iterator();

while (iter.hasNext()) {
res.add(iter.next());
}
return res;
}
}
17 changes: 17 additions & 0 deletions src/main/scala/io/sqooba/conf/IterToList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package io.sqooba.conf;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class IterToList {
public static <T> List<T> fromIterToList(Iterable<T> src) {
List<T> res = new ArrayList<>();
Iterator<T> iter = src.iterator();

while (iter.hasNext()) {
res.add(iter.next());
}
return res;
}
}
4 changes: 4 additions & 0 deletions src/main/scala/io/sqooba/conf/JavaSqConf.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.sqooba.conf

import java.util

class JavaSqConf(sqConf: SqConf) {

def getSqConf: SqConf = sqConf
Expand All @@ -20,6 +22,8 @@ class JavaSqConf(sqConf: SqConf) {
list.asJava
}

def getListOf[T](key: String): util.List[T] = IterToList.fromIterToList(getIterable[T](key))

def keyAsEnv(key: String): String = sqConf.keyAsEnv(key)

def withOverwrites(ow: java.util.Map[java.lang.String, java.lang.String]): JavaSqConf = {
Expand Down
19 changes: 1 addition & 18 deletions src/main/scala/io/sqooba/conf/SqConf.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import java.io.File
import java.time.Duration

import scala.util.Properties
import collection.JavaConverters._
import scala.collection.JavaConverters._

import com.typesafe.config.Config
Expand Down Expand Up @@ -99,22 +98,6 @@ class SqConf(fileName: String = null,
x.asInstanceOf[T]
}).toList
}
/*
def getListOfWithConversion[T](key: String, convert: String => T): List[T] = {
val fullKey = buildKey(key)
def stringToT(string: String): List[T] = string.split(',').map(convert).toList
if (valueOverwrites.contains(fullKey)) {
stringToT(valueOverwrites(fullKey))
} else {
Properties.envOrNone(keyAsEnv(fullKey)) match {
case Some(env) => stringToT(env)
case None => getListOf[T](fullKey)
}
}
}
*/

def getListOfInt(key: String): List[Int] = getListOfWithConversion(key, str => str.trim.toInt)

Expand All @@ -125,7 +108,7 @@ class SqConf(fileName: String = null,
def getListOfBoolean(key: String): List[Boolean] = getListOfWithConversion(key, str => str.trim.toBoolean)

def getListOfDuration(key: String): List[Duration] = getListOfWithConversion[Duration](key, str =>
DurationParser.parseDurationString(str, key, "listOfDuration"), false)
DurationParser.parseDurationString(str, key, "listOfDuration"), cast = false)

def getListOfWithConversion[T](key: String, convert: String => T, cast: Boolean = true): List[T] = {
val fullKey = buildKey(key)
Expand Down
1 change: 0 additions & 1 deletion src/test/resources/application.conf
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ some {
testLongValue = 100
testBigIntValue = 123456789
testStringListValue = ["some","list", "last"]
testIntListValue = [100, 200, 300]
testBooleanListValue = [true, false, true]
testIntListValue = [123, 23, 69]
testDurationValue = 10m
Expand Down
9 changes: 9 additions & 0 deletions src/test/scala/io/sqooba/conf/JavaWrapperSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,13 @@ class JavaWrapperSpec extends FlatSpec with Matchers {
stringy shouldBe a [java.lang.String]
stringy shouldBe "string thing"
}

"get list of ints" should "return valid java list" in {
// testIntListValue = [123, 23, 69]
val listOf = javaWrapper.getListOf[Integer]("some.testIntListValue")
listOf.size() shouldBe 3
listOf.get(0) shouldBe 123
listOf.get(1) shouldBe 23
listOf.get(2) shouldBe 69
}
}

0 comments on commit ced4f72

Please sign in to comment.