1
|
/**
|
2
|
* Copyright (C) 2007 EDIT
|
3
|
* European Distributed Institute of Taxonomy
|
4
|
* http://www.e-taxonomy.eu
|
5
|
*
|
6
|
* The contents of this file are subject to the Mozilla Public License Version 1.1
|
7
|
* See LICENSE.TXT at the top of this package for the full license terms.
|
8
|
*/
|
9
|
package eu.etaxonomy.cdm.common;
|
10
|
|
11
|
import static org.junit.Assert.assertNotNull;
|
12
|
|
13
|
import java.io.IOException;
|
14
|
import java.io.InputStream;
|
15
|
import java.util.regex.Pattern;
|
16
|
|
17
|
import org.apache.logging.log4j.LogManager;
|
18
|
import org.apache.logging.log4j.Logger;
|
19
|
import org.junit.Assert;
|
20
|
import org.junit.Test;
|
21
|
|
22
|
/**
|
23
|
* @author a.mueller
|
24
|
* @since 22.01.2008
|
25
|
*/
|
26
|
public class CdmUtilsTest {
|
27
|
|
28
|
private static final Logger logger = LogManager.getLogger(CdmUtilsTest.class);
|
29
|
|
30
|
/************************** TESTS ****************************************/
|
31
|
|
32
|
@Test
|
33
|
public void testFindLibrary() {
|
34
|
logger.debug("{}", CdmUtils.findLibrary(CdmUtils.class));
|
35
|
|
36
|
String library = CdmUtils.findLibrary(CdmUtils.class);
|
37
|
String endOfLibrary = "target/classes/eu/etaxonomy/cdm/common/CdmUtils.class";
|
38
|
String libraryContains = "/cdmlib-commons/";
|
39
|
|
40
|
Assert.assertTrue(library.endsWith(endOfLibrary));
|
41
|
Assert.assertTrue(library.contains(libraryContains));
|
42
|
}
|
43
|
|
44
|
/**
|
45
|
* This is a default test for fast running any simple test. It can be overriden and ignored whenever needed.
|
46
|
*/
|
47
|
@Test
|
48
|
public void testAny(){
|
49
|
String str = "Noms vernaculaires:";
|
50
|
if (! str.matches("Nom(s)? vernaculaire(s)?\\:")){
|
51
|
System.out.println("NO");
|
52
|
}
|
53
|
}
|
54
|
|
55
|
@Test
|
56
|
public void testEqualsIgnoreWS(){
|
57
|
String str1 = null;
|
58
|
String str2 = null;
|
59
|
Assert.assertTrue(CdmUtils.equalsIgnoreWS(str1, str2));
|
60
|
|
61
|
str2 = "Any ";
|
62
|
Assert.assertFalse(CdmUtils.equalsIgnoreWS(str1, str2));
|
63
|
|
64
|
str1 = "Any ";
|
65
|
Assert.assertTrue(CdmUtils.equalsIgnoreWS(str1, str2));
|
66
|
|
67
|
str1 = "An y eer";
|
68
|
str2 = "A nye er";
|
69
|
Assert.assertTrue(CdmUtils.equalsIgnoreWS(str1, str2));
|
70
|
|
71
|
str1 = "An y eer";
|
72
|
str2 = "A nyfffe er";
|
73
|
Assert.assertFalse(CdmUtils.equalsIgnoreWS(str1, str2));
|
74
|
}
|
75
|
|
76
|
@Test
|
77
|
public void testquoteRegExWithWildcard(){
|
78
|
String regExBase = ".(*$[ms^";
|
79
|
String regEx = CdmUtils.quoteRegExWithWildcard(regExBase);
|
80
|
Assert.assertEquals("\\Q.(\\E.*\\Q$[ms^\\E", regEx);
|
81
|
boolean matches = ".(*$[ms^".matches(regEx);
|
82
|
Assert.assertTrue(matches);
|
83
|
matches = ".(aaaaaa$[ms^".matches(regEx);
|
84
|
Assert.assertTrue(matches);
|
85
|
matches = "b(aaaaaa$[ms^".matches(regEx);
|
86
|
Assert.assertFalse(matches);
|
87
|
|
88
|
regEx = CdmUtils.quoteRegExWithWildcard("*abc*");
|
89
|
Assert.assertEquals(".*\\Qabc\\E.*", regEx);
|
90
|
Assert.assertTrue("abc".matches(regEx));
|
91
|
Assert.assertTrue("a80/(--e*wabc?äe".matches(regEx));
|
92
|
}
|
93
|
|
94
|
/**
|
95
|
* This test can be used for functional testing of any task but should
|
96
|
* never be committed when failing.
|
97
|
*/
|
98
|
@Test
|
99
|
public void testSomething(){
|
100
|
String str = ".(*$[ms^";
|
101
|
String patQuote = Pattern.quote("str");
|
102
|
// System.out.println(patQuote);
|
103
|
// String matchQuote = Matcher.quoteReplacement(str);
|
104
|
// System.out.println(matchQuote);
|
105
|
// System.out.println(CdmUtils.quoteRegExWithWildcard(str));
|
106
|
}
|
107
|
|
108
|
@Test
|
109
|
public void testGetReadableResourceStream() {
|
110
|
String resourceFileName = "MUST-EXIST.txt";
|
111
|
try {
|
112
|
InputStream inputStream = CdmUtils.getReadableResourceStream(resourceFileName);
|
113
|
assertNotNull(inputStream);
|
114
|
} catch (IOException e) {
|
115
|
Assert.fail("IOException");
|
116
|
}
|
117
|
}
|
118
|
|
119
|
@Test
|
120
|
public void testConcat(){
|
121
|
String str1 = "Str1";
|
122
|
String str2 = "Str2";
|
123
|
String str3 = "Str3";
|
124
|
Assert.assertEquals("Str1;Str2", CdmUtils.concat(";", str1, str2));
|
125
|
Assert.assertEquals("Str2", CdmUtils.concat(";", null, str2));
|
126
|
Assert.assertEquals("Str1", CdmUtils.concat(";", str1, null));
|
127
|
Assert.assertNull(CdmUtils.concat(";", null, null));
|
128
|
Assert.assertEquals("Str1;Str2;Str3", CdmUtils.concat(";", str1, str2, str3));
|
129
|
Assert.assertEquals("Str1: Str2", CdmUtils.concat(": ", str1, str2));
|
130
|
Assert.assertEquals("Str2;Str3", CdmUtils.concat(";", "", str2, str3));
|
131
|
Assert.assertEquals("Str1;Str3", CdmUtils.concat(";", str1, "", str3));
|
132
|
Assert.assertEquals("Str1; ;Str3", CdmUtils.concat(";", str1, " ", str3));
|
133
|
}
|
134
|
}
|