### Question:
FileConsumer implements StreamConsumer { public void consumeLine( String line ) { Matcher m = filePattern.matcher( line ); if ( m.matches() ) { int i = 1; String fileName = null; while ( fileName == null && i <= m.groupCount() ) { fileName = m.group( i++ ); } if ( fileName != null ) { matchedFiles.add( new File( fileName ) ); } } } FileConsumer( List<File> matchedFilesAccumulator, Pattern filematcher ); void consumeLine( String line ); static final Pattern ADD_PATTERN; static final Pattern UPDATE_PATTERN; static final Pattern POPULATE_PATTERN; static final Pattern PROMOTE_PATTERN; static final Pattern STAT_PATTERN; static final Pattern DEFUNCT_PATTERN; }### Answer:
@Test public void testConsumeAdd() throws IOException { List<File> extractedFiles = new ArrayList<File>(); StreamConsumer consumer = new FileConsumer( extractedFiles, FileConsumer.ADD_PATTERN ); consumer.consumeLine( "Added and kept element /./src/main/java/Application.java" ); consumer.consumeLine( "Added and kept element \\.\\src\\main\\java\\Windows.java" ); assertThat( extractedFiles.size(), is( 2 ) ); assertThat( extractedFiles, hasItem( new File( "src/main/java/Application.java" ) ) ); assertThat( extractedFiles, hasItem( new File( "src\\main\\java\\Windows.java" ) ) ); }
@Test public void testConsumeUpdate() throws IOException { List<File> extractedFiles = new ArrayList<File>(); StreamConsumer consumer = new FileConsumer( extractedFiles, FileConsumer.UPDATE_PATTERN ); consumer.consumeLine( "Content (1 K) of \"readme.txt\" - ok" ); consumer.consumeLine( "Creating dir \"src/main/java/org\" ." ); consumer.consumeLine( "Updating (creating) dir /./src/test/java" ); consumer.consumeLine( "Updating element \\.\\src\\main\\java\\Application.java" ); assertThat( extractedFiles.size(), is( 2 ) ); assertThat( extractedFiles, hasItem( new File( "readme.txt" ) ) ); assertThat( extractedFiles, hasItem( new File( "src\\main\\java\\Application.java" ) ) ); }
@Test public void testConsumePromoted() throws IOException { List<File> extractedFiles = new ArrayList<File>(); StreamConsumer consumer = new FileConsumer( extractedFiles, FileConsumer.PROMOTE_PATTERN ); consumer.consumeLine( "Promoted element /./src/main/java/Application.java" ); consumer.consumeLine( "Promoted element \\.\\src\\main\\java\\Windows.java" ); assertThat( extractedFiles.size(), is( 2 ) ); assertThat( extractedFiles, hasItem( new File( "src/main/java/Application.java" ) ) ); assertThat( extractedFiles, hasItem( new File( "src\\main\\java\\Windows.java" ) ) ); }
@Test public void testConsumeRemoved() throws IOException { List<File> extractedFiles = new ArrayList<File>(); StreamConsumer consumer = new FileConsumer( extractedFiles, FileConsumer.DEFUNCT_PATTERN ); consumer.consumeLine( "Recursively removing \"tcktests/src\" ." ); consumer.consumeLine( "Removing \"tcktests/src/main/java/Application.java\" ." ); consumer.consumeLine( "Removing \"tcktests/src/main/java\" ." ); consumer.consumeLine( "Removing \"tcktests/src/main\" ." ); consumer.consumeLine( "Removing \"tcktests/src\" ." ); assertThat( extractedFiles.size(), is( 4 ) ); assertThat( extractedFiles, hasItem( new File( "tcktests/src" ) ) ); assertThat( extractedFiles, hasItem( new File( "tcktests/src/main/java/Application.java" ) ) ); }