Skip to content

Commit f9fabc3

Browse files
iMouseWuharawata
authored andcommitted
Add resource path to the exception message when parsing XML mapper failed (#1172)
* print error detail when xmlConfigBuilder parse error * modify get location from context to variables
1 parent 2caa67b commit f9fabc3

File tree

3 files changed

+45
-1
lines changed

3 files changed

+45
-1
lines changed

src/main/java/org/apache/ibatis/builder/xml/XMLMapperBuilder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ private void configurationElement(XNode context) {
117117
sqlElement(context.evalNodes("/mapper/sql"));
118118
buildStatementFromContext(context.evalNodes("select|insert|update|delete"));
119119
} catch (Exception e) {
120-
throw new BuilderException("Error parsing Mapper XML. Cause: " + e, e);
120+
throw new BuilderException("Error parsing Mapper XML.The XML location is " + resource + " Cause: " + e, e);
121121
}
122122
}
123123

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
4+
Copyright 2010-2016 the original author or authors.
5+
6+
Licensed under the Apache License, Version 2.0 (the "License");
7+
you may not use this file except in compliance with the License.
8+
You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
18+
-->
19+
<!DOCTYPE mapper
20+
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
21+
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
22+
<mapper namespace="org.mybatis.spring.TestProblemMapper">
23+
24+
<select id="findProblemTest">
25+
SELECT *
26+
FROM table_test WHERE id = #{}
27+
</select>
28+
</mapper>

src/test/java/org/apache/ibatis/builder/XmlMapperBuilderTest.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,10 @@
2424
import org.apache.ibatis.mapping.ResultSetType;
2525
import org.apache.ibatis.mapping.StatementType;
2626
import org.apache.ibatis.session.Configuration;
27+
import org.junit.Rule;
2728
import org.apache.ibatis.type.TypeHandler;
2829
import org.junit.Test;
30+
import org.junit.rules.ExpectedException;
2931

3032
import static com.googlecode.catchexception.apis.BDDCatchException.*;
3133
import static org.assertj.core.api.BDDAssertions.then;
@@ -34,6 +36,9 @@
3436

3537
public class XmlMapperBuilderTest {
3638

39+
@Rule
40+
public ExpectedException expectedEx = ExpectedException.none();
41+
3742
@Test
3843
public void shouldSuccessfullyLoadXMLMapperFile() throws Exception {
3944
Configuration configuration = new Configuration();
@@ -167,6 +172,17 @@ public void useCacheRefNamespaceIsUndefined() {
167172
.hasMessage("No cache for namespace 'eee' could be found.");
168173
}
169174

175+
@Test
176+
public void shouldFailedLoadXMLMapperFile() throws Exception {
177+
expectedEx.expect(BuilderException.class);
178+
expectedEx.expectMessage("Error parsing Mapper XML.The XML location is org/apache/ibatis/builder/ProblemMapper.xml");
179+
Configuration configuration = new Configuration();
180+
String resource = "org/apache/ibatis/builder/ProblemMapper.xml";
181+
InputStream inputStream = Resources.getResourceAsStream(resource);
182+
XMLMapperBuilder builder = new XMLMapperBuilder(inputStream, configuration, resource, configuration.getSqlFragments());
183+
builder.parse();
184+
}
185+
170186
// @Test
171187
// public void shouldNotLoadTheSameNamespaceFromTwoResourcesWithDifferentNames() throws Exception {
172188
// Configuration configuration = new Configuration();

0 commit comments

Comments
 (0)