|
31 | 31 |
|
32 | 32 | public class AutomappingTest {
|
33 | 33 |
|
34 |
| - private static SqlSessionFactory sqlSessionFactory; |
| 34 | + private static SqlSessionFactory sqlSessionFactory; |
35 | 35 |
|
36 |
| - @BeforeClass |
37 |
| - public static void setUp() throws Exception { |
38 |
| - // create a SqlSessionFactory |
39 |
| - Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/automapping/mybatis-config.xml"); |
40 |
| - sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader); |
41 |
| - reader.close(); |
| 36 | + @BeforeClass |
| 37 | + public static void setUp() throws Exception { |
| 38 | + // create a SqlSessionFactory |
| 39 | + Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/automapping/mybatis-config.xml"); |
| 40 | + sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader); |
| 41 | + reader.close(); |
42 | 42 |
|
43 |
| - // populate in-memory database |
44 |
| - SqlSession session = sqlSessionFactory.openSession(); |
45 |
| - Connection conn = session.getConnection(); |
46 |
| - reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/automapping/CreateDB.sql"); |
47 |
| - ScriptRunner runner = new ScriptRunner(conn); |
48 |
| - runner.setLogWriter(null); |
49 |
| - runner.runScript(reader); |
50 |
| - reader.close(); |
51 |
| - session.close(); |
52 |
| - } |
| 43 | + // populate in-memory database |
| 44 | + SqlSession session = sqlSessionFactory.openSession(); |
| 45 | + Connection conn = session.getConnection(); |
| 46 | + reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/automapping/CreateDB.sql"); |
| 47 | + ScriptRunner runner = new ScriptRunner(conn); |
| 48 | + runner.setLogWriter(null); |
| 49 | + runner.runScript(reader); |
| 50 | + reader.close(); |
| 51 | + session.close(); |
| 52 | + } |
53 | 53 |
|
54 |
| - @Test |
55 |
| - public void shouldGetAUser() { |
56 |
| - SqlSession sqlSession = sqlSessionFactory.openSession(); |
57 |
| - try { |
58 |
| - Mapper mapper = sqlSession.getMapper(Mapper.class); |
59 |
| - User user = mapper.getUser(1); |
60 |
| - Assert.assertEquals("User1", user.getName()); |
61 |
| - } finally { |
62 |
| - sqlSession.close(); |
63 |
| - } |
64 |
| - } |
| 54 | + @Test |
| 55 | + public void shouldGetAUser() { |
| 56 | + SqlSession sqlSession = sqlSessionFactory.openSession(); |
| 57 | + try { |
| 58 | + Mapper mapper = sqlSession.getMapper(Mapper.class); |
| 59 | + User user = mapper.getUser(1); |
| 60 | + Assert.assertEquals("User1", user.getName()); |
| 61 | + } finally { |
| 62 | + sqlSession.close(); |
| 63 | + } |
| 64 | + } |
65 | 65 |
|
66 |
| - @Test |
67 |
| - public void shouldGetAUserWithPets() { |
68 |
| - SqlSession sqlSession = sqlSessionFactory.openSession(); |
69 |
| - try { |
70 |
| - Mapper mapper = sqlSession.getMapper(Mapper.class); |
71 |
| - User user = mapper.getUserWithPets(2); |
72 |
| - Assert.assertEquals(Integer.valueOf(2), user.getId()); |
73 |
| - Assert.assertEquals("User2", user.getName()); |
74 |
| - Assert.assertEquals(2, user.getPets().size()); |
75 |
| - Assert.assertEquals(Integer.valueOf(12), user.getPets().get(0).getPetId()); |
76 |
| - Assert.assertEquals("John", user.getPets().get(0).getBreeder().getBreederName()); |
77 |
| - Assert.assertEquals("Kotetsu", user.getPets().get(1).getPetName()); |
78 |
| - } finally { |
79 |
| - sqlSession.close(); |
80 |
| - } |
81 |
| - } |
| 66 | + @Test |
| 67 | + public void shouldGetAUserWithPets() { |
| 68 | + SqlSession sqlSession = sqlSessionFactory.openSession(); |
| 69 | + try { |
| 70 | + Mapper mapper = sqlSession.getMapper(Mapper.class); |
| 71 | + User user = mapper.getUserWithPets(2); |
| 72 | + Assert.assertEquals(Integer.valueOf(2), user.getId()); |
| 73 | + Assert.assertEquals("User2", user.getName()); |
| 74 | + Assert.assertEquals(2, user.getPets().size()); |
| 75 | + Assert.assertEquals(Integer.valueOf(12), user.getPets().get(0).getPetId()); |
| 76 | + Assert.assertEquals("John", user.getPets().get(0).getBreeder().getBreederName()); |
| 77 | + Assert.assertEquals("Kotetsu", user.getPets().get(1).getPetName()); |
| 78 | + } finally { |
| 79 | + sqlSession.close(); |
| 80 | + } |
| 81 | + } |
82 | 82 |
|
83 |
| - @Test |
84 |
| - public void shouldGetBooks() { |
85 |
| - //set automapping to default partial |
86 |
| - sqlSessionFactory.getConfiguration().setAutoMappingBehavior(AutoMappingBehavior.PARTIAL); |
87 |
| - SqlSession sqlSession = sqlSessionFactory.openSession(); |
88 |
| - try { |
89 |
| - Mapper mapper = sqlSession.getMapper(Mapper.class); |
90 |
| - //no errors throw |
91 |
| - List<Book> books = mapper.getBooks(); |
92 |
| - Assert.assertTrue("should return results,no errors throw", !books.isEmpty()); |
93 |
| - } finally { |
94 |
| - sqlSession.close(); |
95 |
| - } |
96 |
| - } |
| 83 | + @Test |
| 84 | + public void shouldGetBooks() { |
| 85 | + // set automapping to default partial |
| 86 | + sqlSessionFactory.getConfiguration().setAutoMappingBehavior(AutoMappingBehavior.PARTIAL); |
| 87 | + SqlSession sqlSession = sqlSessionFactory.openSession(); |
| 88 | + try { |
| 89 | + Mapper mapper = sqlSession.getMapper(Mapper.class); |
| 90 | + // no errors throw |
| 91 | + List<Book> books = mapper.getBooks(); |
| 92 | + Assert.assertTrue("should return results,no errors throw", !books.isEmpty()); |
| 93 | + } finally { |
| 94 | + sqlSession.close(); |
| 95 | + } |
| 96 | + } |
97 | 97 |
|
98 |
| - @Test |
99 |
| - public void shouldUpdateFinalField() { |
100 |
| - //set automapping to default partial |
101 |
| - sqlSessionFactory.getConfiguration().setAutoMappingBehavior(AutoMappingBehavior.PARTIAL); |
102 |
| - SqlSession sqlSession = sqlSessionFactory.openSession(); |
103 |
| - try { |
104 |
| - Mapper mapper = sqlSession.getMapper(Mapper.class); |
105 |
| - Article article = mapper.getArticle(); |
106 |
| - //Java Language Specification 17.5.3 Subsequent Modification of Final Fields |
107 |
| - //http://docs.oracle.com/javase/specs/jls/se5.0/html/memory.html#17.5.3 |
108 |
| - //The final field should be updated in mapping |
109 |
| - Assert.assertTrue("should update version in mapping", article.version > 0); |
110 |
| - } finally { |
111 |
| - sqlSession.close(); |
112 |
| - } |
113 |
| - } |
| 98 | + @Test |
| 99 | + public void shouldUpdateFinalField() { |
| 100 | + // set automapping to default partial |
| 101 | + sqlSessionFactory.getConfiguration().setAutoMappingBehavior(AutoMappingBehavior.PARTIAL); |
| 102 | + SqlSession sqlSession = sqlSessionFactory.openSession(); |
| 103 | + try { |
| 104 | + Mapper mapper = sqlSession.getMapper(Mapper.class); |
| 105 | + Article article = mapper.getArticle(); |
| 106 | + // Java Language Specification 17.5.3 Subsequent Modification of Final Fields |
| 107 | + // http://docs.oracle.com/javase/specs/jls/se5.0/html/memory.html#17.5.3 |
| 108 | + // The final field should be updated in mapping |
| 109 | + Assert.assertTrue("should update version in mapping", article.version > 0); |
| 110 | + } finally { |
| 111 | + sqlSession.close(); |
| 112 | + } |
| 113 | + } |
114 | 114 | }
|
0 commit comments