View Javadoc
1   /*
2    * Copyright (C) 2009, Google Inc. and others
3    *
4    * This program and the accompanying materials are made available under the
5    * terms of the Eclipse Distribution License v. 1.0 which is available at
6    * https://www.eclipse.org/org/documents/edl-v10.php.
7    *
8    * SPDX-License-Identifier: BSD-3-Clause
9    */
10  
11  package org.eclipse.jgit.util;
12  
13  import static org.junit.Assert.assertEquals;
14  import static org.junit.Assert.assertFalse;
15  import static org.junit.Assert.assertThrows;
16  import static org.junit.Assert.assertTrue;
17  
18  import org.junit.Test;
19  
20  public class StringUtilsTest {
21  	@Test
22  	public void testToLowerCaseChar() {
23  		assertEquals('a', StringUtils.toLowerCase('A'));
24  		assertEquals('z', StringUtils.toLowerCase('Z'));
25  
26  		assertEquals('a', StringUtils.toLowerCase('a'));
27  		assertEquals('z', StringUtils.toLowerCase('z'));
28  
29  		assertEquals((char) 0, StringUtils.toLowerCase((char) 0));
30  		assertEquals((char) 0xffff, StringUtils.toLowerCase((char) 0xffff));
31  	}
32  
33  	@Test
34  	public void testToLowerCaseString() {
35  		assertEquals("\n abcdefghijklmnopqrstuvwxyz\n", StringUtils
36  				.toLowerCase("\n ABCDEFGHIJKLMNOPQRSTUVWXYZ\n"));
37  	}
38  
39  	@Test
40  	public void testEqualsIgnoreCase1() {
41  		final String a = "FOO";
42  		assertTrue(StringUtils.equalsIgnoreCase(a, a));
43  	}
44  
45  	@Test
46  	public void testEqualsIgnoreCase2() {
47  		assertFalse(StringUtils.equalsIgnoreCase("a", ""));
48  	}
49  
50  	@Test
51  	public void testEqualsIgnoreCase3() {
52  		assertFalse(StringUtils.equalsIgnoreCase("a", "b"));
53  		assertFalse(StringUtils.equalsIgnoreCase("ac", "ab"));
54  	}
55  
56  	@Test
57  	public void testEqualsIgnoreCase4() {
58  		assertTrue(StringUtils.equalsIgnoreCase("a", "a"));
59  		assertTrue(StringUtils.equalsIgnoreCase("A", "a"));
60  		assertTrue(StringUtils.equalsIgnoreCase("a", "A"));
61  	}
62  
63  	@Test
64  	public void testReplaceLineBreaks() {
65  		assertEquals("a b c ",
66  				StringUtils.replaceLineBreaksWithSpace("a b\nc\r"));
67  		assertEquals("a b c ",
68  				StringUtils.replaceLineBreaksWithSpace("a b\nc\n"));
69  		assertEquals("a b c ",
70  				StringUtils.replaceLineBreaksWithSpace("a b\nc\r\n"));
71  		assertEquals("a b c d",
72  				StringUtils.replaceLineBreaksWithSpace("a\r\nb\nc d"));
73  	}
74  
75  	@Test
76  	public void testFormatWithSuffix() {
77  		assertEquals("1023", StringUtils.formatWithSuffix(1023));
78  		assertEquals("1k", StringUtils.formatWithSuffix(1024));
79  		assertEquals("1025", StringUtils.formatWithSuffix(1025));
80  		assertEquals("1048575", StringUtils.formatWithSuffix(1024 * 1024 - 1));
81  		assertEquals("1m", StringUtils.formatWithSuffix(1024 * 1024));
82  		assertEquals("1048577", StringUtils.formatWithSuffix(1024 * 1024 + 1));
83  		assertEquals("1073741823",
84  				StringUtils.formatWithSuffix(1024 * 1024 * 1024 - 1));
85  		assertEquals("1g", StringUtils.formatWithSuffix(1024 * 1024 * 1024));
86  		assertEquals("1073741825",
87  				StringUtils.formatWithSuffix(1024 * 1024 * 1024 + 1));
88  		assertEquals("3k", StringUtils.formatWithSuffix(3 * 1024));
89  		assertEquals("3m", StringUtils.formatWithSuffix(3 * 1024 * 1024));
90  		assertEquals("2050k",
91  				StringUtils.formatWithSuffix(2 * 1024 * 1024 + 2048));
92  		assertEquals("3g",
93  				StringUtils.formatWithSuffix(3L * 1024 * 1024 * 1024));
94  		assertEquals("3000", StringUtils.formatWithSuffix(3000));
95  		assertEquals("3000000", StringUtils.formatWithSuffix(3_000_000));
96  		assertEquals("1953125k", StringUtils.formatWithSuffix(2_000_000_000));
97  		assertEquals("2000000010", StringUtils.formatWithSuffix(2_000_000_010));
98  		assertEquals("3000000000",
99  				StringUtils.formatWithSuffix(3_000_000_000L));
100 	}
101 
102 	@Test
103 	public void testParseWithSuffix() {
104 		assertEquals(1024, StringUtils.parseIntWithSuffix("1k", true));
105 		assertEquals(1024, StringUtils.parseIntWithSuffix("1 k", true));
106 		assertEquals(1024, StringUtils.parseIntWithSuffix("1  k", true));
107 		assertEquals(1024, StringUtils.parseIntWithSuffix(" \t1  k  \n", true));
108 		assertEquals(1024, StringUtils.parseIntWithSuffix("1k", false));
109 		assertEquals(1024, StringUtils.parseIntWithSuffix("1K", false));
110 		assertEquals(1024 * 1024, StringUtils.parseIntWithSuffix("1m", false));
111 		assertEquals(1024 * 1024, StringUtils.parseIntWithSuffix("1M", false));
112 		assertEquals(-1024 * 1024,
113 				StringUtils.parseIntWithSuffix("-1M", false));
114 		assertEquals(1_000_000,
115 				StringUtils.parseIntWithSuffix("  1000000\r\n", false));
116 		assertEquals(1024 * 1024 * 1024,
117 				StringUtils.parseIntWithSuffix("1g", false));
118 		assertEquals(1024 * 1024 * 1024,
119 				StringUtils.parseIntWithSuffix("1G", false));
120 		assertEquals(3L * 1024 * 1024 * 1024,
121 				StringUtils.parseLongWithSuffix("3g", false));
122 		assertEquals(3L * 1024 * 1024 * 1024,
123 				StringUtils.parseLongWithSuffix("3G", false));
124 		assertThrows(NumberFormatException.class,
125 				() -> StringUtils.parseIntWithSuffix("2G", false));
126 		assertEquals(2L * 1024 * 1024 * 1024,
127 				StringUtils.parseLongWithSuffix("2G", false));
128 		assertThrows(NumberFormatException.class,
129 				() -> StringUtils.parseLongWithSuffix("-1m", true));
130 		assertThrows(NumberFormatException.class,
131 				() -> StringUtils.parseLongWithSuffix("-1000", true));
132 		assertThrows(StringIndexOutOfBoundsException.class,
133 				() -> StringUtils.parseLongWithSuffix("", false));
134 		assertThrows(StringIndexOutOfBoundsException.class,
135 				() -> StringUtils.parseLongWithSuffix("   \t   \n", false));
136 		assertThrows(StringIndexOutOfBoundsException.class,
137 				() -> StringUtils.parseLongWithSuffix("k", false));
138 		assertThrows(StringIndexOutOfBoundsException.class,
139 				() -> StringUtils.parseLongWithSuffix("m", false));
140 		assertThrows(StringIndexOutOfBoundsException.class,
141 				() -> StringUtils.parseLongWithSuffix("g", false));
142 		assertThrows(NumberFormatException.class,
143 				() -> StringUtils.parseLongWithSuffix("1T", false));
144 		assertThrows(NumberFormatException.class,
145 				() -> StringUtils.parseLongWithSuffix("1t", false));
146 		assertThrows(NumberFormatException.class,
147 				() -> StringUtils.parseLongWithSuffix("Nonumber", false));
148 		assertThrows(NumberFormatException.class,
149 				() -> StringUtils.parseLongWithSuffix("0x001f", false));
150 		assertThrows(NumberFormatException.class,
151 				() -> StringUtils.parseLongWithSuffix("beef", false));
152 		assertThrows(NumberFormatException.class,
153 				() -> StringUtils.parseLongWithSuffix("8000000000000000000G",
154 						false));
155 	}
156 }