Unit tests for ListBuilder and VersionUtils
This commit is contained in:
parent
93c5adb911
commit
0abfa991b9
4 changed files with 129 additions and 2 deletions
|
|
@ -19,6 +19,11 @@ dependencies {
|
||||||
implementation 'co.aikar:acf-paper:0.5.0-SNAPSHOT'
|
implementation 'co.aikar:acf-paper:0.5.0-SNAPSHOT'
|
||||||
implementation 'org.bstats:bstats-bukkit:1.7'
|
implementation 'org.bstats:bstats-bukkit:1.7'
|
||||||
compileOnly 'com.destroystokyo.paper:paper-api:1.16.1-R0.1-SNAPSHOT'
|
compileOnly 'com.destroystokyo.paper:paper-api:1.16.1-R0.1-SNAPSHOT'
|
||||||
|
|
||||||
|
testCompile 'org.assertj:assertj-core:3.15.0'
|
||||||
|
testCompile 'org.junit.jupiter:junit-jupiter-api:5.6.2'
|
||||||
|
testCompile 'org.junit.jupiter:junit-jupiter-params:5.6.2'
|
||||||
|
testCompile 'org.junit.jupiter:junit-jupiter-engine:5.6.2'
|
||||||
}
|
}
|
||||||
|
|
||||||
processResources {
|
processResources {
|
||||||
|
|
@ -49,4 +54,8 @@ checkstyle {
|
||||||
maxWarnings = 0
|
maxWarnings = 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
test {
|
||||||
|
useJUnitPlatform()
|
||||||
|
}
|
||||||
|
|
||||||
build.dependsOn shadowJar
|
build.dependsOn shadowJar
|
||||||
|
|
@ -1,7 +1,11 @@
|
||||||
package net.frankheijden.serverutils.utils;
|
package net.frankheijden.serverutils.utils;
|
||||||
|
|
||||||
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
public class VersionUtils {
|
public class VersionUtils {
|
||||||
|
|
||||||
|
private static final Pattern integerPattern = Pattern.compile("[^0-9]");
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Compares two versions in X.X.X format.
|
* Compares two versions in X.X.X format.
|
||||||
* Returns true if version is newer than the old one.
|
* 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.
|
* @return true iff new version is newer than old version.
|
||||||
*/
|
*/
|
||||||
public static boolean isNewVersion(String oldVersion, String newVersion) {
|
public static boolean isNewVersion(String oldVersion, String newVersion) {
|
||||||
|
if (oldVersion == null || newVersion == null) return false;
|
||||||
String[] oldVersionSplit = oldVersion.split("\\.");
|
String[] oldVersionSplit = oldVersion.split("\\.");
|
||||||
String[] newVersionSplit = newVersion.split("\\.");
|
String[] newVersionSplit = newVersion.split("\\.");
|
||||||
|
|
||||||
int i = 0;
|
int i = 0;
|
||||||
while (i < oldVersionSplit.length && i < newVersionSplit.length) {
|
while (i < oldVersionSplit.length && i < newVersionSplit.length) {
|
||||||
int o = Integer.parseInt(oldVersionSplit[i]);
|
int o = extractInteger(oldVersionSplit[i]);
|
||||||
int n = Integer.parseInt(newVersionSplit[i]);
|
int n = extractInteger(newVersionSplit[i]);
|
||||||
if (i != oldVersionSplit.length - 1 && i != newVersionSplit.length - 1) {
|
if (i != oldVersionSplit.length - 1 && i != newVersionSplit.length - 1) {
|
||||||
if (n < o) return false;
|
if (n < o) return false;
|
||||||
}
|
}
|
||||||
|
|
@ -25,4 +30,8 @@ public class VersionUtils {
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static Integer extractInteger(String str) {
|
||||||
|
return Integer.parseInt(integerPattern.matcher(str).replaceAll(""));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -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)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue