Skip to content

Commit d0a120e

Browse files
Fixes #53. XMLMapperBuilder validates that the namespace is not empty.
1 parent 52af14c commit d0a120e

File tree

5 files changed

+206
-0
lines changed

5 files changed

+206
-0
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,9 @@ public XNode getSqlFragment(String refid) {
103103
private void configurationElement(XNode context) {
104104
try {
105105
String namespace = context.getStringAttribute("namespace");
106+
if (namespace.equals("")) {
107+
throw new BuilderException("Mapper's namespace cannot be empty");
108+
}
106109
builderAssistant.setCurrentNamespace(namespace);
107110
cacheRefElement(context.evalNode("cache-ref"));
108111
cacheElement(context.evalNode("cache"));
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright 2009-2012 The MyBatis Team
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.apache.ibatis.submitted.empty_namespace;
17+
18+
import java.io.Reader;
19+
20+
import org.apache.ibatis.exceptions.PersistenceException;
21+
import org.apache.ibatis.io.Resources;
22+
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
23+
import org.junit.Test;
24+
25+
public class EmptyNamespaceTest {
26+
@Test(expected = PersistenceException.class)
27+
public void testEmptyNamespace() throws Exception {
28+
Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/empty_namespace/ibatisConfig.xml");
29+
try {
30+
new SqlSessionFactoryBuilder().build(reader);
31+
} finally {
32+
reader.close();
33+
}
34+
}
35+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright 2009-2012 The MyBatis Team
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.apache.ibatis.submitted.empty_namespace;
17+
18+
public class Person {
19+
public enum Type {
20+
EMPLOYEE,
21+
DIRECTOR
22+
}
23+
24+
private Long id;
25+
private String firstName;
26+
private String lastName;
27+
public String getFirstName() {
28+
return firstName;
29+
}
30+
public void setFirstName(String firstName) {
31+
this.firstName = firstName;
32+
}
33+
public String getLastName() {
34+
return lastName;
35+
}
36+
public void setLastName(String lastName) {
37+
this.lastName = lastName;
38+
}
39+
public Long getId() {
40+
return id;
41+
}
42+
public void setId(Long id) {
43+
this.id = id;
44+
}
45+
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
2+
<!--
3+
4+
Copyright 2009-2012 The MyBatis Team
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+
23+
<mapper namespace="">
24+
25+
<resultMap id="personMap" type="Person">
26+
<id property="id" column="id"/>
27+
<result property="firstName" column="firstName"/>
28+
<result property="lastName" column="lastName"/>
29+
</resultMap>
30+
31+
32+
<select id="selectAllByType" resultMap="personMap" parameterType="org.apache.ibatis.submitted.ognl_enum.Person$Type">
33+
SELECT id, firstName, lastName, personType
34+
FROM person
35+
<where>
36+
<if test="_parameter != null and _parameter.toString() == 'EMPLOYEE'">
37+
AND personType = #{type}
38+
</if>
39+
<if test="_parameter != null and _parameter.toString() == 'DIRECTOR'">
40+
AND personType = #{type}
41+
</if>
42+
</where>
43+
</select>
44+
<select id="selectAllByTypeNameAttribute" resultMap="personMap" parameterType="org.apache.ibatis.submitted.ognl_enum.Person$Type">
45+
SELECT id, firstName, lastName, personType
46+
FROM person
47+
<where>
48+
<if test="name == 'EMPLOYEE'">
49+
AND personType = #{type}
50+
</if>
51+
<if test="name == 'DIRECTOR'">
52+
AND personType = #{type}
53+
</if>
54+
</where>
55+
</select>
56+
57+
<select id="selectAllByTypeWithInterface" resultMap="personMap" parameterType="org.apache.ibatis.submitted.ognl_enum.PersonMapper$PersonType">
58+
SELECT id, firstName, lastName, personType
59+
FROM person
60+
<where>
61+
<if test="type.toString() == 'EMPLOYEE'">
62+
AND personType = #{type}
63+
</if>
64+
<if test="type.toString() == 'DIRECTOR'">
65+
AND personType = #{type}
66+
</if>
67+
</where>
68+
</select>
69+
<select id="selectAllByTypeNameAttributeWithInterface" resultMap="personMap" parameterType="org.apache.ibatis.submitted.ognl_enum.PersonMapper$PersonType">
70+
SELECT id, firstName, lastName, personType
71+
FROM person
72+
<where>
73+
<if test="type.toString() == 'EMPLOYEE'">
74+
AND personType = #{type}
75+
</if>
76+
<if test="type.toString() == 'DIRECTOR'">
77+
AND personType = #{type}
78+
</if>
79+
</where>
80+
</select>
81+
</mapper>
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
2+
<!--
3+
4+
Copyright 2009-2012 The MyBatis Team
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 configuration
20+
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
21+
"http://mybatis.org/dtd/mybatis-3-config.dtd">
22+
23+
<configuration>
24+
<typeAliases>
25+
<typeAlias alias="Person" type="org.apache.ibatis.submitted.empty_namespace.Person"/>
26+
</typeAliases>
27+
28+
<environments default="test">
29+
<environment id="test">
30+
<transactionManager type="JDBC"></transactionManager>
31+
<dataSource type="UNPOOLED">
32+
<property name="driver" value="org.hsqldb.jdbcDriver"/>
33+
<property name="url" value="jdbc:hsqldb:mem:empty_namespace"/>
34+
<property name="username" value="sa"/>
35+
</dataSource>
36+
</environment>
37+
</environments>
38+
39+
<mappers>
40+
<mapper resource="org/apache/ibatis/submitted/empty_namespace/Person.xml"/>
41+
</mappers>
42+
</configuration>

0 commit comments

Comments
 (0)