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 import org.apache.cactus.ServletTestCase; 25 import org.apache.turbine.Turbine; 26 27 28 38 public class TSVParserTest 39 extends ServletTestCase 40 { 41 Turbine turbine = null; 42 43 47 public TSVParserTest(String name) 48 { 49 super(name); 50 } 51 52 56 57 protected void setUp() 58 throws Exception 59 { 60 super.setUp(); 61 66 config.setInitParameter("properties", 67 "/WEB-INF/conf/TurbineComplete.properties"); 68 turbine = new Turbine(); 69 turbine.init(config); 70 } 71 72 77 protected void tearDown() 78 throws Exception 79 { 80 turbine.destroy(); 81 super.tearDown(); 82 } 83 84 89 public static Test suite() 90 { 91 return new TestSuite(TSVParserTest.class); 92 } 93 94 97 public void testEmptyFieldValues() 98 { 99 String values = "field1\tfield2\tfield3\tfield4\nvalue11\t\tvalue13\t\nvalue21\t\tvalue23\t"; 100 CharArrayReader reader = new CharArrayReader (values.toCharArray()); 101 TSVParser parser = new TSVParser(reader); 102 StringBuffer sb = new StringBuffer (); 103 try 104 { 105 parser.readColumnNames(); 106 int currentRecord = 1; 107 while (parser.hasNextRow()) 108 { 109 ValueParser vp = parser.nextRow(); 110 int currentField = 1; 111 while (currentField <= 4) 112 { 113 if (currentField == 2 || currentField == 4) 114 { 115 assertNull(vp.getString("field" + currentField)); 116 } 117 else 118 { 119 assertEquals("value" + currentRecord + currentField, vp.getString("field" + currentField)); 120 } 121 currentField += 1; 122 } 123 currentRecord += 1; 124 } 125 } 126 catch (IOException ioe) 127 { 128 fail("Unexpected exception in testcase occured : " + ioe.toString()); 129 } 130 } 131 132 135 public void testNormalFieldValues() 136 { 137 String values = "field1\tfield2\tfield3\tfield4\nvalue11\tvalue12\tvalue13\tvalue14\nvalue21\tvalue22\tvalue23\tvalue24"; 138 CharArrayReader reader = new CharArrayReader (values.toCharArray()); 139 TSVParser parser = new TSVParser(reader); 140 StringBuffer sb = new StringBuffer (); 141 try 142 { 143 parser.readColumnNames(); 144 int currentRecord = 1; 145 while (parser.hasNextRow()) 146 { 147 ValueParser vp = parser.nextRow(); 148 int currentField = 1; 149 while (currentField <= 4) 150 { 151 assertEquals("value" + currentRecord + currentField, vp.getString("field" + currentField)); 152 currentField += 1; 153 } 154 currentRecord += 1; 155 } 156 } 157 catch (IOException ioe) 158 { 159 fail("Unexpected exception in testcase occured : " + ioe.toString()); 160 } 161 } 162 163 166 public void testEmptyFieldNames() 167 { 168 String values = "field1\t\tfield3\t\nvalue11\tvalue12\tvalue13\tvalue14\tvalue21\tvalue22\tvalue23\tvalue24"; 169 CharArrayReader reader = new CharArrayReader (values.toCharArray()); 170 TSVParser parser = new TSVParser(reader); 171 StringBuffer sb = new StringBuffer (); 172 try 173 { 174 parser.readColumnNames(); 175 int currentRecord = 1; 176 177 while (parser.hasNextRow()) 178 { 179 ValueParser vp = parser.nextRow(); 180 int currentField = 1; 181 while (currentField <= 4) 182 { 183 if (currentField == 2 || currentField == 4) 184 { 185 assertEquals("value" + currentRecord + currentField, vp.getString(DataStreamParser.EMPTYFIELDNAME + currentField)); 186 } 187 else 188 { 189 assertEquals("value" + currentRecord + currentField, vp.getString("field" + currentField)); 190 } 191 currentField += 1; 192 } 193 currentRecord += 1; 194 } 195 } 196 catch (IOException ioe) 197 { 198 fail("Unexpected exception in testcase occured : " + ioe.toString()); 199 } 200 } 201 } 202 | Popular Tags |