Configuration { protected static int timeStringToMillis(String timeString) throws InvalidTimeException, ConfigurationException { if (timeString == null) { throw new ConfigurationException("null time"); } timeString = timeString.trim(); int timeInMillis = 0; try { if (timeString.endsWith("ms")) { timeInMillis = Integer.parseInt(timeString.substring(0, timeString.length() - 2).trim()); } else if (timeString.endsWith("s")) { timeInMillis = Integer.parseInt(timeString.substring(0, timeString.length() - 1).trim()) * 1000; } else if (timeString.endsWith("m")) { timeInMillis = Integer.parseInt(timeString.substring(0, timeString.length() - 1).trim()) * 1000 * 60; } else { timeInMillis = Integer.parseInt(timeString) * 1000; } if (timeInMillis < 0) { throw new InvalidTimeException("Invalid time value : {0}", timeInMillis); } return timeInMillis; } catch (NumberFormatException e) { throw new ConfigurationException("Unrecognized time value : ''{0}''", timeString); } }  void setConfigurationProperties(Map<String, String> configurationProperties);  }
@Test public void testTimeStringToMillis() throws Exception { int millis = Configuration.timeStringToMillis("12"); Assert.assertTrue(millis == 12000); millis = Configuration.timeStringToMillis(" 12 "); Assert.assertTrue(millis == 12000); millis = Configuration.timeStringToMillis("12ms"); Assert.assertTrue(millis == 12); millis = Configuration.timeStringToMillis("12 ms"); Assert.assertTrue(millis == 12); millis = Configuration.timeStringToMillis(" 12 ms "); Assert.assertTrue(millis == 12); millis = Configuration.timeStringToMillis("2 m"); Assert.assertTrue(millis == 120000); millis = Configuration.timeStringToMillis("2m"); Assert.assertTrue(millis == 120000); millis = Configuration.timeStringToMillis(" 2 m "); Assert.assertTrue(millis == 120000); millis = Configuration.timeStringToMillis("2 s"); Assert.assertTrue(millis == 2000); millis = Configuration.timeStringToMillis("2s"); Assert.assertTrue(millis == 2000); millis = Configuration.timeStringToMillis(" 2 s "); Assert.assertTrue(millis == 2000); }
@Test public void testTimeStringToMillisInvalidValue() throws Configuration.InvalidTimeException { try { Configuration.timeStringToMillis("foo"); Assert.fail("should not get here"); } catch (ConfigurationException e) { } }
@Test public void testTimeStringToMillisNegativeValue() throws ConfigurationException { try { Configuration.timeStringToMillis("-10"); Assert.fail("should not get here"); } catch (Configuration.InvalidTimeException e) { } }
@Test public void testTimeStringToMillisNullValue() throws Configuration.InvalidTimeException { try { Configuration.timeStringToMillis(null); Assert.fail("should not get here"); } catch (ConfigurationException e) { } }
@Test public void testTimeStringToMillisEmptyValue() throws Configuration.InvalidTimeException { try { Configuration.timeStringToMillis(""); Assert.fail("should not get here"); } catch (ConfigurationException e) { } }