|
3 | 3 | import io.ebean.datasource.DataSourceConfig; |
4 | 4 | import io.ebean.datasource.DataSourceFactory; |
5 | 5 | import io.ebean.datasource.DataSourcePool; |
| 6 | +import org.junit.jupiter.api.Assertions; |
6 | 7 | import org.junit.jupiter.api.Test; |
7 | 8 |
|
8 | 9 | import java.sql.Connection; |
9 | 10 | import java.sql.PreparedStatement; |
| 11 | +import java.sql.ResultSet; |
| 12 | +import java.sql.SQLException; |
10 | 13 |
|
11 | 14 | public class FactoryTest { |
12 | 15 |
|
@@ -49,4 +52,39 @@ public void dataSourceFactory_get_createPool() throws Exception { |
49 | 52 | } |
50 | 53 |
|
51 | 54 | } |
| 55 | + |
| 56 | + |
| 57 | + @Test |
| 58 | + public void testPreparedStatement() throws Exception { |
| 59 | + |
| 60 | + DataSourceConfig config = new DataSourceConfig(); |
| 61 | + config.setDriver("org.h2.Driver"); |
| 62 | + config.setUrl("jdbc:h2:mem:tests"); |
| 63 | + config.setUsername("sa"); |
| 64 | + config.setPassword(""); |
| 65 | + |
| 66 | + DataSourcePool pool = DataSourceFactory.create("test", config); |
| 67 | + String sql = "select * from information_schema.settings where setting_name != ?"; |
| 68 | + try (Connection connection = pool.getConnection()) { |
| 69 | + try (PreparedStatement stmt = connection.prepareStatement(sql)) { |
| 70 | + stmt.executeQuery(); |
| 71 | + Assertions.fail("Not expected"); |
| 72 | + } catch (SQLException e) { |
| 73 | + // expected: Parameter "#1" not set |
| 74 | + } |
| 75 | + |
| 76 | + try (PreparedStatement stmt = connection.prepareStatement(sql)) { |
| 77 | + stmt.setString(1, "foo"); |
| 78 | + ResultSet rs = stmt.executeQuery(); |
| 79 | + rs.close(); |
| 80 | + } |
| 81 | + |
| 82 | + try (PreparedStatement stmt = connection.prepareStatement(sql)) { |
| 83 | + stmt.executeQuery(); |
| 84 | + Assertions.fail("Not expected"); |
| 85 | + } catch (SQLException e) { |
| 86 | + // expected: Parameter "#1" not set |
| 87 | + } |
| 88 | + } |
| 89 | + } |
52 | 90 | } |
0 commit comments