Unit tests for ListBuilder and VersionUtils

This commit is contained in:
Frank van der Heijden 2020-06-30 20:20:10 +02:00
parent 93c5adb911
commit 0abfa991b9
No known key found for this signature in database
GPG key ID: 26DA56488D314D11
4 changed files with 129 additions and 2 deletions

View file

@ -1,7 +1,11 @@
package net.frankheijden.serverutils.utils;
import java.util.regex.Pattern;
public class VersionUtils {
private static final Pattern integerPattern = Pattern.compile("[^0-9]");
/**
* Compares two versions in X.X.X format.
* Returns true if version is newer than the old one.
@ -10,13 +14,14 @@ public class VersionUtils {
* @return true iff new version is newer than old version.
*/
public static boolean isNewVersion(String oldVersion, String newVersion) {
if (oldVersion == null || newVersion == null) return false;
String[] oldVersionSplit = oldVersion.split("\\.");
String[] newVersionSplit = newVersion.split("\\.");
int i = 0;
while (i < oldVersionSplit.length && i < newVersionSplit.length) {
int o = Integer.parseInt(oldVersionSplit[i]);
int n = Integer.parseInt(newVersionSplit[i]);
int o = extractInteger(oldVersionSplit[i]);
int n = extractInteger(newVersionSplit[i]);
if (i != oldVersionSplit.length - 1 && i != newVersionSplit.length - 1) {
if (n < o) return false;
}
@ -25,4 +30,8 @@ public class VersionUtils {
}
return false;
}
private static Integer extractInteger(String str) {
return Integer.parseInt(integerPattern.matcher(str).replaceAll(""));
}
}

View file

@ -0,0 +1,67 @@
package net.frankheijden.serverutils.utils;
import static java.util.Arrays.asList;
import static java.util.Collections.singletonList;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.util.List;
import org.junit.jupiter.api.Test;
class ListBuilderTest {
private final String seperator = ", ";
private final String lastSeperator = " and ";
@Test
void testToStringOneElement() {
String list = ListBuilder.createStrings(singletonList("Nice"))
.seperator(seperator)
.lastSeperator(lastSeperator)
.toString();
assertEquals("Nice", list);
}
@Test
void testToStringTwoElements() {
String list = ListBuilder.createStrings(asList("Nice", "List"))
.seperator(seperator)
.lastSeperator(lastSeperator)
.toString();
assertEquals("Nice and List", list);
}
@Test
void testToStringMultipleElements() {
String list = ListBuilder.createStrings(asList("Nice", "List", "You", "Having", "There"))
.seperator(seperator)
.lastSeperator(lastSeperator)
.toString();
assertEquals("Nice, List, You, Having and There", list);
}
@Test
void testToStringCustomFormat() {
List<TestObject> objects = asList(
new TestObject("pre1", 2),
new TestObject("pre2", 3),
new TestObject("pre3", 4)
);
String list = ListBuilder.create(objects)
.format(obj -> obj.prefix + "-" + obj.value)
.seperator("; ")
.lastSeperator(" and at last ")
.toString();
assertEquals("pre1-2; pre2-3 and at last pre3-4", list);
}
private static class TestObject {
private final String prefix;
private final int value;
public TestObject(String prefix, int value) {
this.prefix = prefix;
this.value = value;
}
}
}

View file

@ -0,0 +1,42 @@
package net.frankheijden.serverutils.utils;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.params.provider.Arguments.of;
import java.util.stream.Stream;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
class VersionUtilsTest {
@ParameterizedTest(name = "old = {0}, new = {1}, expected = {2}")
@MethodSource("versionGenerator")
void isNewVersion(String oldVersion, String newVersion, boolean expected) {
assertThat(VersionUtils.isNewVersion(oldVersion, newVersion)).isEqualTo(expected);
}
private static Stream<Arguments> versionGenerator() {
return Stream.of(
of("0", "1", true),
of("1", "0", false),
of("9", "10", true),
of("10", "9", false),
of("-1", "5", true),
of("5", "-1", false),
of("10.1", "10.0", false),
of("100.0", "120.0", true),
of("1.0.0", "1.0.1", true),
of("1.0.0", "1.1.0", true),
of("1.0.0", "2.0.0", true),
of("0.0.1", "0.0.1", false),
of("0.0.1", "0.0.0", false),
of("0.1.0", "0.0.1", false),
of("1.0.0", "0.0.1", false),
of("1.1.0", "0.1.1", false),
of("1.0.0.0", "1.0.0.1", true),
of("1.0.1-DEV", "1.0.2", true)
);
}
}