|
| 1 | +package g3401_3500.s3465_find_products_with_valid_serial_numbers; |
| 2 | + |
| 3 | +import static org.hamcrest.CoreMatchers.equalTo; |
| 4 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 5 | + |
| 6 | +import java.io.BufferedReader; |
| 7 | +import java.io.FileNotFoundException; |
| 8 | +import java.io.FileReader; |
| 9 | +import java.sql.Connection; |
| 10 | +import java.sql.ResultSet; |
| 11 | +import java.sql.SQLException; |
| 12 | +import java.sql.Statement; |
| 13 | +import java.util.stream.Collectors; |
| 14 | +import javax.sql.DataSource; |
| 15 | +import org.junit.jupiter.api.Test; |
| 16 | +import org.zapodot.junit.db.annotations.EmbeddedDatabase; |
| 17 | +import org.zapodot.junit.db.annotations.EmbeddedDatabaseTest; |
| 18 | +import org.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 | +class MysqlTest { |
| 39 | + @Test |
| 40 | + void testScript(@EmbeddedDatabase DataSource dataSource) |
| 41 | + throws SQLException, FileNotFoundException { |
| 42 | + try (final Connection connection = dataSource.getConnection()) { |
| 43 | + try (final Statement statement = connection.createStatement(); |
| 44 | + final ResultSet resultSet = |
| 45 | + statement.executeQuery( |
| 46 | + new BufferedReader( |
| 47 | + new FileReader( |
| 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 | +} |
0 commit comments