Skip to content

Commit f23f45f

Browse files
authored
Merge pull request #1573 from peterchenhdu/master
[tests] Improve test coverage.
2 parents 06e6db5 + 57abaa0 commit f23f45f

File tree

3 files changed

+203
-16
lines changed

3 files changed

+203
-16
lines changed

src/main/java/org/apache/ibatis/annotations/CacheNamespace.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.lang.annotation.RetentionPolicy;
2222
import java.lang.annotation.Target;
2323

24+
import org.apache.ibatis.cache.Cache;
2425
import org.apache.ibatis.cache.decorators.LruCache;
2526
import org.apache.ibatis.cache.impl.PerpetualCache;
2627

@@ -32,9 +33,9 @@
3233
@Retention(RetentionPolicy.RUNTIME)
3334
@Target(ElementType.TYPE)
3435
public @interface CacheNamespace {
35-
Class<? extends org.apache.ibatis.cache.Cache> implementation() default PerpetualCache.class;
36+
Class<? extends Cache> implementation() default PerpetualCache.class;
3637

37-
Class<? extends org.apache.ibatis.cache.Cache> eviction() default LruCache.class;
38+
Class<? extends Cache> eviction() default LruCache.class;
3839

3940
long flushInterval() default 0;
4041

src/test/java/org/apache/ibatis/io/VFSTest.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
*/
1616
package org.apache.ibatis.io;
1717

18+
import java.io.IOException;
19+
import java.lang.reflect.Method;
20+
1821
import org.junit.jupiter.api.Assertions;
1922
import org.junit.jupiter.api.Test;
2023

@@ -59,6 +62,40 @@ void getInstanceShouldNotBeNullInMultiThreadEnv() throws InterruptedException {
5962
}
6063
}
6164

65+
@Test
66+
void getExistMethod() {
67+
Method method = VFS.getMethod(VFS.class, "list", String.class);
68+
Assertions.assertNotNull(method);
69+
}
70+
71+
@Test
72+
void getNotExistMethod() {
73+
Method method = VFS.getMethod(VFS.class, "listXxx", String.class);
74+
Assertions.assertNull(method);
75+
}
76+
77+
@Test
78+
void invoke() throws IOException, NoSuchMethodException {
79+
VFS vfs = VFS.invoke(VFS.class.getMethod("getInstance"), VFS.class);
80+
Assertions.assertEquals(vfs, VFS.getInstance());
81+
82+
Assertions.assertThrows(RuntimeException.class, () -> {
83+
//java.lang.IllegalArgumentException: wrong number of arguments
84+
VFS.invoke(VFS.class.getMethod("getInstance"), VFS.class, "unnecessaryArgument");
85+
});
86+
87+
Assertions.assertThrows(IOException.class, () -> {
88+
//InvocationTargetException.getTargetException -> IOException
89+
VFS.invoke(Resources.class.getMethod("getResourceAsProperties", String.class), Resources.class, "invalidResource");
90+
});
91+
92+
Assertions.assertThrows(RuntimeException.class, () -> {
93+
//Other InvocationTargetException
94+
VFS.invoke(Integer.class.getMethod("valueOf", String.class), Resources.class, "InvalidIntegerNumber");
95+
});
96+
97+
}
98+
6299
private class InstanceGetterProcedure implements Runnable {
63100

64101
volatile VFS instanceGot;

src/test/java/org/apache/ibatis/parsing/XPathParserTest.java

Lines changed: 163 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,31 +17,180 @@
1717

1818
import static org.junit.jupiter.api.Assertions.assertEquals;
1919

20+
import javax.xml.parsers.DocumentBuilder;
21+
import javax.xml.parsers.DocumentBuilderFactory;
22+
import java.io.BufferedReader;
23+
import java.io.IOException;
2024
import java.io.InputStream;
25+
import java.io.Reader;
2126

27+
import org.apache.ibatis.builder.BuilderException;
2228
import org.apache.ibatis.io.Resources;
2329
import org.junit.jupiter.api.Test;
30+
import org.w3c.dom.Document;
31+
import org.xml.sax.*;
2432

2533
class XPathParserTest {
34+
private String resource = "resources/nodelet_test.xml";
2635

36+
//InputStream Source
2737
@Test
28-
void shouldTestXPathParserMethods() throws Exception {
29-
String resource = "resources/nodelet_test.xml";
38+
void constructorWithInputStreamValidationVariablesEntityResolver() throws Exception {
39+
3040
try (InputStream inputStream = Resources.getResourceAsStream(resource)) {
3141
XPathParser parser = new XPathParser(inputStream, false, null, null);
32-
assertEquals((Long) 1970L, parser.evalLong("/employee/birth_date/year"));
33-
assertEquals((short) 6, (short) parser.evalShort("/employee/birth_date/month"));
34-
assertEquals((Integer) 15, parser.evalInteger("/employee/birth_date/day"));
35-
assertEquals((Float) 5.8f, parser.evalFloat("/employee/height"));
36-
assertEquals((Double) 5.8d, parser.evalDouble("/employee/height"));
37-
assertEquals("${id_var}", parser.evalString("/employee/@id"));
38-
assertEquals(Boolean.TRUE, parser.evalBoolean("/employee/active"));
39-
assertEquals("<id>${id_var}</id>", parser.evalNode("/employee/@id").toString().trim());
40-
assertEquals(7, parser.evalNodes("/employee/*").size());
41-
XNode node = parser.evalNode("/employee/height");
42-
assertEquals("employee/height", node.getPath());
43-
assertEquals("employee[${id_var}]_height", node.getValueBasedIdentifier());
42+
testEvalMethod(parser);
43+
}
44+
}
45+
46+
@Test
47+
void constructorWithInputStreamValidationVariables() throws IOException {
48+
try (InputStream inputStream = Resources.getResourceAsStream(resource)) {
49+
XPathParser parser = new XPathParser(inputStream, false, null);
50+
testEvalMethod(parser);
51+
}
52+
}
53+
54+
@Test
55+
void constructorWithInputStreamValidation() throws IOException {
56+
try (InputStream inputStream = Resources.getResourceAsStream(resource)) {
57+
XPathParser parser = new XPathParser(inputStream, false);
58+
testEvalMethod(parser);
59+
}
60+
}
61+
62+
@Test
63+
void constructorWithInputStream() throws IOException {
64+
try (InputStream inputStream = Resources.getResourceAsStream(resource)) {
65+
XPathParser parser = new XPathParser(inputStream);
66+
testEvalMethod(parser);
67+
}
68+
}
69+
70+
//Reader Source
71+
@Test
72+
void constructorWithReaderValidationVariablesEntityResolver() throws Exception {
73+
74+
try (Reader reader = Resources.getResourceAsReader(resource)) {
75+
XPathParser parser = new XPathParser(reader, false, null, null);
76+
testEvalMethod(parser);
77+
}
78+
}
79+
80+
@Test
81+
void constructorWithReaderValidationVariables() throws IOException {
82+
try (Reader reader = Resources.getResourceAsReader(resource)) {
83+
XPathParser parser = new XPathParser(reader, false, null);
84+
testEvalMethod(parser);
85+
}
86+
}
87+
88+
@Test
89+
void constructorWithReaderValidation() throws IOException {
90+
try (Reader reader = Resources.getResourceAsReader(resource)) {
91+
XPathParser parser = new XPathParser(reader, false);
92+
testEvalMethod(parser);
93+
}
94+
}
95+
96+
@Test
97+
void constructorWithReader() throws IOException {
98+
try (Reader reader = Resources.getResourceAsReader(resource)) {
99+
XPathParser parser = new XPathParser(reader);
100+
testEvalMethod(parser);
44101
}
45102
}
46103

104+
//Xml String Source
105+
@Test
106+
void constructorWithStringValidationVariablesEntityResolver() throws Exception {
107+
XPathParser parser = new XPathParser(getXmlString(resource), false, null, null);
108+
testEvalMethod(parser);
109+
}
110+
111+
@Test
112+
void constructorWithStringValidationVariables() throws IOException {
113+
XPathParser parser = new XPathParser(getXmlString(resource), false, null);
114+
testEvalMethod(parser);
115+
}
116+
117+
@Test
118+
void constructorWithStringValidation() throws IOException {
119+
XPathParser parser = new XPathParser(getXmlString(resource), false);
120+
testEvalMethod(parser);
121+
}
122+
123+
@Test
124+
void constructorWithString() throws IOException {
125+
XPathParser parser = new XPathParser(getXmlString(resource));
126+
testEvalMethod(parser);
127+
}
128+
129+
//Document Source
130+
@Test
131+
void constructorWithDocumentValidationVariablesEntityResolver() {
132+
XPathParser parser = new XPathParser(getDocument(resource), false, null, null);
133+
testEvalMethod(parser);
134+
}
135+
136+
@Test
137+
void constructorWithDocumentValidationVariables() {
138+
XPathParser parser = new XPathParser(getDocument(resource), false, null);
139+
testEvalMethod(parser);
140+
}
141+
142+
@Test
143+
void constructorWithDocumentValidation() {
144+
XPathParser parser = new XPathParser(getDocument(resource), false);
145+
testEvalMethod(parser);
146+
}
147+
148+
@Test
149+
void constructorWithDocument() {
150+
XPathParser parser = new XPathParser(getDocument(resource));
151+
testEvalMethod(parser);
152+
}
153+
154+
private Document getDocument(String resource) {
155+
try {
156+
InputSource inputSource = new InputSource(Resources.getResourceAsReader(resource));
157+
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
158+
factory.setNamespaceAware(false);
159+
factory.setIgnoringComments(true);
160+
factory.setIgnoringElementContentWhitespace(false);
161+
factory.setCoalescing(false);
162+
factory.setExpandEntityReferences(true);
163+
DocumentBuilder builder = factory.newDocumentBuilder();
164+
return builder.parse(inputSource);//already closed resource in builder.parse method
165+
} catch (Exception e) {
166+
throw new BuilderException("Error creating document instance. Cause: " + e, e);
167+
}
168+
}
169+
170+
private String getXmlString(String resource) throws IOException {
171+
try (BufferedReader bufferedReader = new BufferedReader(Resources.getResourceAsReader(resource))) {
172+
StringBuilder sb = new StringBuilder();
173+
String temp;
174+
while ((temp = bufferedReader.readLine()) != null) {
175+
sb.append(temp);
176+
}
177+
return sb.toString();
178+
}
179+
}
180+
181+
private void testEvalMethod(XPathParser parser) {
182+
assertEquals((Long) 1970L, parser.evalLong("/employee/birth_date/year"));
183+
assertEquals((short) 6, (short) parser.evalShort("/employee/birth_date/month"));
184+
assertEquals((Integer) 15, parser.evalInteger("/employee/birth_date/day"));
185+
assertEquals((Float) 5.8f, parser.evalFloat("/employee/height"));
186+
assertEquals((Double) 5.8d, parser.evalDouble("/employee/height"));
187+
assertEquals("${id_var}", parser.evalString("/employee/@id"));
188+
assertEquals(Boolean.TRUE, parser.evalBoolean("/employee/active"));
189+
assertEquals("<id>${id_var}</id>", parser.evalNode("/employee/@id").toString().trim());
190+
assertEquals(7, parser.evalNodes("/employee/*").size());
191+
XNode node = parser.evalNode("/employee/height");
192+
assertEquals("employee/height", node.getPath());
193+
assertEquals("employee[${id_var}]_height", node.getValueBasedIdentifier());
194+
}
195+
47196
}

0 commit comments

Comments
 (0)