|
| 1 | +/* |
| 2 | + * Copyright 2009-2013 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 | + * 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.raw_sql_source; |
| 17 | + |
| 18 | +import java.io.Reader; |
| 19 | +import java.sql.Connection; |
| 20 | + |
| 21 | +import org.apache.ibatis.io.Resources; |
| 22 | +import org.apache.ibatis.jdbc.ScriptRunner; |
| 23 | +import org.apache.ibatis.mapping.SqlSource; |
| 24 | +import org.apache.ibatis.scripting.defaults.RawSqlSource; |
| 25 | +import org.apache.ibatis.scripting.xmltags.DynamicSqlSource; |
| 26 | +import org.apache.ibatis.session.SqlSession; |
| 27 | +import org.apache.ibatis.session.SqlSessionFactory; |
| 28 | +import org.apache.ibatis.session.SqlSessionFactoryBuilder; |
| 29 | +import org.junit.Assert; |
| 30 | +import org.junit.BeforeClass; |
| 31 | +import org.junit.Test; |
| 32 | + |
| 33 | +public class RawSqlSourceTest { |
| 34 | + |
| 35 | + private static SqlSessionFactory sqlSessionFactory; |
| 36 | + |
| 37 | + @BeforeClass |
| 38 | + public static void setUp() throws Exception { |
| 39 | + // create an SqlSessionFactory |
| 40 | + Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/raw_sql_source/mybatis-config.xml"); |
| 41 | + sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader); |
| 42 | + reader.close(); |
| 43 | + |
| 44 | + // populate in-memory database |
| 45 | + SqlSession session = sqlSessionFactory.openSession(); |
| 46 | + Connection conn = session.getConnection(); |
| 47 | + reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/raw_sql_source/CreateDB.sql"); |
| 48 | + ScriptRunner runner = new ScriptRunner(conn); |
| 49 | + runner.setLogWriter(null); |
| 50 | + runner.runScript(reader); |
| 51 | + reader.close(); |
| 52 | + session.close(); |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + public void shouldUseRawSqlSourceForAnStaticStatement() { |
| 57 | + test("getUser1", RawSqlSource.class); |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + public void shouldUseDynamicSqlSourceForAnStatementWithInlineArguments() { |
| 62 | + test("getUser2", DynamicSqlSource.class); |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + public void shouldUseDynamicSqlSourceForAnStatementWithXmlTags() { |
| 67 | + test("getUser3", DynamicSqlSource.class); |
| 68 | + } |
| 69 | + |
| 70 | + private void test(String statement, Class<? extends SqlSource> sqlSource) { |
| 71 | + SqlSession sqlSession = sqlSessionFactory.openSession(); |
| 72 | + try { |
| 73 | + Assert.assertEquals(sqlSource, sqlSession.getConfiguration().getMappedStatement(statement).getSqlSource().getClass()); |
| 74 | + User user = sqlSession.selectOne(statement, 1); |
| 75 | + Assert.assertEquals("User1", user.getName()); |
| 76 | + } finally { |
| 77 | + sqlSession.close(); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | +} |
0 commit comments