Skip to content

Commit 36d587d

Browse files
committed
Tests for type converting function
Tests for #197
1 parent 8c72895 commit 36d587d

File tree

5 files changed

+304
-0
lines changed

5 files changed

+304
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* Copyright 2016-2020 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.type_conversion;
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 MyFilesDynamicSqlSupport {
24+
public static final MyFiles myfiles = new MyFiles();
25+
public static final SqlColumn<Integer> fileId = myfiles.fileId;
26+
public static final SqlColumn<byte[]> fileContents = myfiles.fileContents;
27+
28+
public static final class MyFiles extends SqlTable {
29+
public final SqlColumn<Integer> fileId = column("file_id", JDBCType.INTEGER);
30+
public final SqlColumn<byte[]> fileContents = column("file_contents", JDBCType.LONGVARBINARY);
31+
32+
public MyFiles() {
33+
super("MyFiles");
34+
}
35+
}
36+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* Copyright 2016-2020 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.type_conversion;
17+
18+
import java.util.Map;
19+
20+
import org.apache.ibatis.annotations.InsertProvider;
21+
import org.apache.ibatis.annotations.SelectProvider;
22+
import org.mybatis.dynamic.sql.insert.render.GeneralInsertStatementProvider;
23+
import org.mybatis.dynamic.sql.select.render.SelectStatementProvider;
24+
import org.mybatis.dynamic.sql.util.SqlProviderAdapter;
25+
26+
public interface MyFilesMapper {
27+
28+
@InsertProvider(type=SqlProviderAdapter.class, method="generalInsert")
29+
int insert(GeneralInsertStatementProvider insertStatement);
30+
31+
@SelectProvider(type=SqlProviderAdapter.class, method="select")
32+
Map<String, Object> generalSelect(SelectStatementProvider selectStatement);
33+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/**
2+
* Copyright 2016-2020 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.type_conversion;
17+
18+
import java.sql.JDBCType;
19+
import java.util.Optional;
20+
21+
import org.mybatis.dynamic.sql.BindableColumn;
22+
import org.mybatis.dynamic.sql.render.TableAliasCalculator;
23+
import org.mybatis.dynamic.sql.select.function.AbstractTypeConvertingFunction;
24+
25+
public class ToBase64 extends AbstractTypeConvertingFunction<byte[], String, ToBase64> {
26+
27+
protected ToBase64(BindableColumn<byte[]> column) {
28+
super(column);
29+
}
30+
31+
@Override
32+
public Optional<JDBCType> jdbcType() {
33+
return Optional.of(JDBCType.VARCHAR);
34+
}
35+
36+
@Override
37+
public Optional<String> typeHandler() {
38+
return Optional.empty();
39+
}
40+
41+
@Override
42+
public String renderWithTableAlias(TableAliasCalculator tableAliasCalculator) {
43+
return "TO_BASE64(" //$NON-NLS-1$
44+
+ column.renderWithTableAlias(tableAliasCalculator)
45+
+ ")"; //$NON-NLS-1$
46+
}
47+
48+
@Override
49+
protected ToBase64 copy() {
50+
return new ToBase64(column);
51+
}
52+
53+
public static ToBase64 toBase64(BindableColumn<byte[]> column) {
54+
return new ToBase64(column);
55+
}
56+
}
Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
/**
2+
* Copyright 2016-2020 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.type_conversion;
17+
18+
import static examples.type_conversion.MyFilesDynamicSqlSupport.*;
19+
import static org.assertj.core.api.Assertions.assertThat;
20+
import static org.assertj.core.api.Assertions.entry;
21+
import static org.mybatis.dynamic.sql.SqlBuilder.*;
22+
import static examples.type_conversion.ToBase64.toBase64;
23+
import java.io.InputStream;
24+
import java.io.InputStreamReader;
25+
import java.sql.Connection;
26+
import java.sql.DriverManager;
27+
import java.util.Base64;
28+
import java.util.Map;
29+
import java.util.Random;
30+
31+
import org.apache.ibatis.datasource.unpooled.UnpooledDataSource;
32+
import org.apache.ibatis.jdbc.ScriptRunner;
33+
import org.apache.ibatis.mapping.Environment;
34+
import org.apache.ibatis.session.Configuration;
35+
import org.apache.ibatis.session.SqlSession;
36+
import org.apache.ibatis.session.SqlSessionFactory;
37+
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
38+
import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;
39+
import org.junit.jupiter.api.BeforeEach;
40+
import org.junit.jupiter.api.Test;
41+
import org.mybatis.dynamic.sql.insert.render.GeneralInsertStatementProvider;
42+
import org.mybatis.dynamic.sql.render.RenderingStrategies;
43+
import org.mybatis.dynamic.sql.select.render.SelectStatementProvider;
44+
45+
class TypeConversionTest {
46+
47+
private static final String JDBC_URL = "jdbc:hsqldb:mem:aname";
48+
private static final String JDBC_DRIVER = "org.hsqldb.jdbcDriver";
49+
50+
private SqlSessionFactory sqlSessionFactory;
51+
52+
@BeforeEach
53+
void setup() throws Exception {
54+
Class.forName(JDBC_DRIVER);
55+
InputStream is = getClass().getResourceAsStream("/examples/type_conversion/CreateDB.sql");
56+
try (Connection connection = DriverManager.getConnection(JDBC_URL, "sa", "")) {
57+
ScriptRunner sr = new ScriptRunner(connection);
58+
sr.setLogWriter(null);
59+
sr.runScript(new InputStreamReader(is));
60+
}
61+
62+
UnpooledDataSource ds = new UnpooledDataSource(JDBC_DRIVER, JDBC_URL, "sa", "");
63+
Environment environment = new Environment("test", new JdbcTransactionFactory(), ds);
64+
Configuration config = new Configuration(environment);
65+
config.addMapper(MyFilesMapper.class);
66+
sqlSessionFactory = new SqlSessionFactoryBuilder().build(config);
67+
}
68+
69+
@Test
70+
void testFunctionInSelect() {
71+
try (SqlSession session = sqlSessionFactory.openSession()) {
72+
MyFilesMapper mapper = session.getMapper(MyFilesMapper.class);
73+
74+
Random random = new Random();
75+
byte[] randomBlob = new byte[1024];
76+
random.nextBytes(randomBlob);
77+
78+
GeneralInsertStatementProvider insertStatement = insertInto(myfiles)
79+
.set(fileId).toValue(1)
80+
.set(fileContents).toValue(randomBlob)
81+
.build()
82+
.render(RenderingStrategies.MYBATIS3);
83+
84+
int rows = mapper.insert(insertStatement);
85+
assertThat(rows).isEqualTo(1);
86+
87+
SelectStatementProvider selectStatement = select(fileId, fileContents)
88+
.from(myfiles)
89+
.where(fileId, isEqualTo(1))
90+
.build()
91+
.render(RenderingStrategies.MYBATIS3);
92+
93+
Map<String, Object> row = mapper.generalSelect(selectStatement);
94+
assertThat(row).containsExactly(entry("FILE_ID", 1), entry("FILE_CONTENTS", randomBlob));
95+
96+
selectStatement = select(fileId, toBase64(fileContents).as("checksum"))
97+
.from(myfiles)
98+
.where(fileId, isEqualTo(1))
99+
.build()
100+
.render(RenderingStrategies.MYBATIS3);
101+
102+
String expected = "select file_id, TO_BASE64(file_contents) as checksum from MyFiles "
103+
+ "where file_id = #{parameters.p1,jdbcType=INTEGER}";
104+
assertThat(selectStatement.getSelectStatement()).isEqualTo(expected);
105+
106+
row = mapper.generalSelect(selectStatement);
107+
108+
String base64 = Base64.getEncoder().encodeToString(randomBlob);
109+
assertThat(row).contains(entry("FILE_ID", 1), entry("CHECKSUM", base64));
110+
}
111+
}
112+
113+
@Test
114+
void testFunctionInWhere() {
115+
try (SqlSession session = sqlSessionFactory.openSession()) {
116+
MyFilesMapper mapper = session.getMapper(MyFilesMapper.class);
117+
118+
Random random = new Random();
119+
byte[] randomBlob = new byte[1024];
120+
random.nextBytes(randomBlob);
121+
122+
GeneralInsertStatementProvider insertStatement = insertInto(myfiles)
123+
.set(fileId).toValue(1)
124+
.set(fileContents).toValue(randomBlob)
125+
.build()
126+
.render(RenderingStrategies.MYBATIS3);
127+
128+
int rows = mapper.insert(insertStatement);
129+
assertThat(rows).isEqualTo(1);
130+
131+
SelectStatementProvider selectStatement = select(fileId, fileContents)
132+
.from(myfiles)
133+
.where(fileId, isEqualTo(1))
134+
.build()
135+
.render(RenderingStrategies.MYBATIS3);
136+
137+
Map<String, Object> row = mapper.generalSelect(selectStatement);
138+
assertThat(row).contains(entry("FILE_ID", 1), entry("FILE_CONTENTS", randomBlob));
139+
140+
String base64 = Base64.getEncoder().encodeToString(randomBlob);
141+
selectStatement = select(fileId, fileContents)
142+
.from(myfiles)
143+
.where(toBase64(fileContents), isEqualTo(base64))
144+
.build()
145+
.render(RenderingStrategies.MYBATIS3);
146+
147+
String expected = "select file_id, file_contents from MyFiles "
148+
+ "where TO_BASE64(file_contents) = #{parameters.p1,jdbcType=VARCHAR}";
149+
assertThat(selectStatement.getSelectStatement()).isEqualTo(expected);
150+
151+
row = mapper.generalSelect(selectStatement);
152+
153+
assertThat(row).contains(entry("FILE_ID", 1), entry("FILE_CONTENTS", randomBlob));
154+
}
155+
}
156+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
--
2+
-- Copyright 2016-2020 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+
17+
drop table MyFiles if exists;
18+
19+
create table MyFiles (
20+
file_id int not null,
21+
file_contents longvarbinary not null,
22+
primary key(file_id)
23+
);

0 commit comments

Comments
 (0)