Revision 50644
Added by Alessia Bardi about 5 years ago
modules/cnr-misc-utils/tags/cnr-misc-utils-1.0.5/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.5/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.5/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.5/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.5/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.5/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.5/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.5/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.5/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.5/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.5/src/test/java/eu/dnetlib/miscutils/hstree/VisitorTest.java | ||
---|---|---|
1 |
package eu.dnetlib.miscutils.hstree; |
|
2 |
|
|
3 |
import static org.junit.Assert.*; // NOPMD |
|
4 |
|
|
5 |
import org.junit.Before; |
|
6 |
import org.junit.Test; |
|
7 |
|
|
8 |
import eu.dnetlib.miscutils.hstree.sample.resources.ChildResource; |
|
9 |
import eu.dnetlib.miscutils.hstree.sample.resources.SampleResource; |
|
10 |
import eu.dnetlib.miscutils.hstree.sample.resources.SubResource; |
|
11 |
import eu.dnetlib.miscutils.hstree.sample.tree.FirstChild; |
|
12 |
import eu.dnetlib.miscutils.hstree.sample.tree.MyComputingVisitor; |
|
13 |
import eu.dnetlib.miscutils.hstree.sample.tree.MyLoggingVisitor; |
|
14 |
import eu.dnetlib.miscutils.hstree.sample.tree.MyTreeNode; |
|
15 |
import eu.dnetlib.miscutils.hstree.sample.tree.MyTreeVisitor; |
|
16 |
import eu.dnetlib.miscutils.hstree.sample.tree.RootTree; |
|
17 |
|
|
18 |
public class VisitorTest { |
|
19 |
|
|
20 |
transient RootTree root = null; |
|
21 |
transient MyTreeVisitor loggingVisitor = null; |
|
22 |
transient MyComputingVisitor computingVisitor = null; |
|
23 |
|
|
24 |
@Before |
|
25 |
public void setUp() throws Exception { |
|
26 |
root = new RootTree(new SampleResource()); |
|
27 |
|
|
28 |
root.addChild(new ChildResource()).appendChild(new SubResource()).appendChild(new SubResource()).addChild(new SubResource()); |
|
29 |
|
|
30 |
loggingVisitor = new MyLoggingVisitor(); |
|
31 |
computingVisitor = new MyComputingVisitor(); |
|
32 |
} |
|
33 |
|
|
34 |
@Test |
|
35 |
public void testVisit() { |
|
36 |
final MyTreeNode<SampleResource, ChildResource, FirstChild> root = new RootTree(new SampleResource()); |
|
37 |
assertNotNull("exists", root); |
|
38 |
|
|
39 |
final FirstChild ch1 = root.addChild(new ChildResource()); |
|
40 |
ch1.addChild(new SubResource()); |
|
41 |
ch1.addChild(new SubResource()); |
|
42 |
|
|
43 |
final FirstChild ch2 = root.addChild(new ChildResource()); |
|
44 |
ch2.addChild(new SubResource()); |
|
45 |
ch2.addChild(new SubResource()); |
|
46 |
ch2.addChild(new SubResource()); |
|
47 |
|
|
48 |
root.breadthFirst(loggingVisitor); |
|
49 |
} |
|
50 |
|
|
51 |
@Test |
|
52 |
public void testVisit2() { |
|
53 |
final MyTreeNode<SampleResource, ChildResource, FirstChild> root = new RootTree(new SampleResource()); |
|
54 |
assertNotNull("exists", root); |
|
55 |
|
|
56 |
root.addChild(new ChildResource()).appendChild(new SubResource()).appendChild(new SubResource()).addChild(new SubResource()); |
|
57 |
root.appendChild(new ChildResource()).appendChild(new ChildResource()).appendChild(new ChildResource()).addChild(new ChildResource()).addChild( |
|
58 |
new SubResource()); |
|
59 |
|
|
60 |
root.breadthFirst(loggingVisitor); |
|
61 |
} |
|
62 |
|
|
63 |
@Test |
|
64 |
public void testVisit3() { |
|
65 |
final MyTreeNode<SampleResource, ChildResource, FirstChild> root = new RootTree(new SampleResource()); |
|
66 |
assertNotNull("exists", root); |
|
67 |
|
|
68 |
root.addChild(new ChildResource()).appendChild(new SubResource()).appendChild(new SubResource()).addChild(new SubResource()); |
|
69 |
root.appendChild(new ChildResource()).appendChild(new ChildResource()).appendChild(new ChildResource()).addChild(new ChildResource()).addChild( |
|
70 |
new SubResource()); |
|
71 |
|
|
72 |
root.depthFirst(loggingVisitor); |
|
73 |
} |
|
74 |
|
|
75 |
@Test |
|
76 |
public void testMyVisitor() { |
|
77 |
assertNotNull("dummy", root); |
|
78 |
|
|
79 |
root.accept(loggingVisitor); |
|
80 |
root.getChildren().get(0).accept(loggingVisitor); |
|
81 |
} |
|
82 |
|
|
83 |
@Test |
|
84 |
public void testComputingVisitor() { |
|
85 |
assertNotNull("dummy", root); |
|
86 |
|
|
87 |
root.breadthFirst(computingVisitor); |
|
88 |
assertEquals("check count", 113, computingVisitor.getCount()); |
|
89 |
} |
|
90 |
} |
modules/cnr-misc-utils/tags/cnr-misc-utils-1.0.5/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.5/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.5/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.5/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.5/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.5/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); |
|
13 |
} |
|
14 |
} |
modules/cnr-misc-utils/tags/cnr-misc-utils-1.0.5/src/test/java/eu/dnetlib/miscutils/hstree/sample/tree/MyLoggingVisitor.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 MyLoggingVisitor implements MyTreeVisitor { |
|
11 |
private static final Log log = LogFactory.getLog(MyLoggingVisitor.class); // NOPMD by marko on 11/24/08 5:02 PM |
|
12 |
|
|
13 |
/* |
|
14 |
* (non-Javadoc) |
|
15 |
* |
|
16 |
* @see |
|
17 |
* eu.rinfrastructures.genericstest.sample.wrongtree.MyTreeVisitor#visit(eu.rinfrastructures.genericstest.sample |
|
18 |
* .resources.SampleResource) |
|
19 |
*/ |
|
20 |
@Override |
|
21 |
public void visit(final SampleResource resource) { |
|
22 |
log.info("sample"); |
|
23 |
} |
|
24 |
|
|
25 |
/* |
|
26 |
* (non-Javadoc) |
|
27 |
* |
|
28 |
* @see |
|
29 |
* eu.rinfrastructures.genericstest.sample.wrongtree.MyTreeVisitor#visit(eu.rinfrastructures.genericstest.sample |
|
30 |
* .resources.ChildResource) |
|
31 |
*/ |
|
32 |
@Override |
|
33 |
public void visit(final ChildResource resource) { |
|
34 |
log.info("child"); |
|
35 |
} |
|
36 |
|
|
37 |
/* |
|
38 |
* (non-Javadoc) |
|
39 |
* |
|
40 |
* @see |
|
41 |
* eu.rinfrastructures.genericstest.sample.wrongtree.MyTreeVisitor#visit(eu.rinfrastructures.genericstest.sample |
|
42 |
* .resources.SubResource) |
|
43 |
*/ |
|
44 |
@Override |
|
45 |
public void visit(final SubResource resource) { |
|
46 |
log.info("sub"); |
|
47 |
} |
|
48 |
|
|
49 |
} |
modules/cnr-misc-utils/tags/cnr-misc-utils-1.0.5/src/test/java/eu/dnetlib/miscutils/hstree/sample/resources/SubResource.java | ||
---|---|---|
1 |
package eu.dnetlib.miscutils.hstree.sample.resources; |
|
2 |
|
|
3 |
public class SubResource extends Resource { // NOPMD |
|
4 |
|
|
5 |
} |
modules/cnr-misc-utils/tags/cnr-misc-utils-1.0.5/src/test/java/eu/dnetlib/miscutils/hstree/sample/resources/Resource.java | ||
---|---|---|
1 |
package eu.dnetlib.miscutils.hstree.sample.resources; |
|
2 |
|
|
3 |
public class Resource { // NOPMD |
|
4 |
|
|
5 |
} |
modules/cnr-misc-utils/tags/cnr-misc-utils-1.0.5/src/test/java/eu/dnetlib/miscutils/hstree/sample/resources/SampleResource.java | ||
---|---|---|
1 |
package eu.dnetlib.miscutils.hstree.sample.resources; |
|
2 |
|
|
3 |
public class SampleResource extends Resource { // NOPMD |
|
4 |
|
|
5 |
} |
modules/cnr-misc-utils/tags/cnr-misc-utils-1.0.5/src/test/java/eu/dnetlib/miscutils/hstree/sample/resources/ChildResource.java | ||
---|---|---|
1 |
package eu.dnetlib.miscutils.hstree.sample.resources; |
|
2 |
|
|
3 |
public class ChildResource extends Resource { // NOPMD |
|
4 |
|
|
5 |
} |
modules/cnr-misc-utils/tags/cnr-misc-utils-1.0.5/src/test/java/eu/dnetlib/miscutils/hstree/sample/wrongtree/WrongRootTree.java | ||
---|---|---|
1 |
package eu.dnetlib.miscutils.hstree.sample.wrongtree; |
|
2 |
|
|
3 |
import eu.dnetlib.miscutils.hstree.TreeNode; |
|
4 |
import eu.dnetlib.miscutils.hstree.sample.resources.ChildResource; |
|
5 |
import eu.dnetlib.miscutils.hstree.sample.resources.SampleResource; |
|
6 |
import eu.dnetlib.miscutils.hstree.sample.tree.MyLoggingVisitor; |
|
7 |
|
|
8 |
public class WrongRootTree extends TreeNode<SampleResource, ChildResource, MyLoggingVisitor, WrongFirstChild> { |
|
9 |
|
|
10 |
public WrongRootTree(final SampleResource resource) { |
|
11 |
super(resource); |
|
12 |
} |
|
13 |
|
|
14 |
} |
modules/cnr-misc-utils/tags/cnr-misc-utils-1.0.5/src/test/java/eu/dnetlib/miscutils/hstree/sample/wrongtree/WrongFirstChild.java | ||
---|---|---|
1 |
package eu.dnetlib.miscutils.hstree.sample.wrongtree; |
|
2 |
|
|
3 |
import eu.dnetlib.miscutils.hstree.TreeNode; |
|
4 |
import eu.dnetlib.miscutils.hstree.sample.resources.ChildResource; |
|
5 |
import eu.dnetlib.miscutils.hstree.sample.resources.SubResource; |
|
6 |
import eu.dnetlib.miscutils.hstree.sample.tree.MyLoggingVisitor; |
|
7 |
|
|
8 |
public class WrongFirstChild extends TreeNode<ChildResource, SubResource, MyLoggingVisitor, WrongSecondChild> { // NOPMD |
|
9 |
|
|
10 |
} |
modules/cnr-misc-utils/tags/cnr-misc-utils-1.0.5/src/test/java/eu/dnetlib/miscutils/hstree/sample/wrongtree/WrongSecondChild.java | ||
---|---|---|
1 |
package eu.dnetlib.miscutils.hstree.sample.wrongtree; |
|
2 |
|
|
3 |
import eu.dnetlib.miscutils.hstree.NilTreeNode; |
|
4 |
import eu.dnetlib.miscutils.hstree.TreeNode; |
|
5 |
import eu.dnetlib.miscutils.hstree.sample.resources.SubResource; |
|
6 |
import eu.dnetlib.miscutils.hstree.sample.tree.MyLoggingVisitor; |
|
7 |
|
|
8 |
public class WrongSecondChild extends TreeNode<SubResource, Void, MyLoggingVisitor, NilTreeNode<MyLoggingVisitor>> { // NOPMD |
|
9 |
|
|
10 |
} |
modules/cnr-misc-utils/tags/cnr-misc-utils-1.0.5/src/test/java/eu/dnetlib/miscutils/hstree/WrongTreeNodeTest.java | ||
---|---|---|
1 |
package eu.dnetlib.miscutils.hstree; |
|
2 |
|
|
3 |
import static org.junit.Assert.assertTrue; |
|
4 |
|
|
5 |
import org.junit.Test; |
|
6 |
|
|
7 |
import eu.dnetlib.miscutils.hstree.sample.resources.ChildResource; |
|
8 |
import eu.dnetlib.miscutils.hstree.sample.resources.SampleResource; |
|
9 |
import eu.dnetlib.miscutils.hstree.sample.resources.SubResource; |
|
10 |
import eu.dnetlib.miscutils.hstree.sample.wrongtree.WrongFirstChild; |
|
11 |
import eu.dnetlib.miscutils.hstree.sample.wrongtree.WrongRootTree; |
|
12 |
import eu.dnetlib.miscutils.hstree.sample.wrongtree.WrongSecondChild; |
|
13 |
|
|
14 |
public class WrongTreeNodeTest { |
|
15 |
|
|
16 |
/** |
|
17 |
* Try to break/misalign the signatures of the 'sample.wrongtree' package. The code should not be able to compile. |
|
18 |
*/ |
|
19 |
@Test |
|
20 |
public void simpleTest() { |
|
21 |
final WrongRootTree root = new WrongRootTree(new SampleResource()); |
|
22 |
|
|
23 |
final WrongFirstChild ch1 = root.addChild(new ChildResource()); |
|
24 |
ch1.addChild(new SubResource()); |
|
25 |
ch1.addChild(new SubResource()); |
|
26 |
|
|
27 |
final WrongFirstChild ch2 = root.addChild(new ChildResource()); |
|
28 |
ch2.addChild(new SubResource()); |
|
29 |
ch2.addChild(new SubResource()); |
|
30 |
final WrongSecondChild sc1 = ch2.addChild(new SubResource()); |
|
31 |
|
|
32 |
// of course we can do obscene things like this, |
|
33 |
// but if somebody casts there is no escape. |
|
34 |
boolean gotRuntimeEx = false; // NOPMD |
|
35 |
try { |
|
36 |
sc1.addChild((Void) null); |
|
37 |
} catch (RuntimeException e) { |
|
38 |
if (e.getMessage().equals(TreeNode.CHILDR_UNDER_LEAF)) |
|
39 |
gotRuntimeEx = true; |
|
40 |
} |
|
41 |
assertTrue("got exception", gotRuntimeEx); // TODO: exploit junit4 for checking exceptions |
|
42 |
|
|
43 |
} |
|
44 |
} |
modules/cnr-misc-utils/tags/cnr-misc-utils-1.0.5/src/test/java/eu/dnetlib/miscutils/hstree/TreeNodeTest.java | ||
---|---|---|
1 |
package eu.dnetlib.miscutils.hstree; |
|
2 |
|
|
3 |
import static org.junit.Assert.assertNotNull; |
|
4 |
|
|
5 |
import org.junit.Test; |
|
6 |
|
|
7 |
import eu.dnetlib.miscutils.hstree.sample.resources.ChildResource; |
|
8 |
import eu.dnetlib.miscutils.hstree.sample.resources.SampleResource; |
|
9 |
import eu.dnetlib.miscutils.hstree.sample.resources.SubResource; |
|
10 |
import eu.dnetlib.miscutils.hstree.sample.tree.FirstChild; |
|
11 |
import eu.dnetlib.miscutils.hstree.sample.tree.MyTreeNode; |
|
12 |
import eu.dnetlib.miscutils.hstree.sample.tree.RootTree; |
|
13 |
|
|
14 |
public class TreeNodeTest { |
|
15 |
|
|
16 |
private static final String EXISTS = "exists"; |
|
17 |
|
|
18 |
/** |
|
19 |
* This test simply shows how to create a statically checked tree. The tree grammar for this test is defined in |
|
20 |
* package eu.rinfrastructures.genericstest.sample.tree and it contains resource payloads defined in package |
|
21 |
* eu.rinfrastructures.genericstest.sample.resources |
|
22 |
*/ |
|
23 |
@Test |
|
24 |
public void simpleTest() { |
|
25 |
final MyTreeNode<SampleResource, ChildResource, FirstChild> root = new RootTree(new SampleResource()); |
|
26 |
|
|
27 |
final FirstChild ch1 = root.addChild(new ChildResource()); |
|
28 |
assertNotNull(EXISTS, ch1); |
|
29 |
|
|
30 |
ch1.addChild(new SubResource()); |
|
31 |
ch1.addChild(new SubResource()); |
|
32 |
|
|
33 |
final FirstChild ch2 = root.addChild(new ChildResource()); |
|
34 |
assertNotNull(EXISTS, ch2); |
|
35 |
|
|
36 |
ch2.addChild(new SubResource()); |
|
37 |
ch2.addChild(new SubResource()); |
|
38 |
ch2.addChild(new SubResource()); |
|
39 |
|
|
40 |
} |
|
41 |
|
|
42 |
@Test |
|
43 |
public void typeSafeness() { |
|
44 |
final MyTreeNode<SampleResource, ChildResource, FirstChild> root = new RootTree(new SampleResource()); |
|
45 |
|
|
46 |
final FirstChild ch1 = root.addChild(new ChildResource()); |
|
47 |
assertNotNull(EXISTS, ch1); |
|
48 |
|
|
49 |
ch1.addChild(new SubResource()); |
|
50 |
|
|
51 |
// ILLEGAL: |
|
52 |
//c1.addChild(new SampleResource()); |
|
53 |
} |
|
54 |
|
|
55 |
@Test |
|
56 |
public void cascade() { |
|
57 |
final MyTreeNode<SampleResource, ChildResource, FirstChild> root = new RootTree(new SampleResource()); |
|
58 |
assertNotNull(EXISTS, root); |
|
59 |
|
|
60 |
root.addChild(new ChildResource()).appendChild(new SubResource()).appendChild(new SubResource()).addChild(new SubResource()); |
|
61 |
|
|
62 |
} |
|
63 |
} |
modules/cnr-misc-utils/tags/cnr-misc-utils-1.0.5/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.5/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.5/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.5/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.5/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.5/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.5/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.5/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)); |
Also available in: Unified diff
[maven-release-plugin] copy for tag cnr-misc-utils-1.0.5