ConsoleResultConverter implements ResultConverter<List<String>> { public List<String> convert(final Object result) throws Exception { try { final List<Object> resultLines = new ArrayList<Object>(); final Iterator itty; if (result instanceof Iterable) { itty = ((Iterable) result).iterator(); } else if (result instanceof Iterator) { itty = (Iterator) result; } else if (result instanceof Object[]) { itty = new ArrayIterator((Object[]) result); } else if (result instanceof Map) { itty = ((Map) result).entrySet().iterator(); } else if (result instanceof Throwable) { itty = new SingleIterator<Object>(((Throwable) result).getMessage()); } else { itty = new SingleIterator<Object>(result); } while (itty.hasNext()) { resultLines.add(itty.next()); } final List<String> outputLines = new ArrayList<String>(); final String[] printLines = this.outputWriter.toString().split(LINE_SEPARATOR); if (printLines.length > 0 && printLines[0].length() > 0) { for (String printLine : printLines) { outputLines.add(printLine); } } if (resultLines != null && resultLines.size() > 0) { for (Object resultLine : resultLines) { outputLines.add(resultLine != null ? resultLine.toString() : "null"); } } return outputLines; } catch (Exception ex) { final ArrayList<String> resultList = new ArrayList<String>(); resultList.add(ex.getMessage()); return resultList; } } ConsoleResultConverter(final Writer outputWriter); List<String> convert(final Object result);  }
@Test public void convertNullResultReturnsZeroSizeList() throws Exception { List<String> converted = this.converter.convert(null); Assert.assertNotNull(converted); Assert.assertEquals(1, converted.size()); Assert.assertEquals("null", converted.get(0)); }
@Test public void convertIterable() throws Exception { ArrayList<FunObject> funList = new ArrayList<FunObject>(); funList.add(new FunObject("x")); funList.add(new FunObject("y")); Iterable<FunObject> iterable = funList; List<String> converted = this.converter.convert(iterable); Assert.assertNotNull(converted); Assert.assertEquals(2, converted.size()); Assert.assertEquals("x", converted.get(0)); Assert.assertEquals("y", converted.get(1)); }
@Test public void convertIterableWithNull() throws Exception { ArrayList<FunObject> funList = new ArrayList<FunObject>(); funList.add(new FunObject("x")); funList.add(null); funList.add(new FunObject("y")); Iterable<FunObject> iterable = funList; List<String> converted = this.converter.convert(iterable); Assert.assertNotNull(converted); Assert.assertEquals(3, converted.size()); Assert.assertEquals("x", converted.get(0)); Assert.assertEquals("null", converted.get(1)); Assert.assertEquals("y", converted.get(2)); }
@Test public void convertIterator() throws Exception { ArrayList<FunObject> funList = new ArrayList<FunObject>(); funList.add(new FunObject("x")); funList.add(new FunObject("y")); Iterator<FunObject> iterable = funList.iterator(); List<String> converted = this.converter.convert(iterable); Assert.assertNotNull(converted); Assert.assertEquals(2, converted.size()); Assert.assertEquals("x", converted.get(0)); Assert.assertEquals("y", converted.get(1)); }
@Test public void convertMap() throws Exception { Map<String, FunObject> map = new HashMap<String, FunObject>(); map.put("X", new FunObject("x")); map.put("Y", new FunObject("y")); List<String> converted = this.converter.convert(map); Assert.assertNotNull(converted); Assert.assertEquals(2, converted.size()); boolean foundX = false; boolean foundY = false; for (String line : converted) { if (line.equals("X=x")) { foundX = true; } else if (line.equals("Y=y")) { foundY = true; } } Assert.assertTrue(foundX); Assert.assertTrue(foundY); }
@Test public void convertThrowable() throws Exception { Throwable throwable = new Exception("message"); List<String> converted = this.converter.convert(throwable); Assert.assertNotNull(converted); Assert.assertEquals(1, converted.size()); Assert.assertEquals("message", converted.get(0)); }
@Test public void convertWriter() throws Exception { this.writer.write("x" + LINE_SEPARATOR); this.writer.write("y"); List<String> converted = this.converter.convert(null); Assert.assertNotNull(converted); Assert.assertEquals(3, converted.size()); Assert.assertEquals("x", converted.get(0)); Assert.assertEquals("y", converted.get(1)); Assert.assertEquals("null", converted.get(2)); }