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

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