Skip to content

Commit 623eadd

Browse files
author
Philipp Wagner
committed
Added Tests for Binary Mappings
1 parent 4d940b4 commit 623eadd

5 files changed

Lines changed: 186 additions & 1 deletion

File tree

JSqlServerBulkInsert/src/main/java/de/bytefish/jsqlserverbulkinsert/SqlServerBulkInsert.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,10 @@ protected void mapBigInt(String columnName, Func2<TEntity, BigInteger> propertyG
117117
addColumn(columnName, Types.BIGINT, wrapper);
118118
}
119119

120+
protected void mapBigIntLong(String columnName, Func2<TEntity, Long> propertyGetter) {
121+
addColumn(columnName, Types.BIGINT, propertyGetter);
122+
}
123+
120124
protected void mapDate(String columnName, Func2<TEntity, LocalDate> propertyGetter) {
121125
addColumn(columnName, Types.DATE, propertyGetter);
122126
}
@@ -153,6 +157,10 @@ protected void mapString(String columnName, Func2<TEntity, String> propertyGette
153157
addColumn(columnName, Types.NVARCHAR, propertyGetter);
154158
}
155159

160+
protected void mapVarBinary(String columnName, int maxLength, Func2<TEntity, byte[]> propertyGetter) {
161+
addColumn(columnName, Types.VARBINARY, maxLength, 0, false, propertyGetter);
162+
}
163+
156164
private void addColumn(String name, int type, boolean isAutoIncrement, Func2<TEntity, Object> propertyGetter)
157165
{
158166
// Create the current Column Meta Data:
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// Copyright (c) Philipp Wagner. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
package de.bytefish.jsqlserverbulkinsert.util;
5+
6+
public class BinaryUtils {
7+
8+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
// Copyright (c) Philipp Wagner. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
package de.bytefish.jsqlserverbulkinsert.test.mapping;
5+
6+
import de.bytefish.jsqlserverbulkinsert.SqlServerBulkInsert;
7+
import de.bytefish.jsqlserverbulkinsert.test.base.TransactionalTestBase;
8+
import org.junit.Assert;
9+
import org.junit.Test;
10+
11+
import java.math.BigInteger;
12+
import java.sql.ResultSet;
13+
import java.sql.SQLException;
14+
import java.sql.Statement;
15+
import java.util.Arrays;
16+
import java.util.List;
17+
18+
public class BigIntegerLongMappingTest extends TransactionalTestBase {
19+
20+
private class BigIntegerEntity extends SampleEntity<Long> {
21+
22+
public BigIntegerEntity(Long value) {
23+
super(value);
24+
}
25+
}
26+
27+
private class BigIntegerInsert extends SqlServerBulkInsert<BigIntegerEntity> {
28+
29+
public BigIntegerInsert() {
30+
super("dbo", "UnitTest");
31+
32+
mapBigIntLong("BigIntegerValue", BigIntegerEntity::getValue);
33+
}
34+
35+
}
36+
37+
@Override
38+
protected void onSetUpInTransaction() throws Exception {
39+
createTestTable();
40+
}
41+
42+
@Test
43+
public void bulkInsertPersonDataTest() throws SQLException {
44+
long longValue = 47878778228484l;
45+
// Create the Value:
46+
List<BigIntegerEntity> entities = Arrays.asList(new BigIntegerEntity(longValue));
47+
// Create the BulkInserter:
48+
BigIntegerInsert localDateInsert = new BigIntegerInsert();
49+
// Now save all entities of a given stream:
50+
localDateInsert.saveAll(connection, entities.stream());
51+
// And assert all have been written to the database:
52+
ResultSet rs = getAll();
53+
// We have a Value:
54+
Assert.assertEquals(true, rs.next());
55+
// Get the Date we have written:
56+
long resultBigIntegerValue = rs.getLong("BigIntegerValue");
57+
// Assert both are equal:
58+
Assert.assertEquals(longValue, resultBigIntegerValue);
59+
// Assert only one record was read:
60+
Assert.assertEquals(false, rs.next());
61+
}
62+
63+
private ResultSet getAll() throws SQLException {
64+
65+
String sqlStatement = "SELECT * FROM dbo.UnitTest";
66+
67+
Statement statement = connection.createStatement();
68+
69+
return statement.executeQuery(sqlStatement);
70+
}
71+
72+
private void createTestTable() throws SQLException {
73+
String sqlStatement = "CREATE TABLE [dbo].[UnitTest]\n" +
74+
" (\n" +
75+
" BigIntegerValue bigint\n" +
76+
" );";
77+
78+
Statement statement = connection.createStatement();
79+
80+
statement.execute(sqlStatement);
81+
}
82+
83+
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
// Copyright (c) Philipp Wagner. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
package de.bytefish.jsqlserverbulkinsert.test.mapping;
5+
6+
import de.bytefish.jsqlserverbulkinsert.SqlServerBulkInsert;
7+
import de.bytefish.jsqlserverbulkinsert.test.base.TransactionalTestBase;
8+
import org.junit.Assert;
9+
import org.junit.Test;
10+
11+
import java.sql.ResultSet;
12+
import java.sql.SQLException;
13+
import java.sql.Statement;
14+
import java.util.Arrays;
15+
import java.util.List;
16+
17+
public class BinaryMappingTest extends TransactionalTestBase {
18+
19+
private class BinaryDataEntity extends SampleEntity<byte[]> {
20+
21+
public BinaryDataEntity(byte[] value) {
22+
super(value);
23+
}
24+
}
25+
26+
private class ByteInsert extends SqlServerBulkInsert<BinaryDataEntity> {
27+
28+
public ByteInsert() {
29+
super("dbo", "UnitTest");
30+
31+
mapVarBinary("ByteValue", 255, BinaryDataEntity::getValue);
32+
}
33+
34+
}
35+
36+
@Override
37+
protected void onSetUpInTransaction() throws Exception {
38+
createTestTable();
39+
}
40+
41+
@Test
42+
public void bulkInsertPersonDataTest() throws SQLException {
43+
byte[] binaryDataValue = new byte[] { 1, 2, 3};
44+
// Create the Value:
45+
List<BinaryDataEntity> entities = Arrays.asList(new BinaryDataEntity(binaryDataValue));
46+
// Create the BulkInserter:
47+
ByteInsert localDateInsert = new ByteInsert();
48+
// Now save all entities of a given stream:
49+
localDateInsert.saveAll(connection, entities.stream());
50+
// And assert all have been written to the database:
51+
ResultSet rs = getAll();
52+
// We have a Value:
53+
Assert.assertEquals(true, rs.next());
54+
// Get the Date we have written:
55+
byte[] resultByteValue = rs.getBytes("ByteValue");
56+
// Assert both are equal:
57+
Assert.assertEquals(binaryDataValue.length, resultByteValue.length);
58+
// Check Content:
59+
for(int i = 0; i < resultByteValue.length; i++) {
60+
Assert.assertEquals((byte) binaryDataValue[i], (byte) resultByteValue[i]);
61+
}
62+
// Assert only one record was read:
63+
Assert.assertEquals(false, rs.next());
64+
}
65+
66+
private ResultSet getAll() throws SQLException {
67+
68+
String sqlStatement = "SELECT * FROM dbo.UnitTest";
69+
70+
Statement statement = connection.createStatement();
71+
72+
return statement.executeQuery(sqlStatement);
73+
}
74+
75+
private void createTestTable() throws SQLException {
76+
String sqlStatement = "CREATE TABLE [dbo].[UnitTest]\n" +
77+
" (\n" +
78+
" ByteValue VARBINARY(255)\n" +
79+
" );";
80+
81+
Statement statement = connection.createStatement();
82+
83+
statement.execute(sqlStatement);
84+
}
85+
86+
}

JSqlServerBulkInsert/src/test/java/de/bytefish/jsqlserverbulkinsert/test/mapping/IntMappingTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ protected void onSetUpInTransaction() throws Exception {
4040

4141
@Test
4242
public void bulkInsertPersonDataTest() throws SQLException {
43-
Integer IntegerValue = 3412358;
43+
Integer IntegerValue = 214483647;
4444
// Create the Value:
4545
List<IntegerEntity> entities = Arrays.asList(new IntegerEntity(IntegerValue));
4646
// Create the BulkInserter:

0 commit comments

Comments
 (0)