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 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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 org.junit.Test;
  20. import com.gitblit.utils.StringUtils;
  21. public class StringUtilsTest extends GitblitUnitTest {
  22. @Test
  23. public void testIsEmpty() throws Exception {
  24. assertTrue(StringUtils.isEmpty((String)null));
  25. assertTrue(StringUtils.isEmpty(""));
  26. assertTrue(StringUtils.isEmpty(" "));
  27. assertFalse(StringUtils.isEmpty("A"));
  28. }
  29. @Test
  30. public void testIsEmptyCharArray() throws Exception {
  31. assertTrue(StringUtils.isEmpty((char[])null));
  32. assertTrue(StringUtils.isEmpty(new char[0]));
  33. assertTrue(StringUtils.isEmpty(new char[]{ ' ' }));
  34. assertTrue(StringUtils.isEmpty(new char[]{ ' '}));
  35. assertTrue(StringUtils.isEmpty(new char[]{ ' ', ' ' }));
  36. assertTrue(StringUtils.isEmpty(new char[]{ ' ', ' ', ' ' }));
  37. assertFalse(StringUtils.isEmpty(new char[]{ '\u0020', 'f' }));
  38. assertFalse(StringUtils.isEmpty(new char[]{ '\u0148', '\u0020' }));
  39. assertFalse(StringUtils.isEmpty(new char[]{ 'A' }));
  40. }
  41. @Test
  42. public void testBreakLinesForHtml() throws Exception {
  43. String input = "this\nis\r\na\rtest\r\n\r\nof\n\nline\r\rbreaking";
  44. String output = "this<br/>is<br/>a<br/>test<br/><br/>of<br/><br/>line<br/><br/>breaking";
  45. assertEquals(output, StringUtils.breakLinesForHtml(input));
  46. }
  47. @Test
  48. public void testEncodeUrl() throws Exception {
  49. String input = "test /";
  50. String output = "test%20%2F";
  51. assertEquals(output, StringUtils.encodeURL(input));
  52. }
  53. @Test
  54. public void testEscapeForHtml() throws Exception {
  55. String input = "& < > \" \t";
  56. String outputNoChange = "&amp; &lt; &gt; &quot; \t";
  57. String outputChange = "&amp;&nbsp;&lt;&nbsp;&gt;&nbsp;&quot;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
  58. assertEquals(outputNoChange, StringUtils.escapeForHtml(input, false));
  59. assertEquals(outputChange, StringUtils.escapeForHtml(input, true));
  60. }
  61. @Test
  62. public void testDecodeForHtml() throws Exception {
  63. String input = "&amp; &lt; &gt; &quot;";
  64. String output = "& < > \"";
  65. assertEquals(output, StringUtils.decodeFromHtml(input));
  66. }
  67. @Test
  68. public void testFlattenStrings() throws Exception {
  69. String[] strings = { "A", "B", "C", "D" };
  70. assertEquals("A B C D", StringUtils.flattenStrings(Arrays.asList(strings)));
  71. }
  72. @Test
  73. public void testTrim() throws Exception {
  74. String input = "123456789 123456789 123456789 123456789 123456789 123456789 123456789 ";
  75. String output = "123456789 123456789 123456789 123456789 123456789 1234567...";
  76. assertEquals(output, StringUtils.trimString(input, 60));
  77. assertEquals(input, StringUtils.trimString(input, input.length()));
  78. }
  79. @Test
  80. public void testPadding() throws Exception {
  81. String input = "test";
  82. assertEquals(" test", StringUtils.leftPad(input, 6 + input.length(), ' '));
  83. assertEquals("test ", StringUtils.rightPad(input, 6 + input.length(), ' '));
  84. assertEquals(input, StringUtils.leftPad(input, input.length(), ' '));
  85. assertEquals(input, StringUtils.rightPad(input, input.length(), ' '));
  86. }
  87. @Test
  88. public void testSHA1() throws Exception {
  89. assertEquals("bd9dbf5aae1a3862dd1526723246b20206e5fc37",
  90. StringUtils.getSHA1("blob 16\000what is up, doc?"));
  91. }
  92. @Test
  93. public void testMD5() throws Exception {
  94. assertEquals("77fb8d95331f0d557472f6776d3aedf6",
  95. StringUtils.getMD5("blob 16\000what is up, doc?"));
  96. }
  97. @Test
  98. public void testRootPath() throws Exception {
  99. String input = "/nested/path/to/repository";
  100. String output = "/nested/path/to";
  101. assertEquals(output, StringUtils.getRootPath(input));
  102. assertEquals("", StringUtils.getRootPath("repository"));
  103. }
  104. @Test
  105. public void testStringsFromValue() throws Exception {
  106. List<String> strings = StringUtils.getStringsFromValue("\"A A \" B \"C C\" D \"\" \"E\"");
  107. assertEquals(6, strings.size());
  108. assertEquals("A A", strings.get(0));
  109. assertEquals("B", strings.get(1));
  110. assertEquals("C C", strings.get(2));
  111. assertEquals("D", strings.get(3));
  112. assertEquals("", strings.get(4));
  113. assertEquals("E", strings.get(5));
  114. strings = StringUtils.getStringsFromValue("\"A A \", B, \"C C\", D, \"\", \"E\"", ",");
  115. assertEquals(6, strings.size());
  116. assertEquals("A A", strings.get(0));
  117. assertEquals("B", strings.get(1));
  118. assertEquals("C C", strings.get(2));
  119. assertEquals("D", strings.get(3));
  120. assertEquals("", strings.get(4));
  121. assertEquals("E", strings.get(5));
  122. }
  123. @Test
  124. public void testStringsFromValue2() throws Exception {
  125. List<String> strings = StringUtils.getStringsFromValue("common/* libraries/*");
  126. assertEquals(2, strings.size());
  127. assertEquals("common/*", strings.get(0));
  128. assertEquals("libraries/*", strings.get(1));
  129. }
  130. @Test
  131. public void testFuzzyMatching() throws Exception {
  132. assertTrue(StringUtils.fuzzyMatch("12345", "12345"));
  133. assertTrue(StringUtils.fuzzyMatch("AbCdEf", "abcdef"));
  134. assertTrue(StringUtils.fuzzyMatch("AbCdEf", "abc*"));
  135. assertTrue(StringUtils.fuzzyMatch("AbCdEf", "*def"));
  136. assertTrue(StringUtils.fuzzyMatch("AbCdEfHIJ", "abc*hij"));
  137. assertFalse(StringUtils.fuzzyMatch("123", "12345"));
  138. assertFalse(StringUtils.fuzzyMatch("AbCdEfHIJ", "abc*hhh"));
  139. }
  140. @Test
  141. public void testGetRepositoryPath() throws Exception {
  142. assertEquals("gitblit/gitblit.git", StringUtils.extractRepositoryPath("git://github.com/gitblit/gitblit.git", new String [] { ".*?://github.com/(.*)" }));
  143. assertEquals("gitblit.git", StringUtils.extractRepositoryPath("git://github.com/gitblit/gitblit.git", new String [] { ".*?://github.com/[^/].*?/(.*)" }));
  144. assertEquals("gitblit.git", StringUtils.extractRepositoryPath("git://github.com/gitblit/gitblit.git"));
  145. }
  146. }