Skip to content

Commit 53ab8d2

Browse files
authored
Merge pull request #144 from jeffgbutler/master
Add some example tests
2 parents 67c069f + 1f1a0bb commit 53ab8d2

16 files changed

+678
-25
lines changed

src/test/java/examples/springbatch/common/Person.java renamed to src/test/java/examples/generated/always/PersonRecord.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package examples.springbatch.common;
16+
package examples.generated.always;
1717

18-
public class Person {
18+
public class PersonRecord {
1919
private Integer id;
2020
private String firstName;
2121
private String lastName;
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* Copyright 2016-2019 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 examples.generated.always.mybatis;
17+
18+
public class GeneratedKey {
19+
20+
private Integer key;
21+
22+
public Integer getKey() {
23+
return key;
24+
}
25+
26+
public void setKey(Integer key) {
27+
this.key = key;
28+
}
29+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* Copyright 2016-2019 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 examples.generated.always.mybatis;
17+
18+
import java.util.ArrayList;
19+
20+
21+
public class GeneratedKeyList extends ArrayList<GeneratedKey> {
22+
23+
public GeneratedKeyList(int estimatedCapacity) {
24+
for (int i = 0; i < estimatedCapacity; i++) {
25+
add(new GeneratedKey());
26+
}
27+
}
28+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* Copyright 2016-2019 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 examples.generated.always.mybatis;
17+
18+
import java.sql.JDBCType;
19+
20+
import org.mybatis.dynamic.sql.SqlColumn;
21+
import org.mybatis.dynamic.sql.SqlTable;
22+
23+
public final class PersonDynamicSqlSupport {
24+
public static final Person person = new Person();
25+
public static final SqlColumn<Integer> id = person.id;
26+
public static final SqlColumn<String> firstName = person.firstName;
27+
public static final SqlColumn<String> lastName = person.lastName;
28+
29+
public static final class Person extends SqlTable {
30+
public final SqlColumn<Integer> id = column("id", JDBCType.INTEGER);
31+
public final SqlColumn<String> firstName = column("first_name", JDBCType.VARCHAR);
32+
public final SqlColumn<String> lastName = column("last_name", JDBCType.VARCHAR);
33+
34+
public Person() {
35+
super("Person");
36+
}
37+
}
38+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/**
2+
* Copyright 2016-2019 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 examples.generated.always.mybatis;
17+
18+
import java.util.List;
19+
import java.util.Map;
20+
21+
import org.apache.ibatis.annotations.Insert;
22+
import org.apache.ibatis.annotations.InsertProvider;
23+
import org.apache.ibatis.annotations.Options;
24+
import org.apache.ibatis.annotations.Param;
25+
import org.apache.ibatis.annotations.SelectProvider;
26+
import org.mybatis.dynamic.sql.insert.render.InsertSelectStatementProvider;
27+
import org.mybatis.dynamic.sql.insert.render.InsertStatementProvider;
28+
import org.mybatis.dynamic.sql.insert.render.MultiRowInsertStatementProvider;
29+
import org.mybatis.dynamic.sql.select.render.SelectStatementProvider;
30+
import org.mybatis.dynamic.sql.util.SqlProviderAdapter;
31+
32+
import examples.generated.always.PersonRecord;
33+
34+
public interface PersonMapper {
35+
36+
@InsertProvider(type = SqlProviderAdapter.class, method = "insertSelect")
37+
@Options(useGeneratedKeys = true, keyProperty = "parameters.id")
38+
int insertSelect(InsertSelectStatementProvider insertSelectStatement);
39+
40+
@InsertProvider(type = SqlProviderAdapter.class, method = "insert")
41+
@Options(useGeneratedKeys = true, keyProperty = "record.id")
42+
int insert(InsertStatementProvider<PersonRecord> insertStatement);
43+
44+
@Insert({
45+
"${insertStatement}"
46+
})
47+
@Options(useGeneratedKeys = true, keyProperty = "records.id")
48+
int insertMultiple(@Param("insertStatement") String insertStatement, @Param("records") List<PersonRecord> records);
49+
50+
default int insertMultiple(MultiRowInsertStatementProvider<PersonRecord> multiRowInsertStatement) {
51+
return insertMultiple(multiRowInsertStatement.getInsertStatement(), multiRowInsertStatement.getRecords());
52+
}
53+
54+
@SelectProvider(type = SqlProviderAdapter.class, method="select")
55+
List<PersonRecord> selectMany(SelectStatementProvider selectStatement);
56+
57+
// insertSelect when there are multiple generated keys expected
58+
@Insert({
59+
"${insertStatement}"
60+
})
61+
@Options(useGeneratedKeys = true, keyProperty = "keys.key")
62+
int insertSelectMultiple(@Param("insertStatement") String insertStatement, @Param("parameters") Map<String, Object> parameters,
63+
@Param("keys") GeneratedKeyList keys);
64+
65+
default int insertSelect(InsertSelectStatementProvider insertSelectStatement, GeneratedKeyList keys) {
66+
return insertSelectMultiple(insertSelectStatement.getInsertStatement(), insertSelectStatement.getParameters(), keys);
67+
}
68+
}
Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
/**
2+
* Copyright 2016-2019 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 examples.generated.always.mybatis;
17+
18+
import static examples.generated.always.mybatis.PersonDynamicSqlSupport.*;
19+
import static org.assertj.core.api.Assertions.assertThat;
20+
21+
import java.io.InputStream;
22+
import java.io.InputStreamReader;
23+
import java.sql.Connection;
24+
import java.sql.DriverManager;
25+
import java.util.ArrayList;
26+
import java.util.List;
27+
28+
import org.apache.ibatis.datasource.unpooled.UnpooledDataSource;
29+
import org.apache.ibatis.jdbc.ScriptRunner;
30+
import org.apache.ibatis.mapping.Environment;
31+
import org.apache.ibatis.session.Configuration;
32+
import org.apache.ibatis.session.SqlSession;
33+
import org.apache.ibatis.session.SqlSessionFactory;
34+
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
35+
import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;
36+
import org.junit.jupiter.api.BeforeEach;
37+
import org.junit.jupiter.api.Test;
38+
import org.mybatis.dynamic.sql.SqlBuilder;
39+
import org.mybatis.dynamic.sql.insert.render.InsertSelectStatementProvider;
40+
import org.mybatis.dynamic.sql.insert.render.InsertStatementProvider;
41+
import org.mybatis.dynamic.sql.insert.render.MultiRowInsertStatementProvider;
42+
import org.mybatis.dynamic.sql.render.RenderingStrategies;
43+
import org.mybatis.dynamic.sql.select.render.SelectStatementProvider;
44+
45+
import examples.generated.always.PersonRecord;
46+
47+
public class PersonMapperTest {
48+
49+
private static final String JDBC_URL = "jdbc:hsqldb:mem:aname";
50+
private static final String JDBC_DRIVER = "org.hsqldb.jdbcDriver";
51+
52+
private SqlSessionFactory sqlSessionFactory;
53+
54+
@BeforeEach
55+
public void setup() throws Exception {
56+
Class.forName(JDBC_DRIVER);
57+
InputStream is = getClass().getResourceAsStream("/examples/generated/always/CreateGeneratedAlwaysDB.sql");
58+
try (Connection connection = DriverManager.getConnection(JDBC_URL, "sa", "")) {
59+
ScriptRunner sr = new ScriptRunner(connection);
60+
sr.setLogWriter(null);
61+
sr.runScript(new InputStreamReader(is));
62+
}
63+
64+
UnpooledDataSource ds = new UnpooledDataSource(JDBC_DRIVER, JDBC_URL, "sa", "");
65+
Environment environment = new Environment("test", new JdbcTransactionFactory(), ds);
66+
Configuration config = new Configuration(environment);
67+
config.addMapper(PersonMapper.class);
68+
sqlSessionFactory = new SqlSessionFactoryBuilder().build(config);
69+
}
70+
71+
@Test
72+
public void testInsertSelectWithOneRecord() {
73+
try (SqlSession session = sqlSessionFactory.openSession()) {
74+
PersonMapper mapper = session.getMapper(PersonMapper.class);
75+
76+
PersonRecord record = new PersonRecord();
77+
record.setFirstName("Fred");
78+
record.setLastName("Flintstone");
79+
80+
InsertStatementProvider<PersonRecord> insertStatement = SqlBuilder.insert(record)
81+
.into(person)
82+
.map(firstName).toProperty("firstName")
83+
.map(lastName).toProperty("lastName")
84+
.build()
85+
.render(RenderingStrategies.MYBATIS3);
86+
87+
int rows = mapper.insert(insertStatement);
88+
89+
assertThat(rows).isEqualTo(1);
90+
assertThat(record.getId()).isEqualTo(22);
91+
92+
InsertSelectStatementProvider insertSelectStatement = SqlBuilder.insertInto(person)
93+
.withColumnList(firstName, lastName)
94+
.withSelectStatement(SqlBuilder.select(firstName, lastName).from(person))
95+
.build()
96+
.render(RenderingStrategies.MYBATIS3);
97+
98+
rows = mapper.insertSelect(insertSelectStatement);
99+
assertThat(rows).isEqualTo(1);
100+
assertThat(insertSelectStatement.getParameters().get("id")).isEqualTo(23);
101+
102+
SelectStatementProvider selectStatement = SqlBuilder.select(id, firstName, lastName)
103+
.from(person)
104+
.orderBy(id)
105+
.build()
106+
.render(RenderingStrategies.MYBATIS3);
107+
108+
List<PersonRecord> records = mapper.selectMany(selectStatement);
109+
assertThat(records.size()).isEqualTo(2);
110+
assertThat(records.get(0).getId()).isEqualTo(22);
111+
assertThat(records.get(1).getId()).isEqualTo(23);
112+
}
113+
}
114+
115+
@Test
116+
public void testInsertSelectWithMultipleRecords() {
117+
try (SqlSession session = sqlSessionFactory.openSession()) {
118+
PersonMapper mapper = session.getMapper(PersonMapper.class);
119+
120+
PersonRecord record1 = new PersonRecord();
121+
record1.setFirstName("Fred");
122+
record1.setLastName("Flintstone");
123+
124+
PersonRecord record2 = new PersonRecord();
125+
record2.setFirstName("Barney");
126+
record2.setLastName("Rubble");
127+
128+
MultiRowInsertStatementProvider<PersonRecord> insertStatement = SqlBuilder.insertMultiple(record1, record2)
129+
.into(person)
130+
.map(firstName).toProperty("firstName")
131+
.map(lastName).toProperty("lastName")
132+
.build()
133+
.render(RenderingStrategies.MYBATIS3);
134+
135+
int rows = mapper.insertMultiple(insertStatement);
136+
137+
assertThat(rows).isEqualTo(2);
138+
assertThat(record1.getId()).isEqualTo(22);
139+
assertThat(record2.getId()).isEqualTo(23);
140+
141+
InsertSelectStatementProvider insertSelectStatement = SqlBuilder.insertInto(person)
142+
.withColumnList(firstName, lastName)
143+
.withSelectStatement(SqlBuilder.select(firstName, lastName).from(person))
144+
.build()
145+
.render(RenderingStrategies.MYBATIS3);
146+
147+
insertSelectStatement.getParameters().put("keys", new ArrayList<GeneratedKey>());
148+
149+
GeneratedKeyList keys = new GeneratedKeyList(5);
150+
151+
rows = mapper.insertSelect(insertSelectStatement, keys);
152+
assertThat(rows).isEqualTo(2);
153+
System.out.println(keys);
154+
assertThat(keys.get(0).getKey()).isEqualTo(24);
155+
assertThat(keys.get(1).getKey()).isEqualTo(25);
156+
157+
SelectStatementProvider selectStatement = SqlBuilder.select(id, firstName, lastName)
158+
.from(person)
159+
.orderBy(id)
160+
.build()
161+
.render(RenderingStrategies.MYBATIS3);
162+
163+
List<PersonRecord> records = mapper.selectMany(selectStatement);
164+
assertThat(records.size()).isEqualTo(4);
165+
assertThat(records.get(0).getId()).isEqualTo(22);
166+
assertThat(records.get(1).getId()).isEqualTo(23);
167+
assertThat(records.get(2).getId()).isEqualTo(24);
168+
assertThat(records.get(3).getId()).isEqualTo(25);
169+
}
170+
}
171+
}

0 commit comments

Comments
 (0)