001: package org.apache.turbine.util;
002:
003: /*
004: * Copyright 2001-2005 The Apache Software Foundation.
005: *
006: * Licensed under the Apache License, Version 2.0 (the "License")
007: * you may not use this file except in compliance with the License.
008: * You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */
018:
019: import java.io.IOException;
020: import java.io.CharArrayReader;
021: import java.io.StringBufferInputStream;
022: import java.util.Iterator;
023: import java.util.Vector;
024:
025: import org.apache.turbine.Turbine;
026:
027: import org.apache.cactus.ServletTestCase;
028: import junit.framework.Test;
029: import junit.framework.TestSuite;
030:
031: /**
032: * Test the CSVParser.
033: *
034: * NOTE : I am assuming (as is in the code of DataStreamParser.java
035: * that the values are reusing the same object for the values.
036: * If this shouldn't be, we need to fix that in the code!.
037: *
038: * @author <a href="mailto:martin@mvdb.net">Martin van den Bemt</a>
039: * @version $Id: CSVParserTest.java 264148 2005-08-29 14:21:04Z henning $
040: */
041: public class CSVParserTest extends ServletTestCase {
042:
043: Turbine turbine = null;
044:
045: /**
046: * Constructor for CSVParserTest.
047: * @param arg0
048: */
049: public CSVParserTest(String name) {
050: super (name);
051: }
052:
053: /**
054: * This will setup an instance of turbine to use when testing
055: * @exception if an exception occurs.
056: */
057:
058: protected void setUp() throws Exception {
059: super .setUp();
060: /* Note: we are using the properties file from the cache test
061: * since we don't really need any specific property at this
062: * time. Future tests may require a test case specific
063: * properties file to be used.:
064: */
065: config.setInitParameter("properties",
066: "/WEB-INF/conf/TurbineDefault.properties");
067: turbine = new Turbine();
068: turbine.init(config);
069: }
070:
071: /**
072: * Shut down our turbine servlet and let our parents clean up also.
073: *
074: * @exception Exception if an error occurs
075: */
076: protected void tearDown() throws Exception {
077: turbine.destroy();
078: super .tearDown();
079: }
080:
081: /**
082: * Return a test suite of all our tests.
083: *
084: * @return a <code>Test</code> value
085: */
086: public static Test suite() {
087: return new TestSuite(CSVParserTest.class);
088: }
089:
090: /**
091: * Tests if you can leave field values empty
092: */
093: public void testEmptyFieldValues() {
094: String values = "field1,field2,field3,field4\nvalue11,,value13,\nvalue21,,value23,";
095: CharArrayReader reader = new CharArrayReader(values
096: .toCharArray());
097: CSVParser parser = new CSVParser(reader);
098: StringBuffer sb = new StringBuffer();
099: try {
100: parser.readColumnNames();
101: int currentRecord = 1;
102: while (parser.hasNextRow()) {
103: ValueParser vp = parser.nextRow();
104: int currentField = 1;
105: while (currentField <= 4) {
106: if (currentField == 2 || currentField == 4) {
107: assertNull(vp.getString("field" + currentField));
108: } else {
109: assertEquals("value" + currentRecord
110: + currentField, vp.getString("field"
111: + currentField));
112: }
113: currentField += 1;
114: }
115: currentRecord += 1;
116: }
117: } catch (IOException ioe) {
118: fail("Unexpected exception in testcase occured : "
119: + ioe.toString());
120: }
121: }
122:
123: /**
124: * Tests if normal operation is still working
125: */
126: public void testNormalFieldValues() {
127: String values = "field1,field2,field3,field4\nvalue11,value12,value13,value14\nvalue21,value22,value23,value24";
128: CharArrayReader reader = new CharArrayReader(values
129: .toCharArray());
130: CSVParser parser = new CSVParser(reader);
131: StringBuffer sb = new StringBuffer();
132: try {
133: parser.readColumnNames();
134: int currentRecord = 1;
135: while (parser.hasNextRow()) {
136: ValueParser vp = parser.nextRow();
137: int currentField = 1;
138: while (currentField <= 4) {
139: assertEquals(
140: "value" + currentRecord + currentField, vp
141: .getString("field" + currentField));
142: currentField += 1;
143: }
144: currentRecord += 1;
145: }
146: } catch (IOException ioe) {
147: fail("Unexpected exception in testcase occured : "
148: + ioe.toString());
149: }
150: }
151:
152: /**
153: * Tests if some fields are empty, but the values exists..
154: */
155: public void testEmptyFieldNames() {
156: String values = "field1,,field3,\nvalue11,value12,value13,value14\nvalue21,value22,value23,value24";
157: CharArrayReader reader = new CharArrayReader(values
158: .toCharArray());
159: CSVParser parser = new CSVParser(reader);
160: StringBuffer sb = new StringBuffer();
161: try {
162: parser.readColumnNames();
163: int currentRecord = 1;
164:
165: while (parser.hasNextRow()) {
166: ValueParser vp = parser.nextRow();
167: int currentField = 1;
168: while (currentField <= 4) {
169: if (currentField == 2 || currentField == 4) {
170: assertEquals(
171: "value" + currentRecord + currentField,
172: vp
173: .getString(DataStreamParser.EMPTYFIELDNAME
174: + currentField));
175: } else {
176: assertEquals("value" + currentRecord
177: + currentField, vp.getString("field"
178: + currentField));
179: }
180: currentField += 1;
181: }
182: currentRecord += 1;
183: }
184: } catch (IOException ioe) {
185: fail("Unexpected exception in testcase occured : "
186: + ioe.toString());
187: }
188: }
189: }
|