PatternProvider { public static List<String[]> recommendPatterns(String fileName) { fileName = replaceColon(fileName); List<String[]> patternCombinations = new ArrayList<String[]>(); for (String dateString : DATE_PATTERNS) { dateString = replaceColon(dateString); final Pattern datePattern = convertDateStringToPattern(dateString); Matcher matcher = datePattern.matcher(fileName); if (matcher.matches()) { TimeStampExtractor timeStampExtractor = new TimeStampExtractor(dateString, FILENAME_PATTERNS[0]); try { timeStampExtractor.extractTimeStamps(fileName); patternCombinations.add(new String[]{insertColon(dateString), FILENAME_PATTERNS[0]}); try { timeStampExtractor = new TimeStampExtractor(insertColon(dateString), FILENAME_PATTERNS[1]); timeStampExtractor.extractTimeStamps(fileName); patternCombinations.add(new String[]{dateString, FILENAME_PATTERNS[1]}); } catch (ValidationException e) { } } catch (ValidationException e) { } } } return patternCombinations; }  static List<String[]> recommendPatterns(String fileName); static final String[] DATE_PATTERNS; static final String[] FILENAME_PATTERNS; }
@Test public void testRecognizePatterns_non_valid() throws Exception { assertEquals(0, PatternProvider.recommendPatterns("no_valid_pattern").size()); }
@Test public void testRecommendPatterns_firstPattern() throws Exception { List<String[]> patternCombinations = PatternProvider.recommendPatterns("sth__20100101:200101.nc"); assertTrue(patternCombinations.size() >= 3); containAssertEquals(patternCombinations, new String[]{"yyyyMMdd:hhmmss", "*${startDate}*"}); containAssertEquals(patternCombinations, new String[]{"yyyyMMdd", "*${startDate}*"}); containAssertEquals(patternCombinations, new String[]{"yyyyMM", "*${startDate}*"}); }
@Test public void testRecommendPatterns_secondPattern_twoDates() throws Exception { List<String[]> patternCombinations = PatternProvider.recommendPatterns("sth__20100101_192345_20110101_192345.nc"); assertTrue(patternCombinations.size() >= 6); containAssertEquals(patternCombinations, new String[]{"yyyyMMdd_hhmmss", "*${startDate}*"}); containAssertEquals(patternCombinations, new String[]{"yyyyMMdd_hhmmss", "*${startDate}*${endDate}*"}); containAssertEquals(patternCombinations, new String[]{"yyyyMMdd", "*${startDate}*"}); containAssertEquals(patternCombinations, new String[]{"yyyyMMdd", "*${startDate}*${endDate}*"}); containAssertEquals(patternCombinations, new String[]{"yyyyMM", "*${startDate}*"}); containAssertEquals(patternCombinations, new String[]{"yyyyMM", "*${startDate}*${endDate}*"}); }
@Test public void testRecommendPatterns_variousPossiblePatterns() throws Exception { List<String[]> patternCombinations = PatternProvider.recommendPatterns("sth__20100101192345_99999999.nc"); assertTrue(patternCombinations.size() >= 7); containAssertEquals(patternCombinations, new String[]{"yyyyMMdd_hhmmss", "*${startDate}*"}); containAssertEquals(patternCombinations, new String[]{"yyyyMMddhhmmss", "*${startDate}*"}); containAssertEquals(patternCombinations, new String[]{"yyyyMMddhh", "*${startDate}*"}); containAssertEquals(patternCombinations, new String[]{"yyyyMMdd", "*${startDate}*"}); containAssertEquals(patternCombinations, new String[]{"yyyyMMdd", "*${startDate}*${endDate}*"}); containAssertEquals(patternCombinations, new String[]{"yyyyMM", "*${startDate}*"}); containAssertEquals(patternCombinations, new String[]{"yyyyMM", "*${startDate}*${endDate}*"}); }