Revision 47646
Added by Claudio Atzori almost 7 years ago
modules/cnr-misc-utils/tags/cnr-misc-utils-1.0.4/deploy.info | ||
---|---|---|
1 |
{"type_source": "SVN", "goal": "package -U -T 4C source:jar", "url": "http://svn-public.driver.research-infrastructures.eu/driver/dnet45/modules/cnr-misc-utils/trunk/", "deploy_repository": "dnet45-snapshots", "version": "4", "mail": "sandro.labruzzo@isti.cnr.it,michele.artini@isti.cnr.it, claudio.atzori@isti.cnr.it, alessia.bardi@isti.cnr.it", "deploy_repository_url": "http://maven.research-infrastructures.eu/nexus/content/repositories/dnet45-snapshots", "name": "cnr-misc-utils"} |
modules/cnr-misc-utils/tags/cnr-misc-utils-1.0.4/src/test/java/eu/dnetlib/miscutils/datetime/DateUtilsTest.java | ||
---|---|---|
1 |
package eu.dnetlib.miscutils.datetime; |
|
2 |
|
|
3 |
import static org.junit.Assert.assertEquals; |
|
4 |
|
|
5 |
import org.junit.Test; |
|
6 |
|
|
7 |
public class DateUtilsTest { |
|
8 |
|
|
9 |
private final DateUtils dateUtils = new DateUtils(); |
|
10 |
|
|
11 |
@Test |
|
12 |
public void testGetDuration() { |
|
13 |
String theDate = "1900-01-01T00:13:57"; |
|
14 |
String duration = dateUtils.getDuration(theDate); |
|
15 |
assertEquals("00:13:57", duration); |
|
16 |
} |
|
17 |
|
|
18 |
} |
modules/cnr-misc-utils/tags/cnr-misc-utils-1.0.4/src/test/java/eu/dnetlib/miscutils/iterators/xml/IterableXmlParserTest.java | ||
---|---|---|
1 |
package eu.dnetlib.miscutils.iterators.xml; |
|
2 |
|
|
3 |
import static org.junit.Assert.assertNotNull; |
|
4 |
import static org.junit.Assert.assertTrue; |
|
5 |
|
|
6 |
import java.io.ByteArrayInputStream; |
|
7 |
import java.io.IOException; |
|
8 |
import java.io.InputStream; |
|
9 |
import java.io.StringReader; |
|
10 |
import java.io.StringWriter; |
|
11 |
import java.util.zip.GZIPInputStream; |
|
12 |
import java.util.zip.ZipInputStream; |
|
13 |
|
|
14 |
import org.apache.commons.io.IOUtils; |
|
15 |
import org.dom4j.Document; |
|
16 |
import org.dom4j.DocumentException; |
|
17 |
import org.dom4j.io.SAXReader; |
|
18 |
import org.junit.Before; |
|
19 |
import org.junit.Test; |
|
20 |
import org.springframework.core.io.ClassPathResource; |
|
21 |
import org.springframework.core.io.Resource; |
|
22 |
|
|
23 |
public class IterableXmlParserTest { |
|
24 |
|
|
25 |
private Resource xmlZip = new ClassPathResource("eu/dnetlib/miscutils/iterators/xml/opendoar.zip"); |
|
26 |
|
|
27 |
private Resource xmlGz = new ClassPathResource("eu/dnetlib/miscutils/iterators/xml/opendoar.xml.gz"); |
|
28 |
|
|
29 |
private Resource xmlZipErr = new ClassPathResource("eu/dnetlib/miscutils/iterators/xml/opendoarErr.zip"); |
|
30 |
|
|
31 |
private Resource xmlSingle = new ClassPathResource("eu/dnetlib/miscutils/iterators/xml/singleRepo.xml"); |
|
32 |
|
|
33 |
private String element = "repository"; |
|
34 |
|
|
35 |
private IterableXmlParser parser; |
|
36 |
|
|
37 |
private SAXReader reader; |
|
38 |
|
|
39 |
@Before |
|
40 |
public void setUp() throws Exception { |
|
41 |
reader = new SAXReader(); |
|
42 |
} |
|
43 |
|
|
44 |
@Test |
|
45 |
public void testGz() throws Exception { |
|
46 |
doTest(new GZIPInputStream(xmlGz.getInputStream()), element); |
|
47 |
} |
|
48 |
|
|
49 |
@Test |
|
50 |
public void test() throws Exception { |
|
51 |
doTest(read(new ZipInputStream(xmlZip.getInputStream())), element); |
|
52 |
} |
|
53 |
|
|
54 |
@Test |
|
55 |
public void testErr() throws Exception { |
|
56 |
doTest(read(new ZipInputStream(xmlZipErr.getInputStream())), element); |
|
57 |
} |
|
58 |
|
|
59 |
@Test |
|
60 |
public void testSingle() throws Exception { |
|
61 |
doTest(xmlSingle.getInputStream(), element); |
|
62 |
} |
|
63 |
|
|
64 |
@Test |
|
65 |
public void testOaiRecord() throws Exception { |
|
66 |
int count = doTest(new ClassPathResource("eu/dnetlib/miscutils/iterators/xml/oaiRecord.xml").getInputStream(), "record"); |
|
67 |
assertTrue(count == 1); |
|
68 |
} |
|
69 |
|
|
70 |
@Test |
|
71 |
public void testWeird() throws Exception { |
|
72 |
int count = doTest(new ClassPathResource("eu/dnetlib/miscutils/iterators/xml/weirdRecords.xml").getInputStream(), "record"); |
|
73 |
assertTrue(count == 3); |
|
74 |
} |
|
75 |
|
|
76 |
@Test |
|
77 |
public void testWeirdGz() throws Exception { |
|
78 |
int count = doTest(new GZIPInputStream(new ClassPathResource("eu/dnetlib/miscutils/iterators/xml/weirdRecords.xml.gz").getInputStream()), "record"); |
|
79 |
assertTrue(count == 3); |
|
80 |
} |
|
81 |
|
|
82 |
private int doTest(final InputStream stream, final String element) throws DocumentException { |
|
83 |
parser = new IterableXmlParser(element, stream); |
|
84 |
int count = 0; |
|
85 |
for (String xml : parser) { |
|
86 |
System.out.println(xml); |
|
87 |
Document doc = reader.read(new StringReader(xml)); |
|
88 |
assertNotNull(doc); |
|
89 |
assertNotNull(doc.selectSingleNode("//" + element)); |
|
90 |
count++; |
|
91 |
} |
|
92 |
return count; |
|
93 |
} |
|
94 |
|
|
95 |
// helper method, reads the compressed text out of the xmlZip file |
|
96 |
private InputStream read(final ZipInputStream zis) throws IOException { |
|
97 |
|
|
98 |
final StringWriter sw = new StringWriter(); |
|
99 |
while (zis.getNextEntry() != null) { |
|
100 |
|
|
101 |
byte[] buffer = new byte[1]; |
|
102 |
|
|
103 |
while (zis.read(buffer) != -1) { |
|
104 |
IOUtils.write(buffer, sw, "UTF-8"); |
|
105 |
} |
|
106 |
} |
|
107 |
zis.close(); |
|
108 |
sw.close(); |
|
109 |
|
|
110 |
return new ByteArrayInputStream(sw.toString().getBytes()); |
|
111 |
} |
|
112 |
|
|
113 |
} |
modules/cnr-misc-utils/tags/cnr-misc-utils-1.0.4/src/test/java/eu/dnetlib/miscutils/dom4j/XPathHelperTest.java | ||
---|---|---|
1 |
/** |
|
2 |
* |
|
3 |
*/ |
|
4 |
package eu.dnetlib.miscutils.dom4j; |
|
5 |
|
|
6 |
import static org.junit.Assert.assertEquals; |
|
7 |
|
|
8 |
import java.io.StringReader; |
|
9 |
|
|
10 |
import org.dom4j.Document; |
|
11 |
import org.dom4j.DocumentException; |
|
12 |
import org.dom4j.Element; |
|
13 |
import org.dom4j.io.SAXReader; |
|
14 |
import org.junit.Test; |
|
15 |
|
|
16 |
/** |
|
17 |
* @author marko |
|
18 |
* |
|
19 |
*/ |
|
20 |
public class XPathHelperTest { |
|
21 |
|
|
22 |
/** |
|
23 |
* Test method for {@link eu.dnetlib.miscutils.dom4j.XPathHelper#selectElements(org.dom4j.Node, java.lang.String)}. |
|
24 |
* |
|
25 |
* @throws DocumentException |
|
26 |
*/ |
|
27 |
@Test |
|
28 |
public void testSelectElements() throws DocumentException { |
|
29 |
final String xmlSource = "<root><child/>xxxx<child/>xxxx</root>"; |
|
30 |
|
|
31 |
final Document document = new SAXReader().read(new StringReader(xmlSource)); |
|
32 |
|
|
33 |
for (Element el : XPathHelper.selectElements(document, "//child")) { |
|
34 |
assertEquals("check elements", el.asXML(), "<child/>"); |
|
35 |
} |
|
36 |
} |
|
37 |
|
|
38 |
} |
modules/cnr-misc-utils/tags/cnr-misc-utils-1.0.4/src/test/java/eu/dnetlib/miscutils/collections/TypeFilteredCollectionTest.java | ||
---|---|---|
1 |
package eu.dnetlib.miscutils.collections; |
|
2 |
|
|
3 |
import java.io.StringReader; |
|
4 |
import java.util.ArrayList; |
|
5 |
import java.util.List; |
|
6 |
|
|
7 |
import org.dom4j.Document; |
|
8 |
import org.dom4j.DocumentException; |
|
9 |
import org.dom4j.Element; |
|
10 |
import org.dom4j.Node; |
|
11 |
import org.dom4j.io.SAXReader; |
|
12 |
import org.junit.Test; |
|
13 |
|
|
14 |
import static org.junit.Assert.*; // NOPMD |
|
15 |
|
|
16 |
|
|
17 |
public class TypeFilteredCollectionTest { |
|
18 |
|
|
19 |
private void checkContainsTestString(final Iterable<String> iter) { |
|
20 |
int count = 0; // NOPMD |
|
21 |
for (String el : iter) { |
|
22 |
assertEquals("check array", el, "test"); |
|
23 |
count++; // NOPMD |
|
24 |
} |
|
25 |
assertEquals("check count", count, 1); |
|
26 |
} |
|
27 |
|
|
28 |
@Test |
|
29 |
public void testFilter() { |
|
30 |
final List<Object> list = new ArrayList<Object>(); |
|
31 |
list.add(1); |
|
32 |
list.add("test"); |
|
33 |
|
|
34 |
final TypeFilteredCollection<Object, String> tfc; |
|
35 |
tfc = new TypeFilteredCollection<Object, String>(list, String.class); |
|
36 |
checkContainsTestString(tfc); |
|
37 |
} |
|
38 |
|
|
39 |
@Test |
|
40 |
public void testNull() { |
|
41 |
final List<Object> list = new ArrayList<Object>(); |
|
42 |
assertNotNull("dummy", list); |
|
43 |
|
|
44 |
list.add(null); |
|
45 |
list.add("test"); |
|
46 |
|
|
47 |
final TypeFilteredCollection<Object, String> tfc; |
|
48 |
tfc = new TypeFilteredCollection<Object, String>(list, String.class); |
|
49 |
checkContainsTestString(tfc); |
|
50 |
} |
|
51 |
|
|
52 |
@Test |
|
53 |
public void testJDom() throws DocumentException { |
|
54 |
final String xmlSource = "<root><child/>xxxx<child/>xxxx</root>"; |
|
55 |
|
|
56 |
final Document document = new SAXReader().read(new StringReader(xmlSource)); |
|
57 |
|
|
58 |
@SuppressWarnings("unchecked") |
|
59 |
final List<Node> children = document.selectNodes("//child"); |
|
60 |
|
|
61 |
final TypeFilteredCollection<Node, Element> tfc; |
|
62 |
tfc = new TypeFilteredCollection<Node, Element>(children, Element.class); |
|
63 |
for (Element el : tfc) { |
|
64 |
assertEquals("check array", el.asXML(), "<child/>"); |
|
65 |
} |
|
66 |
|
|
67 |
} |
|
68 |
|
|
69 |
} |
modules/cnr-misc-utils/tags/cnr-misc-utils-1.0.4/src/test/java/eu/dnetlib/miscutils/collections/BloomFilterTest.java | ||
---|---|---|
1 |
/** |
|
2 |
* This program is free software: you can redistribute it and/or modify |
|
3 |
* it under the terms of the GNU Lesser General Public License as published by |
|
4 |
* the Free Software Foundation, either version 3 of the License, or |
|
5 |
* (at your option) any later version. |
|
6 |
* |
|
7 |
* This program is distributed in the hope that it will be useful, |
|
8 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
9 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
10 |
* GNU Lesser General Public License for more details. |
|
11 |
* |
|
12 |
* You should have received a copy of the GNU Lesser General Public License |
|
13 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
14 |
*/ |
|
15 |
|
|
16 |
package eu.dnetlib.miscutils.collections; |
|
17 |
|
|
18 |
import static org.junit.Assert.assertArrayEquals; |
|
19 |
import static org.junit.Assert.assertEquals; |
|
20 |
import static org.junit.Assert.assertFalse; |
|
21 |
import static org.junit.Assert.assertNotSame; |
|
22 |
import static org.junit.Assert.assertSame; |
|
23 |
import static org.junit.Assert.assertTrue; |
|
24 |
|
|
25 |
import java.io.UnsupportedEncodingException; |
|
26 |
import java.util.ArrayList; |
|
27 |
import java.util.List; |
|
28 |
import java.util.Random; |
|
29 |
import java.util.UUID; |
|
30 |
|
|
31 |
import org.junit.Test; |
|
32 |
|
|
33 |
/** |
|
34 |
* Tests for BloomFilter.java |
|
35 |
* |
|
36 |
* @author Magnus Skjegstad <magnus@skjegstad.com> |
|
37 |
*/ |
|
38 |
public class BloomFilterTest { |
|
39 |
static Random r = new Random(); |
|
40 |
|
|
41 |
@Test |
|
42 |
public void testConstructorCNK() throws Exception { |
|
43 |
System.out.println("BloomFilter(c,n,k)"); |
|
44 |
|
|
45 |
for (int i = 0; i < 10000; i++) { |
|
46 |
double c = r.nextInt(20) + 1; |
|
47 |
int n = r.nextInt(10000) + 1; |
|
48 |
int k = r.nextInt(20) + 1; |
|
49 |
BloomFilter<?> bf = new BloomFilter<Object>(c, n, k); |
|
50 |
assertEquals(bf.getK(), k); |
|
51 |
assertEquals(bf.getExpectedBitsPerElement(), c, 0); |
|
52 |
assertEquals(bf.getExpectedNumberOfElements(), n); |
|
53 |
assertEquals(bf.size(), c*n, 0); |
|
54 |
} |
|
55 |
} |
|
56 |
|
|
57 |
|
|
58 |
/** |
|
59 |
* Test of createHash method, of class BloomFilter. |
|
60 |
* @throws Exception |
|
61 |
*/ |
|
62 |
@Test |
|
63 |
public void testCreateHash_String() throws Exception { |
|
64 |
System.out.println("createHash"); |
|
65 |
String val = UUID.randomUUID().toString(); |
|
66 |
int result1 = BloomFilter.createHash(val); |
|
67 |
int result2 = BloomFilter.createHash(val); |
|
68 |
assertEquals(result2, result1); |
|
69 |
int result3 = BloomFilter.createHash(UUID.randomUUID().toString()); |
|
70 |
assertNotSame(result3, result2); |
|
71 |
|
|
72 |
int result4 = BloomFilter.createHash(val.getBytes("UTF-8")); |
|
73 |
assertEquals(result4, result1); |
|
74 |
} |
|
75 |
|
|
76 |
/** |
|
77 |
* Test of createHash method, of class BloomFilter. |
|
78 |
* @throws UnsupportedEncodingException |
|
79 |
*/ |
|
80 |
@Test |
|
81 |
public void testCreateHash_byteArr() throws UnsupportedEncodingException { |
|
82 |
System.out.println("createHash"); |
|
83 |
String val = UUID.randomUUID().toString(); |
|
84 |
byte[] data = val.getBytes("UTF-8"); |
|
85 |
int result1 = BloomFilter.createHash(data); |
|
86 |
int result2 = BloomFilter.createHash(val); |
|
87 |
assertEquals(result1, result2); |
|
88 |
} |
|
89 |
|
|
90 |
/** |
|
91 |
* Test of createHash method, of class BloomFilter. |
|
92 |
* @throws UnsupportedEncodingException |
|
93 |
*/ |
|
94 |
@Test |
|
95 |
public void testCreateHashes_byteArr() throws UnsupportedEncodingException { |
|
96 |
System.out.println("createHashes"); |
|
97 |
String val = UUID.randomUUID().toString(); |
|
98 |
byte[] data = val.getBytes("UTF-8"); |
|
99 |
int[] result1 = BloomFilter.createHashes(data, 10); |
|
100 |
int[] result2 = BloomFilter.createHashes(data, 10); |
|
101 |
assertEquals(result1.length, 10); |
|
102 |
assertEquals(result2.length, 10); |
|
103 |
assertArrayEquals(result1, result2); |
|
104 |
int[] result3 = BloomFilter.createHashes(data, 5); |
|
105 |
assertEquals(result3.length, 5); |
|
106 |
for (int i = 0; i < result3.length; i++) |
|
107 |
assertEquals(result3[i], result1[i]); |
|
108 |
|
|
109 |
} |
|
110 |
|
|
111 |
/** |
|
112 |
* Test of equals method, of class BloomFilter. |
|
113 |
* @throws UnsupportedEncodingException |
|
114 |
*/ |
|
115 |
@Test |
|
116 |
public void testEquals() throws UnsupportedEncodingException { |
|
117 |
System.out.println("equals"); |
|
118 |
BloomFilter<String> instance1 = new BloomFilter<String>(1000, 100); |
|
119 |
BloomFilter<String> instance2 = new BloomFilter<String>(1000, 100); |
|
120 |
|
|
121 |
for (int i = 0; i < 100; i++) { |
|
122 |
String val = UUID.randomUUID().toString(); |
|
123 |
instance1.add(val); |
|
124 |
instance2.add(val); |
|
125 |
} |
|
126 |
|
|
127 |
assert(instance1.equals(instance2)); |
|
128 |
assert(instance2.equals(instance1)); |
|
129 |
|
|
130 |
instance1.add("Another entry"); // make instance1 and instance2 different before clearing |
|
131 |
|
|
132 |
instance1.clear(); |
|
133 |
instance2.clear(); |
|
134 |
|
|
135 |
assert(instance1.equals(instance2)); |
|
136 |
assert(instance2.equals(instance1)); |
|
137 |
|
|
138 |
for (int i = 0; i < 100; i++) { |
|
139 |
String val = UUID.randomUUID().toString(); |
|
140 |
instance1.add(val); |
|
141 |
instance2.add(val); |
|
142 |
} |
|
143 |
|
|
144 |
assertTrue(instance1.equals(instance2)); |
|
145 |
assertTrue(instance2.equals(instance1)); |
|
146 |
} |
|
147 |
|
|
148 |
/** |
|
149 |
* Test of hashCode method, of class BloomFilter. |
|
150 |
* @throws UnsupportedEncodingException |
|
151 |
*/ |
|
152 |
@Test |
|
153 |
public void testHashCode() throws UnsupportedEncodingException { |
|
154 |
System.out.println("hashCode"); |
|
155 |
|
|
156 |
BloomFilter<String> instance1 = new BloomFilter<String>(1000, 100); |
|
157 |
BloomFilter<String> instance2 = new BloomFilter<String>(1000, 100); |
|
158 |
|
|
159 |
assertTrue(instance1.hashCode() == instance2.hashCode()); |
|
160 |
|
|
161 |
for (int i = 0; i < 100; i++) { |
|
162 |
String val = UUID.randomUUID().toString(); |
|
163 |
instance1.add(val); |
|
164 |
instance2.add(val); |
|
165 |
} |
|
166 |
|
|
167 |
assertTrue(instance1.hashCode() == instance2.hashCode()); |
|
168 |
|
|
169 |
instance1.clear(); |
|
170 |
instance2.clear(); |
|
171 |
|
|
172 |
assertTrue(instance1.hashCode() == instance2.hashCode()); |
|
173 |
|
|
174 |
instance1 = new BloomFilter<String>(100, 10); |
|
175 |
instance2 = new BloomFilter<String>(100, 9); |
|
176 |
assertFalse(instance1.hashCode() == instance2.hashCode()); |
|
177 |
|
|
178 |
instance1 = new BloomFilter<String>(100, 10); |
|
179 |
instance2 = new BloomFilter<String>(99, 9); |
|
180 |
assertFalse(instance1.hashCode() == instance2.hashCode()); |
|
181 |
|
|
182 |
instance1 = new BloomFilter<String>(100, 10); |
|
183 |
instance2 = new BloomFilter<String>(50, 10); |
|
184 |
assertFalse(instance1.hashCode() == instance2.hashCode()); |
|
185 |
} |
|
186 |
|
|
187 |
/** |
|
188 |
* Test of expectedFalsePositiveProbability method, of class BloomFilter. |
|
189 |
*/ |
|
190 |
@Test |
|
191 |
public void testExpectedFalsePositiveProbability() { |
|
192 |
// These probabilities are taken from the bloom filter probability table at |
|
193 |
// http://pages.cs.wisc.edu/~cao/papers/summary-cache/node8.html |
|
194 |
System.out.println("expectedFalsePositiveProbability"); |
|
195 |
BloomFilter<?> instance = new BloomFilter<Object>(1000, 100); |
|
196 |
double expResult = 0.00819; // m/n=10, k=7 |
|
197 |
double result = instance.expectedFalsePositiveProbability(); |
|
198 |
assertEquals(instance.getK(), 7); |
|
199 |
assertEquals(expResult, result, 0.000009); |
|
200 |
|
|
201 |
instance = new BloomFilter<Object>(100, 10); |
|
202 |
expResult = 0.00819; // m/n=10, k=7 |
|
203 |
result = instance.expectedFalsePositiveProbability(); |
|
204 |
assertEquals(instance.getK(), 7); |
|
205 |
assertEquals(expResult, result, 0.000009); |
|
206 |
|
|
207 |
instance = new BloomFilter<Object>(20, 10); |
|
208 |
expResult = 0.393; // m/n=2, k=1 |
|
209 |
result = instance.expectedFalsePositiveProbability(); |
|
210 |
assertEquals(1, instance.getK()); |
|
211 |
assertEquals(expResult, result, 0.0005); |
|
212 |
|
|
213 |
instance = new BloomFilter<Object>(110, 10); |
|
214 |
expResult = 0.00509; // m/n=11, k=8 |
|
215 |
result = instance.expectedFalsePositiveProbability(); |
|
216 |
assertEquals(8, instance.getK()); |
|
217 |
assertEquals(expResult, result, 0.00001); |
|
218 |
} |
|
219 |
|
|
220 |
/** |
|
221 |
* Test of clear method, of class BloomFilter. |
|
222 |
*/ |
|
223 |
@Test |
|
224 |
public void testClear() { |
|
225 |
System.out.println("clear"); |
|
226 |
BloomFilter<?> instance = new BloomFilter<Object>(1000, 100); |
|
227 |
for (int i = 0; i < instance.size(); i++) |
|
228 |
instance.setBit(i, true); |
|
229 |
instance.clear(); |
|
230 |
for (int i = 0; i < instance.size(); i++) |
|
231 |
assertSame(instance.getBit(i), false); |
|
232 |
} |
|
233 |
|
|
234 |
/** |
|
235 |
* Test of add method, of class BloomFilter. |
|
236 |
* @throws Exception |
|
237 |
*/ |
|
238 |
@Test |
|
239 |
public void testAdd() throws Exception { |
|
240 |
System.out.println("add"); |
|
241 |
BloomFilter<String> instance = new BloomFilter<String>(1000, 100); |
|
242 |
|
|
243 |
for (int i = 0; i < 100; i++) { |
|
244 |
String val = UUID.randomUUID().toString(); |
|
245 |
instance.add(val); |
|
246 |
assert(instance.contains(val)); |
|
247 |
} |
|
248 |
} |
|
249 |
|
|
250 |
/** |
|
251 |
* Test of addAll method, of class BloomFilter. |
|
252 |
* @throws Exception |
|
253 |
*/ |
|
254 |
@Test |
|
255 |
public void testAddAll() throws Exception { |
|
256 |
System.out.println("addAll"); |
|
257 |
List<String> v = new ArrayList<String>(); |
|
258 |
BloomFilter<String> instance = new BloomFilter<String>(1000, 100); |
|
259 |
|
|
260 |
for (int i = 0; i < 100; i++) |
|
261 |
v.add(UUID.randomUUID().toString()); |
|
262 |
|
|
263 |
instance.addAll(v); |
|
264 |
|
|
265 |
for (int i = 0; i < 100; i++) |
|
266 |
assert(instance.contains(v.get(i))); |
|
267 |
} |
|
268 |
|
|
269 |
/** |
|
270 |
* Test of contains method, of class BloomFilter. |
|
271 |
* @throws Exception |
|
272 |
*/ |
|
273 |
@Test |
|
274 |
public void testContains() throws Exception { |
|
275 |
System.out.println("contains"); |
|
276 |
BloomFilter<String> instance = new BloomFilter<String>(10000, 10); |
|
277 |
|
|
278 |
for (int i = 0; i < 10; i++) { |
|
279 |
instance.add(Integer.toBinaryString(i)); |
|
280 |
assert(instance.contains(Integer.toBinaryString(i))); |
|
281 |
} |
|
282 |
|
|
283 |
assertFalse(instance.contains(UUID.randomUUID().toString())); |
|
284 |
} |
|
285 |
|
|
286 |
/** |
|
287 |
* Test of containsAll method, of class BloomFilter. |
|
288 |
* @throws Exception |
|
289 |
*/ |
|
290 |
@Test |
|
291 |
public void testContainsAll() throws Exception { |
|
292 |
System.out.println("containsAll"); |
|
293 |
List<String> v = new ArrayList<String>(); |
|
294 |
BloomFilter<String> instance = new BloomFilter<String>(1000, 100); |
|
295 |
|
|
296 |
for (int i = 0; i < 100; i++) { |
|
297 |
v.add(UUID.randomUUID().toString()); |
|
298 |
instance.add(v.get(i)); |
|
299 |
} |
|
300 |
|
|
301 |
assert(instance.containsAll(v)); |
|
302 |
} |
|
303 |
|
|
304 |
/** |
|
305 |
* Test of getBit method, of class BloomFilter. |
|
306 |
*/ |
|
307 |
@Test |
|
308 |
public void testGetBit() { |
|
309 |
System.out.println("getBit"); |
|
310 |
BloomFilter<?> instance = new BloomFilter<Object>(1000, 100); |
|
311 |
Random r = new Random(); |
|
312 |
|
|
313 |
for (int i = 0; i < 100; i++) { |
|
314 |
boolean b = r.nextBoolean(); |
|
315 |
instance.setBit(i, b); |
|
316 |
assertSame(instance.getBit(i), b); |
|
317 |
} |
|
318 |
} |
|
319 |
|
|
320 |
/** |
|
321 |
* Test of setBit method, of class BloomFilter. |
|
322 |
*/ |
|
323 |
@Test |
|
324 |
public void testSetBit() { |
|
325 |
System.out.println("setBit"); |
|
326 |
|
|
327 |
BloomFilter<?> instance = new BloomFilter<Object>(1000, 100); |
|
328 |
|
|
329 |
for (int i = 0; i < 100; i++) { |
|
330 |
instance.setBit(i, true); |
|
331 |
assertSame(instance.getBit(i), true); |
|
332 |
} |
|
333 |
|
|
334 |
for (int i = 0; i < 100; i++) { |
|
335 |
instance.setBit(i, false); |
|
336 |
assertSame(instance.getBit(i), false); |
|
337 |
} |
|
338 |
} |
|
339 |
|
|
340 |
/** |
|
341 |
* Test of size method, of class BloomFilter. |
|
342 |
*/ |
|
343 |
@Test |
|
344 |
public void testSize() { |
|
345 |
System.out.println("size"); |
|
346 |
for (int i = 100; i < 1000; i++) { |
|
347 |
BloomFilter<?> instance = new BloomFilter<Object>(i, 10); |
|
348 |
assertEquals(instance.size(), i); |
|
349 |
} |
|
350 |
} |
|
351 |
|
|
352 |
/** Test error rate * |
|
353 |
* @throws UnsupportedEncodingException |
|
354 |
*/ |
|
355 |
@Test |
|
356 |
public void testFalsePositiveRate1() throws UnsupportedEncodingException { |
|
357 |
// Numbers are from // http://pages.cs.wisc.edu/~cao/papers/summary-cache/node8.html |
|
358 |
System.out.println("falsePositiveRate1"); |
|
359 |
|
|
360 |
for (int j = 10; j < 21; j++) { |
|
361 |
System.out.print(j-9 + "/11"); |
|
362 |
List<byte[]> v = new ArrayList<byte[]>(); |
|
363 |
BloomFilter<Object> instance = new BloomFilter<Object>(100*j,100); |
|
364 |
|
|
365 |
for (int i = 0; i < 100; i++) { |
|
366 |
byte[] bytes = new byte[100]; |
|
367 |
r.nextBytes(bytes); |
|
368 |
v.add(bytes); |
|
369 |
} |
|
370 |
instance.addAll(v); |
|
371 |
|
|
372 |
long f = 0; |
|
373 |
double tests = 300000; |
|
374 |
for (int i = 0; i < tests; i++) { |
|
375 |
byte[] bytes = new byte[100]; |
|
376 |
r.nextBytes(bytes); |
|
377 |
if (instance.contains(bytes)) { |
|
378 |
if (!v.contains(bytes)) { |
|
379 |
f++; |
|
380 |
} |
|
381 |
} |
|
382 |
} |
|
383 |
|
|
384 |
double ratio = f / tests; |
|
385 |
|
|
386 |
System.out.println(" - got " + ratio + ", math says " + instance.expectedFalsePositiveProbability()); |
|
387 |
assertEquals(instance.expectedFalsePositiveProbability(), ratio, 0.01); |
|
388 |
} |
|
389 |
} |
|
390 |
|
|
391 |
/** Test for correct k **/ |
|
392 |
@Test |
|
393 |
public void testGetK() { |
|
394 |
// Numbers are from http://pages.cs.wisc.edu/~cao/papers/summary-cache/node8.html |
|
395 |
System.out.println("testGetK"); |
|
396 |
BloomFilter<?> instance = null; |
|
397 |
|
|
398 |
instance = new BloomFilter<Object>(2, 1); |
|
399 |
assertEquals(1, instance.getK()); |
|
400 |
|
|
401 |
instance = new BloomFilter<Object>(3, 1); |
|
402 |
assertEquals(2, instance.getK()); |
|
403 |
|
|
404 |
instance = new BloomFilter<Object>(4, 1); |
|
405 |
assertEquals(3, instance.getK()); |
|
406 |
|
|
407 |
instance = new BloomFilter<Object>(5, 1); |
|
408 |
assertEquals(3, instance.getK()); |
|
409 |
|
|
410 |
instance = new BloomFilter<Object>(6, 1); |
|
411 |
assertEquals(4, instance.getK()); |
|
412 |
|
|
413 |
instance = new BloomFilter<Object>(7, 1); |
|
414 |
assertEquals(5, instance.getK()); |
|
415 |
|
|
416 |
instance = new BloomFilter<Object>(8, 1); |
|
417 |
assertEquals(6, instance.getK()); |
|
418 |
|
|
419 |
instance = new BloomFilter<Object>(9, 1); |
|
420 |
assertEquals(6, instance.getK()); |
|
421 |
|
|
422 |
instance = new BloomFilter<Object>(10, 1); |
|
423 |
assertEquals(7, instance.getK()); |
|
424 |
|
|
425 |
instance = new BloomFilter<Object>(11, 1); |
|
426 |
assertEquals(8, instance.getK()); |
|
427 |
|
|
428 |
instance = new BloomFilter<Object>(12, 1); |
|
429 |
assertEquals(8, instance.getK()); |
|
430 |
} |
|
431 |
|
|
432 |
/** |
|
433 |
* Test of contains method, of class BloomFilter. |
|
434 |
*/ |
|
435 |
@Test |
|
436 |
public void testContains_GenericType() { |
|
437 |
System.out.println("contains"); |
|
438 |
int items = 100; |
|
439 |
BloomFilter<String> instance = new BloomFilter<String>(0.01, items); |
|
440 |
|
|
441 |
for (int i = 0; i < items; i++) { |
|
442 |
String s = UUID.randomUUID().toString(); |
|
443 |
instance.add(s); |
|
444 |
assertTrue(instance.contains(s)); |
|
445 |
} |
|
446 |
} |
|
447 |
|
|
448 |
/** |
|
449 |
* Test of contains method, of class BloomFilter. |
|
450 |
*/ |
|
451 |
@Test |
|
452 |
public void testContains_byteArr() { |
|
453 |
System.out.println("contains"); |
|
454 |
|
|
455 |
int items = 100; |
|
456 |
BloomFilter<?> instance = new BloomFilter<Object>(0.01, items); |
|
457 |
|
|
458 |
for (int i = 0; i < items; i++) { |
|
459 |
byte[] bytes = new byte[500]; |
|
460 |
r.nextBytes(bytes); |
|
461 |
instance.add(bytes); |
|
462 |
assertTrue(instance.contains(bytes)); |
|
463 |
} |
|
464 |
} |
|
465 |
|
|
466 |
/** |
|
467 |
* Test of count method, of class BloomFilter. |
|
468 |
*/ |
|
469 |
@Test |
|
470 |
public void testCount() { |
|
471 |
System.out.println("count"); |
|
472 |
int expResult = 100; |
|
473 |
BloomFilter<String> instance = new BloomFilter<String>(0.01, expResult); |
|
474 |
for (int i = 0; i < expResult; i++) { |
|
475 |
byte[] bytes = new byte[100]; |
|
476 |
r.nextBytes(bytes); |
|
477 |
instance.add(bytes); |
|
478 |
} |
|
479 |
int result = instance.count(); |
|
480 |
assertEquals(expResult, result); |
|
481 |
|
|
482 |
instance = new BloomFilter<String>(0.01, expResult); |
|
483 |
for (int i = 0; i < expResult; i++) { |
|
484 |
instance.add(UUID.randomUUID().toString()); |
|
485 |
} |
|
486 |
result = instance.count(); |
|
487 |
assertEquals(expResult, result); |
|
488 |
} |
|
489 |
|
|
490 |
|
|
491 |
} |
modules/cnr-misc-utils/tags/cnr-misc-utils-1.0.4/src/test/java/eu/dnetlib/miscutils/collections/MappedCollectionTest.java | ||
---|---|---|
1 |
package eu.dnetlib.miscutils.collections; |
|
2 |
|
|
3 |
import static org.junit.Assert.assertEquals; |
|
4 |
|
|
5 |
import java.util.ArrayList; |
|
6 |
import java.util.List; |
|
7 |
|
|
8 |
import org.junit.Test; |
|
9 |
|
|
10 |
import eu.dnetlib.miscutils.functional.UnaryFunction; |
|
11 |
|
|
12 |
public class MappedCollectionTest { |
|
13 |
|
|
14 |
@Test |
|
15 |
public void testIterator() { |
|
16 |
final List<Integer> list = new ArrayList<Integer>(); |
|
17 |
list.add(100); |
|
18 |
|
|
19 |
final MappedCollection<String, Integer> mapped = new MappedCollection<String, Integer>(list, new UnaryFunction<String, Integer>() { //NOPMD |
|
20 |
@Override |
|
21 |
public String evaluate(final Integer arg) { |
|
22 |
return arg.toString(); |
|
23 |
} |
|
24 |
}); |
|
25 |
for (String el : mapped) |
|
26 |
assertEquals("check array", el, "100"); |
|
27 |
} |
|
28 |
|
|
29 |
} |
modules/cnr-misc-utils/tags/cnr-misc-utils-1.0.4/src/test/java/eu/dnetlib/miscutils/collections/AffixCollectionTest.java | ||
---|---|---|
1 |
package eu.dnetlib.miscutils.collections; |
|
2 |
|
|
3 |
import static org.junit.Assert.assertEquals; |
|
4 |
|
|
5 |
import java.util.ArrayList; |
|
6 |
import java.util.List; |
|
7 |
|
|
8 |
import org.junit.Test; |
|
9 |
|
|
10 |
public class AffixCollectionTest { |
|
11 |
|
|
12 |
@Test |
|
13 |
public void testIterator() { |
|
14 |
final List<String> list = new ArrayList<String>(); |
|
15 |
list.add("100"); |
|
16 |
|
|
17 |
final AffixCollection mapped = new AffixCollection(list, "_test"); |
|
18 |
for (String el : mapped) |
|
19 |
assertEquals("check array", "100_test", el); |
|
20 |
} |
|
21 |
|
|
22 |
} |
modules/cnr-misc-utils/tags/cnr-misc-utils-1.0.4/src/test/java/eu/dnetlib/miscutils/collections/FilteredCollectionTest.java | ||
---|---|---|
1 |
package eu.dnetlib.miscutils.collections; |
|
2 |
|
|
3 |
import static org.junit.Assert.assertTrue; |
|
4 |
|
|
5 |
import java.util.ArrayList; |
|
6 |
import java.util.List; |
|
7 |
|
|
8 |
import org.junit.Test; |
|
9 |
|
|
10 |
import eu.dnetlib.miscutils.functional.CompositeUnaryFunction; |
|
11 |
import eu.dnetlib.miscutils.functional.UnaryFunction; |
|
12 |
|
|
13 |
public class FilteredCollectionTest { |
|
14 |
|
|
15 |
@Test |
|
16 |
public void testFilterComposition() { |
|
17 |
final List<Integer> list = new ArrayList<Integer>(); |
|
18 |
list.add(1); |
|
19 |
list.add(4); |
|
20 |
list.add(8); |
|
21 |
|
|
22 |
final Filter<Integer> fi1 = new Filter<Integer>() { // NOPMD |
|
23 |
@Override |
|
24 |
public Boolean evaluate(final Integer arg) { |
|
25 |
return arg < 8; |
|
26 |
} |
|
27 |
}; |
|
28 |
|
|
29 |
final UnaryFunction<Integer, Integer> square = new UnaryFunction<Integer, Integer>() { |
|
30 |
@Override |
|
31 |
public Integer evaluate(final Integer arg) { |
|
32 |
return arg * arg; |
|
33 |
} |
|
34 |
}; |
|
35 |
|
|
36 |
final FilteredCollection<Integer> fic = new FilteredCollection<Integer>(list, fi1); |
|
37 |
for (Integer el : fic) |
|
38 |
assertTrue("check array", el < 8); |
|
39 |
|
|
40 |
final CompositeUnaryFunction<Boolean, Integer> cf1 = new CompositeUnaryFunction<Boolean, Integer>(fi1); |
|
41 |
final FilteredCollection<Integer> fc2 = new FilteredCollection<Integer>(list, cf1.of(square)); |
|
42 |
for (Integer el : fc2) |
|
43 |
assertTrue("check array", el < 8); |
|
44 |
|
|
45 |
} |
|
46 |
|
|
47 |
} |
modules/cnr-misc-utils/tags/cnr-misc-utils-1.0.4/src/test/java/eu/dnetlib/miscutils/maps/ConcurrentSizedMapTest.java | ||
---|---|---|
1 |
package eu.dnetlib.miscutils.maps; |
|
2 |
|
|
3 |
import static org.junit.Assert.assertEquals; |
|
4 |
import static org.junit.Assert.assertNotNull; |
|
5 |
import static org.junit.Assert.assertNull; |
|
6 |
|
|
7 |
import org.junit.Before; |
|
8 |
import org.junit.Test; |
|
9 |
|
|
10 |
import eu.dnetlib.miscutils.maps.ConcurrentSizedMap; |
|
11 |
|
|
12 |
public class ConcurrentSizedMapTest { |
|
13 |
|
|
14 |
private ConcurrentSizedMap<String, String> map; |
|
15 |
|
|
16 |
private int size = 2; |
|
17 |
|
|
18 |
@Before |
|
19 |
public void setUp() throws Exception { |
|
20 |
map = new ConcurrentSizedMap<String, String>(); |
|
21 |
map.setQueueSize(size); |
|
22 |
} |
|
23 |
|
|
24 |
@Test |
|
25 |
public void testMap() throws InterruptedException { |
|
26 |
|
|
27 |
map.put("a", "value a"); |
|
28 |
assertNotNull((map.get("a"))); |
|
29 |
|
|
30 |
map.put("b", "value b"); |
|
31 |
assertNotNull((map.get("b"))); |
|
32 |
|
|
33 |
map.put("c", "value c"); |
|
34 |
assertNotNull((map.get("c"))); |
|
35 |
assertEquals(size, map.size()); |
|
36 |
|
|
37 |
assertNull(map.get("a")); |
|
38 |
map.put("a", "new value a"); |
|
39 |
|
|
40 |
assertEquals("new value a", map.get("a")); |
|
41 |
assertEquals(size, map.size()); |
|
42 |
} |
|
43 |
|
|
44 |
} |
modules/cnr-misc-utils/tags/cnr-misc-utils-1.0.4/src/test/java/eu/dnetlib/miscutils/functional/UnsafeUnaryFunction.java | ||
---|---|---|
1 |
package eu.dnetlib.miscutils.functional; |
|
2 |
|
|
3 |
import eu.dnetlib.miscutils.functional.UnaryFunction; |
|
4 |
|
|
5 |
public class UnsafeUnaryFunction implements UnaryFunction<String, Integer> { |
|
6 |
@Override |
|
7 |
public String evaluate(Integer arg) { |
|
8 |
return String.valueOf(arg); |
|
9 |
} |
|
10 |
} |
modules/cnr-misc-utils/tags/cnr-misc-utils-1.0.4/src/test/java/eu/dnetlib/miscutils/functional/string/SanitizerTest.java | ||
---|---|---|
1 |
package eu.dnetlib.miscutils.functional.string; |
|
2 |
|
|
3 |
import static org.junit.Assert.assertEquals; |
|
4 |
import static org.junit.Assert.assertFalse; |
|
5 |
|
|
6 |
import java.io.StringWriter; |
|
7 |
|
|
8 |
import org.apache.commons.io.IOUtils; |
|
9 |
import org.junit.Test; |
|
10 |
|
|
11 |
public class SanitizerTest { |
|
12 |
|
|
13 |
@Test |
|
14 |
public void testIdentity() { |
|
15 |
verifyIdentity("+-@°#()!$%&/()?' "); |
|
16 |
verifyIdentity("asça"); |
|
17 |
verifyIdentity("qwéèrtç°§"); |
|
18 |
verifyIdentity("AÁaáCĆcćEÉeéIÍiíLĹlĺNŃnńOÓoóRŔrŕSŚsśUÚuúYÝyýZŹzź"); |
|
19 |
verifyIdentity("AÂaâCĈcĉEÊeêGĜgĝHĤhĥIÎiîJĴjĵOÔoôSŜsŝUÛuûWŴwŵYŶyŷ"); |
|
20 |
verifyIdentity("CÇcçGĢgģKĶkķLĻlļNŅnņRŖrŗSŞsşTŢtţ"); |
|
21 |
verifyIdentity("ΑΒΓΔΕΖΗΘΙΚΛΜΝΞΟΠΡΣΤΥΦΧΨΩΆΈΉΊΌΎΏΪΫαβγδεζηθικλμνξοπρσςτυφχψωάέήίόύώΐΰ"); |
|
22 |
verifyIdentity("АБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯ"); |
|
23 |
verifyIdentity("абвгдеёжзийклмнопрстуфхцчшщъыьэюя"); |
|
24 |
verifyIdentity("ـآ آ ـا ا بـبـب ب تـتـت ت ثـثـث ث جـجـج ج حـحـح ح خـخـخ خ ـد د ـذ ذ ـر ر ـز ز سـسـس س"); |
|
25 |
} |
|
26 |
|
|
27 |
@Test |
|
28 |
public void testInvalid() throws Exception { |
|
29 |
StringWriter sw = new StringWriter(); |
|
30 |
|
|
31 |
IOUtils.copy(getClass().getResourceAsStream("invalid.txt"), sw); |
|
32 |
|
|
33 |
String pre = sw.toString(); |
|
34 |
String post = Sanitizer.sanitize(pre); |
|
35 |
|
|
36 |
assertFalse(pre.equals(post)); |
|
37 |
} |
|
38 |
|
|
39 |
private void verifyIdentity(String s) { |
|
40 |
assertEquals(s, Sanitizer.sanitize(s)); |
|
41 |
} |
|
42 |
|
|
43 |
} |
modules/cnr-misc-utils/tags/cnr-misc-utils-1.0.4/src/test/java/eu/dnetlib/miscutils/functional/string/EscapeUnescapeTest.java | ||
---|---|---|
1 |
package eu.dnetlib.miscutils.functional.string; |
|
2 |
|
|
3 |
import java.io.IOException; |
|
4 |
import java.io.InputStream; |
|
5 |
import java.io.StringWriter; |
|
6 |
|
|
7 |
import org.apache.commons.io.IOUtils; |
|
8 |
import org.junit.Test; |
|
9 |
|
|
10 |
public class EscapeUnescapeTest { |
|
11 |
|
|
12 |
@Test |
|
13 |
public void testEscape() throws IOException { |
|
14 |
String xml = getRecord(); |
|
15 |
System.out.println(xml); |
|
16 |
System.out.println("##############"); |
|
17 |
String escaped = new EscapeHtml().evaluate(xml); |
|
18 |
System.out.println(escaped); |
|
19 |
|
|
20 |
String unescaped = new UnescapeHtml().evaluate(escaped); |
|
21 |
System.out.println("##############"); |
|
22 |
System.out.println(unescaped); |
|
23 |
|
|
24 |
} |
|
25 |
|
|
26 |
private String getRecord() throws IOException { |
|
27 |
InputStream is = getClass().getResourceAsStream("record.xml"); |
|
28 |
StringWriter sw = new StringWriter(); |
|
29 |
IOUtils.copy(is, sw); |
|
30 |
|
|
31 |
return sw.toString(); |
|
32 |
} |
|
33 |
|
|
34 |
} |
modules/cnr-misc-utils/tags/cnr-misc-utils-1.0.4/src/test/java/eu/dnetlib/miscutils/functional/CompositeUnaryFunctionTest.java | ||
---|---|---|
1 |
package eu.dnetlib.miscutils.functional; |
|
2 |
|
|
3 |
import static org.junit.Assert.*; // NOPMD |
|
4 |
|
|
5 |
import org.junit.Test; |
|
6 |
|
|
7 |
public class CompositeUnaryFunctionTest { |
|
8 |
|
|
9 |
@Test |
|
10 |
public void testOf() { |
|
11 |
final UnaryFunction<String, Integer> toStringBase = new UnaryFunction<String, Integer>() { // NOPMD |
|
12 |
@Override |
|
13 |
public String evaluate(final Integer arg) { |
|
14 |
return arg.toString(); |
|
15 |
} |
|
16 |
}; |
|
17 |
|
|
18 |
final UnaryFunction<Integer, String> fromStringBase = new UnaryFunction<Integer, String>() { |
|
19 |
@Override |
|
20 |
public Integer evaluate(final String arg) { |
|
21 |
return Integer.decode(arg); |
|
22 |
} |
|
23 |
}; |
|
24 |
|
|
25 |
final CompositeUnaryFunction<String, Integer> toString = new CompositeUnaryFunction<String, Integer>(toStringBase); |
|
26 |
final CompositeUnaryFunction<Integer, String> fromString = new CompositeUnaryFunction<Integer, String>(fromStringBase); |
|
27 |
|
|
28 |
assertEquals("evaluation", toString.evaluate(10), "10"); |
|
29 |
assertEquals("evaluation", fromString.evaluate("10"), (Integer) 10); |
|
30 |
|
|
31 |
final UnaryFunction<String, String> stringIdentity = toString.of(fromString); |
|
32 |
final UnaryFunction<Integer, Integer> integerIdentity = fromString.of(toString); |
|
33 |
final UnaryFunction<Integer, Integer> integerIdentity2 = fromString.of(stringIdentity).of(toString).of(integerIdentity); |
|
34 |
|
|
35 |
assertEquals("string evaluation", stringIdentity.evaluate("10"), "10"); |
|
36 |
assertEquals("integer evaluation", integerIdentity.evaluate(10), (Integer) 10); |
|
37 |
assertEquals("integer identity", integerIdentity2.evaluate(10), (Integer) 10); |
|
38 |
} |
|
39 |
|
|
40 |
private transient int calls; |
|
41 |
private transient final static int TIMES = 100; |
|
42 |
|
|
43 |
@Test |
|
44 |
public void testRecursive() { |
|
45 |
calls = 0; |
|
46 |
final UnaryFunction<Integer, Integer> incrementBase = new UnaryFunction<Integer, Integer>() { // NOPMD |
|
47 |
@Override |
|
48 |
public Integer evaluate(final Integer arg) { |
|
49 |
calls++; |
|
50 |
return arg + 1; |
|
51 |
} |
|
52 |
}; |
|
53 |
|
|
54 |
final CompositeUnaryFunction<Integer, Integer> increment = new CompositeUnaryFunction<Integer, Integer>(incrementBase); |
|
55 |
|
|
56 |
CompositeUnaryFunction<Integer, Integer> comp = increment; |
|
57 |
for (int i = 1; i <= 100; i++) { |
|
58 |
assertEquals("compare", comp.evaluate(0), (Integer) i); |
|
59 |
comp = comp.of(increment); |
|
60 |
} |
|
61 |
|
|
62 |
assertEquals("number of calls", calls, (TIMES * (TIMES + 1) / 2)); |
|
63 |
} |
|
64 |
|
|
65 |
@Test |
|
66 |
public void testCurry() { |
|
67 |
final UnaryFunction<UnaryFunction<Integer, Integer>, Integer> add = new UnaryFunction<UnaryFunction<Integer, Integer>, Integer>() { // NOPMD |
|
68 |
@Override |
|
69 |
public UnaryFunction<Integer, Integer> evaluate(final Integer arg) { |
|
70 |
return new UnaryFunction<Integer, Integer>() { |
|
71 |
@Override |
|
72 |
public Integer evaluate(final Integer brg) { |
|
73 |
return arg + brg; |
|
74 |
} |
|
75 |
}; |
|
76 |
|
|
77 |
} |
|
78 |
}; |
|
79 |
|
|
80 |
final UnaryFunction<Integer, Integer> increment = add.evaluate(1); |
|
81 |
|
|
82 |
assertEquals("check add", add.evaluate(1).evaluate(2), (Integer) 3); |
|
83 |
assertEquals("check increment", increment.evaluate(1), (Integer) 2); |
|
84 |
} |
|
85 |
|
|
86 |
} |
modules/cnr-misc-utils/tags/cnr-misc-utils-1.0.4/src/test/java/eu/dnetlib/miscutils/functional/ThreadSafeUnaryFunctionTest.java | ||
---|---|---|
1 |
package eu.dnetlib.miscutils.functional; |
|
2 |
|
|
3 |
import static org.junit.Assert.*; |
|
4 |
|
|
5 |
import java.util.concurrent.ExecutorService; |
|
6 |
import java.util.concurrent.Executors; |
|
7 |
|
|
8 |
import org.junit.Before; |
|
9 |
import org.junit.Test; |
|
10 |
|
|
11 |
import eu.dnetlib.miscutils.functional.ThreadSafeUnaryFunction; |
|
12 |
import eu.dnetlib.miscutils.functional.UnaryFunction; |
|
13 |
|
|
14 |
public class ThreadSafeUnaryFunctionTest { |
|
15 |
|
|
16 |
/** |
|
17 |
* Object under test. |
|
18 |
*/ |
|
19 |
private ThreadSafeUnaryFunction<String, Integer> tsUnaryFunction; |
|
20 |
|
|
21 |
private final static int N_THREAD = 10; |
|
22 |
|
|
23 |
private ExecutorService executor; |
|
24 |
|
|
25 |
@Before |
|
26 |
public void setUp() { |
|
27 |
executor = Executors.newFixedThreadPool(N_THREAD); |
|
28 |
} |
|
29 |
|
|
30 |
/** |
|
31 |
* method creates N_THREAD threads such that each one performs a function.evaluate call |
|
32 |
* @param function |
|
33 |
*/ |
|
34 |
private void createThreads(final ThreadSafeUnaryFunction<String, Integer> function) { |
|
35 |
for (int i = 0; i < N_THREAD; i++) { |
|
36 |
final Integer j = i; |
|
37 |
executor.execute(new Runnable() { |
|
38 |
|
|
39 |
@Override |
|
40 |
public void run() { |
|
41 |
assertEquals(String.valueOf(j), function.evaluate(j)); |
|
42 |
} |
|
43 |
}); |
|
44 |
} |
|
45 |
} |
|
46 |
|
|
47 |
@Test |
|
48 |
public void testThreadSafeUnaryFunction_1() { |
|
49 |
tsUnaryFunction = new ThreadSafeUnaryFunction<String, Integer>(new ThreadLocal<UnaryFunction<String, Integer>>() { |
|
50 |
@Override |
|
51 |
protected synchronized UnaryFunction<String, Integer> initialValue() { |
|
52 |
return new UnsafeUnaryFunction(); |
|
53 |
} |
|
54 |
}); |
|
55 |
createThreads(tsUnaryFunction); |
|
56 |
} |
|
57 |
|
|
58 |
} |
modules/cnr-misc-utils/tags/cnr-misc-utils-1.0.4/src/test/java/eu/dnetlib/miscutils/functional/xml/ApplyXsltTest.java | ||
---|---|---|
1 |
package eu.dnetlib.miscutils.functional.xml; |
|
2 |
|
|
3 |
import static org.junit.Assert.*; |
|
4 |
|
|
5 |
import java.io.IOException; |
|
6 |
|
|
7 |
import javax.xml.transform.TransformerFactory; |
|
8 |
|
|
9 |
import org.apache.commons.io.IOUtils; |
|
10 |
import org.junit.Test; |
|
11 |
import org.junit.runner.RunWith; |
|
12 |
import org.mockito.runners.MockitoJUnit44Runner; |
|
13 |
import org.springframework.core.io.ClassPathResource; |
|
14 |
|
|
15 |
@RunWith(MockitoJUnit44Runner.class) |
|
16 |
public class ApplyXsltTest { |
|
17 |
|
|
18 |
TransformerFactory tf = TransformerFactory.newInstance(); |
|
19 |
|
|
20 |
private static final Class<?> transformerClass = net.sf.saxon.TransformerFactoryImpl.class; |
|
21 |
|
|
22 |
@Test |
|
23 |
public void testTransformerFactoryType() { |
|
24 |
assertEquals(tf.getClass(), transformerClass); |
|
25 |
} |
|
26 |
|
|
27 |
@Test |
|
28 |
public void applyXslt() throws IOException { |
|
29 |
String record = IOUtils.toString((new ClassPathResource("/eu/dnetlib/miscutils/functional/xml/sampleRecord.xml")).getInputStream()); |
|
30 |
String layout = IOUtils.toString((new ClassPathResource("/eu/dnetlib/miscutils/functional/xml/sampleIndexLayout.xml")).getInputStream()); |
|
31 |
String indexXsltOfXslt = IOUtils.toString((new ClassPathResource("/eu/dnetlib/miscutils/functional/xml/layoutToRecordStylesheet.xsl")).getInputStream()); |
|
32 |
ApplyXslt applyXslt = new ApplyXslt(IOUtils.toString((new ClassPathResource("/eu/dnetlib/miscutils/functional/xml/recordStylesheet.xsl")).getInputStream())); |
|
33 |
ApplyXslt xslt1 = new ApplyXslt(indexXsltOfXslt); |
|
34 |
String indexXslt = xslt1.evaluate(layout); |
|
35 |
ApplyXslt xslt2 = new ApplyXslt(indexXslt); |
|
36 |
String response = xslt2.evaluate(record); |
|
37 |
System.out.println(response); |
|
38 |
|
|
39 |
} |
|
40 |
|
|
41 |
} |
modules/cnr-misc-utils/tags/cnr-misc-utils-1.0.4/src/test/java/eu/dnetlib/miscutils/functional/xml/dnetFunctionsTest.java | ||
---|---|---|
1 |
package eu.dnetlib.miscutils.functional.xml; |
|
2 |
|
|
3 |
import static org.junit.Assert.assertEquals; |
|
4 |
|
|
5 |
import java.io.StringReader; |
|
6 |
import java.io.StringWriter; |
|
7 |
|
|
8 |
import javax.xml.transform.Transformer; |
|
9 |
import javax.xml.transform.TransformerFactory; |
|
10 |
import javax.xml.transform.stream.StreamResult; |
|
11 |
import javax.xml.transform.stream.StreamSource; |
|
12 |
|
|
13 |
import org.dom4j.Document; |
|
14 |
import org.dom4j.io.SAXReader; |
|
15 |
import org.junit.Before; |
|
16 |
import org.junit.Test; |
|
17 |
import org.junit.runner.RunWith; |
|
18 |
import org.mockito.runners.MockitoJUnit44Runner; |
|
19 |
|
|
20 |
@RunWith(MockitoJUnit44Runner.class) |
|
21 |
public class dnetFunctionsTest { |
|
22 |
|
|
23 |
private static final String XSLT = |
|
24 |
"<?xml version='1.0' encoding='UTF-8'?>" + |
|
25 |
"<xsl:stylesheet version='2.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform' xmlns:dnet='eu.dnetlib.miscutils.functional.xml.DnetXsltFunctions'>" + |
|
26 |
"<xsl:template match='/'>" + |
|
27 |
" <OUTS>" + |
|
28 |
" <OUTPUT decade='{dnet:decade(//INPUT/DATE)}' />" + |
|
29 |
" <OUTPUT year='{dnet:extractYear(//INPUT/DATE)}' />" + |
|
30 |
" </OUTS>" + |
|
31 |
"</xsl:template>" + |
|
32 |
"</xsl:stylesheet>"; |
|
33 |
|
|
34 |
private Transformer transformer; |
|
35 |
|
|
36 |
@Before |
|
37 |
public void setUp() { |
|
38 |
try { |
|
39 |
transformer = TransformerFactory.newInstance().newTransformer(new StreamSource(new StringReader(XSLT))); |
|
40 |
} catch (Exception e) { |
|
41 |
System.out.println("**** INITIALIZATION FAILED ****"); |
|
42 |
} |
|
43 |
} |
|
44 |
|
|
45 |
@Test |
|
46 |
public void testDecade_1() throws Exception { |
|
47 |
assertEquals("1980-1989", process("primi anni 80", "decade")); |
|
48 |
} |
|
49 |
|
|
50 |
@Test |
|
51 |
public void testDecade_2() throws Exception { |
|
52 |
assertEquals("2000-2009", process("04-05-2007", "decade")); |
|
53 |
} |
|
54 |
|
|
55 |
@Test |
|
56 |
public void testDecade_3() throws Exception { |
|
57 |
assertEquals("1960-1969", process("1964", "decade")); |
|
58 |
} |
|
59 |
|
|
60 |
@Test |
|
61 |
public void testDecade_4() throws Exception { |
|
62 |
assertEquals("n/a", process("XXXXXXXXXXXX", "decade")); |
|
63 |
} |
|
64 |
|
|
65 |
@Test |
|
66 |
public void testDecade_5() throws Exception { |
|
67 |
assertEquals("1880-1889", process(" 1887-01-01 ", "decade")); |
|
68 |
} |
|
69 |
|
|
70 |
@Test |
|
71 |
public void testDecade_6() throws Exception { |
|
72 |
assertEquals("1880-1889", process("\n\n 1887-01-01 \n\n", "decade")); |
|
73 |
} |
|
74 |
|
|
75 |
@Test |
|
76 |
public void testDecade_7() throws Exception { |
|
77 |
assertEquals("1840-1849", process(" 1840-03-05 ", "decade")); |
|
78 |
} |
|
79 |
|
|
80 |
@Test |
|
81 |
public void testYear_1() throws Exception { |
|
82 |
assertEquals("1840", process("1840-03-05", "year")); |
|
83 |
} |
|
84 |
|
|
85 |
@Test |
|
86 |
public void testYear_2() throws Exception { |
|
87 |
assertEquals("1840", process("1840/03/05", "year")); |
|
88 |
} |
|
89 |
|
|
90 |
private String process(String s, String attr) throws Exception { |
|
91 |
String xml = "<INPUT><DATE>" + s + "</DATE></INPUT>"; |
|
92 |
|
|
93 |
StringWriter strw = new StringWriter(); |
|
94 |
transformer.transform(new StreamSource(new StringReader(xml)), new StreamResult(strw)); |
|
95 |
|
|
96 |
Document document = (new SAXReader()).read(new StringReader((strw.toString()))); |
|
97 |
|
|
98 |
System.out.println(document.asXML()); |
|
99 |
|
|
100 |
String res = document.valueOf("/OUTS/OUTPUT/@" + attr); |
|
101 |
|
|
102 |
System.out.println(s + " --> " + res); |
|
103 |
|
|
104 |
return res; |
|
105 |
} |
|
106 |
|
|
107 |
} |
modules/cnr-misc-utils/tags/cnr-misc-utils-1.0.4/src/test/java/eu/dnetlib/miscutils/functional/xml/IndentXmlStringTest.java | ||
---|---|---|
1 |
package eu.dnetlib.miscutils.functional.xml; |
|
2 |
|
|
3 |
import org.junit.Before; |
|
4 |
import org.junit.Test; |
|
5 |
|
|
6 |
public class IndentXmlStringTest { |
|
7 |
|
|
8 |
@Before |
|
9 |
public void setUp() throws Exception { |
|
10 |
|
|
11 |
} |
|
12 |
|
|
13 |
@Test |
|
14 |
public void testEvaluate() { |
|
15 |
String indented = new IndentXmlString().evaluate("<root><a/></root>"); |
|
16 |
System.out.println(indented); |
|
17 |
} |
|
18 |
|
|
19 |
} |
modules/cnr-misc-utils/tags/cnr-misc-utils-1.0.4/src/test/java/eu/dnetlib/miscutils/hstree/sample/tree/SecondChild.java | ||
---|---|---|
1 |
package eu.dnetlib.miscutils.hstree.sample.tree; |
|
2 |
|
|
3 |
import eu.dnetlib.miscutils.hstree.NilTreeNode; |
|
4 |
import eu.dnetlib.miscutils.hstree.sample.resources.SubResource; |
|
5 |
|
|
6 |
public class SecondChild extends MyTreeNode<SubResource, Void, NilTreeNode<MyTreeVisitor>> { |
|
7 |
@Override |
|
8 |
public void accept(final MyTreeVisitor visitor) { |
|
9 |
visitor.visit(getResource()); |
|
10 |
} |
|
11 |
|
|
12 |
} |
modules/cnr-misc-utils/tags/cnr-misc-utils-1.0.4/src/test/java/eu/dnetlib/miscutils/hstree/sample/tree/RootTree.java | ||
---|---|---|
1 |
package eu.dnetlib.miscutils.hstree.sample.tree; |
|
2 |
|
|
3 |
import eu.dnetlib.miscutils.hstree.sample.resources.ChildResource; |
|
4 |
import eu.dnetlib.miscutils.hstree.sample.resources.SampleResource; |
|
5 |
|
|
6 |
public class RootTree extends MyTreeNode<SampleResource, ChildResource, FirstChild> { |
|
7 |
|
|
8 |
public RootTree(final SampleResource resource) { |
|
9 |
super(resource); |
|
10 |
} |
|
11 |
|
|
12 |
@Override |
|
13 |
public void accept(final MyTreeVisitor visitor) { |
|
14 |
visitor.visit(getResource()); |
|
15 |
} |
|
16 |
} |
modules/cnr-misc-utils/tags/cnr-misc-utils-1.0.4/src/test/java/eu/dnetlib/miscutils/hstree/sample/tree/MyTreeVisitor.java | ||
---|---|---|
1 |
package eu.dnetlib.miscutils.hstree.sample.tree; |
|
2 |
|
|
3 |
import eu.dnetlib.miscutils.hstree.sample.resources.ChildResource; |
|
4 |
import eu.dnetlib.miscutils.hstree.sample.resources.SampleResource; |
|
5 |
import eu.dnetlib.miscutils.hstree.sample.resources.SubResource; |
|
6 |
|
|
7 |
public interface MyTreeVisitor { |
|
8 |
|
|
9 |
void visit(SampleResource resource); |
|
10 |
|
|
11 |
void visit(ChildResource resource); |
|
12 |
|
|
13 |
void visit(SubResource resource); |
|
14 |
|
|
15 |
} |
modules/cnr-misc-utils/tags/cnr-misc-utils-1.0.4/src/test/java/eu/dnetlib/miscutils/hstree/sample/tree/FirstChild.java | ||
---|---|---|
1 |
package eu.dnetlib.miscutils.hstree.sample.tree; |
|
2 |
|
|
3 |
import eu.dnetlib.miscutils.hstree.sample.resources.ChildResource; |
|
4 |
import eu.dnetlib.miscutils.hstree.sample.resources.SubResource; |
|
5 |
|
|
6 |
public class FirstChild extends MyTreeNode<ChildResource, SubResource, SecondChild> { |
|
7 |
@Override |
|
8 |
public void accept(final MyTreeVisitor visitor) { |
|
9 |
visitor.visit(getResource()); |
|
10 |
} |
|
11 |
|
|
12 |
} |
modules/cnr-misc-utils/tags/cnr-misc-utils-1.0.4/src/test/java/eu/dnetlib/miscutils/hstree/sample/tree/MyComputingVisitor.java | ||
---|---|---|
1 |
package eu.dnetlib.miscutils.hstree.sample.tree; |
|
2 |
|
|
3 |
import org.apache.commons.logging.Log; |
|
4 |
import org.apache.commons.logging.LogFactory; |
|
5 |
|
|
6 |
import eu.dnetlib.miscutils.hstree.sample.resources.ChildResource; |
|
7 |
import eu.dnetlib.miscutils.hstree.sample.resources.SampleResource; |
|
8 |
import eu.dnetlib.miscutils.hstree.sample.resources.SubResource; |
|
9 |
|
|
10 |
public class MyComputingVisitor implements MyTreeVisitor { |
|
11 |
private static final Log log = LogFactory.getLog(MyComputingVisitor.class); // NOPMD by marko on 11/24/08 5:02 PM |
|
12 |
|
|
13 |
int count; |
|
14 |
|
|
15 |
@Override |
|
16 |
public void visit(final SampleResource resource) { |
|
17 |
log.info("increasing sampleresource"); |
|
18 |
count += 100; |
|
19 |
} |
|
20 |
|
|
21 |
@Override |
|
22 |
public void visit(final ChildResource resource) { |
|
23 |
log.info("increasing childresource"); |
|
24 |
count += 10; |
|
25 |
} |
|
26 |
|
|
27 |
@Override |
|
28 |
public void visit(final SubResource resource) { |
|
29 |
log.info("increasing subresource"); |
|
30 |
count += 1; |
|
31 |
} |
|
32 |
|
|
33 |
public int getCount() { |
|
34 |
return count; |
|
35 |
} |
|
36 |
|
|
37 |
public void setCount(final int count) { |
|
38 |
this.count = count; |
|
39 |
} |
|
40 |
|
|
41 |
} |
modules/cnr-misc-utils/tags/cnr-misc-utils-1.0.4/src/test/java/eu/dnetlib/miscutils/hstree/sample/tree/MyTreeNode.java | ||
---|---|---|
1 |
package eu.dnetlib.miscutils.hstree.sample.tree; |
|
2 |
|
|
3 |
import eu.dnetlib.miscutils.hstree.TreeNode; |
|
4 |
|
|
5 |
public class MyTreeNode<T, N, C extends TreeNode<N, ?, MyTreeVisitor, ?>> extends TreeNode<T, N, MyTreeVisitor, C> { |
|
6 |
|
|
7 |
public MyTreeNode() { |
|
8 |
// no action |
|
9 |
} |
|
10 |
|
|
11 |
public MyTreeNode(final T resource) { |
|
12 |
super(resource); |
Also available in: Unified diff
cleanup