Last active
December 10, 2015 04:18
-
-
Save egmanoj/4379763 to your computer and use it in GitHub Desktop.
Test case for bug (See http://stackoverflow.com/questions/14036441/how-can-i-tell-supercsv-to-map-empty-string-attributes-when-using-csvdozerbea)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package supercsv.test; | |
import static org.supercsv.prefs.CsvPreference.STANDARD_PREFERENCE; | |
import java.io.IOException; | |
import java.io.PrintWriter; | |
import org.supercsv.cellprocessor.Optional; | |
import org.supercsv.cellprocessor.ift.CellProcessor; | |
import org.supercsv.io.dozer.CsvDozerBeanWriter; | |
import org.supercsv.io.dozer.ICsvDozerBeanWriter; | |
public class DozerBugTest { | |
private static final String[] FIELD_MAPPING = new String[]{ "firstName", | |
"middleName", "lastName" }; | |
private static final CellProcessor[] PROCESSORS = new CellProcessor[]{ | |
new Optional(), new Optional(), new Optional() }; | |
private static final String[] HEADER = new String[]{ "First_Name", | |
"Middle_Name", "Last_Name" }; | |
public static void main(String[] args) throws Exception { | |
Person person = new Person(); | |
person.setFirstName("The Joker"); | |
person.setMiddleName(null); | |
person.setLastName(""); // Empty String | |
ICsvDozerBeanWriter beanWriter = null; | |
try { | |
beanWriter = new CsvDozerBeanWriter(new PrintWriter(System.out), | |
STANDARD_PREFERENCE); | |
beanWriter.configureBeanMapping(Person.class, FIELD_MAPPING); | |
beanWriter.writeHeader(HEADER); | |
beanWriter.write(person, PROCESSORS); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} finally { | |
if (beanWriter != null) { | |
beanWriter.close(); | |
} | |
} | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package supercsv.test; | |
public class Person { | |
private String firstName; | |
private String middleName; | |
private String lastName; | |
public String getFirstName() { | |
return firstName; | |
} | |
public String getLastName() { | |
return lastName; | |
} | |
public String getMiddleName() { | |
return middleName; | |
} | |
public void setFirstName(String firstName) { | |
this.firstName = firstName; | |
} | |
public void setLastName(String lastName) { | |
this.lastName = lastName; | |
} | |
public void setMiddleName(String middleName) { | |
this.middleName = middleName; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
First_Name,Middle_Name,Last_Name | |
Exception in thread "main" org.supercsv.exception.SuperCsvException: The number of columns to be processed (2) must match the number of CellProcessors (3): check that the number of CellProcessors you have defined matches the expected number of columns being read/written | |
context={lineNo=2, rowNo=2, columnNo=1, rowSource=[The Joker, null]} | |
at org.supercsv.util.Util.executeCellProcessors(Util.java:78) | |
at org.supercsv.io.dozer.CsvDozerBeanWriter.write(CsvDozerBeanWriter.java:133) | |
at supercsv.test.DozerBugTest.main(DozerBugTest.java:36) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment