1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package com.tomgibara.pronto.util;
19
20 import java.io.ByteArrayInputStream;
21 import java.io.IOException;
22 import java.util.Arrays;
23 import java.util.HashMap;
24 import java.util.Properties;
25
26 import junit.framework.TestCase;
27
28 public class StringsTest extends TestCase {
29
30 public void testSplit() {
31 testSplit("1,2,3", "1", "2", "3");
32 testSplit(",,", "", "", "");
33 testSplit(" a , b , c ", "a", "b", "c");
34 }
35
36 public void testParse() throws Exception {
37 testParse("width: 250; height: 400; color: red", "width=250\nheight=400\ncolor=red");
38 testParse("width: 250", "width=250");
39 testParse("width:;height:", "width=\nheight=");
40 testParse("", "");
41 testParse(";", "");
42 testParse("a:1;;;;b:2", "a=1\nb=2");
43 testParse("key: value with spaces", "key=value with spaces");
44 }
45
46 public void testJoin() {
47 assertEquals("", Strings.join(new String[0], ","));
48 assertEquals("1", Strings.join("1".split(","), ","));
49 assertEquals("1,2", Strings.join("1,2".split(","), ","));
50 assertEquals("1,2,3", Strings.join("1,2,3".split(","), ","));
51
52 assertEquals("null", Strings.join(new String[] { null }, ", "));
53 assertEquals("null, null", Strings.join(new String[] { null, null }, ", "));
54 assertEquals("null, null, null", Strings.join(new String[] { null, null, null }, ", "));
55
56 assertEquals("ABC", Strings.join(new String[] { "A", "B", "C" }, ""));
57 }
58
59 private static void testSplit(String s, String... answers) {
60 assertEquals(Strings.splitCommas(s), Arrays.asList(answers));
61 }
62
63 private static void testParse(String s, String p) throws IOException {
64 Properties props = new Properties();
65 props.load(new ByteArrayInputStream(p.getBytes()));
66 assertEquals(Strings.parseProperties(s), new HashMap(props));
67 }
68
69 }