KickJava   Java API By Example, From Geeks To Geeks.

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


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

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

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

57
58     protected void setUp()
59     throws Exception JavaDoc
60     {
61         super.setUp();
62
63         config.setInitParameter("properties",
64                                 "/WEB-INF/conf/TurbineComplete.properties");
65         turbine = new Turbine();
66         turbine.init(config);
67     }
68
69     /**
70      * Shut down our turbine servlet and let our parents clean up also.
71      *
72      * @exception Exception if an error occurs
73      */

74     protected void tearDown()
75     throws Exception JavaDoc
76     {
77         turbine.destroy();
78         super.tearDown();
79     }
80
81     /**
82      * Return a test suite of all our tests.
83      *
84      * @return a <code>Test</code> value
85      */

86     public static Test suite()
87     {
88         return new TestSuite(CSVParserTest.class);
89     }
90
91     /**
92      * Tests if you can leave field values empty
93      */

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

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

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