LineMapperRpcResponseExtractor implements RpcResponseExtractor<List<T>> { @Override public List<T> extractData(RpcResponse response) throws DataAccessException { List<T> results = (this.linesExpected > 0 ? new ArrayList<T>(this.linesExpected) : new ArrayList<T>()); if (response.length() > 0) { String[] lines = response.toLines(); for (int i = 0; i < lines.length; i++) { results.add(lineMapper.mapLine(lines[i], i)); } } return results; } LineMapperRpcResponseExtractor(LineMapper<T> lineMapper);  LineMapperRpcResponseExtractor(LineMapper<T> lineMapper, int linesExpected); @Override List<T> extractData(RpcResponse response);  }
@Test public void extractData() { RpcResponse mockResponse = new RpcResponse("fred\r\nbarney\r\nwilma\r\nbetty"); LineMapperRpcResponseExtractor<Foo> extractor = new LineMapperRpcResponseExtractor<Foo>(new FooLineMapper()); List<Foo> fooList = extractor.extractData(mockResponse); assertEquals(4, fooList.size()); assertEquals("fred", fooList.get(0).getText()); assertEquals("barney", fooList.get(1).getText()); assertEquals("wilma", fooList.get(2).getText()); assertEquals("betty", fooList.get(3).getText()); }
@Test public void extractDataFromEmptyResponse() { RpcResponse mockResponse = new RpcResponse(""); LineMapperRpcResponseExtractor<Foo> extractor = new LineMapperRpcResponseExtractor<Foo>(new FooLineMapper()); List<Foo> fooList = extractor.extractData(mockResponse); assertTrue(fooList.isEmpty()); }