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