Skip to content

Commit 79a3649

Browse files
committed
Simpler tests
1 parent d65f4e8 commit 79a3649

File tree

7 files changed

+113
-201
lines changed

7 files changed

+113
-201
lines changed

src/test/java/org/apache/ibatis/submitted/record_type/CreateDB.sql

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
--
2-
-- Copyright 2009-2019 the original author or authors.
2+
-- Copyright 2009-2022 the original author or authors.
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.
@@ -14,10 +14,12 @@
1414
-- limitations under the License.
1515
--
1616

17-
drop table properties if exists;
17+
drop table prop if exists;
1818

19-
create table properties (
20-
item_id int,
21-
property_id int,
22-
value varchar(20)
19+
create table prop (
20+
id int,
21+
val varchar(20),
22+
url varchar(32)
2323
);
24+
25+
insert into prop (id, val, url) values (1, 'Val1', 'https://www.google.com');

src/test/java/org/apache/ibatis/submitted/record_type/IdTypeHandler.java

Lines changed: 0 additions & 47 deletions
This file was deleted.

src/test/java/org/apache/ibatis/submitted/record_type/Mapper.xml

Lines changed: 0 additions & 24 deletions
This file was deleted.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
* Copyright 2009-2022 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.record_type;
17+
18+
public record Property(int id, String value, String URL) {
19+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright 2009-2022 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.record_type;
17+
18+
import org.apache.ibatis.annotations.Arg;
19+
import org.apache.ibatis.annotations.Insert;
20+
import org.apache.ibatis.annotations.Select;
21+
22+
public interface RecordTypeMapper {
23+
24+
@Select("select id, val, url from prop where id = #{id}")
25+
Property selectPropertyAutomapping(int id);
26+
27+
@Arg(column = "id", javaType = int.class)
28+
@Arg(column = "val", javaType = String.class)
29+
@Arg(column = "url", javaType = String.class)
30+
@Select("select val, id, url from prop where id = #{id}")
31+
Property selectProperty(int id);
32+
33+
@Insert("insert into prop (id, val, url) values (#{id}, #{value}, #{URL})")
34+
int insertProperty(Property property);
35+
36+
}

src/test/java/org/apache/ibatis/submitted/record_type/RecordTypeTest.java

Lines changed: 31 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2009-2021 the original author or authors.
2+
* Copyright 2009-2022 the original author or authors.
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,96 +15,19 @@
1515
*/
1616
package org.apache.ibatis.submitted.record_type;
1717

18+
import static org.junit.jupiter.api.Assertions.*;
19+
1820
import java.io.Reader;
19-
import java.util.List;
20-
import java.util.function.Supplier;
2121

2222
import org.apache.ibatis.BaseDataTest;
23-
import org.apache.ibatis.annotations.Arg;
24-
import org.apache.ibatis.annotations.ConstructorArgs;
25-
import org.apache.ibatis.annotations.Select;
26-
import org.apache.ibatis.annotations.Update;
2723
import org.apache.ibatis.io.Resources;
2824
import org.apache.ibatis.session.SqlSession;
2925
import org.apache.ibatis.session.SqlSessionFactory;
3026
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
31-
import org.junit.jupiter.api.Assertions;
3227
import org.junit.jupiter.api.BeforeAll;
3328
import org.junit.jupiter.api.Test;
3429

35-
public class RecordTypeTest {
36-
37-
static record Property(int id, String value) {
38-
}
39-
40-
static record Item(int id, List<Property> properties) {
41-
}
42-
43-
static record ItemId(Long get) implements Supplier<Long> {
44-
}
45-
public static class ItemIdTypeHandler extends IdTypeHandler<Long, ItemId> {
46-
public ItemIdTypeHandler() {
47-
super(Long.class, ItemId::new);
48-
}
49-
}
50-
51-
static record PropId(Integer get) implements Supplier<Integer> {
52-
}
53-
public static class PropIdTypeHandler extends IdTypeHandler<Integer, PropId> {
54-
public PropIdTypeHandler() {
55-
super(Integer.class, PropId::new);
56-
}
57-
}
58-
59-
static record Property2(PropId id, String value) {
60-
}
61-
62-
static record Item2(ItemId id, List<Property2> properties) {
63-
public String idStr() {
64-
return String.valueOf(id);
65-
}
66-
}
67-
68-
static interface Mapper {
69-
70-
@Update("""
71-
<script>
72-
insert into properties (item_id, property_id, value) values
73-
<foreach collection="properties" item="prop" close=";" separator=",">
74-
(#{id},
75-
#{prop.id},
76-
#{prop.value})
77-
</foreach>
78-
</script>
79-
""")
80-
void updateProps(Item item);
81-
82-
@Update("""
83-
<script>
84-
insert into properties (item_id, property_id, value) values
85-
<foreach collection="properties" item="prop" close=";" separator=",">
86-
(#{id, jdbcType=NUMERIC, typeHandler=org.apache.ibatis.submitted.record_type.RecordTypeTest$ItemIdTypeHandler},
87-
#{prop.id, jdbcType=NUMERIC, typeHandler=org.apache.ibatis.submitted.record_type.RecordTypeTest$PropIdTypeHandler},
88-
#{prop.value})
89-
</foreach>
90-
</script>
91-
""")
92-
void updateProps2(Item2 item);
93-
94-
@Select("select property_id, value from properties order by property_id")
95-
@ConstructorArgs(value = {
96-
@Arg(column = "property_id", javaType = int.class),
97-
@Arg(column = "value", javaType = String.class),
98-
})
99-
List<Property> allProps();
100-
101-
@Select("select property_id, value from properties order by property_id")
102-
@ConstructorArgs(value = {
103-
@Arg(column="property_id", javaType=PropId.class, typeHandler=PropIdTypeHandler.class),
104-
@Arg(column="value", javaType=String.class),
105-
})
106-
List<Property2> allProps2();
107-
}
30+
class RecordTypeTest {
10831

10932
private static SqlSessionFactory sqlSessionFactory;
11033

@@ -114,41 +37,42 @@ static void setUp() throws Exception {
11437
try (Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/record_type/mybatis-config.xml")) {
11538
sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
11639
}
117-
11840
// populate in-memory database
11941
BaseDataTest.runScript(sqlSessionFactory.getConfiguration().getEnvironment().getDataSource(),
120-
"org/apache/ibatis/submitted/record_type/CreateDB.sql");
42+
"org/apache/ibatis/submitted/record_type/CreateDB.sql");
12143
}
12244

12345
@Test
124-
void test1() {
46+
void testSelectRecord() {
12547
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
126-
Mapper mapper = sqlSession.getMapper(Mapper.class);
127-
128-
mapper.updateProps(new Item(10, List.of(
129-
new Property(11, "value11"),
130-
new Property(12, "value12"))));
131-
132-
mapper.updateProps2(new Item2(new ItemId(20L), List.of(
133-
new Property2(new PropId(21), "value21"),
134-
new Property2(new PropId(22), "value22"))));
48+
RecordTypeMapper mapper = sqlSession.getMapper(RecordTypeMapper.class);
49+
Property prop = mapper.selectProperty(1);
50+
assertEquals("Val1", prop.value());
51+
}
52+
}
13553

136-
var l1 = mapper.allProps();
137-
Assertions.assertEquals(4, l1.size());
138-
Assertions.assertEquals(l1.get(0), new Property(11, "value11"));
139-
Assertions.assertEquals(l1.get(1), new Property(12, "value12"));
140-
Assertions.assertEquals(l1.get(2), new Property(21, "value21"));
141-
Assertions.assertEquals(l1.get(3), new Property(22, "value22"));
54+
@Test
55+
void testSelectRecordAutomapping() {
56+
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
57+
RecordTypeMapper mapper = sqlSession.getMapper(RecordTypeMapper.class);
58+
Property prop = mapper.selectPropertyAutomapping(1);
59+
assertEquals("Val1", prop.value());
60+
assertEquals("https://www.google.com", prop.URL());
61+
}
62+
}
14263

143-
var l2 = mapper.allProps2();
144-
Assertions.assertEquals(4, l2.size());
145-
Assertions.assertEquals(l2.get(0), new Property2(new PropId(11), "value11"));
146-
Assertions.assertEquals(l2.get(1), new Property2(new PropId(12), "value12"));
147-
Assertions.assertEquals(l2.get(2), new Property2(new PropId(21), "value21"));
148-
Assertions.assertEquals(l2.get(3), new Property2(new PropId(22), "value22"));
64+
@Test
65+
void testInsertRecord() {
66+
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
67+
RecordTypeMapper mapper = sqlSession.getMapper(RecordTypeMapper.class);
68+
assertEquals(1, mapper.insertProperty(new Property(2, "Val2", "https://mybatis.org")));
69+
sqlSession.commit();
14970
}
150-
catch (Exception e) {
151-
e.printStackTrace();
71+
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
72+
RecordTypeMapper mapper = sqlSession.getMapper(RecordTypeMapper.class);
73+
Property prop = mapper.selectProperty(2);
74+
assertEquals("Val2", prop.value());
75+
assertEquals("https://mybatis.org", prop.URL());
15276
}
15377
}
15478

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="UTF-8" ?>
22
<!--
33
4-
Copyright 2009-2019 the original author or authors.
4+
Copyright 2009-2022 the original author or authors.
55
66
Licensed under the Apache License, Version 2.0 (the "License");
77
you may not use this file except in compliance with the License.
@@ -22,21 +22,23 @@
2222

2323
<configuration>
2424

25-
<environments default="development">
26-
<environment id="development">
27-
<transactionManager type="JDBC">
28-
<property name="" value="" />
29-
</transactionManager>
30-
<dataSource type="UNPOOLED">
31-
<property name="driver" value="org.hsqldb.jdbcDriver" />
32-
<property name="url" value="jdbc:hsqldb:mem:record_type" />
33-
<property name="username" value="sa" />
34-
</dataSource>
35-
</environment>
36-
</environments>
37-
38-
<mappers>
39-
<mapper resource="org/apache/ibatis/submitted/record_type/Mapper.xml" />
40-
</mappers>
25+
<environments default="development">
26+
<environment id="development">
27+
<transactionManager type="JDBC">
28+
<property name="" value="" />
29+
</transactionManager>
30+
<dataSource type="UNPOOLED">
31+
<property name="driver" value="org.hsqldb.jdbcDriver" />
32+
<property name="url"
33+
value="jdbc:hsqldb:mem:record_type" />
34+
<property name="username" value="sa" />
35+
</dataSource>
36+
</environment>
37+
</environments>
38+
39+
<mappers>
40+
<mapper
41+
class="org.apache.ibatis.submitted.record_type.RecordTypeMapper" />
42+
</mappers>
4143

4244
</configuration>

0 commit comments

Comments
 (0)