@Test public void testCaseInsensitive() throws Exception { List<CaseInsensitiveRecord> objs = CsvDeserializer.deserialize("NAME,CAMELCASEVALUE\nname,not!", CaseInsensitiveRecord.class); Assert.assertNull("hi", objs.get(0).name); Assert.assertEquals("not!", objs.get(0).camelCasevalue); }
@Test public void testEnum() throws Exception { List<EnumExample> objs = CsvDeserializer.deserialize("id,name\n42,EXAMPLE", EnumExample.class); Assert.assertEquals(42, objs.get(0).getId().intValue()); Assert.assertEquals(EnumExample.Name.EXAMPLE, objs.get(0).getName()); }
@Test public void testSimpleRecord() throws Exception { List<SimpleRecord> objs = CsvDeserializer.deserialize("id,name\n42,example", SimpleRecord.class); Assert.assertEquals(42, objs.get(0).getId().intValue()); Assert.assertEquals("example", objs.get(0).getName()); }
@Test public void testDeserializationPreprocessor() throws Exception { List<DeserializationPreprocessorExample> objs = CsvDeserializer.deserialize("id,name\n,", DeserializationPreprocessorExample.class); Assert.assertTrue(0 == objs.get(0).getId()); Assert.assertNull(objs.get(0).getName()); }
@Test public void testIgnoreGarbage() throws Exception { List<IgnoreGarbageExample> objs = CsvDeserializer.deserialize("id,name\n42,example\nfhh97gh34k3", IgnoreGarbageExample.class); Assert.assertEquals(42, objs.get(0).getId().intValue()); Assert.assertEquals("example", objs.get(0).getName()); }
@Test public void testRenaming() throws Exception { List<RenamingExample> objs = CsvDeserializer.deserialize("internalId,n\n42,example", RenamingExample.class); Assert.assertEquals(42, objs.get(0).getId().intValue()); Assert.assertEquals("example", objs.get(0).getName()); }
@Test public void testDefaultRecordDelimiter() throws Exception { List<SimpleRecord> objs = CsvDeserializer.deserialize( new StringReader("id,name\r\n42,example"), SimpleRecord.class, ',', null); Assert.assertEquals(42, objs.get(0).getId().intValue()); Assert.assertEquals("example", objs.get(0).getName()); }
@Test public void testFieldDelimiter() throws Exception { List<SimpleRecord> objs = CsvDeserializer.deserialize( new StringReader("id|name\n42|example"), SimpleRecord.class, '|', '\n'); Assert.assertEquals(42, objs.get(0).getId().intValue()); Assert.assertEquals("example", objs.get(0).getName()); }
@Test public void testIgnoreForSerialization() throws Exception { List<IgnoreForSerializationExample> objs = CsvDeserializer.deserialize( "id,name,foo\n42,example,bar", IgnoreForSerializationExample.class); Assert.assertNull(objs.get(0).getName()); Assert.assertNull(objs.get(0).getFoo()); }
private void doEncodingTest(Charset charset) throws Exception { String value = "\u00f1example\u00f1"; String fullCsv = "name\n" + value; List<SimpleRecord> objs = CsvDeserializer.deserialize( new InputStreamReader(new ByteArrayInputStream(fullCsv.getBytes(charset)), charset), SimpleRecord.class); Assert.assertEquals(value, objs.get(0).getName()); }
@Test public void testProvidedHeaders() throws Exception { List<SimpleRecord> objs = CsvDeserializer.deserialize( new StringReader("42,example"), SimpleRecord.class, ',', '\n', ImmutableList.<String>of("id", "name")); Assert.assertEquals(42, objs.get(0).getId().intValue()); Assert.assertEquals("example", objs.get(0).getName()); }
@Test(expected = NullPointerException.class) public void testNullReader() throws Exception { CsvDeserializer.deserialize((Reader) null, String.class); }
@Test(expected = NullPointerException.class) public void testNullString() throws Exception { CsvDeserializer.deserialize((String) null, SimpleRecord.class); }
@Test(expected = DeserializingException.class) public void testNoDefaultConstructor() throws Exception { CsvDeserializer.deserialize("id\n5", NoDefaultConstructorExample.class); }
@Test(expected = DeserializingException.class) public void testStaticMemeber() throws Exception { CsvDeserializer.deserialize("name\nexample", StaticMemberExample.class); }
/** * Passing means not throwing an exception * * @throws Exception */ @Test public void testIgnoredFinalMemeber() throws Exception { CsvDeserializer.deserialize("id\n5", IgnoredFinalMemberExample.class); }
@Test(expected = DeserializingException.class) public void testFinalMemeber() throws Exception { CsvDeserializer.deserialize("id\n5", FinalMemberExample.class); }
@Test(expected = DeserializingException.class) public void testUnparsable() throws Exception { CsvDeserializer.deserialize("a,b,,,d\n1\n\n,,,,,,,,,,,,,,,,", SimpleRecord.class); }