Skip to content

Commit 765cf7f

Browse files
committed
Added LiteralStringTest
Literal strings in a mapper should be retained as-is.
1 parent 109f2ef commit 765cf7f

File tree

5 files changed

+203
-0
lines changed

5 files changed

+203
-0
lines changed
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
* Copyright 2009-2024 the original author or authors.
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+
* https://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+
17+
package org.apache.ibatis.submitted.literal_string;
18+
19+
import static org.junit.jupiter.api.Assertions.assertEquals;
20+
21+
import java.io.Reader;
22+
23+
import org.apache.ibatis.BaseDataTest;
24+
import org.apache.ibatis.io.Resources;
25+
import org.apache.ibatis.session.SqlSession;
26+
import org.apache.ibatis.session.SqlSessionFactory;
27+
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
28+
import org.junit.jupiter.api.BeforeAll;
29+
import org.junit.jupiter.api.Test;
30+
31+
class LiteralStringTest {
32+
private static SqlSessionFactory sqlSessionFactory;
33+
34+
@BeforeAll
35+
static void setUp() throws Exception {
36+
// create an SqlSessionFactory
37+
try (Reader reader = Resources
38+
.getResourceAsReader("org/apache/ibatis/submitted/literal_string/mybatis-config.xml")) {
39+
sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
40+
}
41+
42+
// populate in-memory database
43+
BaseDataTest.runScript(sqlSessionFactory.getConfiguration().getEnvironment().getDataSource(),
44+
"org/apache/ibatis/submitted/literal_string/CreateDB.sql");
45+
}
46+
47+
@Test
48+
void shouldRetainControlCharactersInMapper() {
49+
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
50+
Mapper mapper = sqlSession.getMapper(Mapper.class);
51+
mapper.insert(1);
52+
assertEquals("\ta b\r\n\tc\n", mapper.select(1));
53+
}
54+
}
55+
56+
@Test
57+
void shouldRetainControlCharactersInMapperScript() {
58+
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
59+
Mapper mapper = sqlSession.getMapper(Mapper.class);
60+
mapper.insertScript(2);
61+
assertEquals("\ta b\r\n\tc\n", mapper.select(2));
62+
}
63+
}
64+
65+
@Test
66+
void shouldRetainControlCharactersInMapperXml() {
67+
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
68+
Mapper mapper = sqlSession.getMapper(Mapper.class);
69+
mapper.insertXml(3);
70+
assertEquals("\ta b\r\n\tc\n", mapper.select(3));
71+
}
72+
}
73+
74+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright 2009-2024 the original author or authors.
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+
* https://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+
17+
package org.apache.ibatis.submitted.literal_string;
18+
19+
import org.apache.ibatis.annotations.Insert;
20+
import org.apache.ibatis.annotations.Select;
21+
22+
public interface Mapper {
23+
24+
@Select("select txt from literal_string where id = #{id}")
25+
String select(Integer id);
26+
27+
@Insert("insert into literal_string values(#{id}, '\ta b\r\n\tc\n')")
28+
int insert(Integer id);
29+
30+
@Insert("<script>insert into literal_string values(#{id}, '&#009;a&#032;&#032;b&#013;&#010;&#009;c&#010;')</script>")
31+
int insertScript(Integer id);
32+
33+
int insertXml(Integer id);
34+
35+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
--
2+
-- Copyright 2009-2024 the original author or authors.
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+
-- https://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+
17+
drop table literal_string if exists;
18+
19+
create table literal_string (
20+
id int,
21+
txt varchar(32)
22+
);
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
4+
Copyright 2009-2024 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+
https://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+
"https://mybatis.org/dtd/mybatis-3-mapper.dtd">
22+
<mapper
23+
namespace="org.apache.ibatis.submitted.literal_string.Mapper">
24+
25+
<insert id="insertXml">
26+
insert into literal_string values(#{id}, '&#009;a&#032;&#032;b&#013;&#010;&#009;c&#010;')
27+
</insert>
28+
29+
</mapper>
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
<!--
3+
4+
Copyright 2009-2024 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+
https://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+
"https://mybatis.org/dtd/mybatis-3-config.dtd">
22+
23+
<configuration>
24+
25+
<environments default="development">
26+
<environment id="development">
27+
<transactionManager type="JDBC">
28+
<property name="" value="" />
29+
</transactionManager>
30+
<dataSource type="UNPOOLED">
31+
<property name="driver" value="org.hsqldb.jdbcDriver" />
32+
<property name="url" value="jdbc:hsqldb:mem:literalstr" />
33+
<property name="username" value="sa" />
34+
</dataSource>
35+
</environment>
36+
</environments>
37+
38+
<mappers>
39+
<mapper
40+
class="org.apache.ibatis.submitted.literal_string.Mapper" />
41+
</mappers>
42+
43+
</configuration>

0 commit comments

Comments
 (0)