PageRenderTime 34ms CodeModel.GetById 27ms app.highlight 5ms RepoModel.GetById 0ms app.codeStats 0ms

/core/test/unit/src/com/bluemarsh/jswat/core/util/StringsTest.java

http://jswat.googlecode.com/
Java | 117 lines | 81 code | 9 blank | 27 comment | 0 complexity | 31c91c90d6b0e8179cdf41f4366a53b3 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception
  1/*
  2 * The contents of this file are subject to the terms of the Common Development
  3 * and Distribution License (the License). You may not use this file except in
  4 * compliance with the License.
  5 *
  6 * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
  7 * or http://www.netbeans.org/cddl.txt.
  8 *
  9 * When distributing Covered Code, include this CDDL Header Notice in each file
 10 * and include the License file at http://www.netbeans.org/cddl.txt.
 11 * If applicable, add the following below the CDDL Header, with the fields
 12 * enclosed by brackets [] replaced by your own identifying information:
 13 * "Portions Copyrighted [year] [name of copyright owner]"
 14 *
 15 * The Original Software is JSwat. The Initial Developer of the Original
 16 * Software is Nathan L. Fiedler. Portions created by Nathan L. Fiedler
 17 * are Copyright (C) 2002-2010. All Rights Reserved.
 18 *
 19 * Contributor(s): Nathan L. Fiedler.
 20 *
 21 * $Id: StringsTest.java 256 2010-01-25 07:33:24Z nathanfiedler $
 22 */
 23
 24package com.bluemarsh.jswat.core.util;
 25
 26import java.util.ArrayList;
 27import java.util.List;
 28import org.junit.Test;
 29import static org.junit.Assert.*;
 30
 31/**
 32 * Unit tests for the Strings class.
 33 *
 34 * @author  Nathan Fiedler
 35 */
 36public class StringsTest {
 37
 38    @Test
 39    public void test_Strings_cleanForPrinting() {
 40        assertEquals("null", Strings.cleanForPrinting(null, 0));
 41        assertEquals("blah", Strings.cleanForPrinting("blah", 0));
 42        assertEquals("blah", Strings.cleanForPrinting("blah", 10));
 43        assertEquals("this<...>tring", Strings.cleanForPrinting(
 44                "this is a very long string", 15));
 45        assertEquals("abc\\rdef", Strings.cleanForPrinting("abc\rdef", 0));
 46        assertEquals("\\t\\n\\b\\f", Strings.cleanForPrinting("\t\n\b\f", 0));
 47    }
 48
 49    @Test
 50    public void test_Strings_listToString() {
 51        assertNull(Strings.listToString(null));
 52        List<String> list = new ArrayList<String>(3);
 53        assertEquals("", Strings.listToString(list));
 54        list.add("abc");
 55        assertEquals("abc", Strings.listToString(list));
 56        list.add("def");
 57        assertEquals("abc, def", Strings.listToString(list));
 58        list.add("ghi");
 59        assertEquals("abc, def, ghi", Strings.listToString(list));
 60        assertEquals("abc;def;ghi", Strings.listToString(list, ";"));
 61    }
 62
 63    @Test
 64    public void test_Strings_stringToList() {
 65        List list = Strings.stringToList(null);
 66        assertNull(list);
 67        list = Strings.stringToList("");
 68        assertEquals(0, list.size());
 69        list = Strings.stringToList("abc");
 70        assertEquals(1, list.size());
 71        assertEquals("abc", list.get(0));
 72        list = Strings.stringToList("abc,def");
 73        assertEquals(2, list.size());
 74        assertEquals("abc", list.get(0));
 75        assertEquals("def", list.get(1));
 76        list = Strings.stringToList("abc , def , ghi");
 77        assertEquals(3, list.size());
 78        assertEquals("abc", list.get(0));
 79        assertEquals("def", list.get(1));
 80        assertEquals("ghi", list.get(2));
 81    }
 82
 83    @Test
 84    public void test_Strings_toHexString() {
 85        assertEquals("0001", Strings.toHexString(1));
 86        assertEquals("0080", Strings.toHexString(128));
 87        assertEquals("04d2", Strings.toHexString(1234));
 88        assertEquals("ffff", Strings.toHexString(65535));
 89        assertEquals("0000ffff", Strings.toHexString(65535, 8));
 90        assertEquals("ffffffff", Strings.toHexString(-1));
 91        assertEquals("00000000ffffffff", Strings.toHexString(-1, 16));
 92        assertEquals("000000000000000000000000ffffffff",
 93                     Strings.toHexString(-1, 32));
 94    }
 95
 96    @Test
 97    public void test_Strings_trimQuotes() {
 98        assertNull(Strings.trimQuotes(null));
 99        assertEquals("", Strings.trimQuotes(""));
100        assertEquals("  ", Strings.trimQuotes("  "));
101        assertEquals("abc", Strings.trimQuotes("abc"));
102        assertEquals("abc", Strings.trimQuotes("'abc'"));
103        assertEquals("abc'", Strings.trimQuotes("abc'"));
104        assertEquals("abc", Strings.trimQuotes("\"abc\""));
105        assertEquals("abc\"", Strings.trimQuotes("abc\""));
106        assertEquals("'abc", Strings.trimQuotes("'abc"));
107        assertEquals("\"abc", Strings.trimQuotes("\"abc"));
108    }
109
110    @Test
111    public void testExceptionToString() {
112        assertEquals("", Strings.exceptionToString(null));
113        RuntimeException re = new RuntimeException();
114        String actual = Strings.exceptionToString(re);
115        assertTrue(actual.contains("RuntimeException"));
116    }
117}