Skip to content

Commit ed626e5

Browse files
committed
[mybatis-spring] minor updates to SqlSessionFactoryBeanTest; refactored sample code and test cases to consolidate and clean up for new users
1 parent 2a20f8e commit ed626e5

23 files changed

+248
-469
lines changed

src/test/java/org/mybatis/spring/SqlSessionFactoryBeanTest.java

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -143,16 +143,16 @@ public void testSetConfigLocation() throws Exception {
143143
}
144144

145145
@Test
146-
public void testFragmentsAreReadWithMapperLocatios() throws Exception {
147-
setupFactoryBean();
148-
149-
factoryBean.setMapperLocations(new Resource[] {
150-
new ClassPathResource("org/mybatis/spring/TestMapper.xml")});
151-
152-
SqlSessionFactory factory = factoryBean.getObject();
153-
154-
assertEquals(2, factory.getConfiguration().getSqlFragments().size());
155-
}
146+
public void testFragmentsAreReadWithMapperLocations() throws Exception {
147+
setupFactoryBean();
148+
149+
factoryBean.setMapperLocations(new Resource[] { new ClassPathResource("org/mybatis/spring/TestMapper.xml") });
150+
151+
SqlSessionFactory factory = factoryBean.getObject();
152+
153+
// one for 'includedSql' and another for 'org.mybatis.spring.TestMapper.includedSql'
154+
assertEquals(2, factory.getConfiguration().getSqlFragments().size());
155+
}
156156

