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