Test the parsing of the time and all the variations.

This commit is contained in:
graywolf336 2013-12-24 18:45:50 -06:00
parent c4b1eb6d1a
commit ed481ff925
2 changed files with 40 additions and 3 deletions

View File

@ -12,7 +12,7 @@ import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.util.Vector; import org.bukkit.util.Vector;
public class Util { public class Util {
private final static Pattern DURATION_PATTERN = Pattern.compile("^(\\d+)\\s*(min(?:ute)?s?|h(?:ours?)?|d(?:ays?)?|s(?:econd)?s?)?$", Pattern.CASE_INSENSITIVE); private final static Pattern DURATION_PATTERN = Pattern.compile("^(\\d+)\\s*(m(?:inute)?s?|h(?:ours?)?|d(?:ays?)?|s(?:econd)?s?)?$", Pattern.CASE_INSENSITIVE);
/** Checks if the first {@link Vector} is inside the other two. */ /** Checks if the first {@link Vector} is inside the other two. */
public static boolean isInsideAB(Vector point, Vector first, Vector second) { public static boolean isInsideAB(Vector point, Vector first, Vector second) {
@ -79,7 +79,7 @@ public class Util {
String units = match.group(2); String units = match.group(2);
if ("seconds".equals(units) || "second".equals(units) || "s".equals(units)) if ("seconds".equals(units) || "second".equals(units) || "s".equals(units))
t = TimeUnit.MILLISECONDS.convert(Long.valueOf(match.group(1)), TimeUnit.SECONDS); t = TimeUnit.MILLISECONDS.convert(Long.valueOf(match.group(1)), TimeUnit.SECONDS);
if ("minutes".equals(units) || "minute".equals(units) || "mins".equals(units) || "min".equals(units)) if ("minutes".equals(units) || "minute".equals(units) || "mins".equals(units) || "min".equals(units) || "m".equals(units))
t = TimeUnit.MILLISECONDS.convert(Long.valueOf(match.group(1)), TimeUnit.MINUTES); t = TimeUnit.MILLISECONDS.convert(Long.valueOf(match.group(1)), TimeUnit.MINUTES);
else if ("hours".equals(units) || "hour".equals(units) || "h".equals(units)) else if ("hours".equals(units) || "hour".equals(units) || "h".equals(units))
t = TimeUnit.MILLISECONDS.convert(Long.valueOf(match.group(1)), TimeUnit.HOURS); t = TimeUnit.MILLISECONDS.convert(Long.valueOf(match.group(1)), TimeUnit.HOURS);
@ -95,7 +95,7 @@ public class Util {
} }
} }
return t; return Long.valueOf(t);
} }
/* /*

View File

@ -0,0 +1,37 @@
package test.java.com.graywolf336.jail;
import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.is;
import org.junit.Test;
import com.graywolf336.jail.Util;
public class TestTimeParsing {
@Test
public void testSeconds() throws Exception {
assertThat(1000L, is(Util.getTime("1s")));
assertThat(1000L, is(Util.getTime("1second")));
assertThat(1000L, is(Util.getTime("1seconds")));
}
@Test
public void testMinutes() throws Exception {
assertThat(60000L, is(Util.getTime("1m")));
assertThat(60000L, is(Util.getTime("1minute")));
assertThat(60000L, is(Util.getTime("1minutes")));
}
@Test
public void testHours() throws Exception {
assertThat(3600000L, is(Util.getTime("1h")));
assertThat(3600000L, is(Util.getTime("1hours")));
}
@Test
public void testDays() throws Exception {
assertThat(86400000L, is(Util.getTime("1d")));
assertThat(86400000L, is(Util.getTime("1days")));
}
}