From 765cf7ffa404970198fa2d2f3ff2d8ef89fccba3 Mon Sep 17 00:00:00 2001 From: Iwao AVE! Date: Sat, 31 Aug 2024 05:54:58 +0900 Subject: [PATCH] Added LiteralStringTest Literal strings in a mapper should be retained as-is. --- .../literal_string/LiteralStringTest.java | 74 +++++++++++++++++++ .../submitted/literal_string/Mapper.java | 35 +++++++++ .../submitted/literal_string/CreateDB.sql | 22 ++++++ .../submitted/literal_string/Mapper.xml | 29 ++++++++ .../literal_string/mybatis-config.xml | 43 +++++++++++ 5 files changed, 203 insertions(+) create mode 100644 src/test/java/org/apache/ibatis/submitted/literal_string/LiteralStringTest.java create mode 100644 src/test/java/org/apache/ibatis/submitted/literal_string/Mapper.java create mode 100644 src/test/resources/org/apache/ibatis/submitted/literal_string/CreateDB.sql create mode 100644 src/test/resources/org/apache/ibatis/submitted/literal_string/Mapper.xml create mode 100644 src/test/resources/org/apache/ibatis/submitted/literal_string/mybatis-config.xml diff --git a/src/test/java/org/apache/ibatis/submitted/literal_string/LiteralStringTest.java b/src/test/java/org/apache/ibatis/submitted/literal_string/LiteralStringTest.java new file mode 100644 index 00000000000..e986cc4053d --- /dev/null +++ b/src/test/java/org/apache/ibatis/submitted/literal_string/LiteralStringTest.java @@ -0,0 +1,74 @@ +/* + * Copyright 2009-2024 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.ibatis.submitted.literal_string; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import java.io.Reader; + +import org.apache.ibatis.BaseDataTest; +import org.apache.ibatis.io.Resources; +import org.apache.ibatis.session.SqlSession; +import org.apache.ibatis.session.SqlSessionFactory; +import org.apache.ibatis.session.SqlSessionFactoryBuilder; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +class LiteralStringTest { + private static SqlSessionFactory sqlSessionFactory; + + @BeforeAll + static void setUp() throws Exception { + // create an SqlSessionFactory + try (Reader reader = Resources + .getResourceAsReader("org/apache/ibatis/submitted/literal_string/mybatis-config.xml")) { + sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader); + } + + // populate in-memory database + BaseDataTest.runScript(sqlSessionFactory.getConfiguration().getEnvironment().getDataSource(), + "org/apache/ibatis/submitted/literal_string/CreateDB.sql"); + } + + @Test + void shouldRetainControlCharactersInMapper() { + try (SqlSession sqlSession = sqlSessionFactory.openSession()) { + Mapper mapper = sqlSession.getMapper(Mapper.class); + mapper.insert(1); + assertEquals("\ta b\r\n\tc\n", mapper.select(1)); + } + } + + @Test + void shouldRetainControlCharactersInMapperScript() { + try (SqlSession sqlSession = sqlSessionFactory.openSession()) { + Mapper mapper = sqlSession.getMapper(Mapper.class); + mapper.insertScript(2); + assertEquals("\ta b\r\n\tc\n", mapper.select(2)); + } + } + + @Test + void shouldRetainControlCharactersInMapperXml() { + try (SqlSession sqlSession = sqlSessionFactory.openSession()) { + Mapper mapper = sqlSession.getMapper(Mapper.class); + mapper.insertXml(3); + assertEquals("\ta b\r\n\tc\n", mapper.select(3)); + } + } + +} diff --git a/src/test/java/org/apache/ibatis/submitted/literal_string/Mapper.java b/src/test/java/org/apache/ibatis/submitted/literal_string/Mapper.java new file mode 100644 index 00000000000..209a5ed6e8d --- /dev/null +++ b/src/test/java/org/apache/ibatis/submitted/literal_string/Mapper.java @@ -0,0 +1,35 @@ +/* + * Copyright 2009-2024 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.ibatis.submitted.literal_string; + +import org.apache.ibatis.annotations.Insert; +import org.apache.ibatis.annotations.Select; + +public interface Mapper { + + @Select("select txt from literal_string where id = #{id}") + String select(Integer id); + + @Insert("insert into literal_string values(#{id}, '\ta b\r\n\tc\n')") + int insert(Integer id); + + @Insert("") + int insertScript(Integer id); + + int insertXml(Integer id); + +} diff --git a/src/test/resources/org/apache/ibatis/submitted/literal_string/CreateDB.sql b/src/test/resources/org/apache/ibatis/submitted/literal_string/CreateDB.sql new file mode 100644 index 00000000000..e44a233b6ce --- /dev/null +++ b/src/test/resources/org/apache/ibatis/submitted/literal_string/CreateDB.sql @@ -0,0 +1,22 @@ +-- +-- Copyright 2009-2024 the original author or authors. +-- +-- Licensed under the Apache License, Version 2.0 (the "License"); +-- you may not use this file except in compliance with the License. +-- You may obtain a copy of the License at +-- +-- https://www.apache.org/licenses/LICENSE-2.0 +-- +-- Unless required by applicable law or agreed to in writing, software +-- distributed under the License is distributed on an "AS IS" BASIS, +-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +-- See the License for the specific language governing permissions and +-- limitations under the License. +-- + +drop table literal_string if exists; + +create table literal_string ( + id int, + txt varchar(32) +); diff --git a/src/test/resources/org/apache/ibatis/submitted/literal_string/Mapper.xml b/src/test/resources/org/apache/ibatis/submitted/literal_string/Mapper.xml new file mode 100644 index 00000000000..10ff22d6a6e --- /dev/null +++ b/src/test/resources/org/apache/ibatis/submitted/literal_string/Mapper.xml @@ -0,0 +1,29 @@ + + + + + + + insert into literal_string values(#{id}, ' a b c ') + + + diff --git a/src/test/resources/org/apache/ibatis/submitted/literal_string/mybatis-config.xml b/src/test/resources/org/apache/ibatis/submitted/literal_string/mybatis-config.xml new file mode 100644 index 00000000000..8a5bb2d97b0 --- /dev/null +++ b/src/test/resources/org/apache/ibatis/submitted/literal_string/mybatis-config.xml @@ -0,0 +1,43 @@ + + + + + + + + + + + + + + + + + + + + + + + +