### Question:
DefaultLineHandler implements LineHandler { public void handle(String line, long linenum) throws IOException { final String[] cols = tok.tokenize(line); try { final T dat = proc.process(cols); con.consume(dat); } catch (BadDataException e) { err.consume(e, linenum); } } DefaultLineHandler(LineTokenizer tok, RecordProcessor<T> proc,
                            RecordConsumer<T> con, ErrorConsumer err); void handle(String line, long linenum);  }### Answer:
@Test public void handleGood() throws IOException, BadDataException { final LineTokenizer tok = context.mock(LineTokenizer.class); @SuppressWarnings("unchecked") final RecordProcessor<Object> proc = (RecordProcessor<Object>) context.mock(RecordProcessor.class); @SuppressWarnings("unchecked") final RecordConsumer<Object> con = (RecordConsumer<Object>) context.mock(RecordConsumer.class); final ErrorConsumer err = context.mock(ErrorConsumer.class); context.checking(new Expectations() { { for (int i = 0; i < rows.length; ++i) { oneOf(tok).tokenize(with(buildCSV(rows[i]))); will(returnValue(rows[i])); oneOf(proc).process(with(rows[i])); will(returnValue("foo")); oneOf(con).consume(with("foo")); } never(err).consume(with(any(BadDataException.class)), with(any(long.class))); } }); final LineHandler lh = new DefaultLineHandler<Object>(tok, proc, con, err); for (int i = 0; i < rows.length; ++i) { lh.handle(buildCSV(rows[i]), i+1); } }
@Test public void handleBad() throws IOException, BadDataException { final LineTokenizer tok = context.mock(LineTokenizer.class); @SuppressWarnings("unchecked") final RecordProcessor<Object> proc = (RecordProcessor<Object>) context.mock(RecordProcessor.class); @SuppressWarnings("unchecked") final RecordConsumer<Object> con = (RecordConsumer<Object>) context.mock(RecordConsumer.class); final ErrorConsumer err = context.mock(ErrorConsumer.class); context.checking(new Expectations() { { for (int i = 0; i < rows.length; ++i) { oneOf(tok).tokenize(with(buildCSV(rows[i]))); will(returnValue(rows[i])); } oneOf(proc).process(with(rows[0])); will(returnValue("foo")); oneOf(con).consume(with("foo")); oneOf(proc).process(with(rows[1])); will(throwException(new BadDataException())); oneOf(err).consume(with(any(BadDataException.class)), with(2L)); oneOf(proc).process(with(rows[2])); will(returnValue("foo")); oneOf(con).consume(with("foo")); } }); final LineHandler lh = new DefaultLineHandler<Object>(tok, proc, con, err); for (int i = 0; i < rows.length; ++i) { lh.handle(buildCSV(rows[i]), i+1); } }