157157
@Test
158158
public void testNullMapperLocations() throws Exception {
@@ -178,19 +178,21 @@ public void testMapperLocationsWithNullEntry() throws Exception {
178178

179179
assertDefaultConfig(factoryBean.getObject());
180180
}
181-
181+
182182
@Test
183183
public void testAddATypeHandler() throws Exception {
184184
setupFactoryBean();
185-
factoryBean.setTypeHandlers(new TypeHandler[] {new DummyTypeHandler()});
185+
factoryBean.setTypeHandlers(new TypeHandler[] { new DummyTypeHandler() });
186+
186187
TypeHandlerRegistry typeHandlerRegistry = factoryBean.getObject().getConfiguration().getTypeHandlerRegistry();
187188
assertTrue(typeHandlerRegistry.hasTypeHandler(BigInteger.class));
188189
}
189190

190191
@Test
191192
public void testAddATypeAlias() throws Exception {
192193
setupFactoryBean();
193-
factoryBean.setTypeAliases(new Class[] {DummyTypeAlias.class});
194+
195+
factoryBean.setTypeAliases(new Class[] { DummyTypeAlias.class });
194196
TypeAliasRegistry typeAliasRegistry = factoryBean.getObject().getConfiguration().getTypeAliasRegistry();
195197
typeAliasRegistry.resolveAlias("testAlias");
196198
}
@@ -199,8 +201,10 @@ public void testAddATypeAlias() throws Exception {
199201
public void testSearchATypeAliasPackage() throws Exception {
200202
setupFactoryBean();
201203
factoryBean.setTypeAliasesPackage("org/mybatis/spring/type");
204+
202205
TypeAliasRegistry typeAliasRegistry = factoryBean.getObject().getConfiguration().getTypeAliasRegistry();
203206
typeAliasRegistry.resolveAlias("testAlias");
207+
typeAliasRegistry.resolveAlias("testAlias2");
204208
}
205209

206210
private void assertDefaultConfig(SqlSessionFactory factory) {
@@ -219,9 +223,10 @@ private void assertConfig(SqlSessionFactory factory, String environment,
219223
assertSame(factory.getConfiguration().getEnvironment().getTransactionFactory().getClass(),
220224
transactionFactoryClass);
221225

222-
// no mappers configured => no mapped statements
226+
// no mappers configured => no mapped statements or other parsed elements
223227
assertEquals(factory.getConfiguration().getMappedStatementNames().size(), 0);
224228
assertEquals(factory.getConfiguration().getResultMapNames().size(), 0);
225229
assertEquals(factory.getConfiguration().getParameterMapNames().size(), 0);
230+
assertEquals(factory.getConfiguration().getSqlFragments().size(), 0);
226231
}
227232
}
Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2010 The myBatis Team
2+
* Copyright 2010-2011 The myBatis Team
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -13,42 +13,45 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16+
17+
/**
18+
* @version $Id$
19+
*/
1620
package org.mybatis.spring.sample;
1721

1822
import static org.junit.Assert.assertEquals;
1923
import static org.junit.Assert.assertNotNull;
2024

2125
import org.junit.Test;
2226
import org.junit.runner.RunWith;
27+
2328
import org.mybatis.spring.sample.domain.User;
2429
import org.mybatis.spring.sample.service.FooService;
30+
2531
import org.springframework.beans.factory.annotation.Autowired;
32+
2633
import org.springframework.test.annotation.DirtiesContext;
27-
import org.springframework.test.context.ContextConfiguration;
2834
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
2935

3036
/**
31-
* Example of MyBatis-Spring basic integration usage.
32-
* This is the recommended scenario.
33-
*
34-
* @version $Id: MyBatisSampleTest.java 2697 2010-10-14 13:04:41Z eduardo.macarron $
35-
*/
36-
@ContextConfiguration(locations = {"classpath:org/mybatis/spring/sample/applicationContext-basic.xml"})
37+
* @version $Id$
38+
*/
3739
@RunWith(SpringJUnit4ClassRunner.class)
3840
@DirtiesContext
39-
public class SampleBasicTest {
41+
public abstract class AbstractSampleTest {
4042

4143
@Autowired
42-
private FooService fooService;
44+
protected FooService fooService;
4345

44-
public void setFooService(FooService fooService) {
46+
public final void setFooService(FooService fooService) {
4547
this.fooService = fooService;
4648
}
4749

4850
@Test
49-
public void testFooService(){
51+
public final void testFooService() {
5052
User user = this.fooService.doSomeBusinessStuff("u1");
5153
assertNotNull(user);
5254
assertEquals("Pocoyo", user.getName());
5355
}
56+
5457
}

src/test/java/org/mybatis/spring/sample/SampleAutoTest.java

Lines changed: 0 additions & 56 deletions
This file was deleted.
Lines changed: 7 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2010 The myBatis Team
2+
* Copyright 2010-2011 The myBatis Team
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -15,39 +15,17 @@
1515
*/
1616
package org.mybatis.spring.sample;
1717

18-
import static org.junit.Assert.assertEquals;
19-
import static org.junit.Assert.assertNotNull;
20-
21-
import org.junit.Test;
22-
import org.junit.runner.RunWith;
23-
import org.mybatis.spring.sample.domain.User;
24-
import org.mybatis.spring.sample.service.FooService;
25-
import org.springframework.beans.factory.annotation.Autowired;
26-
import org.springframework.test.annotation.DirtiesContext;
2718
import org.springframework.test.context.ContextConfiguration;
28-
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
2919

3020
/**
31-
* Example of MyBatis-Spring batch integration usage
21+
* Example of MyBatis-Spring batch integration usage.
3222
*
3323
* @version $Id: MyBatisSampleTest.java 2697 2010-10-14 13:04:41Z eduardo.macarron $
3424
*/
3525
@ContextConfiguration(locations = {"classpath:org/mybatis/spring/sample/applicationContext-batch.xml"})
36-
@RunWith(SpringJUnit4ClassRunner.class)
37-
@DirtiesContext
38-
public class SampleBatchTest {
39-
40-
private FooService fooService;
41-
42-
@Autowired
43-
public void setFooService(FooService fooService) {
44-
this.fooService = fooService;
45-
}
46-
47-
@Test
48-
public void testFooService() throws Exception {
49-
User user = this.fooService.doSomeBusinessStuff("u1");
50-
assertNotNull(user);
51-
assertEquals("Pocoyo", user.getName());
52-
}
26+
public class SampleBatchTest extends AbstractSampleTest {
27+
// Note this does not actually test batch functionality since FooService
28+
// only calls one DAO method. This class and associated Spring context
29+
// simply show that no implementation changes are needed to enable
30+
// different MyBatis configurations.
5331
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Copyright 2010-2011 The myBatis Team
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.mybatis.spring.sample;
17+
18+
import org.springframework.test.context.ContextConfiguration;
19+
20+
/**
21+
* Example of MyBatis-Spring integration with a DAO created by
22+
* MapperFactoryBean.
23+
*
24+
* @version $Id: MyBatisSampleTest.java 2697 2010-10-14 13:04:41Z eduardo.macarron $
25+
*/
26+
@ContextConfiguration(locations = { "classpath:org/mybatis/spring/sample/applicationContext-mapper.xml" })
27+
public class SampleMapperTest extends AbstractSampleTest {}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Copyright 2010-2011 The myBatis Team
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.mybatis.spring.sample;
17+
18+
import org.springframework.test.context.ContextConfiguration;
19+
20+
/**
21+
* Example of MyBatis-Spring integration with a DAO configured via
22+
* MapperScannerConfigurer.
23+
*
24+
* @version $Id: MyBatisSampleTest.java 2697 2010-10-14 13:04:41Z eduardo.macarron $
25+
*/
26+
@ContextConfiguration(locations = { "classpath:org/mybatis/spring/sample/applicationContext-scanner.xml" })
27+
public class SampleScannerTest extends AbstractSampleTest {}
Lines changed: 4 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2010 The myBatis Team
2+
* Copyright 2010-2011 The myBatis Team
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -15,41 +15,13 @@
1515
*/
1616
package org.mybatis.spring.sample;
1717

18-
import static org.junit.Assert.assertEquals;
19-
import static org.junit.Assert.assertNotNull;
20-
21-
import org.junit.Test;
22-
import org.junit.runner.RunWith;
23-
import org.mybatis.spring.sample.domain.User;
24-
import org.mybatis.spring.sample.service.FooService;
25-
import org.springframework.beans.factory.annotation.Autowired;
26-
import org.springframework.test.annotation.DirtiesContext;
2718
import org.springframework.test.context.ContextConfiguration;
28-
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
2919

3020
/**
31-
* Example of MyBatis-Spring integration usage.
32-
* This sample gets access to the SqlSession provided by Spring to
33-
* call MyBatis methods
21+
* Example of basic MyBatis-Spring integration usage with a manual DAO
22+
* implementation that subclasses SqlSessionDaoSupport.
3423
*
3524
* @version $Id: MyBatisSampleTest.java 2697 2010-10-14 13:04:41Z eduardo.macarron $
3625
*/
3726
@ContextConfiguration(locations = {"classpath:org/mybatis/spring/sample/applicationContext-sqlsession.xml"})
38-
@RunWith(SpringJUnit4ClassRunner.class)
39-
@DirtiesContext
40-
public class SampleSqlSessionTest {
41-
42-
@Autowired
43-
private FooService fooService;
44-
45-
public void setFooService(FooService fooService) {
46-
this.fooService = fooService;
47-
}
48-
49-
@Test
50-
public void testFooService(){
51-
User user = this.fooService.doSomeBusinessStuff("u1");
52-
assertNotNull(user);
53-
assertEquals("Pocoyo", user.getName());
54-
}
55-
}
27+
public class SampleSqlSessionTest extends AbstractSampleTest {}

src/test/java/org/mybatis/spring/sample/dao/UserDaoImpl.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2010 The myBatis Team
2+
* Copyright 2010-2011 The myBatis Team
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -19,16 +19,17 @@
1919
import org.mybatis.spring.support.SqlSessionDaoSupport;
2020

2121
/**
22-
* This DAO extends SqlSessionDaoSupport and uses a Spring managed SqlSession instead of MyBatis one
23-
* SqlSessions are handled by Spring so you don't need to open/close/commit/rollback them.
22+
* This DAO extends SqlSessionDaoSupport and uses a Spring managed SqlSession
23+
* instead of the MyBatis one. SqlSessions are handled by Spring so you don't
24+
* need to open/close/commit/rollback.
2425
* MyBatis exceptions are translated to Spring Data Exceptions.
2526
*
2627
* @version $Id: UserMapperTemplateImpl.java 2444 2010-09-15 07:38:37Z simone.tripodi $
2728
*/
2829
public class UserDaoImpl extends SqlSessionDaoSupport implements UserDao {
2930

3031
public User getUser(String userId) {
31-
return (User) getSqlSession().selectOne("org.mybatis.spring.sample.mapper.UserMapper.getUser", userId);
32+
return (User) getSqlSession().selectOne("org.mybatis.spring.sample.dao.UserDao.getUser", userId);
3233
}
3334

3435
}

0 commit comments

Comments
 (0)