|
| 1 | +package de.bytefish.jsqlserverbulkinsert.test.issues; |
| 2 | + |
| 3 | +import de.bytefish.jsqlserverbulkinsert.SqlServerBulkInsert; |
| 4 | +import de.bytefish.jsqlserverbulkinsert.test.base.TransactionalTestBase; |
| 5 | +import de.bytefish.jsqlserverbulkinsert.test.integration.PersonMapping; |
| 6 | +import de.bytefish.jsqlserverbulkinsert.test.model.Person; |
| 7 | +import de.bytefish.jsqlserverbulkinsert.test.utils.MeasurementUtils; |
| 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.time.LocalDate; |
| 15 | +import java.util.ArrayList; |
| 16 | +import java.util.List; |
| 17 | + |
| 18 | +/** |
| 19 | + * https://github.com/JSqlServerBulkInsert/JSqlServerBulkInsert/issues/17 |
| 20 | + */ |
| 21 | +public class Issue17Test extends TransactionalTestBase { |
| 22 | + |
| 23 | + @Override |
| 24 | + protected void onSetUpInTransaction() throws Exception { |
| 25 | + createTable(); |
| 26 | + } |
| 27 | + |
| 28 | + @Test |
| 29 | + public void bulkInsertPersonDataTest() throws SQLException { |
| 30 | + // The Number of Entities to insert: |
| 31 | + int numEntities = 1000000; |
| 32 | + // Create a large list of Persons: |
| 33 | + List<Person> persons = getPersonList(numEntities); |
| 34 | + // Create the Mapping: |
| 35 | + PersonMapping mapping = new PersonMapping(); |
| 36 | + // Create the Bulk Inserter: |
| 37 | + SqlServerBulkInsert<Person> bulkInsert = new SqlServerBulkInsert<>(mapping); |
| 38 | + // Measure the Bulk Insert time: |
| 39 | + MeasurementUtils.MeasureElapsedTime("Bulk Insert 1000000 Entities", () -> { |
| 40 | + // Now save all entities of a given stream: |
| 41 | + bulkInsert.saveAll(connection, persons.stream()); |
| 42 | + }); |
| 43 | + // And assert all have been written to the database: |
| 44 | + Assert.assertEquals(numEntities, getRowCount()); |
| 45 | + } |
| 46 | + |
| 47 | + private List<Person> getPersonList(int numPersons) { |
| 48 | + List<Person> persons = new ArrayList<>(); |
| 49 | + |
| 50 | + for (int pos = 0; pos < numPersons; pos++) { |
| 51 | + Person p = new Person(); |
| 52 | + |
| 53 | + p.setFirstName("Philipp"); |
| 54 | + p.setLastName("Wagner"); |
| 55 | + p.setBirthDate(LocalDate.of(1986, 5, 12)); |
| 56 | + |
| 57 | + persons.add(p); |
| 58 | + } |
| 59 | + |
| 60 | + return persons; |
| 61 | + } |
| 62 | + |
| 63 | + private boolean createTable() throws SQLException { |
| 64 | + |
| 65 | + String sqlStatement = "CREATE TABLE [dbo].[UnitTest]\n" + |
| 66 | + " (\n" + |
| 67 | + " FirstName NVARCHAR(255),\n" + |
| 68 | + " LastName NVARCHAR(255),\n" + |
| 69 | + " BirthDate DATE\n" + |
| 70 | + " );"; |
| 71 | + |
| 72 | + Statement statement = connection.createStatement(); |
| 73 | + |
| 74 | + return statement.execute(sqlStatement); |
| 75 | + } |
| 76 | + |
| 77 | + private int getRowCount() throws SQLException { |
| 78 | + |
| 79 | + Statement s = connection.createStatement(); |
| 80 | + |
| 81 | + ResultSet r = s.executeQuery("SELECT COUNT(*) AS total FROM [dbo].[UnitTest];"); |
| 82 | + r.next(); |
| 83 | + int count = r.getInt("total"); |
| 84 | + r.close(); |
| 85 | + |
| 86 | + return count; |
| 87 | + } |
| 88 | +} |
0 commit comments