Fix the time parsing not defaulting to minutes & add another test for it

This commit is contained in:
graywolf336 2013-12-24 19:36:14 -06:00
parent ed481ff925
commit 978bd044fe
2 changed files with 18 additions and 15 deletions

View File

@ -79,26 +79,23 @@ public class Util {
String units = match.group(2);
if ("seconds".equals(units) || "second".equals(units) || "s".equals(units))
t = TimeUnit.MILLISECONDS.convert(Long.valueOf(match.group(1)), TimeUnit.SECONDS);
if ("minutes".equals(units) || "minute".equals(units) || "mins".equals(units) || "min".equals(units) || "m".equals(units))
else 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);
else if ("hours".equals(units) || "hour".equals(units) || "h".equals(units))
t = TimeUnit.MILLISECONDS.convert(Long.valueOf(match.group(1)), TimeUnit.HOURS);
else if ("days".equals(units) || "day".equals(units) || "d".equals(units))
t = TimeUnit.MILLISECONDS.convert(Long.valueOf(match.group(1)), TimeUnit.DAYS);
else {
try {
t = TimeUnit.MILLISECONDS.convert(Long.parseLong(time), TimeUnit.MINUTES);
}catch(NumberFormatException e) {
throw new Exception("Invalid format.");
}
}
}else {
try {
Long l = Long.parseLong(time);
t = TimeUnit.MILLISECONDS.convert(l, TimeUnit.MINUTES);
}catch(NumberFormatException e) {
throw new Exception("Invalid format.");
}
throw new Exception("Invalid format.");
}
return Long.valueOf(t);
}
/*
*/
}

View File

@ -9,11 +9,17 @@ import com.graywolf336.jail.Util;
public class TestTimeParsing {
@Test
public void testNoFormat() throws Exception {
assertThat(60000L, is(Util.getTime("1")));
assertThat(360000L, is(Util.getTime("6")));
}
@Test
public void testSeconds() throws Exception {
assertThat(1000L, is(Util.getTime("1s")));
assertThat(1000L, is(Util.getTime("1second")));
assertThat(1000L, is(Util.getTime("1seconds")));
assertThat(2000L, is(Util.getTime("2s")));
assertThat(2000L, is(Util.getTime("2second")));
assertThat(2000L, is(Util.getTime("2seconds")));
}
@Test