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