/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

  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. package com.bluemarsh.jswat.core.util;
  24. import java.util.ArrayList;
  25. import java.util.List;
  26. import org.junit.Test;
  27. import static org.junit.Assert.*;
  28. /**
  29. * Unit tests for the Strings class.
  30. *
  31. * @author Nathan Fiedler
  32. */
  33. public class StringsTest {
  34. @Test
  35. public void test_Strings_cleanForPrinting() {
  36. assertEquals("null", Strings.cleanForPrinting(null, 0));
  37. assertEquals("blah", Strings.cleanForPrinting("blah", 0));
  38. assertEquals("blah", Strings.cleanForPrinting("blah", 10));
  39. assertEquals("this<...>tring", Strings.cleanForPrinting(
  40. "this is a very long string", 15));
  41. assertEquals("abc\\rdef", Strings.cleanForPrinting("abc\rdef", 0));
  42. assertEquals("\\t\\n\\b\\f", Strings.cleanForPrinting("\t\n\b\f", 0));
  43. }
  44. @Test
  45. public void test_Strings_listToString() {
  46. assertNull(Strings.listToString(null));
  47. List<String> list = new ArrayList<String>(3);
  48. assertEquals("", Strings.listToString(list));
  49. list.add("abc");
  50. assertEquals("abc", Strings.listToString(list));
  51. list.add("def");
  52. assertEquals("abc, def", Strings.listToString(list));
  53. list.add("ghi");
  54. assertEquals("abc, def, ghi", Strings.listToString(list));
  55. assertEquals("abc;def;ghi", Strings.listToString(list, ";"));
  56. }
  57. @Test
  58. public void test_Strings_stringToList() {
  59. List list = Strings.stringToList(null);
  60. assertNull(list);
  61. list = Strings.stringToList("");
  62. assertEquals(0, list.size());
  63. list = Strings.stringToList("abc");
  64. assertEquals(1, list.size());
  65. assertEquals("abc", list.get(0));
  66. list = Strings.stringToList("abc,def");
  67. assertEquals(2, list.size());
  68. assertEquals("abc", list.get(0));
  69. assertEquals("def", list.get(1));
  70. list = Strings.stringToList("abc , def , ghi");
  71. assertEquals(3, list.size());
  72. assertEquals("abc", list.get(0));
  73. assertEquals("def", list.get(1));
  74. assertEquals("ghi", list.get(2));
  75. }
  76. @Test
  77. public void test_Strings_toHexString() {
  78. assertEquals("0001", Strings.toHexString(1));
  79. assertEquals("0080", Strings.toHexString(128));
  80. assertEquals("04d2", Strings.toHexString(1234));
  81. assertEquals("ffff", Strings.toHexString(65535));
  82. assertEquals("0000ffff", Strings.toHexString(65535, 8));
  83. assertEquals("ffffffff", Strings.toHexString(-1));
  84. assertEquals("00000000ffffffff", Strings.toHexString(-1, 16));
  85. assertEquals("000000000000000000000000ffffffff",
  86. Strings.toHexString(-1, 32));
  87. }
  88. @Test
  89. public void test_Strings_trimQuotes() {
  90. assertNull(Strings.trimQuotes(null));
  91. assertEquals("", Strings.trimQuotes(""));
  92. assertEquals(" ", Strings.trimQuotes(" "));
  93. assertEquals("abc", Strings.trimQuotes("abc"));
  94. assertEquals("abc", Strings.trimQuotes("'abc'"));
  95. assertEquals("abc'", Strings.trimQuotes("abc'"));
  96. assertEquals("abc", Strings.trimQuotes("\"abc\""));
  97. assertEquals("abc\"", Strings.trimQuotes("abc\""));
  98. assertEquals("'abc", Strings.trimQuotes("'abc"));
  99. assertEquals("\"abc", Strings.trimQuotes("\"abc"));
  100. }
  101. @Test
  102. public void testExceptionToString() {
  103. assertEquals("", Strings.exceptionToString(null));
  104. RuntimeException re = new RuntimeException();
  105. String actual = Strings.exceptionToString(re);
  106. assertTrue(actual.contains("RuntimeException"));
  107. }
  108. }