1. Project Clover database Mon Dec 22 2025 14:59:38 CST
  2. Package org.europa.together.utils

File StringUtilsTest.java

 

Code metrics

0
107
30
1
307
246
30
0.28
3.57
30
1

Classes

Class Line # Actions
StringUtilsTest 28 107 0% 30 0
1.0100%
 

Contributing tests

This file is covered by 30 tests. .

Source view

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"})
 
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   
 
38  1 toggle @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   
 
48  1 toggle @Test
49    void concatString() {
50  1 assertEquals("ABCD", StringUtils.concatString("A", "B", "C", "D"));
51    }
52   
 
53  1 toggle @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   
 
62  1 toggle @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   
 
70  1 toggle @Test
71    void failDecodeBase64Url() {
72  1 assertEquals("", StringUtils.base64UrlDecoding(null));
73    }
74   
 
75  1 toggle @Test
76    void generateStringOfLength() {
77  1 assertEquals("012345678", StringUtils.generateStringOfLength(9));
78  1 assertEquals("01234567890123", StringUtils.generateStringOfLength(14));
79    }
80   
 
81  1 toggle @Test
82    void byteToString() {
83  1 assertEquals("23", StringUtils.byteToString("#".getBytes()));
84    }
85   
 
86  1 toggle @Test
87    void isEmpty() {
88  1 assertTrue(StringUtils.isEmpty(""));
89  1 assertTrue(StringUtils.isEmpty(null));
90    }
91   
 
92  1 toggle @Test
93    void isNotEmpty() {
94  1 assertFalse(StringUtils.isEmpty(" "));
95  1 assertFalse(StringUtils.isEmpty("test"));
96    }
97   
 
98  1 toggle @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   
 
109  1 toggle @Test
110    void shaHashToInt() {
111  1 assertEquals(2470,
112    StringUtils.hashToInt(cryptoTools.calculateHash("sha", HashAlgorithm.SHA)));
113    }
114   
 
115  1 toggle @Test
116    void sha256HashToInt() {
117  1 assertEquals(4340,
118    StringUtils.hashToInt(cryptoTools.calculateHash("sha-256", HashAlgorithm.SHA256)));
119    }
120   
 
121  1 toggle @Test
122    void sha512HashToInt() {
123  1 assertEquals(8941,
124    StringUtils.hashToInt(cryptoTools.calculateHash("sha-512", HashAlgorithm.SHA512)));
125    }
126   
 
127  1 toggle @Test
128    void md5HashToInt() {
129  1 assertEquals(2235,
130    StringUtils.hashToInt(cryptoTools.calculateHash("md5", HashAlgorithm.MD5)));
131    }
132   
 
133  1 toggle @Test
134    void generateUUID() {
135    //UUID: a3ae3672-22bc-411f-81c5-103652a5846e
136  1 String uuid = StringUtils.generateUUID();
137  1 assertNotNull(uuid);
138  1 assertEquals(36, uuid.length());
139    }
140   
 
141  1 toggle @Test
142    void generateLoremIpsum() {
143  1 String full = StringUtils.generateLoremIpsum(0);
144  1 String reducede = StringUtils.generateLoremIpsum(50);
145  1 String fail_01 = StringUtils.generateLoremIpsum(-1);
146  1 String fail_02 = StringUtils.generateLoremIpsum(4000);
147   
148  1 assertEquals(2127, full.length());
149  1 assertEquals(2127, fail_01.length());
150  1 assertEquals(2127, fail_02.length());
151   
152  1 assertEquals(50, reducede.length());
153  1 assertEquals("Lorem ipsum dolor sit amet, consetetur sadipscing ", reducede);
154    }
155   
 
156  1 toggle @Test
157    void replaceXmlCharacters() {
158  1 String orginal = "<root>&nbsp;</root> \" 'home\\dir'";
159  1 String replaced = "&#0060;root&#0062;&#0038;nbsp;&#0060;/root&#0062; &#0034; &#0039;home\\dir&#0039;";
160   
161  1 assertEquals(replaced, StringUtils.escapeXmlCharacters(orginal));
162    }
163   
 
164  1 toggle @Test
165    void removeWhitespacesForReplaceXmlCharacters() {
166  1 String input = " hallo welt! ";
167  1 String result = "hallo welt!";
168   
169  1 assertEquals(result, StringUtils.escapeXmlCharacters(input));
170    }
171   
 
172  1 toggle @Test
173    void failRemoveBom() throws Exception {
174  1 assertThrows(Exception.class, () -> {
175  1 StringUtils.skipBom(null);
176    });
177  1 assertThrows(Exception.class, () -> {
178  1 StringUtils.skipBom("");
179    });
180    }
181   
 
182  1 toggle @Test
183    void withoutBom() throws Exception {
184  1 String check = "Text File without BOM - Byte Order Mark";
185  1 String bom = FileUtils.readFileStream(new File(FILE_PATH + "/no-BOM"));
186   
187  1 assertNotNull(bom);
188  1 assertEquals(check, StringUtils.skipBom(bom));
189    }
190   
 
