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); 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) || "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); 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);
else if ("days".equals(units) || "day".equals(units) || "d".equals(units)) else if ("days".equals(units) || "day".equals(units) || "d".equals(units))
t = TimeUnit.MILLISECONDS.convert(Long.valueOf(match.group(1)), TimeUnit.DAYS); t = TimeUnit.MILLISECONDS.convert(Long.valueOf(match.group(1)), TimeUnit.DAYS);
}else { else {
try { try {
Long l = Long.parseLong(time); t = TimeUnit.MILLISECONDS.convert(Long.parseLong(time), TimeUnit.MINUTES);
t = TimeUnit.MILLISECONDS.convert(l, TimeUnit.MINUTES);
}catch(NumberFormatException e) { }catch(NumberFormatException e) {
throw new Exception("Invalid format."); throw new Exception("Invalid format.");
} }
} }
}else {
throw new Exception("Invalid format.");
}
return Long.valueOf(t); return Long.valueOf(t);
} }
/*
*/
} }

View File

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