James Moger
2012-02-02 d394d950100a97b7d73f0e162b64b0b8f3cef988
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...";
7e8873 79         assertEquals(output, StringUtils.trimShortLog(input));
JM 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 {
JM 115         List<String> strings = StringUtils.getStringsFromValue("A B C D");
7e8873 116         assertEquals(4, strings.size());
JM 117         assertEquals("A", strings.get(0));
118         assertEquals("B", strings.get(1));
119         assertEquals("C", strings.get(2));
120         assertEquals("D", strings.get(3));
f339f5 121     }
831469 122
7e8873 123     @Test
831469 124     public void testStringsFromValue2() throws Exception {
JM 125         List<String> strings = StringUtils.getStringsFromValue("common/* libraries/*");
7e8873 126         assertEquals(2, strings.size());
JM 127         assertEquals("common/*", strings.get(0));
128         assertEquals("libraries/*", strings.get(1));
831469 129     }
JM 130
7e8873 131     @Test
831469 132     public void testFuzzyMatching() throws Exception {
JM 133         assertTrue(StringUtils.fuzzyMatch("12345", "12345"));
134         assertTrue(StringUtils.fuzzyMatch("AbCdEf", "abcdef"));
135         assertTrue(StringUtils.fuzzyMatch("AbCdEf", "abc*"));
136         assertTrue(StringUtils.fuzzyMatch("AbCdEf", "*def"));
137         assertTrue(StringUtils.fuzzyMatch("AbCdEfHIJ", "abc*hij"));
138
139         assertFalse(StringUtils.fuzzyMatch("123", "12345"));
140         assertFalse(StringUtils.fuzzyMatch("AbCdEfHIJ", "abc*hhh"));
141     }
ec97f7 142 }