191  1 toggle @Test
192    void removeUtf8Bom() throws Exception {
193  1 String check = " UTF-8 BOM";
194  1 LOGGER.log("Test:" + check, LogLevel.DEBUG);
195   
196  1 String bom = FileUtils.readFileStream(new File(FILE_PATH + "/utf-8-BOM"));
197  1 assertNotNull(bom);
198   
199  1 assertEquals(check, StringUtils.skipBom(bom));
200    }
201   
 
202  1 toggle @Test
203    void removeUtf16LeBom() throws Exception {
204  1 String check = " UTF-16 BOM LE";
205  1 LOGGER.log("Test:" + check, LogLevel.DEBUG);
206   
207  1 String bom = FileUtils.readFileStream(new File(FILE_PATH + "/utf-16-BOM_LE"),
208    ByteOrderMark.UTF_16LE);
209  1 assertNotNull(bom);
210   
211  1 assertEquals(new String(check.getBytes(), Charset.forName("UTF-16LE")),
212    StringUtils.skipBom(bom));
213    }
214   
 
215  1 toggle @Test
216    void removeUtf16BeBom() throws Exception {
217  1 String check = " UTF-16 BOM BE";
218  1 LOGGER.log("Test:" + check, LogLevel.DEBUG);
219   
220  1 String bom = FileUtils.readFileStream(new File(FILE_PATH + "/utf-16-BOM_BE"),
221    ByteOrderMark.UTF_16BE);
222  1 assertNotNull(bom);
223   
224  1 assertEquals(new String(check.getBytes(), Charset.forName("UTF-16BE")),
225    StringUtils.skipBom(bom));
226    }
227   
 
228  1 toggle @Test
229    void removeUtf32LeBom() throws Exception {
230  1 String check = " UTF-32 BOM LE";
231  1 LOGGER.log("Test:" + check, LogLevel.DEBUG);
232   
233  1 String bom = FileUtils.readFileStream(new File(FILE_PATH + "/utf-32-BOM_LE"),
234    ByteOrderMark.UTF_32LE);
235  1 assertNotNull(bom);
236   
237  1 assertEquals(new String(check.getBytes(), Charset.forName("UTF-32LE")),
238    StringUtils.skipBom(bom));
239    }
240   
 
241  1 toggle @Test
242    void removeUtf32BeBom() throws Exception {
243  1 String check = " UTF-32 BOM BE";
244  1 LOGGER.log("Test:" + check, LogLevel.DEBUG);
245   
246  1 String bom = FileUtils.readFileStream(new File(FILE_PATH + "/utf-32-BOM_BE"),
247    ByteOrderMark.UTF_32BE);
248  1 assertNotNull(bom);
249   
250  1 assertEquals(new String(check.getBytes(), Charset.forName("UTF-32BE")),
251    StringUtils.skipBom(bom));
252    }
253   
 
254  1 toggle @Test
255    void shrinkContent() throws Exception {
256  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>";
257  1 String content = FileUtils.readFileStream(new File(Constraints.SYSTEM_APP_DIR + "/target/test-classes/shrink.txt"));
258   
259  1 LOGGER.log("Shrink:", LogLevel.DEBUG);
260  1 LOGGER.log(StringUtils.shrink(content), LogLevel.DEBUG);
261   
262  1 assertEquals(shrink, StringUtils.shrink(content));
263    }
264   
 
265  1 toggle @Test
266    void failCreateDateFromString() throws Exception {
267  1 assertThrows(Exception.class, () -> {
268  1 StringUtils.createDateFromString("2020.01.01-12:00:01");
269    });
270    }
271   
 
272  1 toggle @Test
273    void createDateFromString() throws ParseException {
274   
275  1 Date convert = StringUtils.createDateFromString("2020-01-01 12:00:01");
276  1 Date compare = new Date();
277  1 compare.setTime(convert.getTime());
278   
279  1 assertEquals(compare, convert);
280    }
281   
 
282  1 toggle @Test
283    void splitDigitStringToIntegerListTest() {
284  1 List<Integer> integerList = new ArrayList<>();
285  1 integerList.add(1);
286  1 integerList.add(2);
287  1 integerList.add(3);
288  1 integerList.add(4);
289  1 integerList.add(5);
290  1 integerList.add(6);
291   
292  1 assertEquals(integerList, StringUtils.splitDigitStringToIntegerList("123456"));
293    }
294   
 
295  1 toggle @Test
296    void failSplitDigitStringToIntegerListTest() {
297  1 List<Integer> integerList = new ArrayList<>();
298  1 integerList.add(1);
299  1 integerList.add(2);
300  1 integerList.add(3);
301   
302  1 assertNotEquals(integerList, StringUtils.splitDigitStringToIntegerList("123z456"));
303  1 assertNotEquals(integerList, StringUtils.splitDigitStringToIntegerList("1@456"));
304  1 assertNotEquals(integerList, StringUtils.splitDigitStringToIntegerList(".3z456"));
305  1 assertNotEquals(integerList, StringUtils.splitDigitStringToIntegerList("X23z456"));
306    }
307    }