FilenameParser { public long extractIdFromFilename(String filename) throws UnrecognisedFilenameException { Matcher matches = pattern.matcher(filename); if (!matches.matches() || matches.groupCount() != 1) throw new UnrecognisedFilenameException("Could not extract a change script number from filename: " + filename); return Long.parseLong(matches.group(1)); } FilenameParser(); long extractIdFromFilename(String filename);  }
@Test public void canParseAnyFilenameThatStartsWithANumber() throws Exception { FilenameParser parser = new FilenameParser(); assertEquals(1L, parser.extractIdFromFilename("0001_a_filename.txt")); assertEquals(1L, parser.extractIdFromFilename("1_a_filename.txt")); assertEquals(1L, parser.extractIdFromFilename("1 a filename.txt")); assertEquals(1L, parser.extractIdFromFilename("1.txt")); assertEquals(123L, parser.extractIdFromFilename("00123_something.txt")); }
@Test public void throwsWhenFilenameDoesNotStartWithANumber() throws Exception { FilenameParser parser = new FilenameParser(); try { parser.extractIdFromFilename("blah blah blah"); fail("expected exception"); } catch (UnrecognisedFilenameException e) { assertEquals("Could not extract a change script number from filename: blah blah blah", e.getMessage() ); } }