Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: junit/framework/ZDelimitedTokenizer.java


1   package junit.framework;
2   
3   import java.io.IOException;
4   import java.io.StringReader;
5   
6   import junit.framework.TestCase;
7   
8   import net.sourceforge.jbird.io.DelimitedTokenizer;
9   
10  /**
11    * A junit test class for net.sourceforge.jbird.io.DelimitedTokenizer.
12    * @author Dick Repasky
13    * @since J-Bird 0.1.3
14    * @see net.sourceforge.jbird.TestJB
15    * @see net.sourceforge.jbird.io.DelimitedTokenizer
16  */
17  
18  public class ZDelimitedTokenizer extends TestCase {
19  
20        /** Determines whether each expectation and
21          result are printed.  Set to true if 
22          you run into trouble and really want
23          to know what's going on. */
24  
25      public static final boolean PRINTEM = false;
26  
27    public ZDelimitedTokenizer (String name) {
28      super(name);
29    }
30  
31    protected void setUp() {
32    }
33  
34    protected void tearDown() {
35    }
36  
37      /** Test a quoted element that contains no delimiter. */
38  
39    public void testEmptyQuoteLineTwo() throws IOException {
40      String data = "quote,containing comma,\"a , there\"\n"
41          + "Hello,\"\"\"there\"\"\",Bob";
42      Object[] expectations = new Object[2];
43      String[] answera = new String[3];
44      answera[0] = "quote";
45      answera[1] = "containing comma";
46      answera[2] = "a , there";
47      expectations[0] = answera;
48      String[] answerb = new String[3];
49      answerb[0] = "Hello";
50      answerb[1] = "\"there\"";
51      answerb[2] = "Bob";
52      expectations[1] = answerb;
53      doIt(data, expectations);
54    }
55  
56      /** Test quote as last field on line that ends with 
57        a newline. */
58    public void testQuoteLastItemEndNewline() throws IOException {
59      String data = "a,junk,line\n"
60          + "quote,last,\"\"\"field\"\"\"\n";
61      Object[] expectations = new Object[2];
62      String[] answera = new String[3];
63      answera[0] = "a";
64      answera[1] = "junk";
65      answera[2] = "line";
66      expectations[0] = answera;
67      String[] answerb = new String[3];
68      answerb[0] = "quote";
69      answerb[1] = "last";
70      answerb[2] = "\"field\"";
71      expectations[1] = answerb;
72      doIt(data, expectations);
73    }
74  
75  
76      /** Test interior quotes such as aaaa,bbb ", "". */
77  
78    public void testInteriorQuotes() throws IOException {
79      String data = "trailing,quote,quote here ->\"\n"
80          + "interior,quote,isolated \"\" there";
81      Object[] expectations = new Object[2];
82      String[] answera = new String[3];
83      answera[0] = "trailing";
84      answera[1] = "quote";
85      answera[2] = "quote here ->\"";
86      expectations[0] = answera;
87      String[] answerb = new String[3];
88      answerb[0] = "interior";
89      answerb[1] = "quote";
90      answerb[2] = "isolated \"\" there";
91      expectations[1] = answerb;
92      doIt(data, expectations);
93    }
94  
95      /** Test quote escapes \" withing quoted string. */
96  
97    public void testEscapeQuotes() throws IOException {
98      String data = "legal,escape,\"\"\"enclosed\"\"\"\n"
99          + "backslash,escape,\"Howdy \\\"Barney\\\"\"";
100     Object[] expectations = new Object[2];
101     String[] answera = new String[3];
102     answera[0] = "legal";
103     answera[1] = "escape";
104     answera[2] = "\"enclosed\"";
105     expectations[0] = answera;
106     String[] answerb = new String[3];
107     answerb[0] = "backslash";
108     answerb[1] = "escape";
109     answerb[2] = "Howdy \"Barney\"";
110     expectations[1] = answerb;
111     doIt(data, expectations);
112   }
113 
114     /** Test unescaped quotes within quoted strings such as
115       "hello there "myrtle" dear". Although these
116       are not expected, the best way to deal with
117       them seems to pass them. */
118 
119   public void testUnEscapedQuotesInQuoted() throws IOException {
120     String data = "Unescaped,within,\"\"\"quoted\"\"\"\n"
121         + "'back \" \" slash',\" \" \",\"Howdy \"Barney\" \"";
122     Object[] expectations = new Object[2];
123     String[] answera = new String[3];
124     answera[0] = "Unescaped";
125     answera[1] = "within";
126     answera[2] = "\"quoted\"";
127     expectations[0] = answera;
128     String[] answerb = new String[3];
129     answerb[0] = "back \" \" slash";
130     answerb[1] = " \" ";
131     answerb[2] = "Howdy \"Barney\" ";
132     expectations[1] = answerb;
133     doIt(data, expectations);
134   }
135 
136   ///////////////////////// parse and compare with expectations ////////
137 
138   protected void doIt(String data, Object[] expectations) 
139       throws IOException {
140     DelimitedTokenizer dt = new DelimitedTokenizer(
141         new StringReader(data));
142     for (int lineno = 0; lineno < expectations.length; lineno ++) {
143       String[] linexpect = (String[]) expectations[lineno];
144       Object[] result = dt.getLineObjects();
145       assertEquals("bad field count", 
146           new Integer(linexpect.length), 
147           new Integer(result.length));
148       for (int itemno = 0; itemno < result.length; itemno ++){
149   
150         if (PRINTEM)
151         System.out.println(linexpect[itemno] + "\t" + 
152             (String)result[itemno]);
153         assertEquals(linexpect[itemno], 
154             (String)result[itemno]);
155       }
156     }
157     dt.close();
158   }
159 
160 }