View Javadoc
1   /*
2    *
3    * The DbUnit Database Testing Framework
4    * Copyright (C)2002-2004, DbUnit.org
5    *
6    * This library is free software; you can redistribute it and/or
7    * modify it under the terms of the GNU Lesser General Public
8    * License as published by the Free Software Foundation; either
9    * version 2.1 of the License, or (at your option) any later version.
10   *
11   * This library is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   * Lesser General Public License for more details.
15   *
16   * You should have received a copy of the GNU Lesser General Public
17   * License along with this library; if not, write to the Free Software
18   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19   *
20   */
21  
22  package org.dbunit.dataset.common.handlers;
23  
24  import java.util.ArrayList;
25  import java.util.Iterator;
26  import java.util.List;
27  
28  import junit.framework.TestCase;
29  
30  public class HandlersTest extends TestCase {
31  
32      Pipeline pipeline;
33  
34      public void testEmptyFields() throws IllegalInputCharacterException, PipelineException {
35          pipeline.putFront(SeparatorHandler.ENDPIECE());
36          pipeline.putFront(WhitespacesHandler.IGNORE());
37          pipeline.putFront(TransparentHandler.IGNORE());
38  
39          String words = ",, ,";
40  
41          feed (pipeline, words);
42  
43          assertEquals(4, pipeline.getProducts().size());
44  
45          for (int i = 0; i < pipeline.getProducts().size(); i++) {
46              assertEquals("", pipeline.getProducts().get(i).toString());
47          }
48      }
49  
50      public void testUnquotedFieldsParser() throws IllegalInputCharacterException, PipelineException {
51  
52          pipeline.putFront(SeparatorHandler.ENDPIECE());
53          pipeline.putFront(IsAlnumHandler.QUOTE());
54          pipeline.putFront(WhitespacesHandler.IGNORE());
55          pipeline.putFront(TransparentHandler.IGNORE());
56  
57          String words = "Today: Hello , World!";
58  
59          feed (pipeline, words);
60  
61          assertEquals(2, pipeline.getProducts().size());
62          assertEquals("Today: Hello ", pipeline.getProducts().get(0));
63          assertEquals("World!", pipeline.getProducts().get(1));
64      }
65  
66      public void testQuotedFieldWithEscapedCharacterAssembler () throws PipelineException, IllegalInputCharacterException {
67          pipeline.putFront(SeparatorHandler.ENDPIECE());
68          pipeline.putFront(IsAlnumHandler.ACCEPT());
69          pipeline.putFront(WhitespacesHandler.IGNORE());
70          pipeline.putFront(QuoteHandler.QUOTE());
71  
72          String words = " \"Hello, \\\"World!\" ";
73  
74          feed (pipeline, words);
75  
76          assertEquals(1, pipeline.getProducts().size());
77          assertEquals("Hello, \"World!", pipeline.getProducts().get(0).toString());
78      }
79  
80      public void testUnquotedFieldWithEscapedCharacterAssembler () throws PipelineException, IllegalInputCharacterException {
81          pipeline.putFront(SeparatorHandler.ENDPIECE());
82          pipeline.putFront(EscapeHandler.ACCEPT());
83          pipeline.putFront(IsAlnumHandler.QUOTE());
84          pipeline.putFront(WhitespacesHandler.IGNORE());
85          pipeline.putFront(TransparentHandler.IGNORE());
86  
87          String words = "Hello \\\"World!";
88  
89          feed (pipeline, words);
90  
91          assertEquals(1, pipeline.getProducts().size());
92          assertEquals("Hello \\\"World!", pipeline.getProducts().get(0).toString());
93      }
94  
95      public void testEscapedFieldAssembler () throws PipelineException, IllegalInputCharacterException {
96          String words = "\"He\"llo, \"World, !\", \\\"St. James O\"Connor";
97  
98          pipeline.putFront(SeparatorHandler.ENDPIECE());
99          pipeline.putFront(EscapeHandler.ACCEPT());
100         pipeline.putFront(IsAlnumHandler.QUOTE());
101         pipeline.putFront(QuoteHandler.QUOTE());
102         pipeline.putFront(EscapeHandler.ESCAPE());
103         pipeline.putFront(WhitespacesHandler.IGNORE());
104         pipeline.putFront(TransparentHandler.IGNORE());
105 
106         feed (pipeline, words);
107 
108         assertEquals(3, pipeline.getProducts().size());
109         assertEquals("Hello", pipeline.getProducts().get(0));
110         assertEquals("World, !", pipeline.getProducts().get(1));
111         assertEquals("\"St. James O\"Connor", pipeline.getProducts().get(2));
112     }
113 
114     private void dump(List products) {
115         Iterator it = products.iterator();
116         int i = 0;
117         while (it.hasNext()) {
118             System.out.println(i++ + ": " + it.next());
119         }
120     }
121 
122     private void feed(Pipeline pipeline, String words) throws PipelineException, IllegalInputCharacterException {
123         for (int i = 0; i < words.length(); i++) {
124             pipeline.handle(words.toCharArray()[i]);
125         }
126         pipeline.thePieceIsDone();
127     }
128 
129     public void testQuotedFieldAssembler() throws IllegalInputCharacterException, PipelineException {
130         pipeline.putFront(SeparatorHandler.ENDPIECE());
131         pipeline.putFront(IsAlnumHandler.ACCEPT());
132         pipeline.putFront(WhitespacesHandler.IGNORE());
133         pipeline.putFront(QuoteHandler.QUOTE());
134 
135         String words = " \"Hello, World!\" ";
136 
137         feed (pipeline, words);
138 
139         assertEquals(1, pipeline.getProducts().size());
140         assertEquals("Hello, World!", pipeline.getProducts().get(0).toString());
141     }
142 
143     public void testQuotedFieldsParser() throws IllegalInputCharacterException, PipelineException {
144         pipeline.putFront(SeparatorHandler.ENDPIECE());
145         pipeline.putFront(IsAlnumHandler.QUOTE());
146         pipeline.putFront(WhitespacesHandler.IGNORE());
147         pipeline.putFront(QuoteHandler.QUOTE());
148         pipeline.putFront(TransparentHandler.IGNORE());
149 
150         String words = "\"Hello\", \"oh my\", \"ehm. oh yeah. World!\", \" craa azy \"";
151 
152         feed (pipeline, words);
153 
154         assertEquals(4, pipeline.getProducts().size());
155 
156         List expected = new ArrayList();
157         expected.add("Hello");
158         expected.add("oh my");
159         expected.add("ehm. oh yeah. World!");
160         expected.add(" craa azy ");
161 
162         List got = new ArrayList();
163 
164         for (int i = 0; i < pipeline.getProducts().size(); i++) {
165             got.add(pipeline.getProducts().get(i).toString());
166         }
167 
168         assertEquals(expected, got);
169 
170         assertEquals("Hello", pipeline.getProducts().get(0).toString());
171         assertEquals("oh my", pipeline.getProducts().get(1).toString());
172         assertEquals("ehm. oh yeah. World!", pipeline.getProducts().get(2).toString());
173         assertEquals(" craa azy ", pipeline.getProducts().get(3).toString());
174 
175     }
176 
177     private void acceptHelper(String toAccept, Handler component) throws IllegalInputCharacterException, PipelineException {
178         for (int i = 0; i < toAccept.length(); i++) {
179             char c = toAccept.charAt(i);
180             assertTrue(c + " should be accepted", component.canHandle(c));
181             // Handle
182             component.handle(c);
183         }
184 
185     }
186 
187     /**
188      * Test the handling of a sequence of empty, unquoted and quoted fields
189      * @throws IllegalInputCharacterException
190      * @throws PipelineException
191      */
192     public void testEmptyQuotedAndUnquotedFieldsParser() throws IllegalInputCharacterException, PipelineException {
193 
194         String words = " , \\\\John \"Fox , \"St. Moritz, 2\" , \\\\, \\\"Steve Wolf, \" \\\"Night & Day\\\", \\\"2nd\\\" edition \", , Again Here, \"and there, of\"";
195 
196         pipeline.putFront(SeparatorHandler.ENDPIECE());
197         pipeline.putFront(EscapeHandler.ACCEPT());
198         pipeline.putFront(IsAlnumHandler.QUOTE());
199         pipeline.putFront(QuoteHandler.QUOTE());
200         pipeline.putFront(EscapeHandler.ESCAPE());
201         pipeline.putFront(WhitespacesHandler.IGNORE());
202         pipeline.putFront(TransparentHandler.IGNORE());
203 
204         feed (pipeline, words);
205 
206         //dump(pipeline.getProducts());
207 
208         assertEquals(9, pipeline.getProducts().size());
209         assertEquals("", pipeline.getProducts().get(0).toString());
210         assertEquals("\\John \"Fox ", pipeline.getProducts().get(1).toString());
211         assertEquals("St. Moritz, 2", pipeline.getProducts().get(2).toString());
212         assertEquals("\\", pipeline.getProducts().get(3).toString());
213 
214         assertEquals("\"Steve Wolf", pipeline.getProducts().get(4).toString());
215         assertEquals(" \"Night & Day\", \"2nd\" edition ", pipeline.getProducts().get(5).toString());
216         assertEquals("", pipeline.getProducts().get(6).toString());
217         assertEquals("Again Here", pipeline.getProducts().get(7).toString());
218         assertEquals("and there, of", pipeline.getProducts().get(8).toString());
219     }
220 
221     private void doNotAcceptHelper(String toAccept, Handler component) throws IllegalInputCharacterException, PipelineException {
222         for (int i = 0; i < toAccept.length(); i++) {
223             char c = toAccept.charAt(i);
224             assertFalse(c + " should not be accepted", component.canHandle(c));
225         }
226     }
227 
228     public void testEscapeHandler () throws PipelineException, IllegalInputCharacterException {
229         String accepted = "\\\"";
230 
231         EscapeHandler escapeHandler = (EscapeHandler) EscapeHandler.ESCAPE();
232         pipeline.putFront(escapeHandler);
233         acceptHelper(accepted, pipeline);
234         assertEquals("\"", pipeline.getCurrentProduct().toString());
235     }
236 
237     public void testWhitespaceHandler() throws Exception {
238 
239         String accepted = " \t";
240 
241         PipelineComponent acceptHandler = WhitespacesHandler.ACCEPT();
242         pipeline.putFront(acceptHandler);
243         acceptHelper(accepted, acceptHandler);
244         acceptHelper(accepted, WhitespacesHandler.IGNORE());
245 
246         assertEquals(accepted, pipeline.getCurrentProduct().toString());
247 
248     }
249 
250 
251     public void testUnquotedHandler() throws IllegalInputCharacterException, PipelineException {
252         String accepted = "_1234567890abcdefghilmnopqrstuvzxywjABCDEFGHILMNOPQRSTUVZXYWJ()/&%$|-_.:;+*<>";
253         String notAccepted = " \t\\";
254 
255         PipelineComponent acceptHandler = IsAlnumHandler.ACCEPT();
256         pipeline.putFront(acceptHandler);
257         acceptHelper(accepted, acceptHandler);
258         
259         PipelineComponent ignoreHandler = IsAlnumHandler.IGNORE();
260         pipeline.putFront(ignoreHandler);
261         acceptHelper(accepted, ignoreHandler);
262 
263         doNotAcceptHelper(notAccepted, acceptHandler);
264         doNotAcceptHelper(notAccepted, ignoreHandler);
265 
266         assertEquals(accepted, pipeline.getCurrentProduct().toString());
267     }
268 
269 
270     protected void setUp() throws Exception {
271         pipeline = new Pipeline();
272     }
273 
274 }