KickJava   Java API By Example, From Geeks To Geeks.

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


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 import org.apache.cactus.ServletTestCase;
25 import org.apache.turbine.Turbine;
26
27
28 /**
29  * Test the TSVParser.
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: TSVParserTest.java,v 1.2.2.2 2004/05/20 03:33:44 seade Exp $
37  */

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

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

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

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

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

89     public static Test suite()
90     {
91         return new TestSuite(TSVParserTest.class);
92     }
93
94     /**
95      * Tests if you can leave field values empty
96      */

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

135     public void testNormalFieldValues()
136     {
137         String JavaDoc values = "field1\tfield2\tfield3\tfield4\nvalue11\tvalue12\tvalue13\tvalue14\nvalue21\tvalue22\tvalue23\tvalue24";
138         CharArrayReader JavaDoc reader = new CharArrayReader JavaDoc(values.toCharArray());
139         TSVParser parser = new TSVParser(reader);
140         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
141         try
142         {
143             parser.readColumnNames();
144             int currentRecord = 1;
145             while (parser.hasNextRow())
146             {
147                 ValueParser vp = parser.nextRow();
148                 int currentField = 1;
149                 while (currentField <= 4)
150                 {
151                     assertEquals("value" + currentRecord + currentField, vp.getString("field" + currentField));
152                     currentField += 1;
153                 }
154                 currentRecord += 1;
155             }
156         }
157         catch (IOException JavaDoc ioe)
158         {
159             fail("Unexpected exception in testcase occured : " + ioe.toString());
160         }
161     }
162
163     /**
164      * Tests if some fields are empty, but the values exists..
165      */

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