James Moger
2011-06-02 a125cf6876e0edc5a2498df57a9df06d60b1f572
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;
19
20 import junit.framework.TestCase;
21
22 import com.gitblit.utils.StringUtils;
23
24 public class StringUtilsTest extends TestCase {
25
26     public void testIsEmpty() throws Exception {
27         assertTrue(StringUtils.isEmpty(null));
28         assertTrue(StringUtils.isEmpty(""));
29         assertTrue(StringUtils.isEmpty("  "));
30         assertFalse(StringUtils.isEmpty("A"));
31     }
32
33     public void testBreakLinesForHtml() throws Exception {
34         String input = "this\nis\r\na\rtest\r\n\r\nof\n\nline\r\rbreaking";
35         String output = "this<br/>is<br/>a<br/>test<br/><br/>of<br/><br/>line<br/><br/>breaking";
36         assertTrue(StringUtils.breakLinesForHtml(input).equals(output));
37     }
db653a 38
ec97f7 39     public void testEscapeForHtml() throws Exception {
JM 40         String input = "& < > \" \t";
41         String output_nochange = "&amp; &lt; &gt; &quot; \t";
42         String output_change = "&amp;&nbsp;&lt;&nbsp;&gt;&nbsp;&quot;&nbsp; &nbsp; &nbsp;";
43         assertTrue(StringUtils.escapeForHtml(input, false).equals(output_nochange));
44         assertTrue(StringUtils.escapeForHtml(input, true).equals(output_change));
45     }
46
47     public void testFlattenStrings() throws Exception {
48         String[] strings = { "A", "B", "C", "D" };
49         assertTrue(StringUtils.flattenStrings(Arrays.asList(strings)).equals("A B C D"));
50     }
51
52     public void testTrim() throws Exception {
53         String input = "123456789 123456789 123456789 123456789 123456789 123456789 123456789 ";
54         String output = "123456789 123456789 123456789 123456789 123456789 1234567...";
55         assertTrue(StringUtils.trimShortLog(input).equals(output));
56         assertTrue(StringUtils.trimString(input, input.length()).equals(input));
57     }
58
59     public void testPadding() throws Exception {
60         String input = "test";
61         assertTrue(StringUtils.leftPad(input, 6 + input.length(), ' ').equals("      test"));
62         assertTrue(StringUtils.rightPad(input, 6 + input.length(), ' ').equals("test      "));
63
64         assertTrue(StringUtils.leftPad(input, input.length(), ' ').equals(input));
65         assertTrue(StringUtils.rightPad(input, input.length(), ' ').equals(input));
66     }
db653a 67
ec97f7 68     public void testSHA1() throws Exception {
db653a 69         assertTrue(StringUtils.getSHA1("blob 16\000what is up, doc?").equals(
JM 70                 "bd9dbf5aae1a3862dd1526723246b20206e5fc37"));
ec97f7 71     }
db653a 72
ec97f7 73     public void testRootPath() throws Exception {
JM 74         String input = "/nested/path/to/repository";
75         String output = "/nested/path/to";
76         assertTrue(StringUtils.getRootPath(input).equals(output));
77         assertTrue(StringUtils.getRootPath("repository").equals(""));
78     }
79 }