Skip to content

Commit 83f82c0

Browse files
committed
Fix for http://code.google.com/p/mybatis/issues/detail?id=254 . Checks that the namespace of a xml file that is loaded as a result of loading a mapper interface matches the name of the interface.
Excuded cglig-nodep from a test dependency because it collides with cglib (dep).
1 parent be36395 commit 83f82c0

File tree

12 files changed

+89
-18
lines changed

12 files changed

+89
-18
lines changed

pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,12 @@
303303
<artifactId>jmock-legacy</artifactId>
304304
<version>2.1.0</version>
305305
<scope>test</scope>
306+
<exclusions>
307+
<exclusion>
308+
<groupId>cglib</groupId>
309+
<artifactId>cglib-nodep</artifactId>
310+
</exclusion>
311+
</exclusions>
306312
</dependency>
307313
<dependency>
308314
<groupId>commons-dbcp</groupId>

src/main/java/org/apache/ibatis/builder/MapperBuilderAssistant.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,20 +31,26 @@ public String getCurrentNamespace() {
3131
}
3232

3333
public void setCurrentNamespace(String currentNamespace) {
34-
if (currentNamespace != null) {
35-
this.currentNamespace = currentNamespace;
34+
if (currentNamespace == null) {
35+
throw new BuilderException(
36+
"The mapper element requires a namespace attribute to be specified.");
3637
}
37-
if (this.currentNamespace == null) {
38-
throw new BuilderException("The mapper element requires a namespace attribute to be specified.");
38+
39+
if (this.currentNamespace != null && !this.currentNamespace.equals(currentNamespace)) {
40+
throw new BuilderException("Wrong namespace. Expected '"
41+
+ this.currentNamespace + "' but found '" + currentNamespace + "'");
3942
}
43+
44+
this.currentNamespace = currentNamespace;
4045
}
4146

4247
public String applyCurrentNamespace(String base) {
4348
if (base == null) return null;
49+
// is it qualified with this or other namespace yet?
4450
if (base.contains(".")) return base;
4551
return currentNamespace + "." + base;
4652
}
47-
53+
4854
public Cache useCacheRef(String namespace) {
4955
if (namespace == null) {
5056
throw new BuilderException("cache-ref element requires a namespace attribute.");

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

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,8 @@ private XMLMapperBuilder(XPathParser parser, Configuration configuration, String
6969

7070
public void parse() {
7171
XNode context = parser.evalNode("/mapper");
72-
// we may inherit a namespace from MapperAnnotationBuilder
73-
String namespace = builderAssistant.getCurrentNamespace();
74-
if (namespace == null) {
75-
namespace = context.getStringAttribute("namespace");
76-
builderAssistant.setCurrentNamespace(namespace);
77-
}
72+
String namespace = context.getStringAttribute("namespace");
73+
builderAssistant.setCurrentNamespace(namespace);
7874
if (!configuration.isResourceLoaded(namespace)) {
7975
configurationElement(context);
8076
configuration.addLoadedResource(namespace);

src/main/java/org/apache/ibatis/session/Configuration.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ public class Configuration {
8282
protected final Map<String, ParameterMap> parameterMaps = new StrictMap<String, ParameterMap>("Parameter Maps collection");
8383
protected final Map<String, KeyGenerator> keyGenerators = new StrictMap<String, KeyGenerator>("Key Generators collection");
8484

85+
// list of namespaces loaded from xml files
8586
protected final Set<String> loadedResources = new HashSet<String>();
8687
protected final Map<String, XNode> sqlFragments = new StrictMap<String, XNode>("XML fragments parsed from previous mappers");
8788

@@ -113,12 +114,12 @@ public Configuration() {
113114
typeAliasRegistry.registerAlias("WEAK", WeakCache.class.getName());
114115
}
115116

116-
public void addLoadedResource(String resource) {
117-
loadedResources.add(resource);
117+
public void addLoadedResource(String namespace) {
118+
loadedResources.add(namespace);
118119
}
119120

120-
public boolean isResourceLoaded(String resource) {
121-
return loadedResources.contains(resource);
121+
public boolean isResourceLoaded(String namespace) {
122+
return loadedResources.contains(namespace);
122123
}
123124

124125
public Environment getEnvironment() {

src/test/java/org/apache/ibatis/binding/BoundAuthorMapper.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
55
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
66

7-
<mapper>
7+
<mapper namespace="org.apache.ibatis.binding.BoundAuthorMapper">
88

99
<insert id="insertAuthor" parameterType="domain.blog.Author">
1010
<selectKey keyProperty="id" resultType="int" order="BEFORE">

src/test/java/org/apache/ibatis/binding/BoundBlogMapper.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
55
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
66

7-
<mapper>
7+
<mapper namespace="org.apache.ibatis.binding.BoundBlogMapper">
88

99
<select id="selectRandom" resultType="int">
1010
select CAST(RANDOM()*1000000 as INTEGER) a from SYSIBM.SYSDUMMY1
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package org.apache.ibatis.binding;
2+
3+
public interface MissingNamespaceMapper {
4+
5+
void get();
6+
7+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
3+
<!DOCTYPE mapper
4+
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
5+
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
6+
7+
<mapper>
8+
9+
<select id="get" resultType="string">
10+
select 1
11+
</select>
12+
13+
</mapper>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package org.apache.ibatis.binding;
2+
3+
public interface WrongNamespaceMapper {
4+
5+
void get();
6+
7+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
3+
<!DOCTYPE mapper
4+
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
5+
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
6+
7+
<mapper namespace="wrong.namespace">
8+
9+
<select id="get" resultType="string">
10+
select 1
11+
</select>
12+
13+
</mapper>

0 commit comments

Comments
 (0)