DSpaceCSV implements Serializable { public DSpaceCSV(boolean exportAll) { init(); this.exportAll = exportAll; } DSpaceCSV(boolean exportAll);  DSpaceCSV(File f, Context c); boolean hasActions(); final void addItem(Item i); final void addItem(String line); final List<DSpaceCSVLine> getCSVLines(); final String[] getCSVLinesAsStringArray(); final void save(String filename); List<String> getHeadings(); @Override final String toString(); String getAuthoritySeparator(); String getEscapedAuthoritySeparator();  }
@Test public void testDSpaceCSV() { try { String[] csv = {"id,collection,\"dc.title[en]\",dc.contributor.author,dc.description.abstract", "+,56599ad5-c7d2-4ac3-8354-a1f277d5a31f,Easy line,\"Lewis, Stuart\",A nice short abstract", "+,56599ad5-c7d2-4ac3-8354-a1f277d5a31f,Two authors,\"Lewis, Stuart||Bloggs, Joe\",Two people wrote this item", "+,56599ad5-c7d2-4ac3-8354-a1f277d5a31f,Three authors,\"Lewis, Stuart||Bloggs, Joe||Loaf, Meat\",Three people wrote this item", "+,56599ad5-c7d2-4ac3-8354-a1f277d5a31f,\"Two line\n\ntitle\",\"Lewis, Stuart\",abstract", "+,56599ad5-c7d2-4ac3-8354-a1f277d5a31f,\"Empty lines\n\nshould work too (DS-3245).\",\"Lewis, Stuart\",abstract", "+,56599ad5-c7d2-4ac3-8354-a1f277d5a31f,\"\"\"Embedded quotes\"\" here\",\"Lewis, Stuart\",\"Abstract with\ntwo\nnew lines\"", "+,56599ad5-c7d2-4ac3-8354-a1f277d5a31f,\"\"\"Unbalanced embedded\"\" quotes\"\" here\",\"Lewis, Stuart\",\"Abstract with\ntwo\nnew lines\"",}; String filename = "test.csv"; BufferedWriter out = new BufferedWriter( new OutputStreamWriter( new FileOutputStream(filename), "UTF-8")); for (String csvLine : csv) { out.write(csvLine + "\n"); } out.flush(); out.close(); out = null; DSpaceCSV dcsv = new DSpaceCSV(new File(filename), context); String[] lines = dcsv.getCSVLinesAsStringArray(); assertThat("testDSpaceCSV Good CSV", lines.length, equalTo(8)); List<DSpaceCSVLine> csvLines = dcsv.getCSVLines(); DSpaceCSVLine line = csvLines.get(5); List<String> value = new ArrayList<String>(); value.add("Abstract with\ntwo\nnew lines"); assertThat("testDSpaceCSV New lines", line.valueToCSV(value, dcsv.valueSeparator), equalTo("\"Abstract with\ntwo\nnew lines\"")); line = null; csv[0] = "id,collection,\"dc.title[en]\",dc.contributor.foobar[en-US],dc.description.abstract"; filename = "test.csv"; out = new BufferedWriter( new OutputStreamWriter( new FileOutputStream(filename), "UTF-8")); for (String csvLine : csv) { out.write(csvLine + "\n"); } out.flush(); out.close(); out = null; try { dcsv = new DSpaceCSV(new File(filename), context); lines = dcsv.getCSVLinesAsStringArray(); fail("An exception should have been thrown due to bad CSV"); } catch (Exception e) { assertThat("testDSpaceCSV Bad heading CSV", e.getMessage(), equalTo("Unknown metadata element in column 4: dc.contributor.foobar")); } lines = dcsv.getCSVLinesAsStringArray(); assertThat("testDSpaceCSV Good CSV", lines.length, equalTo(8)); csv[0] = "id,collection,\"dcdc.title[en]\",dc.contributor[en-US],dc.description.abstract"; filename = "test.csv"; out = new BufferedWriter( new OutputStreamWriter( new FileOutputStream(filename), "UTF-8")); for (String csvLine : csv) { out.write(csvLine + "\n"); } out.flush(); out.close(); out = null; try { dcsv = new DSpaceCSV(new File(filename), context); lines = dcsv.getCSVLinesAsStringArray(); fail("An exception should have been thrown due to bad CSV"); } catch (Exception e) { assertThat("testDSpaceCSV Bad heading CSV", e.getMessage(), equalTo("Unknown metadata schema in column 3: dcdc.title")); } File toDelete = new File(filename); toDelete.delete(); toDelete = null; dcsv = null; lines = null; } catch (Exception ex) { log.error("IO Error while creating test CSV file", ex); fail("IO Error while creating test CSV file"); } }