DateRangeParser { static ProductData.UTC[] tryToGetDateRange(String productName) { final String singleDateMonthPattern = "\\d{6}_.*"; final String startEndPattern = "\\d{8}_\\d{8}_.*"; final String singleDateDayPattern = "\\d{8}_.*"; final String singleDateDayPattern_leadingCharacters = ".*_\\d{8}_.*"; if (productName.matches(singleDateMonthPattern)) { return parseSingleMonthDate(productName); } else if (productName.matches(startEndPattern)) { return parseStartEndDate(productName); } else if (productName.matches(singleDateDayPattern)) { return parseSingleDayDate(productName); } else if (productName.matches(singleDateDayPattern_leadingCharacters)) { return parseSingleDayDate(productName); } throw new IllegalArgumentException("Unable to derive date from product name '" + productName + "'."); }    }
@Test public void testTryToGetDateRangeFromValidDailyProduct() throws Exception { ProductData.UTC[] dateRange = DateRangeParser.tryToGetDateRange("20111103_est_wac_wew_1200.dim"); assertEquals(dateRange.length, 2); assertEquals(ProductData.UTC.parse("2011-11-03", "yyyy-MM-dd").getAsDate().getTime(), dateRange[0].getAsDate().getTime()); assertEquals(dateRange[0].getAsDate().getTime(), dateRange[1].getAsDate().getTime()); }
@Test public void testTryToGetDateRangeFromValidDailyProductWithLeadingCharacters() throws Exception { ProductData.UTC[] dateRange = DateRangeParser.tryToGetDateRange("leading_characters_20111103_est_wac_wew_1200.dim"); assertEquals(dateRange.length, 2); assertEquals(ProductData.UTC.parse("2011-11-03", "yyyy-MM-dd").getAsDate().getTime(), dateRange[0].getAsDate().getTime()); assertEquals(dateRange[0].getAsDate().getTime(), dateRange[1].getAsDate().getTime()); }
@Test public void testTryToGetDateRangeFromValidWeeklyProduct() throws Exception { ProductData.UTC[] dateRange = DateRangeParser.tryToGetDateRange("20110917_20110923_bas_wac_acr_1200.dim"); assertEquals(dateRange.length, 2); assertEquals(ProductData.UTC.parse("2011-09-17", "yyyy-MM-dd").getAsDate().getTime(), dateRange[0].getAsDate().getTime()); assertEquals(ProductData.UTC.parse("2011-09-23", "yyyy-MM-dd").getAsDate().getTime(), dateRange[1].getAsDate().getTime()); }
@Test public void testTryToGetDateRangeFromValidMonthlyProduct() throws Exception { ProductData.UTC[] dateRange = DateRangeParser.tryToGetDateRange("201106_bas_wac_acr_1200.dim"); assertEquals(dateRange.length, 2); assertEquals(ProductData.UTC.parse("2011-06", "yyyy-MM").getAsDate().getTime(), dateRange[0].getAsDate().getTime()); assertEquals(dateRange[0].getAsDate().getTime(), dateRange[1].getAsDate().getTime()); }
@Test(expected = IllegalArgumentException.class) public void testNotMatchAnyPattern() throws Exception { DateRangeParser.tryToGetDateRange("doesNotMatchAnyPattern"); }