1   /*
2    * Copyright (C) 2006  Tom Gibara
3    *
4    * This library is free software; you can redistribute it and/or
5    * modify it under the terms of the GNU Lesser General Public
6    * License as published by the Free Software Foundation; either
7    * version 2.1 of the License, or (at your option) any later version.
8    *
9    * This library is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12   * Lesser General Public License for more details.
13   *
14   * You should have received a copy of the GNU Lesser General Public
15   * License along with this library; if not, write to the Free Software
16   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
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  }