|
| 1 | +// Copyright (c) Philipp Wagner and Victor Lee. 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.mapping.AbstractMapping; |
| 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 VarcharMappingTest extends TransactionalTestBase { |
| 19 | + |
| 20 | + private class StringEntity { |
| 21 | + |
| 22 | + private final String value; |
| 23 | + |
| 24 | + public StringEntity(String value) { |
| 25 | + this.value = value; |
| 26 | + } |
| 27 | + |
| 28 | + public String getValue() { |
| 29 | + return value; |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + private class StringEntityMapping extends AbstractMapping<StringEntity> { |
| 34 | + |
| 35 | + public StringEntityMapping() { |
| 36 | + super("dbo", "UnitTest"); |
| 37 | + |
| 38 | + mapVarchar("StringValue", StringEntity::getValue); |
| 39 | + } |
| 40 | + |
| 41 | + } |
| 42 | + |
| 43 | + @Override |
| 44 | + protected void onSetUpInTransaction() throws Exception { |
| 45 | + createTestTable(); |
| 46 | + } |
| 47 | + |
| 48 | + @Test |
| 49 | + public void bulkInsertPersonDataTest() throws SQLException { |
| 50 | + String stringData = "Halli Hallo Hallöchen"; |
| 51 | + // Create the entity |
| 52 | + List<StringEntity> entities = Arrays.asList(new StringEntity(stringData)); |
| 53 | + // Create the BulkInserter: |
| 54 | + StringEntityMapping mapping = new StringEntityMapping(); |
| 55 | + // Now save all entities of a given stream: |
| 56 | + new SqlServerBulkInsert<>(mapping).saveAll(connection, entities.stream()); |
| 57 | + // And assert all have been written to the database: |
| 58 | + ResultSet rs = getAll(); |
| 59 | + // We have a Value: |
| 60 | + Assert.assertEquals(true, rs.next()); |
| 61 | + // Get the string we have written: |
| 62 | + String resultString = rs.getString("StringValue"); |
| 63 | + // Assert both are equal: |
| 64 | + Assert.assertEquals(stringData, resultString); |
| 65 | + // Assert only one record was read: |
| 66 | + Assert.assertEquals(false, rs.next()); |
| 67 | + } |
| 68 | + |
| 69 | + private ResultSet getAll() throws SQLException { |
| 70 | + |
| 71 | + String sqlStatement = "SELECT * FROM dbo.UnitTest"; |
| 72 | + |
| 73 | + Statement statement = connection.createStatement(); |
| 74 | + |
| 75 | + return statement.executeQuery(sqlStatement); |
| 76 | + } |
| 77 | + |
| 78 | + private void createTestTable() throws SQLException { |
| 79 | + String sqlStatement = "CREATE TABLE [dbo].[UnitTest]\n" + |
| 80 | + " (\n" + |
| 81 | + " StringValue VARCHAR(30)\n" + |
| 82 | + " );"; |
| 83 | + |
| 84 | + Statement statement = connection.createStatement(); |
| 85 | + |
| 86 | + statement.execute(sqlStatement); |
| 87 | + } |
| 88 | + |
| 89 | +} |
0 commit comments