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