1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
16  package org.supercsv;
17  
18  import static org.junit.Assert.assertEquals;
19  
20  import java.util.Arrays;
21  import java.util.Calendar;
22  import java.util.Date;
23  import java.util.List;
24  
25  import org.supercsv.cellprocessor.FmtBool;
26  import org.supercsv.cellprocessor.FmtDate;
27  import org.supercsv.cellprocessor.Optional;
28  import org.supercsv.cellprocessor.ParseBool;
29  import org.supercsv.cellprocessor.ParseDate;
30  import org.supercsv.cellprocessor.ParseInt;
31  import org.supercsv.cellprocessor.ParseLong;
32  import org.supercsv.cellprocessor.ift.CellProcessor;
33  import org.supercsv.mock.CustomerBean;
34  import org.supercsv.mock.CustomerStringBean;
35  import org.supercsv.util.CsvContext;
36  
37  
38  
39  
40  
41  
42  public class SuperCsvTestUtils {
43  	
44  	public static final CsvContext ANONYMOUS_CSVCONTEXT = new CsvContext(1, 2, 3);
45  	
46  	
47  	public static final String[] HEADER = new String[] { "customerNo", "firstName", "lastName", "birthDate",
48  		"mailingAddress", "married", "numberOfKids", "favouriteQuote", "email", "loyaltyPoints" };
49  	public static final String HEADER_CSV = "customerNo,firstName,lastName,birthDate,mailingAddress,married,"
50  		+ "numberOfKids,favouriteQuote,email,loyaltyPoints";
51  	
52  	
53  	public static final String[] PARTIAL_HEADER = new String[] { null, "firstName", "lastName", null, null, null, null,
54  		null, "email", null };
55  	
56  	
57  	public static final CellProcessor[] READ_PROCESSORS = new CellProcessor[] { null, null, null,
58  		new ParseDate("dd/MM/yyyy"), null, new Optional(new ParseBool()), new Optional(new ParseInt()), null, null,
59  		new ParseLong() };
60  	
61  	
62  	public static final CellProcessor[] WRITE_PROCESSORS = new CellProcessor[] { null, null, null,
63  		new FmtDate("dd/MM/yyyy"), null, new Optional(new FmtBool("Y", "N")), null, null, null, null };
64  	
65  	
66  	public static final String JOHN_CSV = "1,John,Dunbar,13/06/1945,\"1600 Amphitheatre Parkway\r\nMountain View, CA 94043\r\nUnited States\","
67  		+ ",,\"\"\"May the Force be with you.\"\" - Star Wars\",jdunbar@gmail.com,0";
68  	public static final CustomerBean JOHN = new CustomerBean("1", "John", "Dunbar", date(1945, 6, 13),
69  		"1600 Amphitheatre Parkway\nMountain View, CA 94043\nUnited States", null, null,
70  		"\"May the Force be with you.\" - Star Wars", "jdunbar@gmail.com", 0L);
71  	public static final CustomerStringBean JOHN_STRING = new CustomerStringBean(JOHN);
72  	
73  	public static final String BOB_CSV = "2,Bob,Down,25/02/1919,\"1601 Willow Rd.\r\nMenlo Park, CA 94025\r\nUnited States\","
74  		+ "Y,0,\"\"\"Frankly, my dear, I don't give a damn.\"\" - Gone With The Wind\",bobdown@hotmail.com,123456";
75  	public static final CustomerBean BOB = new CustomerBean("2", "Bob", "Down", date(1919, 2, 25),
76  		"1601 Willow Rd.\nMenlo Park, CA 94025\nUnited States", true, 0,
77  		"\"Frankly, my dear, I don't give a damn.\" - Gone With The Wind", "bobdown@hotmail.com", 123456L);
78  	public static final CustomerStringBean BOB_STRING = new CustomerStringBean(BOB);
79  	
80  	public static final String ALICE_CSV = "3,Alice,Wunderland,08/08/1985,\"One Microsoft Way\r\nRedmond, WA 98052-6399\r\nUnited States\","
81  		+ "Y,0,\"\"\"Play it, Sam. Play \"\"As Time Goes By.\"\"\"\" - Casablanca\",throughthelookingglass@yahoo.com,2255887799";
82  	public static final CustomerBean ALICE = new CustomerBean("3", "Alice", "Wunderland", date(1985, 8, 8),
83  		"One Microsoft Way\nRedmond, WA 98052-6399\nUnited States", true, 0,
84  		"\"Play it, Sam. Play \"As Time Goes By.\"\" - Casablanca", "throughthelookingglass@yahoo.com", 2255887799L);
85  	public static final CustomerStringBean ALICE_STRING = new CustomerStringBean(ALICE);
86  	
87  	public static final String BILL_CSV = "4,Bill,Jobs,10/07/1973,\"2701 San Tomas Expressway\r\nSanta Clara, CA 95050\r\nUnited States\","
88  		+ "Y,3,\"\"\"You've got to ask yourself one question: \"\"Do I feel lucky?\"\" Well, do ya, punk?\"\" - Dirty Harry\",billy34@hotmail.com,36";
89  	public static final CustomerBean BILL = new CustomerBean("4", "Bill", "Jobs", date(1973, 7, 10),
90  		"2701 San Tomas Expressway\nSanta Clara, CA 95050\nUnited States", true, 3,
91  		"\"You've got to ask yourself one question: \"Do I feel lucky?\" Well, do ya, punk?\" - Dirty Harry",
92  		"billy34@hotmail.com", 36L);
93  	public static final CustomerStringBean BILL_STRING = new CustomerStringBean(BILL);
94  	
95  	public static final String MIRANDA_CSV = "5,Miranda,Feist,03/01/1999,\"2-4 Rue du Sablon\r\nMorges, 1110\r\nSwitzerland\","
96  		+ ",,\"\"\"You had me at \"\"hello.\"\"\"\" - Jerry Maguire\",miranda_feist@gmail.com,54623";
97  	public static final CustomerBean MIRANDA = new CustomerBean("5", "Miranda", "Feist", date(1999, 1, 3),
98  		"2-4 Rue du Sablon\nMorges, 1110\nSwitzerland", null, null, "\"You had me at \"hello.\"\" - Jerry Maguire",
99  		"miranda_feist@gmail.com", 54623L);
100 	public static final CustomerStringBean MIRANDA_STRING = new CustomerStringBean(MIRANDA);
101 	
102 	public static final String STEVE_CSV = "6,Steve,Gates,31/12/2000,\"701 First Avenue\r\nSunnyvale, CA 94089\r\nUnited States\","
103 		+ "N,0,\"\"\"Gentlemen, you can't fight in here! This is the War Room!\"\" - Dr Strangelove\",stevengates@yahoo.com,1341512";
104 	public static final CustomerBean STEVE = new CustomerBean("6", "Steve", "Gates", date(2000, 12, 31),
105 		"701 First Avenue\nSunnyvale, CA 94089\nUnited States", false, 0,
106 		"\"Gentlemen, you can't fight in here! This is the War Room!\" - Dr Strangelove", "stevengates@yahoo.com",
107 		1341512L);
108 	public static final CustomerStringBean STEVE_STRING = new CustomerStringBean(STEVE);
109 	
110 	public static final String ADA_CSV = "7,Ada,Von Trappe,18/11/1956,\"One Dell Way\r\nRound Rock, TX 78682\r\nUnited States\","
111 		+ ",2,\"\"\"Hasta la vista, baby.\"\" - Terminator 2: Judgement Day\",vonada@gmail.com,0";
112 	public static final CustomerBean ADA = new CustomerBean("7", "Ada", "Von Trappe", date(1956, 11, 18),
113 		"One Dell Way\nRound Rock, TX 78682\nUnited States", null, 2,
114 		"\"Hasta la vista, baby.\" - Terminator 2: Judgement Day", "vonada@gmail.com", 0L);
115 	public static final CustomerStringBean ADA_STRING = new CustomerStringBean(ADA);
116 	
117 	public static final String SERGEI_CSV = "8,Sergei,Denisovich,22/06/1944,\"1 Infinite Loop\r\nCupertino, CA 95014\r\nUnited States\","
118 		+ "Y,,\"\"\"Open the pod bay doors, HAL.\"\" - 2001: A Space Odyssey\",sergei@denisovich.com,229431";
119 	public static final CustomerBean SERGEI = new CustomerBean("8", "Sergei", "Denisovich", date(1944, 6, 22),
120 		"1 Infinite Loop\nCupertino, CA 95014\nUnited States", true, null,
121 		"\"Open the pod bay doors, HAL.\" - 2001: A Space Odyssey", "sergei@denisovich.com", 229431L);
122 	public static final CustomerStringBean SERGEI_STRING = new CustomerStringBean(SERGEI);
123 	
124 	public static final String LARRY_CSV = "9,Larry,Ballmer,01/01/1901,\"1-7-1 Konan\r\nMinato-ku\r\nTokyo, 108-0075\r\nJapan\","
125 		+ "N,0,\"\"\"A martini. Shaken, not stirred.\"\" - Goldfinger\",lazza99@gmail.com,164";
126 	public static final CustomerBean LARRY = new CustomerBean("9", "Larry", "Ballmer", date(1901, 1, 1),
127 		"1-7-1 Konan\nMinato-ku\nTokyo, 108-0075\nJapan", false, 0, "\"A martini. Shaken, not stirred.\" - Goldfinger",
128 		"lazza99@gmail.com", 164L);
129 	public static final CustomerStringBean LARRY_STRING = new CustomerStringBean(LARRY);
130 	
131 	public static final String GRACE_CSV = "10,Grace,Fowler,28/11/2003,\"11-1, Kamitoba Hokotate-cho\r\nMinami-ku\r\nKyoto, 601-8501\r\nJapan\","
132 		+ "N,0,\"\"\"Carpe diem. Seize the day, boys. Make your lives extraordinary.\"\" - Dead Poets Society\",gracie@hotmail.com,168841";
133 	public static final CustomerBean GRACE = new CustomerBean("10", "Grace", "Fowler", date(2003, 11, 28),
134 		"11-1, Kamitoba Hokotate-cho\nMinami-ku\nKyoto, 601-8501\nJapan", false, 0,
135 		"\"Carpe diem. Seize the day, boys. Make your lives extraordinary.\" - Dead Poets Society",
136 		"gracie@hotmail.com", 168841L);
137 	public static final CustomerStringBean GRACE_STRING = new CustomerStringBean(GRACE);
138 	
139 	
140 
141 
142 	public static final String CSV_FILE = new StringBuilder(HEADER_CSV).append("\r\n").append(JOHN_CSV).append("\r\n")
143 		.append(BOB_CSV).append("\r\n").append(ALICE_CSV).append("\r\n").append(BILL_CSV).append("\r\n")
144 		.append(MIRANDA_CSV).append("\r\n").append(STEVE_CSV).append("\r\n").append(ADA_CSV).append("\r\n")
145 		.append(SERGEI_CSV).append("\r\n").append(LARRY_CSV).append("\r\n").append(GRACE_CSV).append("\r\n").toString();
146 	
147 	
148 	public static final List<CustomerBean> CUSTOMERS = Arrays.asList(JOHN, BOB, ALICE, BILL, MIRANDA, STEVE, ADA,
149 		SERGEI, LARRY, GRACE);
150 	
151 	
152 	public static final List<CustomerStringBean> STRING_CUSTOMERS = Arrays.asList(JOHN_STRING, BOB_STRING,
153 		ALICE_STRING, BILL_STRING, MIRANDA_STRING, STEVE_STRING, ADA_STRING, SERGEI_STRING, LARRY_STRING, GRACE_STRING);
154 	
155 	
156 
157 
158 
159 
160 
161 
162 
163 
164 
165 
166 	public static Date date(final int year, final int month, final int dayOfMonth) {
167 		final Calendar cal = Calendar.getInstance();
168 		cal.setLenient(false);
169 		cal.set(year, month - 1, dayOfMonth, 0, 0, 0);
170 		cal.set(Calendar.MILLISECOND, 0);
171 		return cal.getTime();
172 	}
173 	
174 	
175 
176 
177 
178 
179 
180 
181 
182 
183 
184 
185 
186 
187 
188 
189 
190 
191 	public static Date date(final int year, final int month, final int dayOfMonth, final int hour, final int minute,
192 		final int second) {
193 		final Calendar cal = Calendar.getInstance();
194 		cal.setLenient(false);
195 		cal.set(year, month - 1, dayOfMonth, hour, minute, second);
196 		cal.set(Calendar.MILLISECOND, 0);
197 		return cal.getTime();
198 	}
199 	
200 	
201 
202 
203 
204 
205 
206 
207 
208 
209 
210 	public static void assertExecution(final CellProcessor processor, final String input, final Object expectedOutput) {
211 		assertEquals(expectedOutput, processor.execute(input, ANONYMOUS_CSVCONTEXT));
212 	}
213 	
214 }