KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > turbine > util > CSVParserTest


1 package org.apache.turbine.util;
2
3 /*
4  * Copyright 2001-2004 The Apache Software Foundation.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License")
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19 import java.io.IOException JavaDoc;
20 import java.io.CharArrayReader JavaDoc;
21 import java.io.StringBufferInputStream JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.Vector JavaDoc;
24
25 import org.apache.turbine.Turbine;
26
27 import org.apache.cactus.ServletTestCase;
28 import junit.framework.Test;
29 import junit.framework.TestSuite;
30
31 /**
32  * Test the CSVParser.
33  *
34  * NOTE : I am assuming (as is in the code of DataStreamParser.java
35  * that the values are reusing the same object for the values.
36  * If this shouldn't be, we need to fix that in the code!.
37  *
38  * @author <a HREF="mailto:martin@mvdb.net">Martin van den Bemt</a>
39  * @version $Id: CSVParserTest.java,v 1.1.4.2 2004/05/20 03:30:06 seade Exp $
40  */

41 public class CSVParserTest
42             extends ServletTestCase
43 {
44
45     Turbine turbine = null;
46
47     /**
48      * Constructor for CSVParserTest.
49      * @param arg0
50      */

51     public CSVParserTest(String JavaDoc name)
52     {
53         super(name);
54     }
55
56     /**
57      * This will setup an instance of turbine to use when testing
58      * @exception if an exception occurs.
59      */

60
61     protected void setUp()
62     throws Exception JavaDoc
63     {
64         super.setUp();
65         /* Note: we are using the properties file from the cache test
66          * since we don't really need any specific property at this
67          * time. Future tests may require a test case specific
68          * properties file to be used.:
69          */

70         config.setInitParameter("properties",
71                                 "/WEB-INF/conf/TurbineDefault.properties");
72         turbine = new Turbine();
73         turbine.init(config);
74     }
75
76     /**
77      * Shut down our turbine servlet and let our parents clean up also.
78      *
79      * @exception Exception if an error occurs
80      */

81     protected void tearDown()
82     throws Exception JavaDoc
83     {
84         turbine.destroy();
85         super.tearDown();
86     }
87
88     /**
89      * Return a test suite of all our tests.
90      *
91      * @return a <code>Test</code> value
92      */

93     public static Test suite()
94     {
95         return new TestSuite(CSVParserTest.class);
96     }
97
98     /**
99      * Tests if you can leave field values empty
100      */

101     public void testEmptyFieldValues()
102     {
103         String JavaDoc values = "field1,field2,field3,field4\nvalue11,,value13,\nvalue21,,value23,";
104         CharArrayReader JavaDoc reader = new CharArrayReader JavaDoc(values.toCharArray());
105         CSVParser parser = new CSVParser(reader);
106         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
107         try
108         {
109             parser.readColumnNames();
110             int currentRecord = 1;
111             while (parser.hasNextRow())
112             {
113                 ValueParser vp = parser.nextRow();
114                 int currentField = 1;
115                 while (currentField <= 4)
116                 {
117                     if (currentField == 2 || currentField == 4)
118                     {
119                         assertNull(vp.getString("field" + currentField));
120                     }
121                     else
122                     {
123                         assertEquals("value" + currentRecord + currentField, vp.getString("field" + currentField));
124                     }
125                     currentField += 1;
126                 }
127                 currentRecord += 1;
128             }
129         }
130         catch (IOException JavaDoc ioe)
131         {
132             fail("Unexpected exception in testcase occured : " + ioe.toString());
133         }
134     }
135
136     /**
137      * Tests if normal operation is still working
138      */

139     public void testNormalFieldValues()
140     {
141         String JavaDoc values = "field1,field2,field3,field4\nvalue11,value12,value13,value14\nvalue21,value22,value23,value24";
142         CharArrayReader JavaDoc reader = new CharArrayReader JavaDoc(values.toCharArray());
143         CSVParser parser = new CSVParser(reader);
144         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
145         try
146         {
147             parser.readColumnNames();
148             int currentRecord = 1;
149             while (parser.hasNextRow())
150             {
151                 ValueParser vp = parser.nextRow();
152                 int currentField = 1;
153                 while (currentField <= 4)
154                 {
155                     assertEquals("value" + currentRecord + currentField, vp.getString("field" + currentField));
156                     currentField += 1;
157                 }
158                 currentRecord += 1;
159             }
160         }
161         catch (IOException JavaDoc ioe)
162         {
163             fail("Unexpected exception in testcase occured : " + ioe.toString());
164         }
165     }
166
167     /**
168      * Tests if some fields are empty, but the values exists..
169      */

170     public void testEmptyFieldNames()
171     {
172         String JavaDoc values = "field1,,field3,\nvalue11,value12,value13,value14\nvalue21,value22,value23,value24";
173         CharArrayReader JavaDoc reader = new CharArrayReader JavaDoc(values.toCharArray());
174         CSVParser parser = new CSVParser(reader);
175         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
176         try
177         {
178             parser.readColumnNames();
179             int currentRecord = 1;
180
181             while (parser.hasNextRow())
182             {
183                 ValueParser vp = parser.nextRow();
184                 int currentField = 1;
185                 while (currentField <= 4)
186                 {
187                     if (currentField == 2 || currentField == 4)
188                     {
189                         assertEquals("value" + currentRecord + currentField,
190                                      vp.getString(DataStreamParser.EMPTYFIELDNAME + currentField));
191                     }
192                     else
193                     {
194                         assertEquals("value" + currentRecord + currentField,
195                                      vp.getString("field" + currentField));
196                     }
197                     currentField += 1;
198                 }
199                 currentRecord += 1;
200             }
201         }
202         catch (IOException JavaDoc ioe)
203         {
204             fail("Unexpected exception in testcase occured : " + ioe.toString());
205         }
206     }
207 }
208
Popular Tags