|
| 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 com.microsoft.sqlserver.jdbc.SQLServerBulkCopyOptions; |
| 7 | +import de.bytefish.jsqlserverbulkinsert.SqlServerBulkInsert; |
| 8 | +import de.bytefish.jsqlserverbulkinsert.test.base.TransactionalTestBase; |
| 9 | +import org.junit.Assert; |
| 10 | +import org.junit.Test; |
| 11 | + |
| 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 AutoIncrementIntMappingTest extends TransactionalTestBase { |
| 19 | + |
| 20 | + private class IntegerEntity extends SampleEntity<Integer> { |
| 21 | + |
| 22 | + public IntegerEntity(Integer value) { |
| 23 | + super(value); |
| 24 | + } |
| 25 | + } |
| 26 | + |
| 27 | + private class IntegerInsert extends SqlServerBulkInsert<IntegerEntity> { |
| 28 | + |
| 29 | + public IntegerInsert() { |
| 30 | + super("dbo", "UnitTest"); |
| 31 | + |
| 32 | + mapInt("PK_ID", x -> null); |
| 33 | + mapInt("IntegerValue", IntegerEntity::getValue); |
| 34 | + } |
| 35 | + |
| 36 | + } |
| 37 | + |
| 38 | + @Override |
| 39 | + protected void onSetUpInTransaction() throws Exception { |
| 40 | + createTestTable(); |
| 41 | + } |
| 42 | + |
| 43 | + @Test |
| 44 | + public void bulkInsertPersonDataTest() throws SQLException { |
| 45 | + Integer IntegerValue = 12; |
| 46 | + // Create the Value: |
| 47 | + List<IntegerEntity> entities = Arrays.asList(new IntegerEntity(IntegerValue), new IntegerEntity(IntegerValue)); |
| 48 | + // Create the BulkInserter: |
| 49 | + IntegerInsert localDateInsert = new IntegerInsert(); |
| 50 | + // Now save all entities of a given stream: |
| 51 | + localDateInsert.saveAll(connection, entities.stream()); |
| 52 | + // And assert all have been written to the database: |
| 53 | + ResultSet rs = getAll(); |
| 54 | + // We have a Value: |
| 55 | + Assert.assertEquals(true, rs.next()); |
| 56 | + // Get the Date we have written: |
| 57 | + Integer resultIntegerValue = rs.getInt("PK_ID"); |
| 58 | + // Assert both are equal: |
| 59 | + Assert.assertEquals(new Integer(1), resultIntegerValue); |
| 60 | + |
| 61 | + // We have a second Value: |
| 62 | + Assert.assertEquals(true, rs.next()); |
| 63 | + // Get the Date we have written: |
| 64 | + Integer resultSecondIntegerValue = rs.getInt("PK_ID"); |
| 65 | + // Assert both are equal: |
| 66 | + Assert.assertEquals(new Integer(2), resultSecondIntegerValue); |
| 67 | + |
| 68 | + // Assert only one record was read: |
| 69 | + Assert.assertEquals(false, rs.next()); |
| 70 | + } |
| 71 | + |
| 72 | + private ResultSet getAll() throws SQLException { |
| 73 | + |
| 74 | + String sqlStatement = "SELECT * FROM dbo.UnitTest"; |
| 75 | + |
| 76 | + Statement statement = connection.createStatement(); |
| 77 | + |
| 78 | + return statement.executeQuery(sqlStatement); |
| 79 | + } |
| 80 | + |
| 81 | + private void createTestTable() throws SQLException { |
| 82 | + String sqlStatement = "CREATE TABLE [dbo].[UnitTest]\n" + |
| 83 | + " (\n" + |
| 84 | + " PK_ID INT IDENTITY(1,1) PRIMARY KEY,\n" + |
| 85 | + " IntegerValue INT" + |
| 86 | + " );"; |
| 87 | + |
| 88 | + Statement statement = connection.createStatement(); |
| 89 | + |
| 90 | + statement.execute(sqlStatement); |
| 91 | + } |
| 92 | + |
| 93 | +} |
0 commit comments