1 21 22 package org.dbunit.dataset.csv.handlers; 23 24 import junit.framework.TestCase; 25 import org.dbunit.dataset.csv.IllegalInputCharacterException; 26 27 import java.util.ArrayList ; 28 import java.util.Iterator ; 29 import java.util.List ; 30 31 public class HandlersTest extends TestCase { 32 33 Pipeline pipeline; 34 35 public void testEmptyFields() throws IllegalInputCharacterException, PipelineException { 36 pipeline.putFront(SeparatorHandler.ENDPIECE()); 37 pipeline.putFront(WhitespacesHandler.IGNORE()); 38 pipeline.putFront(TransparentHandler.IGNORE()); 39 40 String words = ",, ,"; 41 42 feed (pipeline, words); 43 44 assertEquals(4, pipeline.getProducts().size()); 45 46 for (int i = 0; i < pipeline.getProducts().size(); i++) { 47 assertEquals("", pipeline.getProducts().get(i).toString()); 48 } 49 } 50 51 public void testUnquotedFieldsParser() throws IllegalInputCharacterException, PipelineException { 52 53 pipeline.putFront(SeparatorHandler.ENDPIECE()); 54 pipeline.putFront(IsAlnumHandler.QUOTE()); 55 pipeline.putFront(WhitespacesHandler.IGNORE()); 56 pipeline.putFront(TransparentHandler.IGNORE()); 57 58 String words = "Today: Hello , World!"; 59 60 feed (pipeline, words); 61 62 assertEquals(2, pipeline.getProducts().size()); 63 assertEquals("Today: Hello ", pipeline.getProducts().get(0)); 64 assertEquals("World!", pipeline.getProducts().get(1)); 65 } 66 67 public void testQuotedFieldWithEscapedCharacterAssembler () throws PipelineException, IllegalInputCharacterException { 68 pipeline.putFront(SeparatorHandler.ENDPIECE()); 69 pipeline.putFront(IsAlnumHandler.ACCEPT()); 70 pipeline.putFront(WhitespacesHandler.IGNORE()); 71 pipeline.putFront(QuoteHandler.QUOTE()); 72 73 String words = " \"Hello, \\\"World!\" "; 74 75 feed (pipeline, words); 76 77 assertEquals(1, pipeline.getProducts().size()); 78 assertEquals("Hello, \"World!", pipeline.getProducts().get(0).toString()); 79 } 80 81 public void testUnquotedFieldWithEscapedCharacterAssembler () throws PipelineException, IllegalInputCharacterException { 82 pipeline.putFront(SeparatorHandler.ENDPIECE()); 83 pipeline.putFront(EscapeHandler.ACCEPT()); 84 pipeline.putFront(IsAlnumHandler.QUOTE()); 85 pipeline.putFront(WhitespacesHandler.IGNORE()); 86 pipeline.putFront(TransparentHandler.IGNORE()); 87 88 String words = "Hello \\\"World!"; 89 90 feed (pipeline, words); 91 92 assertEquals(1, pipeline.getProducts().size()); 93 assertEquals("Hello \\\"World!", pipeline.getProducts().get(0).toString()); 94 } 95 96 public void testEscapedFieldAssembler () throws PipelineException, IllegalInputCharacterException { 97 String words = "\"He\"llo, \"World, !\", \\\"St. James O\"Connor"; 98 99 pipeline.putFront(SeparatorHandler.ENDPIECE()); 100 pipeline.putFront(EscapeHandler.ACCEPT()); 101 pipeline.putFront(IsAlnumHandler.QUOTE()); 102 pipeline.putFront(QuoteHandler.QUOTE()); 103 pipeline.putFront(EscapeHandler.ESCAPE()); 104 pipeline.putFront(WhitespacesHandler.IGNORE()); 105 pipeline.putFront(TransparentHandler.IGNORE()); 106 107 feed (pipeline, words); 108 109 assertEquals(3, pipeline.getProducts().size()); 110 assertEquals("Hello", pipeline.getProducts().get(0)); 111 assertEquals("World, !", pipeline.getProducts().get(1)); 112 assertEquals("\"St. James O\"Connor", pipeline.getProducts().get(2)); 113 } 114 115 private void dump(List products) { 116 Iterator it = products.iterator(); 117 int i = 0; 118 while (it.hasNext()) { 119 System.out.println(i++ + ": " + it.next()); 120 } 121 } 122 123 private void feed(Pipeline pipeline, String words) throws PipelineException, IllegalInputCharacterException { 124 for (int i = 0; i < words.length(); i++) { 125 pipeline.handle(words.toCharArray()[i]); 126 } 127 pipeline.thePieceIsDone(); 128 } 129 130 public void testQuotedFieldAssembler() throws IllegalInputCharacterException, PipelineException { 131 pipeline.putFront(SeparatorHandler.ENDPIECE()); 132 pipeline.putFront(IsAlnumHandler.ACCEPT()); 133 pipeline.putFront(WhitespacesHandler.IGNORE()); 134 pipeline.putFront(QuoteHandler.QUOTE()); 135 136 String words = " \"Hello, World!\" "; 137 138 feed (pipeline, words); 139 140 assertEquals(1, pipeline.getProducts().size()); 141 assertEquals("Hello, World!", pipeline.getProducts().get(0).toString()); 142 } 143 144 public void testQuotedFieldsParser() throws IllegalInputCharacterException, PipelineException { 145 pipeline.putFront(SeparatorHandler.ENDPIECE()); 146 pipeline.putFront(IsAlnumHandler.QUOTE()); 147 pipeline.putFront(WhitespacesHandler.IGNORE()); 148 pipeline.putFront(QuoteHandler.QUOTE()); 149 pipeline.putFront(TransparentHandler.IGNORE()); 150 151 String words = "\"Hello\", \"oh my\", \"ehm. oh yeah. World!\", \" craa azy \""; 152 153 feed (pipeline, words); 154 155 assertEquals(4, pipeline.getProducts().size()); 156 157 List expected = new ArrayList (); 158 expected.add("Hello"); 159 expected.add("oh my"); 160 expected.add("ehm. oh yeah. World!"); 161 expected.add(" craa azy "); 162 163 List got = new ArrayList (); 164 165 for (int i = 0; i < pipeline.getProducts().size(); i++) { 166 got.add(pipeline.getProducts().get(i).toString()); 167 } 168 169 assertEquals(expected, got); 170 171 assertEquals("Hello", pipeline.getProducts().get(0).toString()); 172 assertEquals("oh my", pipeline.getProducts().get(1).toString()); 173 assertEquals("ehm. oh yeah. World!", pipeline.getProducts().get(2).toString()); 174 assertEquals(" craa azy ", pipeline.getProducts().get(3).toString()); 175 176 } 177 178 private void acceptHelper(String toAccept, Handler component) throws IllegalInputCharacterException, PipelineException { 179 for (int i = 0; i < toAccept.length(); i++) { 180 char c = toAccept.charAt(i); 181 assertTrue(c + " should be accepted", component.canHandle(c)); 182 component.handle(c); 184 } 185 186 } 187 188 193 public void testEmptyQuotedAndUnquotedFieldsParser() throws IllegalInputCharacterException, PipelineException { 194 195 String words = " , \\\\John \"Fox , \"St. Moritz, 2\" , \\\\, \\\"Steve Wolf, \" \\\"Night & Day\\\", \\\"2nd\\\" edition \", , Again Here, \"and there, of\""; 196 197 pipeline.putFront(SeparatorHandler.ENDPIECE()); 198 pipeline.putFront(EscapeHandler.ACCEPT()); 199 pipeline.putFront(IsAlnumHandler.QUOTE()); 200 pipeline.putFront(QuoteHandler.QUOTE()); 201 pipeline.putFront(EscapeHandler.ESCAPE()); 202 pipeline.putFront(WhitespacesHandler.IGNORE()); 203 pipeline.putFront(TransparentHandler.IGNORE()); 204 205 feed (pipeline, words); 206 207 209 assertEquals(9, pipeline.getProducts().size()); 210 assertEquals("", pipeline.getProducts().get(0).toString()); 211 assertEquals("\\John \"Fox ", pipeline.getProducts().get(1).toString()); 212 assertEquals("St. Moritz, 2", pipeline.getProducts().get(2).toString()); 213 assertEquals("\\", pipeline.getProducts().get(3).toString()); 214 215 assertEquals("\"Steve Wolf", pipeline.getProducts().get(4).toString()); 216 assertEquals(" \"Night & Day\", \"2nd\" edition ", pipeline.getProducts().get(5).toString()); 217 assertEquals("", pipeline.getProducts().get(6).toString()); 218 assertEquals("Again Here", pipeline.getProducts().get(7).toString()); 219 assertEquals("and there, of", pipeline.getProducts().get(8).toString()); 220 } 221 222 private void doNotAcceptHelper(String toAccept, Handler component) throws IllegalInputCharacterException, PipelineException { 223 for (int i = 0; i < toAccept.length(); i++) { 224 char c = toAccept.charAt(i); 225 assertFalse(c + " should not be accepted", component.canHandle(c)); 226 } 227 } 228 229 public void testEscapeHandler () throws PipelineException, IllegalInputCharacterException { 230 String accepted = "\\\""; 231 232 EscapeHandler escapeHandler = (EscapeHandler) EscapeHandler.ESCAPE(); 233 pipeline.putFront(escapeHandler); 234 acceptHelper(accepted, pipeline); 235 assertEquals("\"", pipeline.getCurrentProduct().toString()); 236 } 237 238 public void testWhitespaceHandler() throws Exception { 239 240 String accepted = " \t"; 241 242 PipelineComponent acceptHandler = WhitespacesHandler.ACCEPT(); 243 pipeline.putFront(acceptHandler); 244 acceptHelper(accepted, acceptHandler); 245 acceptHelper(accepted, WhitespacesHandler.IGNORE()); 246 247 assertEquals(accepted, pipeline.getCurrentProduct().toString()); 248 249 } 250 251 252 public void testUnquotedHandler() throws IllegalInputCharacterException, PipelineException { 253 String accepted = "_1234567890abcdefghilmnopqrstuvzxywjABCDEFGHILMNOPQRSTUVZXYWJ()/&%$£|-_.:;+*<>"; 254 String notAccepted = " \t\\"; 255 256 PipelineComponent acceptHandler = IsAlnumHandler.ACCEPT(); 257 pipeline.putFront(acceptHandler); 258 acceptHelper(accepted, acceptHandler); 259 acceptHelper(accepted, IsAlnumHandler.IGNORE()); 260 261 doNotAcceptHelper(notAccepted, acceptHandler); 262 doNotAcceptHelper(notAccepted, IsAlnumHandler.IGNORE()); 263 264 assertEquals(accepted, pipeline.getCurrentProduct().toString()); 265 } 266 267 268 protected void setUp() throws Exception { 269 pipeline = new Pipeline(); 270 } 271 272 } 273 | Popular Tags |