You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

StringUtilsTest.java 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. * Copyright 2011 gitblit.com.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.gitblit.tests;
  17. import java.util.Arrays;
  18. import java.util.List;
  19. import junit.framework.TestCase;
  20. import com.gitblit.utils.StringUtils;
  21. public class StringUtilsTest extends TestCase {
  22. public void testIsEmpty() throws Exception {
  23. assertTrue(StringUtils.isEmpty(null));
  24. assertTrue(StringUtils.isEmpty(""));
  25. assertTrue(StringUtils.isEmpty(" "));
  26. assertFalse(StringUtils.isEmpty("A"));
  27. }
  28. public void testBreakLinesForHtml() throws Exception {
  29. String input = "this\nis\r\na\rtest\r\n\r\nof\n\nline\r\rbreaking";
  30. String output = "this<br/>is<br/>a<br/>test<br/><br/>of<br/><br/>line<br/><br/>breaking";
  31. assertTrue(StringUtils.breakLinesForHtml(input).equals(output));
  32. }
  33. public void testEncodeUrl() throws Exception {
  34. String input = "test /";
  35. String output = "test%20%2F";
  36. assertTrue(StringUtils.encodeURL(input).equals(output));
  37. }
  38. public void testEscapeForHtml() throws Exception {
  39. String input = "& < > \" \t";
  40. String outputNoChange = "&amp; &lt; &gt; &quot; \t";
  41. String outputChange = "&amp;&nbsp;&lt;&nbsp;&gt;&nbsp;&quot;&nbsp; &nbsp; &nbsp;";
  42. assertTrue(StringUtils.escapeForHtml(input, false).equals(outputNoChange));
  43. assertTrue(StringUtils.escapeForHtml(input, true).equals(outputChange));
  44. }
  45. public void testDecodeForHtml() throws Exception {
  46. String input = "&amp; &lt; &gt; &quot;";
  47. String output = "& < > \"";
  48. assertTrue(StringUtils.decodeFromHtml(input).equals(output));
  49. }
  50. public void testFlattenStrings() throws Exception {
  51. String[] strings = { "A", "B", "C", "D" };
  52. assertTrue(StringUtils.flattenStrings(Arrays.asList(strings)).equals("A B C D"));
  53. }
  54. public void testTrim() throws Exception {
  55. String input = "123456789 123456789 123456789 123456789 123456789 123456789 123456789 ";
  56. String output = "123456789 123456789 123456789 123456789 123456789 1234567...";
  57. assertTrue(StringUtils.trimShortLog(input).equals(output));
  58. assertTrue(StringUtils.trimString(input, input.length()).equals(input));
  59. }
  60. public void testPadding() throws Exception {
  61. String input = "test";
  62. assertTrue(StringUtils.leftPad(input, 6 + input.length(), ' ').equals(" test"));
  63. assertTrue(StringUtils.rightPad(input, 6 + input.length(), ' ').equals("test "));
  64. assertTrue(StringUtils.leftPad(input, input.length(), ' ').equals(input));
  65. assertTrue(StringUtils.rightPad(input, input.length(), ' ').equals(input));
  66. }
  67. public void testSHA1() throws Exception {
  68. assertTrue(StringUtils.getSHA1("blob 16\000what is up, doc?").equals(
  69. "bd9dbf5aae1a3862dd1526723246b20206e5fc37"));
  70. }
  71. public void testMD5() throws Exception {
  72. assertTrue(StringUtils.getMD5("blob 16\000what is up, doc?").equals(
  73. "77fb8d95331f0d557472f6776d3aedf6"));
  74. }
  75. public void testRootPath() throws Exception {
  76. String input = "/nested/path/to/repository";
  77. String output = "/nested/path/to";
  78. assertTrue(StringUtils.getRootPath(input).equals(output));
  79. assertTrue(StringUtils.getRootPath("repository").equals(""));
  80. }
  81. public void testStringsFromValue() throws Exception {
  82. List<String> strings = StringUtils.getStringsFromValue("A B C D");
  83. assertTrue(strings.size() == 4);
  84. assertTrue(strings.get(0).equals("A"));
  85. assertTrue(strings.get(1).equals("B"));
  86. assertTrue(strings.get(2).equals("C"));
  87. assertTrue(strings.get(3).equals("D"));
  88. }
  89. }