1 |
|
package org.europa.together.utils; |
2 |
|
|
3 |
|
import java.io.File; |
4 |
|
import java.lang.reflect.Constructor; |
5 |
|
import java.nio.charset.Charset; |
6 |
|
import java.text.ParseException; |
7 |
|
import java.util.ArrayList; |
8 |
|
import java.util.Date; |
9 |
|
import java.util.List; |
10 |
|
import org.europa.together.JUnit5Preperator; |
11 |
|
import org.europa.together.application.LogbackLogger; |
12 |
|
import org.europa.together.business.CryptoTools; |
13 |
|
import org.europa.together.business.Logger; |
14 |
|
import org.europa.together.domain.ByteOrderMark; |
15 |
|
import org.europa.together.domain.HashAlgorithm; |
16 |
|
import org.europa.together.domain.LogLevel; |
17 |
|
import static org.junit.jupiter.api.Assertions.*; |
18 |
|
import org.junit.jupiter.api.Test; |
19 |
|
import org.junit.jupiter.api.extension.ExtendWith; |
20 |
|
import org.springframework.beans.factory.annotation.Autowired; |
21 |
|
import org.springframework.test.context.ContextConfiguration; |
22 |
|
import org.springframework.test.context.junit.jupiter.SpringExtension; |
23 |
|
|
24 |
|
@SuppressWarnings("unchecked") |
25 |
|
@ExtendWith({JUnit5Preperator.class}) |
26 |
|
@ExtendWith(SpringExtension.class) |
27 |
|
@ContextConfiguration(locations = {"/applicationContext.xml"}) |
|
|
| 100% |
Uncovered Elements: 0 (127) |
Complexity: 27 |
Complexity Density: 0.27 |
|
28 |
|
public class StringUtilsTest { |
29 |
|
|
30 |
|
private static final Logger LOGGER = new LogbackLogger(StringUtilsTest.class); |
31 |
|
|
32 |
|
private static final String FILE_PATH |
33 |
|
= Constraints.SYSTEM_APP_DIR + "/src/test/resources/org/europa/together/bom"; |
34 |
|
|
35 |
|
@Autowired |
36 |
|
private CryptoTools cryptoTools; |
37 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
1PASS
|
|
38 |
1 |
@Test... |
39 |
|
void privateConstructor() throws Exception { |
40 |
1 |
Constructor<StringUtils> clazz |
41 |
|
= StringUtils.class.getDeclaredConstructor(); |
42 |
1 |
clazz.setAccessible(true); |
43 |
1 |
assertThrows(Exception.class, () -> { |
44 |
1 |
StringUtils call = clazz.newInstance(); |
45 |
|
}); |
46 |
|
} |
47 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
1PASS
|
|
48 |
1 |
@Test... |
49 |
|
void concatString() { |
50 |
1 |
assertEquals("ABCD", StringUtils.concatString("A", "B", "C", "D")); |
51 |
|
} |
52 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
1PASS
|
|
53 |
1 |
@Test... |
54 |
|
void encodeBase64Url() { |
55 |
1 |
String url = "https://elmar-dott.com"; |
56 |
1 |
String base64url = "aHR0cHM6Ly9lbG1hci1kb3R0LmNvbQ=="; |
57 |
|
|
58 |
1 |
assertEquals(base64url, StringUtils.base64UrlEncoding(url)); |
59 |
|
|
60 |
|
} |
61 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
1PASS
|
|
62 |
1 |
@Test... |
63 |
|
void decodeBase64Url() { |
64 |
1 |
String url = "https://elmar-dott.com"; |
65 |
1 |
String base64url = "aHR0cHM6Ly9lbG1hci1kb3R0LmNvbQ=="; |
66 |
|
|
67 |
1 |
assertEquals(url, StringUtils.base64UrlDecoding(base64url)); |
68 |
|
} |
69 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
1PASS
|
|
70 |
1 |
@Test... |
71 |
|
void failDecodeBase64Url() { |
72 |
1 |
assertEquals("", StringUtils.base64UrlDecoding(null)); |
73 |
|
} |
74 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
1PASS
|
|
75 |
1 |
@Test... |
76 |
|
void generateStringOfLength() { |
77 |
1 |
assertEquals("012345678", StringUtils.generateStringOfLength(9)); |
78 |
1 |
assertEquals("01234567890123", StringUtils.generateStringOfLength(14)); |
79 |
|
} |
80 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
1PASS
|
|
81 |
1 |
@Test... |
82 |
|
void byteToString() { |
83 |
1 |
assertEquals("23", StringUtils.byteToString("#".getBytes())); |
84 |
|
} |
85 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
1PASS
|
|
86 |
1 |
@Test... |
87 |
|
void isEmpty() { |
88 |
1 |
assertTrue(StringUtils.isEmpty("")); |
89 |
1 |
assertTrue(StringUtils.isEmpty(null)); |
90 |
|
} |
91 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
1PASS
|
|
92 |
1 |
@Test... |
93 |
|
void isNotEmpty() { |
94 |
1 |
assertFalse(StringUtils.isEmpty(" ")); |
95 |
1 |
assertFalse(StringUtils.isEmpty("test")); |
96 |
|
} |
97 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 1 |
Complexity Density: 0.17 |
1PASS
|
|
98 |
1 |
@Test... |
99 |
|
void stringListBuilder() { |
100 |
1 |
List<String> check = new ArrayList<>(); |
101 |
1 |
check.add("foo"); |
102 |
1 |
check.add("more"); |
103 |
|
|
104 |
1 |
assertEquals(check, StringUtils.stringListBuilder("foo", "more")); |
105 |
1 |
assertEquals(check, StringUtils.stringListBuilder("foo", null, "more")); |
106 |
1 |
assertTrue(StringUtils.stringListBuilder().isEmpty()); |
107 |
|
} |
108 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
1PASS
|
|
109 |
1 |
@Test... |
110 |
|
void shaHashToInt() { |
111 |
1 |
assertEquals(2854, StringUtils.hashToInt(cryptoTools.calculateHash("", HashAlgorithm.SHA))); |
112 |
1 |
assertEquals(2725, StringUtils.hashToInt(cryptoTools.calculateHash(" ", HashAlgorithm.SHA))); |
113 |
1 |
assertEquals(2863, StringUtils.hashToInt(cryptoTools.calculateHash("SHA", HashAlgorithm.SHA))); |
114 |
1 |
assertEquals(2470, StringUtils.hashToInt(cryptoTools.calculateHash("sha", HashAlgorithm.SHA))); |
115 |
|
} |
116 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
1PASS
|
|
117 |
1 |
@Test... |
118 |
|
void sha256HashToInt() { |
119 |
1 |
assertEquals(4375, StringUtils.hashToInt(cryptoTools.calculateHash("", HashAlgorithm.SHA256))); |
120 |
1 |
assertEquals(4653, StringUtils.hashToInt(cryptoTools.calculateHash(" ", HashAlgorithm.SHA256))); |
121 |
1 |
assertEquals(4664, StringUtils.hashToInt(cryptoTools.calculateHash("SHA-256", HashAlgorithm.SHA256))); |
122 |
1 |
assertEquals(4340, StringUtils.hashToInt(cryptoTools.calculateHash("sha-256", HashAlgorithm.SHA256))); |
123 |
|
} |
124 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
1PASS
|
|
125 |
1 |
@Test... |
126 |
|
void sha512HashToInt() { |
127 |
1 |
assertEquals(8933, StringUtils.hashToInt(cryptoTools.calculateHash("", HashAlgorithm.SHA512))); |
128 |
1 |
assertEquals(9220, StringUtils.hashToInt(cryptoTools.calculateHash(" ", HashAlgorithm.SHA512))); |
129 |
1 |
assertEquals(9028, StringUtils.hashToInt(cryptoTools.calculateHash("SHA-512", HashAlgorithm.SHA512))); |
130 |
1 |
assertEquals(8941, StringUtils.hashToInt(cryptoTools.calculateHash("sha-512", HashAlgorithm.SHA512))); |
131 |
|
} |
132 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
1PASS
|
|
133 |
1 |
@Test... |
134 |
|
void md5HashToInt() { |
135 |
1 |
assertEquals(2211, StringUtils.hashToInt(cryptoTools.calculateHash("", HashAlgorithm.MD5))); |
136 |
1 |
assertEquals(2262, StringUtils.hashToInt(cryptoTools.calculateHash(" ", HashAlgorithm.MD5))); |
137 |
1 |
assertEquals(2012, StringUtils.hashToInt(cryptoTools.calculateHash("MD5", HashAlgorithm.MD5))); |
138 |
1 |
assertEquals(2235, StringUtils.hashToInt(cryptoTools.calculateHash("md5", HashAlgorithm.MD5))); |
139 |
|
} |
140 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
1PASS
|
|
141 |
1 |
@Test... |
142 |
|
void generateUUID() { |
143 |
|
|
144 |
1 |
String uuid = StringUtils.generateUUID(); |
145 |
1 |
assertNotNull(uuid); |
146 |
1 |
assertEquals(36, uuid.length()); |
147 |
|
} |
148 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (9) |
Complexity: 1 |
Complexity Density: 0.11 |
1PASS
|
|
149 |
1 |
@Test... |
150 |
|
void generateLoremIpsum() { |
151 |
1 |
String full = StringUtils.generateLoremIpsum(0); |
152 |
1 |
String reducede = StringUtils.generateLoremIpsum(50); |
153 |
1 |
String fail_01 = StringUtils.generateLoremIpsum(-1); |
154 |
1 |
String fail_02 = StringUtils.generateLoremIpsum(4000); |
155 |
|
|
156 |
1 |
assertEquals(2127, full.length()); |
157 |
1 |
assertEquals(2127, fail_01.length()); |
158 |
1 |
assertEquals(2127, fail_02.length()); |
159 |
|
|
160 |
1 |
assertEquals(50, reducede.length()); |
161 |
1 |
assertEquals("Lorem ipsum dolor sit amet, consetetur sadipscing ", reducede); |
162 |
|
} |
163 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
1PASS
|
|
164 |
1 |
@Test... |
165 |
|
void replaceXmlCharacters() { |
166 |
1 |
String orginal = "<root> </root> \" 'home\\dir'"; |
167 |
1 |
String replaced = "<root>&nbsp;</root> " 'home\\dir'"; |
168 |
1 |
assertEquals(replaced, StringUtils.escapeXmlCharacters(orginal)); |
169 |
|
} |
170 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
1PASS
|
|
171 |
1 |
@Test... |
172 |
|
void failRemoveBom() throws Exception { |
173 |
1 |
assertThrows(Exception.class, () -> { |
174 |
1 |
StringUtils.skipBom(null); |
175 |
|
}); |
176 |
1 |
assertThrows(Exception.class, () -> { |
177 |
1 |
StringUtils.skipBom(""); |
178 |
|
}); |
179 |
|
} |
180 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
1PASS
|
|
181 |
1 |
@Test... |
182 |
|
void withoutBom() throws Exception { |
183 |
1 |
String check = "Text File without BOM - Byte Order Mark"; |
184 |
1 |
String bom = FileUtils.readFileStream(new File(FILE_PATH + "/no-BOM")); |
185 |
|
|
186 |
1 |
assertNotNull(bom); |
187 |
1 |
assertEquals(check, StringUtils.skipBom(bom)); |
188 |
|
} |
189 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
1PASS
|
|
190 |
1 |
@Test... |
191 |
|
void removeUtf8Bom() throws Exception { |
192 |
1 |
String check = " UTF-8 BOM"; |
193 |
1 |
LOGGER.log("Test:" + check, LogLevel.DEBUG); |
194 |
|
|
195 |
1 |
String bom = FileUtils.readFileStream(new File(FILE_PATH + "/utf-8-BOM")); |
196 |
1 |
assertNotNull(bom); |
197 |
|
|
198 |
1 |
assertEquals(check, StringUtils.skipBom(bom)); |
199 |
|
} |
200 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
1PASS
|
|
201 |
1 |
@Test... |
202 |
|
void removeUtf16LeBom() throws Exception { |
203 |
1 |
String check = " UTF-16 BOM LE"; |
204 |
1 |
LOGGER.log("Test:" + check, LogLevel.DEBUG); |
205 |
|
|
206 |
1 |
String bom = FileUtils.readFileStream(new File(FILE_PATH + "/utf-16-BOM_LE"), |
207 |
|
ByteOrderMark.UTF_16LE); |
208 |
1 |
assertNotNull(bom); |
209 |
|
|
210 |
1 |
assertEquals(new String(check.getBytes(), Charset.forName("UTF-16LE")), |
211 |
|
StringUtils.skipBom(bom)); |
212 |
|
} |
213 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
1PASS
|
|
214 |
1 |
@Test... |
215 |
|
void removeUtf16BeBom() throws Exception { |
216 |
1 |
String check = " UTF-16 BOM BE"; |
217 |
1 |
LOGGER.log("Test:" + check, LogLevel.DEBUG); |
218 |
|
|
219 |
1 |
String bom = FileUtils.readFileStream(new File(FILE_PATH + "/utf-16-BOM_BE"), |
220 |
|
ByteOrderMark.UTF_16BE); |
221 |
1 |
assertNotNull(bom); |
222 |
|
|
223 |
1 |
assertEquals(new String(check.getBytes(), Charset.forName("UTF-16BE")), |
224 |
|
StringUtils.skipBom(bom)); |
225 |
|
} |
226 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
1PASS
|
|
227 |
1 |
@Test... |
228 |
|
void removeUtf32LeBom() throws Exception { |
229 |
1 |
String check = " UTF-32 BOM LE"; |
230 |
1 |
LOGGER.log("Test:" + check, LogLevel.DEBUG); |
231 |
|
|
232 |
1 |
String bom = FileUtils.readFileStream(new File(FILE_PATH + "/utf-32-BOM_LE"), |
233 |
|
ByteOrderMark.UTF_32LE); |
234 |
1 |
assertNotNull(bom); |
235 |
|
|
236 |
1 |
assertEquals(new String(check.getBytes(), Charset.forName("UTF-32LE")), |
237 |
|
StringUtils.skipBom(bom)); |
238 |
|
} |
239 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
1PASS
|
|
240 |
1 |
@Test... |
241 |
|
void removeUtf32BeBom() throws Exception { |
242 |
1 |
String check = " UTF-32 BOM BE"; |
243 |
1 |
LOGGER.log("Test:" + check, LogLevel.DEBUG); |
244 |
|
|
245 |
1 |
String bom = FileUtils.readFileStream(new File(FILE_PATH + "/utf-32-BOM_BE"), |
246 |
|
ByteOrderMark.UTF_32BE); |
247 |
1 |
assertNotNull(bom); |
248 |
|
|
249 |
1 |
assertEquals(new String(check.getBytes(), Charset.forName("UTF-32BE")), |
250 |
|
StringUtils.skipBom(bom)); |
251 |
|
} |
252 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
1PASS
|
|
253 |
1 |
@Test... |
254 |
|
void shrinkContent() throws Exception { |
255 |
1 |
String shrink = " = \"Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy \" + \"eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam \" + \"voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet \" + \"clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit \" + \"amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam \" + \"nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed \" + \"diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. \" + \"Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor \" + \"sitView Generated Project Site amet. Lorem ipsum dolor sit amet, consetetur \" + \"sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore \" + \"magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo \" + \"dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus \" + \"est Lorem ipsum dolor sit amet.\\n\" function createCache() { var keys = []; function cache(key, value) { if (keys.push(key + \" \") ><catalog><cd><title></title><artist></artist><country></country><company></company><price></price><year></year></cd></catalog>"; |
256 |
1 |
String content = FileUtils.readFileStream(new File(Constraints.SYSTEM_APP_DIR + "/target/test-classes/shrink.txt")); |
257 |
|
|
258 |
1 |
LOGGER.log("Shrink:", LogLevel.DEBUG); |
259 |
1 |
LOGGER.log(StringUtils.shrink(content), LogLevel.DEBUG); |
260 |
|
|
261 |
1 |
assertEquals(shrink, StringUtils.shrink(content)); |
262 |
|
} |
263 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
1PASS
|
|
264 |
1 |
@Test... |
265 |
|
void failCreateDateFromString() throws Exception { |
266 |
1 |
assertThrows(Exception.class, () -> { |
267 |
1 |
StringUtils.createDateFromString("2020.01.01-12:00:01"); |
268 |
|
}); |
269 |
|
} |
270 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
1PASS
|
|
271 |
1 |
@Test... |
272 |
|
void createDateFromString() throws ParseException { |
273 |
|
|
274 |
1 |
Date convert = StringUtils.createDateFromString("2020-01-01 12:00:01"); |
275 |
1 |
Date compare = new Date(); |
276 |
1 |
compare.setTime(convert.getTime()); |
277 |
|
|
278 |
1 |
assertEquals(compare, convert); |
279 |
|
} |
280 |
|
} |