@Test public void testSerializeResources_HeaderOrderInfo() throws Exception { Result result = new ResultImpl(true); result.setResultStatus(new ResultStatus(ResultStatus.STATUS.OK)); TreeNode<Resource> tree = result.getResultTree(); tree.setName("items"); tree.setProperty("isCollection", "true"); tree.setProperty(CsvSerializer.PROPERTY_COLUMN_MAP, new HashMap<String, String>() {{ put("property1", "Property 1"); put("property2", "Property 2"); put("property3", "Property 3"); put("property4", "Property 4"); }}); tree.setProperty(CsvSerializer.PROPERTY_COLUMN_ORDER, Arrays.asList( "property1", "property2", "property3", "property4")); addChildResource(tree, "resource", 0, new HashMap<String, Object>() { { put("property1", "value1a"); put("property2", "value2a"); put("property3", "value3a"); put("property4", "value4a"); } }); addChildResource(tree, "resource", 1, new HashMap<String, Object>() { { put("property1", "value1'b"); put("property2", "value2'b"); put("property3", "value3'b"); put("property4", "value4'b"); } }); addChildResource(tree, "resource", 2, new HashMap<String, Object>() { { put("property1", "value1,c"); put("property2", "value2,c"); put("property3", "value3,c"); put("property4", "value4,c"); } }); replayAll(); Object o = new CsvSerializer().serialize(result).toString().replace("\r", ""); String expected = "Property 1,Property 2,Property 3,Property 4\n" + "value1a,value2a,value3a,value4a\n" + "value1'b,value2'b,value3'b,value4'b\n" + "\"value1,c\",\"value2,c\",\"value3,c\",\"value4,c\"\n"; assertEquals(expected, o); verifyAll(); }
@Override public Object serialize(Result result) { if (result.getStatus().isErrorState()) { return serializeError(result.getStatus()); } else { CSVPrinter csvPrinter = null; try { StringBuffer buffer = new StringBuffer(); TreeNode<Resource> root = result.getResultTree(); if (root != null) { csvPrinter = new CSVPrinter(buffer, CSVFormat.DEFAULT); if ("true".equalsIgnoreCase(root.getStringProperty("isCollection"))) { List<String> fieldNameOrder = processHeader(csvPrinter, root); Collection<TreeNode<Resource>> children = root.getChildren(); if (children != null) { for (TreeNode<Resource> child : children) { processRecord(csvPrinter, child, fieldNameOrder); } } } } return buffer.toString(); } catch (IOException e) { throw new RuntimeException("Unable to serialize to csv: " + e, e); } finally { Closeables.closeSilently(csvPrinter); } } }
CsvSerializer implements ResultSerializer { @Override public Object serialize(Result result) { if (result.getStatus().isErrorState()) { return serializeError(result.getStatus()); } else { CSVPrinter csvPrinter = null; try { StringBuffer buffer = new StringBuffer(); TreeNode<Resource> root = result.getResultTree(); if (root != null) { csvPrinter = new CSVPrinter(buffer, CSVFormat.DEFAULT); if ("true".equalsIgnoreCase(root.getStringProperty("isCollection"))) { List<String> fieldNameOrder = processHeader(csvPrinter, root); Collection<TreeNode<Resource>> children = root.getChildren(); if (children != null) { for (TreeNode<Resource> child : children) { processRecord(csvPrinter, child, fieldNameOrder); } } } } return buffer.toString(); } catch (IOException e) { throw new RuntimeException("Unable to serialize to csv: " + e, e); } finally { Closeables.closeSilently(csvPrinter); } } } }
CsvSerializer implements ResultSerializer { @Override public Object serialize(Result result) { if (result.getStatus().isErrorState()) { return serializeError(result.getStatus()); } else { CSVPrinter csvPrinter = null; try { StringBuffer buffer = new StringBuffer(); TreeNode<Resource> root = result.getResultTree(); if (root != null) { csvPrinter = new CSVPrinter(buffer, CSVFormat.DEFAULT); if ("true".equalsIgnoreCase(root.getStringProperty("isCollection"))) { List<String> fieldNameOrder = processHeader(csvPrinter, root); Collection<TreeNode<Resource>> children = root.getChildren(); if (children != null) { for (TreeNode<Resource> child : children) { processRecord(csvPrinter, child, fieldNameOrder); } } } } return buffer.toString(); } catch (IOException e) { throw new RuntimeException("Unable to serialize to csv: " + e, e); } finally { Closeables.closeSilently(csvPrinter); } } }  }
CsvSerializer implements ResultSerializer { @Override public Object serialize(Result result) { if (result.getStatus().isErrorState()) { return serializeError(result.getStatus()); } else { CSVPrinter csvPrinter = null; try { StringBuffer buffer = new StringBuffer(); TreeNode<Resource> root = result.getResultTree(); if (root != null) { csvPrinter = new CSVPrinter(buffer, CSVFormat.DEFAULT); if ("true".equalsIgnoreCase(root.getStringProperty("isCollection"))) { List<String> fieldNameOrder = processHeader(csvPrinter, root); Collection<TreeNode<Resource>> children = root.getChildren(); if (children != null) { for (TreeNode<Resource> child : children) { processRecord(csvPrinter, child, fieldNameOrder); } } } } return buffer.toString(); } catch (IOException e) { throw new RuntimeException("Unable to serialize to csv: " + e, e); } finally { Closeables.closeSilently(csvPrinter); } } }  @Override Object serialize(Result result); @Override Object serializeError(ResultStatus error); }
CsvSerializer implements ResultSerializer { @Override public Object serialize(Result result) { if (result.getStatus().isErrorState()) { return serializeError(result.getStatus()); } else { CSVPrinter csvPrinter = null; try { StringBuffer buffer = new StringBuffer(); TreeNode<Resource> root = result.getResultTree(); if (root != null) { csvPrinter = new CSVPrinter(buffer, CSVFormat.DEFAULT); if ("true".equalsIgnoreCase(root.getStringProperty("isCollection"))) { List<String> fieldNameOrder = processHeader(csvPrinter, root); Collection<TreeNode<Resource>> children = root.getChildren(); if (children != null) { for (TreeNode<Resource> child : children) { processRecord(csvPrinter, child, fieldNameOrder); } } } } return buffer.toString(); } catch (IOException e) { throw new RuntimeException("Unable to serialize to csv: " + e, e); } finally { Closeables.closeSilently(csvPrinter); } } }  @Override Object serialize(Result result); @Override Object serializeError(ResultStatus error); static final String PROPERTY_COLUMN_MAP; static final String PROPERTY_COLUMN_ORDER; }