1 package org.apache.turbine.util; 2 3 18 19 import java.io.IOException ; 20 import java.io.CharArrayReader ; 21 import java.io.StringBufferInputStream ; 22 import java.util.Iterator ; 23 import java.util.Vector ; 24 25 import org.apache.turbine.Turbine; 26 27 import org.apache.cactus.ServletTestCase; 28 import junit.framework.Test; 29 import junit.framework.TestSuite; 30 31 41 public class CSVParserTest 42 extends ServletTestCase 43 { 44 45 Turbine turbine = null; 46 47 51 public CSVParserTest(String name) 52 { 53 super(name); 54 } 55 56 60 61 protected void setUp() 62 throws Exception 63 { 64 super.setUp(); 65 70 config.setInitParameter("properties", 71 "/WEB-INF/conf/TurbineDefault.properties"); 72 turbine = new Turbine(); 73 turbine.init(config); 74 } 75 76 81 protected void tearDown() 82 throws Exception 83 { 84 turbine.destroy(); 85 super.tearDown(); 86 } 87 88 93 public static Test suite() 94 { 95 return new TestSuite(CSVParserTest.class); 96 } 97 98 101 public void testEmptyFieldValues() 102 { 103 String values = "field1,field2,field3,field4\nvalue11,,value13,\nvalue21,,value23,"; 104 CharArrayReader reader = new CharArrayReader (values.toCharArray()); 105 CSVParser parser = new CSVParser(reader); 106 StringBuffer sb = new StringBuffer (); 107 try 108 { 109 parser.readColumnNames(); 110 int currentRecord = 1; 111 while (parser.hasNextRow()) 112 { 113 ValueParser vp = parser.nextRow(); 114 int currentField = 1; 115 while (currentField <= 4) 116 { 117 if (currentField == 2 || currentField == 4) 118 { 119 assertNull(vp.getString("field" + currentField)); 120 } 121 else 122 { 123 assertEquals("value" + currentRecord + currentField, vp.getString("field" + currentField)); 124 } 125 currentField += 1; 126 } 127 currentRecord += 1; 128 } 129 } 130 catch (IOException ioe) 131 { 132 fail("Unexpected exception in testcase occured : " + ioe.toString()); 133 } 134 } 135 136 139 public void testNormalFieldValues() 140 { 141 String values = "field1,field2,field3,field4\nvalue11,value12,value13,value14\nvalue21,value22,value23,value24"; 142 CharArrayReader reader = new CharArrayReader (values.toCharArray()); 143 CSVParser parser = new CSVParser(reader); 144 StringBuffer sb = new StringBuffer (); 145 try 146 { 147 parser.readColumnNames(); 148 int currentRecord = 1; 149 while (parser.hasNextRow()) 150 { 151 ValueParser vp = parser.nextRow(); 152 int currentField = 1; 153 while (currentField <= 4) 154 { 155 assertEquals("value" + currentRecord + currentField, vp.getString("field" + currentField)); 156 currentField += 1; 157 } 158 currentRecord += 1; 159 } 160 } 161 catch (IOException ioe) 162 { 163 fail("Unexpected exception in testcase occured : " + ioe.toString()); 164 } 165 } 166 167 170 public void testEmptyFieldNames() 171 { 172 String values = "field1,,field3,\nvalue11,value12,value13,value14\nvalue21,value22,value23,value24"; 173 CharArrayReader reader = new CharArrayReader (values.toCharArray()); 174 CSVParser parser = new CSVParser(reader); 175 StringBuffer sb = new StringBuffer (); 176 try 177 { 178 parser.readColumnNames(); 179 int currentRecord = 1; 180 181 while (parser.hasNextRow()) 182 { 183 ValueParser vp = parser.nextRow(); 184 int currentField = 1; 185 while (currentField <= 4) 186 { 187 if (currentField == 2 || currentField == 4) 188 { 189 assertEquals("value" + currentRecord + currentField, 190 vp.getString(DataStreamParser.EMPTYFIELDNAME + currentField)); 191 } 192 else 193 { 194 assertEquals("value" + currentRecord + currentField, 195 vp.getString("field" + currentField)); 196 } 197 currentField += 1; 198 } 199 currentRecord += 1; 200 } 201 } 202 catch (IOException ioe) 203 { 204 fail("Unexpected exception in testcase occured : " + ioe.toString()); 205 } 206 } 207 } 208 | Popular Tags |