@Test public void accessorCanChangeType() throws IOException { EntityInfoBuilder<SomeEntity> entityBuilder = new EntityInfoBuilder<>(SomeEntity.class); entityBuilder.addField("name"); entityBuilder.addColumn("age", FieldTypeEnum.STRING, e -> e.getAge() + " years"); entityBuilder.addColumn("adult", FieldTypeEnum.STRING, e -> e.getAge() >= 18 ? "Si" : "No"); Builder<SomeEntity> config = new Builder<>(entityBuilder.build()); String result = writeCsvToString(config.build(), new SomeEntity("John", 10), new SomeEntity("Amanda", 34)); assertEquals("name,age,adult" + ls + "John,10 years,No" + ls + "Amanda,34 years,Si" + ls, result); }
@Test public void mandatoryTextDelimiterUsedTest() throws IOException { config.textDelimiter('\'').alwaysDelimitText(true); String result = writeCsvToString(config.build(), new SomeEntity("John 'Smith'", 10), new SomeEntity("Amanda", 20)); assertEquals("'name','age'" + ls + "'John ''Smith''','10'" + ls + "'Amanda','20'" + ls, result); }
@Test public void fieldSeparatorTest() throws IOException { config.fieldSeparator(';'); String result = writeCsvToString(config.build(), new SomeEntity("John", 10)); assertEquals("name;age" + ls + "John;10" + ls, result); }
@Test public void canCreateSyntheticColumns() throws IOException { EntityInfoBuilder<SomeEntity> entityBuilder = new EntityInfoBuilder<>(SomeEntity.class); entityBuilder.addFields("name", "age"); entityBuilder.addColumn("adult", FieldTypeEnum.BOOLEAN, e -> e.getAge() >= 18); CsvConfiguration<SomeEntity> config = new CsvConfiguration<>(entityBuilder.build()); String result = writeCsvToString(config, new SomeEntity("John", 10), new SomeEntity("Amanda", 34)); assertEquals("name,age,adult" + ls + "John,10,false" + ls + "Amanda,34,true" + ls, result); }
@Test public void optionalTextDelimiterUsedTest() throws IOException { config.textDelimiter('"'); String result = writeCsvToString(config.build(), new SomeEntity("John \"Smith\"", 10)); assertEquals("name,age" + ls + "\"John \"\"Smith\"\"\",10" + ls, result); }
@Test public void canChangeDefaultSerializer() throws IOException { EntityInfoBuilder<SomeEntity> entityBuilder = new EntityInfoBuilder<>(SomeEntity.class); entityBuilder.addFields("name", "age"); entityBuilder.addColumn("adult", FieldTypeEnum.BOOLEAN, e -> e.getAge() >= 18); BaseTypeSerializer serializer = new BaseTypeSerializer(); serializer.add(FieldTypeEnum.BOOLEAN, (obj) -> ((Boolean) obj).booleanValue() ? "1" : "0"); Builder<SomeEntity> config = new Builder<>(entityBuilder.build()); config.typeSerializer(serializer); String result = writeCsvToString(config.build(), new SomeEntity("John", 10), new SomeEntity("Amanda", 34)); assertEquals("name,age,adult" + ls + "John,10,0" + ls + "Amanda,34,1" + ls, result); }
@Test public void escapeLineDelimiterTest() throws IOException { config.textDelimiter('\''); String result = writeCsvToString(config.build(), new SomeEntity("John \nSmith", 10)); assertEquals("name,age" + ls + "'John \nSmith',10" + ls, result); }
@Test public void optionalTextDelimiterNotUsedTest() throws IOException { config.textDelimiter('$'); String result = writeCsvToString(config.build(), new SomeEntity("John", 10)); assertEquals("name,age" + ls + "John,10" + ls, result); }
@Test public void escapeFieldSeparatorTest() throws IOException { config.fieldSeparator(';').textDelimiter('\''); String result = writeCsvToString(config.build(), new SomeEntity("John;Smith", 10)); assertEquals("name;age" + ls + "'John;Smith';10" + ls, result); }
@Test public void elementIsAfterHeader() throws IOException { CsvConfiguration<SomeEntity> config = new CsvConfiguration<>(createBuilderForSomeEntity().build()); String result = writeCsvToString(config, new SomeEntity("John", 10)); assertEquals("name,age" + ls + "John,10" + ls, result); }
@Test public void elementWithoutHeaderIsAlone() throws IOException { Builder<SomeEntity> config = new Builder<>(createBuilderForSomeEntity().build()); config.header(false); String result = writeCsvToString(config.build(), new SomeEntity("John", 10)); assertEquals("John,10" + ls, result); }
@Test public void columnsMantainsDeclaredOrder() throws IOException { EntityInfoBuilder<SomeEntity> entityBuilder = new EntityInfoBuilder<>(SomeEntity.class); entityBuilder.addColumn("age", FieldTypeEnum.INT, SomeEntity::getAge); entityBuilder.addColumn("name", FieldTypeEnum.STRING, SomeEntity::getName); CsvConfiguration<SomeEntity> config = new CsvConfiguration<>(entityBuilder.build()); String result = writeCsvToString(config, new SomeEntity("John", 10)); assertEquals("age,name" + ls + "10,John" + ls, result); }