Skip to content

Commit 0497117

Browse files
committed
Added tests covering basics with simpler objects
1 parent d79f125 commit 0497117

File tree

18 files changed

+1405
-0
lines changed

18 files changed

+1405
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
* Copyright 2009-2024 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+
* https://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.collection_in_constructor;
17+
18+
import java.util.Objects;
19+
20+
public class Clerk {
21+
22+
private Integer id;
23+
private String name;
24+
25+
public Clerk() {
26+
super();
27+
}
28+
29+
public Clerk(Integer id, String name) {
30+
super();
31+
this.id = id;
32+
this.name = name;
33+
}
34+
35+
public Integer getId() {
36+
return id;
37+
}
38+
39+
public void setId(Integer id) {
40+
this.id = id;
41+
}
42+
43+
public String getName() {
44+
return name;
45+
}
46+
47+
public void setName(String name) {
48+
this.name = name;
49+
}
50+
51+
@Override
52+
public int hashCode() {
53+
return Objects.hash(id, name);
54+
}
55+
56+
@Override
57+
public boolean equals(Object obj) {
58+
if (this == obj) {
59+
return true;
60+
}
61+
if (!(obj instanceof Clerk)) {
62+
return false;
63+
}
64+
Clerk other = (Clerk) obj;
65+
return Objects.equals(id, other.id) && Objects.equals(name, other.name);
66+
}
67+
68+
@Override
69+
public String toString() {
70+
return "Clerk [id=" + id + ", name=" + name + "]";
71+
}
72+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright 2009-2024 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+
* https://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+
17+
package org.apache.ibatis.submitted.collection_in_constructor;
18+
19+
import java.util.List;
20+
21+
import org.apache.ibatis.reflection.factory.DefaultObjectFactory;
22+
23+
public class CollectionInConstructorObjectFactory extends DefaultObjectFactory {
24+
25+
private static final long serialVersionUID = -5912469844471984785L;
26+
27+
@SuppressWarnings("unchecked")
28+
@Override
29+
public <T> T create(Class<T> type, List<Class<?>> constructorArgTypes, List<Object> constructorArgs) {
30+
if (type == Store4.class) {
31+
return (T) Store4.builder().id((Integer) constructorArgs.get(0)).isles((List<Isle>) constructorArgs.get(1))
32+
.build();
33+
}
34+
return super.create(type, constructorArgTypes, constructorArgs);
35+
}
36+
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
/*
2+
* Copyright 2009-2024 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+
* https://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.collection_in_constructor;
17+
18+
import java.io.Reader;
19+
import java.util.Arrays;
20+
import java.util.Collections;
21+
import java.util.List;
22+
23+
import org.apache.ibatis.BaseDataTest;
24+
import org.apache.ibatis.io.Resources;
25+
import org.apache.ibatis.session.SqlSession;
26+
import org.apache.ibatis.session.SqlSessionFactory;
27+
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
28+
import org.junit.jupiter.api.Assertions;
29+
import org.junit.jupiter.api.BeforeAll;
30+
import org.junit.jupiter.api.Disabled;
31+
import org.junit.jupiter.api.Test;
32+
33+
class CollectionInConstructorTest {
34+
35+
private static SqlSessionFactory sqlSessionFactory;
36+
37+
@BeforeAll
38+
static void setUp() throws Exception {
39+
// create an SqlSessionFactory
40+
try (Reader reader = Resources
41+
.getResourceAsReader("org/apache/ibatis/submitted/collection_in_constructor/mybatis-config.xml")) {
42+
sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
43+
}
44+
45+
// populate in-memory database
46+
BaseDataTest.runScript(sqlSessionFactory.getConfiguration().getEnvironment().getDataSource(),
47+
"org/apache/ibatis/submitted/collection_in_constructor/CreateDB.sql");
48+
}
49+
50+
@Test
51+
void testSimple() {
52+
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
53+
Mapper mapper = sqlSession.getMapper(Mapper.class);
54+
Store store = mapper.getAStore(1);
55+
List<Isle> isles = store.getIsles();
56+
Assertions.assertIterableEquals(
57+
Arrays.asList(new Isle(101, "Isle 101"), new Isle(102, "Isle 102"), new Isle(103, "Isle 103")), isles);
58+
}
59+
}
60+
61+
@Test
62+
void testSimpleList() {
63+
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
64+
Mapper mapper = sqlSession.getMapper(Mapper.class);
65+
List<Store> stores = mapper.getStores();
66+
Assertions.assertIterableEquals(
67+
Arrays.asList(new Isle(101, "Isle 101"), new Isle(102, "Isle 102"), new Isle(103, "Isle 103")),
68+
stores.get(0).getIsles());
69+
Assertions.assertTrue(stores.get(1).getIsles().isEmpty());
70+
Assertions.assertIterableEquals(Arrays.asList(new Isle(104, "Isle 104"), new Isle(105, "Isle 105")),
71+
stores.get(2).getIsles());
72+
}
73+
}
74+
75+
@Test
76+
void shouldEmptyListBeReturned() {
77+
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
78+
Mapper mapper = sqlSession.getMapper(Mapper.class);
79+
Assertions.assertTrue(mapper.getAStore(2).getIsles().isEmpty());
80+
}
81+
}
82+
83+
@Test
84+
void testTwoLists() {
85+
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
86+
Mapper mapper = sqlSession.getMapper(Mapper.class);
87+
Store2 store = mapper.getAStore2(1);
88+
List<Clerk> clerks = store.getClerks();
89+
List<Isle> isles = store.getIsles();
90+
Assertions.assertIterableEquals(Arrays.asList(new Clerk(1001, "Clerk 1001"), new Clerk(1002, "Clerk 1002"),
91+
new Clerk(1003, "Clerk 1003"), new Clerk(1004, "Clerk 1004"), new Clerk(1005, "Clerk 1005")), clerks);
92+
Assertions.assertIterableEquals(
93+
Arrays.asList(new Isle(101, "Isle 101"), new Isle(102, "Isle 102"), new Isle(103, "Isle 103")), isles);
94+
}
95+
}
96+
97+
@Test
98+
void testListOfStrings() {
99+
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
100+
Mapper mapper = sqlSession.getMapper(Mapper.class);
101+
Store3 store = mapper.getAStore3(1);
102+
List<String> isleNames = store.getIsleNames();
103+
Assertions.assertEquals(3, isleNames.size());
104+
Assertions.assertIterableEquals(Arrays.asList("Isle 101", "Isle 102", "Isle 103"), isleNames);
105+
}
106+
}
107+
108+
@Test
109+
void testObjectWithBuilder() {
110+
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
111+
Mapper mapper = sqlSession.getMapper(Mapper.class);
112+
Store4 store = mapper.getAStore4(1);
113+
List<Isle> isles = store.getIsles();
114+
Assertions.assertIterableEquals(
115+
Arrays.asList(new Isle(101, "Isle 101"), new Isle(102, "Isle 102"), new Isle(103, "Isle 103")), isles);
116+
}
117+
}
118+
119+
@Test
120+
void testTwoListsOfSameResultMap() {
121+
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
122+
Mapper mapper = sqlSession.getMapper(Mapper.class);
123+
Store5 store = mapper.getAStore5(1);
124+
List<Clerk> clerks = store.getClerks();
125+
List<Clerk> managers = store.getManagers();
126+
Assertions.assertIterableEquals(Arrays.asList(new Clerk(1001, "Clerk 1001"), new Clerk(1002, "Clerk 1002"),
127+
new Clerk(1003, "Clerk 1003"), new Clerk(1004, "Clerk 1004"), new Clerk(1005, "Clerk 1005")), clerks);
128+
Assertions.assertIterableEquals(Arrays.asList(new Clerk(1002, "Clerk 1002"), new Clerk(1005, "Clerk 1005")),
129+
managers);
130+
}
131+
}
132+
133+
@Disabled("Not sure if there is a need for this usage.")
134+
@Test
135+
void testPartiallyImmutableObject() {
136+
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
137+
Mapper mapper = sqlSession.getMapper(Mapper.class);
138+
Store6 store = mapper.getAStore6(1);
139+
List<Isle> isles = store.getIsles();
140+
Assertions.assertEquals("Store 1", store.getName());
141+
Assertions.assertEquals(3, isles.size());
142+
}
143+
}
144+
145+
@Test
146+
void testTwoListsOfString() {
147+
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
148+
Mapper mapper = sqlSession.getMapper(Mapper.class);
149+
Store7 store = mapper.getAStore7(1);
150+
List<String> isleNames = store.getIsleNames();
151+
List<String> clerkNames = store.getClerkNames();
152+
Assertions.assertIterableEquals(Arrays.asList("Isle 101", "Isle 102", "Isle 103"), isleNames);
153+
Assertions.assertIterableEquals(
154+
Arrays.asList("Clerk 1001", "Clerk 1002", "Clerk 1003", "Clerk 1004", "Clerk 1005"), clerkNames);
155+
}
156+
}
157+
158+
@Test
159+
void testCollectionArgWithTypeHandler() {
160+
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
161+
Mapper mapper = sqlSession.getMapper(Mapper.class);
162+
Store8 store = mapper.getAStore8(1);
163+
Assertions.assertIterableEquals(Arrays.asList("a", "b", "c"), store.getStrings());
164+
}
165+
}
166+
167+
@Test
168+
void testImmutableNestedObjects() {
169+
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
170+
Mapper mapper = sqlSession.getMapper(Mapper.class);
171+
Container container = mapper.getAContainer();
172+
Assertions
173+
.assertEquals(
174+
Arrays
175+
.asList(
176+
new Store(1, "Store 1",
177+
Arrays.asList(new Isle(101, "Isle 101"), new Isle(102, "Isle 102"),
178+
new Isle(103, "Isle 103"))),
179+
new Store(2, "Store 2", Collections.emptyList()),
180+
new Store(3, "Store 3", Arrays.asList(new Isle(104, "Isle 104"), new Isle(105, "Isle 105")))),
181+
container.getStores());
182+
}
183+
}
184+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Copyright 2009-2024 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+
* https://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+
17+
package org.apache.ibatis.submitted.collection_in_constructor;
18+
19+
import java.util.List;
20+
21+
public class Container {
22+
private Integer num;
23+
24+
private List<Store> stores;
25+
26+
public Integer getNum() {
27+
return num;
28+
}
29+
30+
public void setNum(Integer num) {
31+
this.num = num;
32+
}
33+
34+
public List<Store> getStores() {
35+
return stores;
36+
}
37+
38+
public void setStores(List<Store> stores) {
39+
this.stores = stores;
40+
}
41+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* Copyright 2009-2024 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+
* https://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+
17+
package org.apache.ibatis.submitted.collection_in_constructor;
18+
19+
import java.sql.CallableStatement;
20+
import java.sql.PreparedStatement;
21+
import java.sql.ResultSet;
22+
import java.sql.SQLException;
23+
import java.util.ArrayList;
24+
import java.util.List;
25+
26+
import org.apache.ibatis.type.BaseTypeHandler;
27+
import org.apache.ibatis.type.JdbcType;
28+
import org.assertj.core.util.Arrays;
29+
30+
public class CsvToListTypeHandler extends BaseTypeHandler<List<?>> {
31+
32+
@Override
33+
public void setNonNullParameter(PreparedStatement ps, int i, List<?> parameter, JdbcType jdbcType)
34+
throws SQLException {
35+
// not relevant for this test
36+
}
37+
38+
@Override
39+
public List<?> getNullableResult(ResultSet rs, String columnName) throws SQLException {
40+
return stringToList(rs.getString(columnName));
41+
}
42+
43+
@Override
44+
public List<?> getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
45+
return stringToList(rs.getString(columnIndex));
46+
}
47+
48+
@Override
49+
public List<?> getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
50+
return stringToList(cs.getString(columnIndex));
51+
}
52+
53+
private List<?> stringToList(String s) {
54+
if (s == null) {
55+
return new ArrayList<>();
56+
}
57+
return Arrays.asList(s.split(","));
58+
}
59+
}

0 commit comments

Comments
 (0)