1 package jimm.datavision.test; 2 import jimm.datavision.source.charsep.DelimParser; 3 import jimm.util.StringUtils; 4 import java.io.*; 5 import java.util.List ; 6 import junit.framework.TestCase; 7 import junit.framework.TestSuite; 8 import junit.framework.Test; 9 10 17 public class DelimParserTest extends TestCase { 18 19 protected static final String TEST_INPUT = 20 AllTests.testDataFile("delim_parser_in.txt"); 21 protected static final String TEST_ANSWERS = 22 AllTests.testDataFile("delim_parser_answers.txt"); 23 24 public static Test suite() { 25 return new TestSuite(DelimParserTest.class); 26 } 27 28 public DelimParserTest(String name) { 29 super(name); 30 } 31 32 protected void sepTest(List answer, DelimParser parser) { 33 List parsed = null; 34 try { 35 parsed = parser.parse(); 36 } 37 catch (IOException e) { 38 fail(e.toString()); 39 } 40 assertEquals(answer, parsed); 41 } 42 43 public void testParser() { 44 BufferedReader in = null, answers = null; 45 try { 46 in = new BufferedReader(new FileReader(TEST_INPUT)); 47 answers = new BufferedReader(new FileReader(TEST_ANSWERS)); 48 DelimParser parser = new DelimParser(in, ','); 49 50 List answer; 51 while ((answer = getNextAnswer(answers)) != null) 52 sepTest(answer, parser); 53 sepTest(null, parser); 54 } 55 catch (Exception e) { 56 fail(e.toString()); 57 } 58 finally { 59 try { 60 if (answers != null) answers.close(); 61 if (in != null) in.close(); 62 } 63 catch (Exception e2) {} 64 } 65 } 66 67 protected List getNextAnswer(BufferedReader in) throws IOException { 68 String line = in.readLine(); 69 if (line == null) 70 return null; 71 72 while (line.endsWith("\\")) { 73 line = line.substring(0, line.length() - 1); 74 line += "\n"; 75 line += in.readLine(); 76 } 77 return StringUtils.split(line, "\t"); 78 } 79 80 public static void main(String [] args) { 81 junit.textui.TestRunner.run(suite()); 82 System.exit(0); 83 } 84 85 } 86 | Popular Tags |