Skip to content

Commit 50c89b5

Browse files
committed
Merge pull request #671 from kazuki43zoo/add-tests-for-MapperAnnotationBuilder
Add tests for MapperAnnotationBuilder
2 parents ebd1b65 + 92391c7 commit 50c89b5

File tree

6 files changed

+160
-1
lines changed

6 files changed

+160
-1
lines changed

src/test/java/org/apache/ibatis/submitted/cache/CacheTest.java

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,4 +260,46 @@ public void shouldApplyFlushCacheOptions() {
260260
sqlSession4.close();
261261
}
262262
}
263+
264+
@Test
265+
public void shouldApplyCacheNamespaceRef() {
266+
{
267+
SqlSession sqlSession = sqlSessionFactory.openSession(true);
268+
try {
269+
PersonMapper pm = sqlSession.getMapper(PersonMapper.class);
270+
Assert.assertEquals(2, pm.findAll().size());
271+
Person p = new Person(3, "hello", "world");
272+
pm.createWithoutFlushCache(p);
273+
} finally {
274+
sqlSession.close();
275+
}
276+
}
277+
{
278+
SqlSession sqlSession = sqlSessionFactory.openSession(true);
279+
try {
280+
PersonMapper pm = sqlSession.getMapper(PersonMapper.class);
281+
Assert.assertEquals(2, pm.findAll().size());
282+
} finally {
283+
sqlSession.close();
284+
}
285+
}
286+
{
287+
SqlSession sqlSession = sqlSessionFactory.openSession(true);
288+
try {
289+
ImportantPersonMapper pm = sqlSession.getMapper(ImportantPersonMapper.class);
290+
Assert.assertEquals(3, pm.findWithFlushCache().size());
291+
} finally {
292+
sqlSession.close();
293+
}
294+
}
295+
{
296+
SqlSession sqlSession = sqlSessionFactory.openSession(true);
297+
try {
298+
PersonMapper pm = sqlSession.getMapper(PersonMapper.class);
299+
Assert.assertEquals(3, pm.findAll().size());
300+
} finally {
301+
sqlSession.close();
302+
}
303+
}
304+
}
263305
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* Copyright 2009-2016 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.cache;
17+
18+
import org.apache.ibatis.annotations.CacheNamespaceRef;
19+
import org.apache.ibatis.annotations.Options;
20+
import org.apache.ibatis.annotations.Options.FlushCachePolicy;
21+
import org.apache.ibatis.annotations.Select;
22+
23+
import java.util.List;
24+
25+
@CacheNamespaceRef(PersonMapper.class)
26+
public interface ImportantPersonMapper {
27+
28+
@Select("select id, firstname, lastname from person")
29+
@Options(flushCache = FlushCachePolicy.TRUE)
30+
List<Person> findWithFlushCache();
31+
32+
}

src/test/java/org/apache/ibatis/submitted/cache/mybatis-config.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
</environments>
4343

4444
<mappers>
45-
<mapper class="org.apache.ibatis.submitted.cache.PersonMapper"/>
45+
<mapper class="org.apache.ibatis.submitted.cache.PersonMapper"/>
46+
<mapper class="org.apache.ibatis.submitted.cache.ImportantPersonMapper"/>
4647
</mappers>
4748
</configuration>

src/test/java/org/apache/ibatis/submitted/sqlprovider/Mapper.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@
1919
import java.util.Map;
2020

2121
import org.apache.ibatis.annotations.Param;
22+
import org.apache.ibatis.annotations.DeleteProvider;
23+
import org.apache.ibatis.annotations.InsertProvider;
2224
import org.apache.ibatis.annotations.SelectProvider;
25+
import org.apache.ibatis.annotations.UpdateProvider;
2326

2427
public interface Mapper {
2528
@SelectProvider(type = OurSqlBuilder.class, method = "buildGetUsersQuery")
@@ -49,4 +52,13 @@ public interface Mapper {
4952
@SelectProvider(type = OurSqlBuilder.class, method = "buildGetUsersByNameWithParamNameQuery")
5053
List<User> getUsersByNameWithParamName(@Param("name") String name);
5154

55+
@InsertProvider(type = OurSqlBuilder.class, method = "buildInsert")
56+
void insert(User user);
57+
58+
@UpdateProvider(type= OurSqlBuilder.class, method= "buildUpdate")
59+
void update(User user);
60+
61+
@DeleteProvider(type= OurSqlBuilder.class, method= "buildDelete")
62+
void delete(Integer id);
63+
5264
}

src/test/java/org/apache/ibatis/submitted/sqlprovider/OurSqlBuilder.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,4 +136,16 @@ public String buildGetUsersByNameWithParamNameQueryUsingMap(Map<String, Object>
136136
}}.toString();
137137
}
138138

139+
public String buildInsert() {
140+
return "insert into users (id, name) values (#{id}, #{name})";
141+
}
142+
143+
public String buildUpdate() {
144+
return "update users set name = #{name} where id = #{id}";
145+
}
146+
147+
public String buildDelete() {
148+
return "delete from users where id = #{id}";
149+
}
150+
139151
}

src/test/java/org/apache/ibatis/submitted/sqlprovider/SqlProviderTest.java

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,66 @@ public void invokeError() throws NoSuchMethodException {
324324
.getBoundSql(new Object());
325325
}
326326

327+
@Test
328+
public void shouldInsertUser() {
329+
SqlSession sqlSession = sqlSessionFactory.openSession();
330+
try {
331+
Mapper mapper = sqlSession.getMapper(Mapper.class);
332+
User user = new User();
333+
user.setId(999);
334+
user.setName("MyBatis");
335+
mapper.insert(user);
336+
337+
User loadedUser = mapper.getUser(999);
338+
assertEquals("MyBatis", loadedUser.getName());
339+
340+
} finally {
341+
sqlSession.close();
342+
}
343+
}
344+
345+
@Test
346+
public void shouldUpdateUser() {
347+
SqlSession sqlSession = sqlSessionFactory.openSession();
348+
try {
349+
Mapper mapper = sqlSession.getMapper(Mapper.class);
350+
User user = new User();
351+
user.setId(999);
352+
user.setName("MyBatis");
353+
mapper.insert(user);
354+
355+
user.setName("MyBatis3");
356+
mapper.update(user);
357+
358+
User loadedUser = mapper.getUser(999);
359+
assertEquals("MyBatis3", loadedUser.getName());
360+
361+
} finally {
362+
sqlSession.close();
363+
}
364+
}
365+
366+
@Test
367+
public void shouldDeleteUser() {
368+
SqlSession sqlSession = sqlSessionFactory.openSession();
369+
try {
370+
Mapper mapper = sqlSession.getMapper(Mapper.class);
371+
User user = new User();
372+
user.setId(999);
373+
user.setName("MyBatis");
374+
mapper.insert(user);
375+
376+
user.setName("MyBatis3");
377+
mapper.delete(999);
378+
379+
User loadedUser = mapper.getUser(999);
380+
assertNull(loadedUser);
381+
382+
} finally {
383+
sqlSession.close();
384+
}
385+
}
386+
327387
public interface ErrorMapper {
328388
@SelectProvider(type = ErrorSqlBuilder.class, method = "methodNotFound")
329389
void methodNotFound();

0 commit comments

Comments
 (0)