1 package org.xmlBlaster.test.classtest;
  2 
  3 import java.util.Map;
  4 import java.util.logging.Logger;
  5 
  6 import junit.framework.TestCase;
  7 
  8 import org.xmlBlaster.util.Global;
  9 import org.xmlBlaster.util.StringPairTokenizer;
 10 import org.xmlBlaster.util.qos.ClientProperty;
 11 
 12 /**
 13  * Test ConnectQos.
 14  * <p />
 15  * All methods starting with 'test' and without arguments are invoked
 16  * automatically
 17  * <p />
 18  * Invoke: java -Djava.compiler= junit.textui.TestRunner -noloading
 19  * org.xmlBlaster.test.classtest.StringPairTokenizerTest
 20  */
 21 public class StringPairTokenizerTest extends TestCase {
 22 
 23    final static String ME = "StringPairTokenizerTest";
 24    protected Global glob;
 25    private static Logger log = Logger.getLogger(StringPairTokenizerTest.class
 26          .getName());
 27    int counter = 0;
 28 
 29    public StringPairTokenizerTest(String name, String[] args) {
 30       super(name);
 31       this.glob = Global.instance();
 32       this.glob.init(args);
 33 
 34    }
 35 
 36    public StringPairTokenizerTest(String name) {
 37       super(name);
 38       this.glob = Global.instance();
 39 
 40    }
 41 
 42    protected void setUp() {
 43    }
 44 
 45    protected void tearDown() {
 46    }
 47 
 48    public void testTokenizerApos1() {
 49       {
 50          boolean preserveInsideQuoteChar = true;
 51          String csv = "b=\"6\"";
 52          // String csv = "a=\"5\",b=\"6,7\"";
 53          String[] toks = StringPairTokenizer.parseLine(new String[] { csv },
 54                ',', '"', false, preserveInsideQuoteChar);
 55          for (String tok : toks) {
 56             System.out.println(tok);
 57          }
 58          assertEquals(1, toks.length);
 59          assertEquals("b=\"6\"", toks[0]);
 60       }
 61    }
 62 
 63    public void testTokenizerApos() {
 64       {
 65          boolean preserveInsideQuoteChar = true;
 66          String csv = "a=\"5\",b=\"6,7\"";
 67          String[] toks = StringPairTokenizer.parseLine(new String[] { csv },
 68                ',', '"', false, preserveInsideQuoteChar);
 69          for (String tok : toks) {
 70             System.out.println(tok);
 71          }
 72          assertEquals(2, toks.length);
 73          assertEquals("a=\"5\"", toks[0]);
 74          assertEquals("b=\"6,7\"", toks[1]);
 75       }
 76       {
 77          boolean preserveInsideQuoteChar = false;
 78          String csv = "a=\"5\",b=\"6,7\"";
 79          String[] toks = StringPairTokenizer.parseLine(new String[] { csv },
 80                ',', '"', false, preserveInsideQuoteChar);
 81          for (String tok : toks) {
 82             System.out.println(tok);
 83          }
 84          assertEquals(2, toks.length);
 85          assertEquals("a=5", toks[0]);
 86          assertEquals("b=6,7", toks[1]);
 87       }
 88       {
 89          boolean preserveInsideQuoteChar = false;
 90          String csv = "a=5,b=6,7";
 91          String[] toks = StringPairTokenizer.parseLine(new String[] { csv },
 92                ',', '"', false, preserveInsideQuoteChar);
 93          for (String tok : toks) {
 94             System.out.println(tok);
 95          }
 96          assertEquals(3, toks.length);
 97          assertEquals("a=5", toks[0]);
 98          assertEquals("b=6", toks[1]);
 99          assertEquals("7", toks[2]);
100       }
101    }
102 
103    public void testEscape() {
104       {
105          // &comma; escapes are not supported
106          String csv = "v=1&comma;2";
107          String[] toks = StringPairTokenizer.parseLine(new String[] { csv },
108                ',', '"', false, true);
109          assertEquals(1, toks.length);
110          assertEquals("v=1&comma;2", toks[0]);
111       }
112       {
113          String csv = "k=1,v=\"1,2\"";
114          String[] toks = StringPairTokenizer.parseLine(new String[] { csv },
115                ',', '"', false, true);
116          assertEquals(2, toks.length);
117          assertEquals("k=1", toks[0]);
118          assertEquals("v=\"1,2\"", toks[1]);
119       }
120       {
121          String csv = "k=1,v=\"1,2\"";
122          String[] toks = StringPairTokenizer.parseLine(new String[] { csv },
123                ',', '"', false, false);
124          assertEquals(2, toks.length);
125          assertEquals("k=1", toks[0]);
126          assertEquals("v=1,2", toks[1]);
127       }
128    }
129 
130    public void testClientProperties() {
131       try {
132          int maxEntries = 0;
133          long maxSize = 0L;
134          boolean consumable = false;
135          long waitingDelay = 4000L;
136 
137          String cmd = "maxEntries=4&maxSize=-1&consumable=true&waitingDelay=1000";
138          Map<String, ClientProperty> props = StringPairTokenizer
139                .parseToStringClientPropertyPairs(cmd, "&", "=");
140 
141          ClientProperty prop = (ClientProperty) props.get("maxEntries");
142          assertNotNull("property should not be null", prop);
143          maxEntries = prop.getIntValue();
144          assertEquals("maxEntries", 4, maxEntries);
145 
146          prop = (ClientProperty) props.get("maxSize");
147          assertNotNull("property should not be null", prop);
148          maxSize = prop.getLongValue();
149          assertEquals("maxSize", -1L, maxSize);
150 
151          prop = (ClientProperty) props.get("consumable");
152          assertNotNull("property should not be null", prop);
153          consumable = prop.getBooleanValue();
154          assertEquals("consumable", true, consumable);
155 
156          prop = (ClientProperty) props.get("waitingDelay");
157          assertNotNull("property should not be null", prop);
158          waitingDelay = prop.getLongValue();
159          assertEquals("waitingDelay", 1000L, waitingDelay);
160       } catch (Exception ex) {
161          ex.printStackTrace();
162          assertTrue("an exception should not occur here", false);
163       }
164    }
165 
166    public void testURL() {
167       // TODO: need to add " around some parameter-values and extend the
168       // tokenizer accordingly
169       String cmd = "nodeClass=node&node=izar&action=initReplication&p1=Hallo";
170       try {
171          Map<String, ClientProperty> props = StringPairTokenizer
172                .parseToStringClientPropertyPairs(cmd, "&", "=");
173 
174          assertTrue("Missing property", props.containsKey("nodeClass"));
175          assertTrue("Missing property", props.containsKey("node"));
176          assertTrue("Missing property", props.containsKey("action"));
177          assertTrue("Missing property", props.containsKey("p1"));
178          assertFalse("Property to much", props.containsKey("izar"));
179 
180          // for some reason, they don't work :-(
181          // assertEquals("wrong value", "node", props.get("nodeClass"));
182          // assertEquals("wrong value", "izar", props.get("node"));
183          // assertEquals("wrong value", "action",
184          // props.get("initReplication"));
185          // assertEquals("wrong value", "p1", props.get("Hallo"));
186       } catch (Exception ex) {
187          ex.printStackTrace();
188          assertTrue("an exception should not occur here", false);
189       }
190 
191    }
192 
193    public void testMapCSV() {
194       {
195          String line = "aKey=\"a value with &#034; apost, and semicolon\",otherKey=2300,third&#061;Key=a key with assignment,key4=\"Hello, == world\"";
196          Map<String, String> map = StringPairTokenizer.CSVToMap(line);
197          String result = StringPairTokenizer.mapToCSV(map);
198          map = StringPairTokenizer.CSVToMap(result);
199          assertEquals(4, map.size());
200          assertEquals("a value with \" apost, and semicolon",
201                (String) map.get("aKey"));
202          assertEquals("2300", (String) map.get("otherKey"));
203          assertEquals("a key with assignment", (String) map.get("third=Key"));
204          assertEquals("Hello, == world", (String) map.get("key4"));
205          assertNull(map.get("bla"));
206          log.info(line);
207          log.info(" to ");
208          log.info(result);
209          log.info("Done");
210       }
211       {
212          String line = "aNullKey,otherEmptyKey=,thirdKey=\" \", fourthKey = Blanks ";
213          Map<String, String> map = StringPairTokenizer.CSVToMap(line);
214          String result = StringPairTokenizer.mapToCSV(map);
215          map = StringPairTokenizer.CSVToMap(result);
216          assertEquals(4, map.size());
217          assertEquals(null, (String) map.get("aNullKey"));
218          assertTrue(map.containsKey("aNullKey"));
219          assertEquals("", (String) map.get("otherEmptyKey"));
220          assertEquals("", (String) map.get("thirdKey"));
221          assertEquals("Blanks", (String) map.get("fourthKey"));
222          assertNull(map.get("bla"));
223          log.info(line);
224          log.info(" to ");
225          log.info(result);
226          log.info("Done");
227       }
228    }
229 
230    /**
231     * <pre>
232     *   java org.xmlBlaster.test.classtest.StringPairTokenizerTest
233     * </pre>
234     */
235    public static void main(String args[]) {
236       StringPairTokenizerTest test = new StringPairTokenizerTest(
237             "StringPairTokenizerTest", args);
238 
239       test.setUp();
240       test.testClientProperties();
241       test.testURL();
242       test.tearDown();
243 
244    }
245 }


syntax highlighted by Code2HTML, v. 0.9.1