View Javadoc
1   /**
2    *    Copyright 2009-2015 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.propertiesinmapperfiles;
17  
18  import java.io.Reader;
19  import java.sql.Connection;
20  import java.util.Properties;
21  
22  import org.apache.ibatis.io.Resources;
23  import org.apache.ibatis.jdbc.ScriptRunner;
24  import org.apache.ibatis.session.SqlSession;
25  import org.apache.ibatis.session.SqlSessionFactory;
26  import org.apache.ibatis.session.SqlSessionFactoryBuilder;
27  import org.junit.Assert;
28  import org.junit.BeforeClass;
29  import org.junit.Test;
30  
31  public class PropertiesInMappersTest {
32  
33    private static SqlSessionFactory sqlSessionFactory;
34  
35    @BeforeClass
36    public static void setUp() throws Exception {
37      
38      // this property value should be replaced on all mapper files
39      Properties p = new Properties();
40      p.put("property", "id");
41      
42      // create a SqlSessionFactory
43      Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/propertiesinmapperfiles/mybatis-config.xml");
44      sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader, p);
45      reader.close();
46  
47      // populate in-memory database
48      SqlSession session = sqlSessionFactory.openSession();
49      Connection conn = session.getConnection();
50      reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/propertiesinmapperfiles/CreateDB.sql");
51      ScriptRunner runner = new ScriptRunner(conn);
52      runner.setLogWriter(null);
53      runner.runScript(reader);
54      reader.close();
55      session.close();
56    }
57  
58    @Test
59    public void shouldGetAUser() {
60      SqlSession sqlSession = sqlSessionFactory.openSession();
61      try {
62        Mapper mapper = sqlSession.getMapper(Mapper.class);
63        User user = mapper.getUser(1);
64        Assert.assertEquals("User1", user.getName());
65      } finally {
66        sqlSession.close();
67      }
68    }
69  
70  }