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.CharArrayReader;
023: import java.io.IOException;
024:
025: import junit.framework.Test;
026: import junit.framework.TestSuite;
027: import org.apache.cactus.ServletTestCase;
028: import org.apache.turbine.Turbine;
029:
030: /**
031: * Test the CSVParser.
032: *
033: * NOTE : I am assuming (as is in the code of DataStreamParser.java
034: * that the values are reusing the same object for the values.
035: * If this shouldn't be, we need to fix that in the code!.
036: *
037: * @author <a href="mailto:martin@mvdb.net">Martin van den Bemt</a>
038: * @version $Id: TSVParserTest.java 534527 2007-05-02 16:10:59Z tv $
039: */
040: public class TSVParserTest extends ServletTestCase {
041: Turbine turbine = null;
042:
043: /**
044: * Constructor for CSVParserTest.
045: * @param arg0
046: */
047: public TSVParserTest(String name) {
048: super (name);
049: }
050:
051: /**
052: * This will setup an instance of turbine to use when testing
053: * @exception if an exception occurs.
054: */
055:
056: protected void setUp() throws Exception {
057: super .setUp();
058: /* Note: we are using the properties file from the cache test
059: * since we don't really need any specific property at this
060: * time. Future tests may require a test case specific
061: * properties file to be used.:
062: */
063: config.setInitParameter("properties",
064: "/WEB-INF/conf/TurbineDefault.properties");
065: turbine = new Turbine();
066: turbine.init(config);
067: }
068:
069: /**
070: * Shut down our turbine servlet and let our parents clean up also.
071: *
072: * @exception Exception if an error occurs
073: */
074: protected void tearDown() throws Exception {
075: turbine.destroy();
076: super .tearDown();
077: }
078:
079: /**
080: * Return a test suite of all our tests.
081: *
082: * @return a <code>Test</code> value
083: */
084: public static Test suite() {
085: return new TestSuite(TSVParserTest.class);
086: }
087:
088: /**
089: * Tests if you can leave field values empty
090: */
091: public void testEmptyFieldValues() {
092: String values = "field1\tfield2\tfield3\tfield4\nvalue11\t\tvalue13\t\nvalue21\t\tvalue23\t";
093: CharArrayReader reader = new CharArrayReader(values
094: .toCharArray());
095: TSVParser parser = new TSVParser(reader);
096: StringBuffer sb = new StringBuffer();
097: try {
098: parser.readColumnNames();
099: int currentRecord = 1;
100: while (parser.hasNextRow()) {
101: ValueParser vp = parser.nextRow();
102: int currentField = 1;
103: while (currentField <= 4) {
104: if (currentField == 2 || currentField == 4) {
105: assertNull(vp.getString("field" + currentField));
106: } else {
107: assertEquals("value" + currentRecord
108: + currentField, vp.getString("field"
109: + currentField));
110: }
111: currentField += 1;
112: }
113: currentRecord += 1;
114: }
115: } catch (IOException ioe) {
116: fail("Unexpected exception in testcase occured : "
117: + ioe.toString());
118: }
119: }
120:
121: /**
122: * Tests if normal operation is still working
123: */
124: public void testNormalFieldValues() {
125: String values = "field1\tfield2\tfield3\tfield4\nvalue11\tvalue12\tvalue13\tvalue14\nvalue21\tvalue22\tvalue23\tvalue24";
126: CharArrayReader reader = new CharArrayReader(values
127: .toCharArray());
128: TSVParser parser = new TSVParser(reader);
129: StringBuffer sb = new StringBuffer();
130: try {
131: parser.readColumnNames();
132: int currentRecord = 1;
133: while (parser.hasNextRow()) {
134: ValueParser vp = parser.nextRow();
135: int currentField = 1;
136: while (currentField <= 4) {
137: assertEquals(
138: "value" + currentRecord + currentField, vp
139: .getString("field" + currentField));
140: currentField += 1;
141: }
142: currentRecord += 1;
143: }
144: } catch (IOException ioe) {
145: fail("Unexpected exception in testcase occured : "
146: + ioe.toString());
147: }
148: }
149:
150: /**
151: * Tests if some fields are empty, but the values exists..
152: */
153: public void testEmptyFieldNames() {
154: String values = "field1\t\tfield3\t\nvalue11\tvalue12\tvalue13\tvalue14\tvalue21\tvalue22\tvalue23\tvalue24";
155: CharArrayReader reader = new CharArrayReader(values
156: .toCharArray());
157: TSVParser parser = new TSVParser(reader);
158: StringBuffer sb = new StringBuffer();
159: try {
160: parser.readColumnNames();
161: int currentRecord = 1;
162:
163: while (parser.hasNextRow()) {
164: ValueParser vp = parser.nextRow();
165: int currentField = 1;
166: while (currentField <= 4) {
167: if (currentField == 2 || currentField == 4) {
168: assertEquals(
169: "value" + currentRecord + currentField,
170: vp
171: .getString(DataStreamParser.EMPTYFIELDNAME
172: + currentField));
173: } else {
174: assertEquals("value" + currentRecord
175: + currentField, vp.getString("field"
176: + currentField));
177: }
178: currentField += 1;
179: }
180: currentRecord += 1;
181: }
182: } catch (IOException ioe) {
183: fail("Unexpected exception in testcase occured : "
184: + ioe.toString());
185: }
186: }
187: }
|