001: package org.apache.turbine.util.parser;
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.CharArrayReader;
023: import java.io.IOException;
024:
025: import junit.framework.Test;
026: import junit.framework.TestSuite;
027:
028: import org.apache.cactus.ServletTestCase;
029: import org.apache.turbine.Turbine;
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: CSVParserCactusTest.java 534527 2007-05-02 16:10:59Z tv $
040: */
041: public class CSVParserCactusTest extends ServletTestCase {
042:
043: Turbine turbine = null;
044:
045: /**
046: * Constructor for CSVParserTest.
047: * @param arg0
048: */
049: public CSVParserCactusTest(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:
061: config.setInitParameter("properties",
062: "/WEB-INF/conf/TurbineComplete.properties");
063: turbine = new Turbine();
064: turbine.init(config);
065: }
066:
067: /**
068: * Shut down our turbine servlet and let our parents clean up also.
069: *
070: * @exception Exception if an error occurs
071: */
072: protected void tearDown() throws Exception {
073: turbine.destroy();
074: super .tearDown();
075: }
076:
077: /**
078: * Return a test suite of all our tests.
079: *
080: * @return a <code>Test</code> value
081: */
082: public static Test suite() {
083: return new TestSuite(CSVParserTest.class);
084: }
085:
086: /**
087: * Tests if you can leave field values empty
088: */
089: public void testEmptyFieldValues() {
090: String values = "field1,field2,field3,field4\nvalue11,,value13,\nvalue21,,value23,";
091: CharArrayReader reader = new CharArrayReader(values
092: .toCharArray());
093: CSVParser parser = new CSVParser(reader);
094: StringBuffer sb = new StringBuffer();
095: try {
096: parser.readColumnNames();
097: int currentRecord = 1;
098: while (parser.hasNextRow()) {
099: ValueParser vp = parser.nextRow();
100: int currentField = 1;
101: while (currentField <= 4) {
102: if (currentField == 2 || currentField == 4) {
103: assertNull(vp.getString("field" + currentField));
104: } else {
105: assertEquals("value" + currentRecord
106: + currentField, vp.getString("field"
107: + currentField));
108: }
109: currentField += 1;
110: }
111: currentRecord += 1;
112: }
113: } catch (IOException ioe) {
114: fail("Unexpected exception in testcase occured : "
115: + ioe.toString());
116: }
117: }
118:
119: /**
120: * Tests if normal operation is still working
121: */
122: public void testNormalFieldValues() {
123: String values = "field1,field2,field3,field4\nvalue11,value12,value13,value14\nvalue21,value22,value23,value24";
124: CharArrayReader reader = new CharArrayReader(values
125: .toCharArray());
126: CSVParser parser = new CSVParser(reader);
127: StringBuffer sb = new StringBuffer();
128: try {
129: parser.readColumnNames();
130: int currentRecord = 1;
131: while (parser.hasNextRow()) {
132: ValueParser vp = parser.nextRow();
133: int currentField = 1;
134: while (currentField <= 4) {
135: assertEquals(
136: "value" + currentRecord + currentField, vp
137: .getString("field" + currentField));
138: currentField += 1;
139: }
140: currentRecord += 1;
141: }
142: } catch (IOException ioe) {
143: fail("Unexpected exception in testcase occured : "
144: + ioe.toString());
145: }
146: }
147:
148: /**
149: * Tests if some fields are empty, but the values exists..
150: */
151: public void testEmptyFieldNames() {
152: String values = "field1,,field3,\nvalue11,value12,value13,value14\nvalue21,value22,value23,value24";
153: CharArrayReader reader = new CharArrayReader(values
154: .toCharArray());
155: CSVParser parser = new CSVParser(reader);
156: StringBuffer sb = new StringBuffer();
157: try {
158: parser.readColumnNames();
159: int currentRecord = 1;
160:
161: while (parser.hasNextRow()) {
162: ValueParser vp = parser.nextRow();
163: int currentField = 1;
164: while (currentField <= 4) {
165: if (currentField == 2 || currentField == 4) {
166: assertEquals(
167: "value" + currentRecord + currentField,
168: vp
169: .getString(DataStreamParser.EMPTYFIELDNAME
170: + currentField));
171: } else {
172: assertEquals("value" + currentRecord
173: + currentField, vp.getString("field"
174: + currentField));
175: }
176: currentField += 1;
177: }
178: currentRecord += 1;
179: }
180: } catch (IOException ioe) {
181: fail("Unexpected exception in testcase occured : "
182: + ioe.toString());
183: }
184: }
185: }
|