|
| 1 | +packageg3401_3500.s3465_find_products_with_valid_serial_numbers; |
| 2 | + |
| 3 | +importstaticorg.hamcrest.CoreMatchers.equalTo; |
| 4 | +importstaticorg.hamcrest.MatcherAssert.assertThat; |
| 5 | + |
| 6 | +importjava.io.BufferedReader; |
| 7 | +importjava.io.FileNotFoundException; |
| 8 | +importjava.io.FileReader; |
| 9 | +importjava.sql.Connection; |
| 10 | +importjava.sql.ResultSet; |
| 11 | +importjava.sql.SQLException; |
| 12 | +importjava.sql.Statement; |
| 13 | +importjava.util.stream.Collectors; |
| 14 | +importjavax.sql.DataSource; |
| 15 | +importorg.junit.jupiter.api.Test; |
| 16 | +importorg.zapodot.junit.db.annotations.EmbeddedDatabase; |
| 17 | +importorg.zapodot.junit.db.annotations.EmbeddedDatabaseTest; |
| 18 | +importorg.zapodot.junit.db.common.CompatibilityMode; |
| 19 | + |
| 20 | +@EmbeddedDatabaseTest( |
| 21 | +compatibilityMode =CompatibilityMode.MySQL, |
| 22 | +initialSqls = |
| 23 | +" CREATE TABLE products (" |
| 24 | + +" product_id INT," |
| 25 | + +" product_name VARCHAR(50)," |
| 26 | + +" description VARCHAR(100)" |
| 27 | + +");" |
| 28 | + +"insert into products (product_id, product_name, description) values " |
| 29 | + +"(1, 'Widget A', 'This is a sample product with SN1234-5678');" |
| 30 | + +"insert into products (product_id, product_name, description) values " |
| 31 | + +"(2, 'Widget B', 'A product with serial SN9876-1234 in the description');" |
| 32 | + +"insert into products (product_id, product_name, description) values " |
| 33 | + +"(3, 'Widget C', 'Product SN1234-56789 is available now');" |
| 34 | + +"insert into products (product_id, product_name, description) values " |
| 35 | + +"(4, 'Widget D', 'No serial number here');" |
| 36 | + +"insert into products (product_id, product_name, description) values " |
| 37 | + +"(5, 'Widget E', 'Check out SN4321-8765 in this description');") |
| 38 | +classMysqlTest { |
| 39 | +@Test |
| 40 | +voidtestScript(@EmbeddedDatabaseDataSourcedataSource) |
| 41 | +throwsSQLException,FileNotFoundException { |
| 42 | +try (finalConnectionconnection =dataSource.getConnection()) { |
| 43 | +try (finalStatementstatement =connection.createStatement(); |
| 44 | +finalResultSetresultSet = |
| 45 | +statement.executeQuery( |
| 46 | +newBufferedReader( |
| 47 | +newFileReader( |
| 48 | +"src/main/java/g3401_3500/" |
| 49 | + +"s3465_find_products_with_valid_serial_numbers/" |
| 50 | + +"script.sql")) |
| 51 | + .lines() |
| 52 | + .collect(Collectors.joining("\n")) |
| 53 | + .replaceAll("#.*?\\r?\\n",""))) { |
| 54 | +assertThat(resultSet.next(),equalTo(true)); |
| 55 | +assertThat(resultSet.getNString(1),equalTo("1")); |
| 56 | +assertThat(resultSet.getNString(2),equalTo("Widget A")); |
| 57 | +assertThat( |
| 58 | +resultSet.getNString(3), |
| 59 | +equalTo("This is a sample product with SN1234-5678")); |
| 60 | +assertThat(resultSet.next(),equalTo(true)); |
| 61 | +assertThat(resultSet.getNString(1),equalTo("2")); |
| 62 | +assertThat(resultSet.getNString(2),equalTo("Widget B")); |
| 63 | +assertThat( |
| 64 | +resultSet.getNString(3), |
| 65 | +equalTo("A product with serial SN9876-1234 in the description")); |
| 66 | +assertThat(resultSet.next(),equalTo(true)); |
| 67 | +assertThat(resultSet.getNString(1),equalTo("5")); |
| 68 | +assertThat(resultSet.getNString(2),equalTo("Widget E")); |
| 69 | +assertThat( |
| 70 | +resultSet.getNString(3), |
| 71 | +equalTo("Check out SN4321-8765 in this description")); |
| 72 | +assertThat(resultSet.next(),equalTo(false)); |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | +} |