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