@Override public H2EmbeddedDataSource get() { H2EmbeddedDataSourceConfig config = injector.getInstance(Key.get(H2EmbeddedDataSourceConfig.class, annotation)); try { return new H2EmbeddedDataSource(config); } catch (Exception e) { throw new ProvisionException("Error creating a H2EmbeddedDataSource", e); } } }
private static void executeCommand(Connection connection, String command) throws SQLException { Statement statement = connection.createStatement(); try { statement.executeUpdate(command); } finally { closeQuietly(statement); } }
@Test public void testInitFromResource() throws Exception { Connection connection = null; Statement statement = null; ResultSet resultSet = null; try { H2EmbeddedDataSourceConfig config = new H2EmbeddedDataSourceConfig() .setFilename(file.getAbsolutePath()) .setInitScript("io/airlift/dbpool/h2.ddl") .setCipher(Cipher.AES) .setFilePassword("filePassword"); H2EmbeddedDataSource dataSource = new H2EmbeddedDataSource(config); connection = dataSource.getConnection(); statement = connection.createStatement(); resultSet = statement.executeQuery("select * from message"); } finally { closeQuietly(resultSet); closeQuietly(statement); closeQuietly(connection); } }
Connection connection = getConnection(); try { setConfig(connection, "CACHE_SIZE", config.getCacheSize()); setConfig(connection, "COMPRESS_LOB", config.getCompressLob()); setConfig(connection, "MAX_MEMORY_ROWS", config.getMaxMemoryRows()); setConfig(connection, "MAX_LENGTH_INPLACE_LOB", config.getMaxLengthInplaceLob()); setConfig(connection, "DB_CLOSE_DELAY ", "-1"); ScriptReader scriptReader = new ScriptReader(reader); for (String statement = scriptReader.readStatement(); statement != null; statement = scriptReader.readStatement()) { executeCommand(connection, statement); setConfig(connection, "ALLOW_LITERALS", config.getAllowLiterals()); closeQuietly(connection);
@Test(groups = "requiresTempFile") public void testCorrectConfigurationPrefix() { final String expectedPrefix = "expected"; final String otherPrefix = "additional"; final String propertySuffixToTest = ".db.connections.max"; final int expectedValue = 1234; // Required properties for construction Map<String, String> properties = createDefaultConfigurationProperties(expectedPrefix, temporaryFile.getAbsolutePath()); // Optional property added with two different prefixes, two different values properties.put(otherPrefix + propertySuffixToTest, Integer.toString(expectedValue + 5678)); properties.put(expectedPrefix + propertySuffixToTest, Integer.toString(expectedValue)); Injector injector = createInjector(properties, new H2EmbeddedDataSourceModule(expectedPrefix, MainBinding.class)); ObjectHolder objectHolder = injector.getInstance(ObjectHolder.class); // Make sure we picked up the value with the expected prefix assertInstanceOf(objectHolder.dataSource, H2EmbeddedDataSource.class); H2EmbeddedDataSource created = (H2EmbeddedDataSource) objectHolder.dataSource; assertEquals(created.getMaxConnections(), expectedValue, "Property value not loaded from correct prefix"); }
@Test public void testInitFromResource() throws Exception { Connection connection = null; Statement statement = null; ResultSet resultSet = null; try { H2EmbeddedDataSourceConfig config = new H2EmbeddedDataSourceConfig() .setFilename(file.getAbsolutePath()) .setInitScript("io/airlift/dbpool/h2.ddl") .setCipher(Cipher.AES) .setFilePassword("filePassword"); H2EmbeddedDataSource dataSource = new H2EmbeddedDataSource(config); connection = dataSource.getConnection(); statement = connection.createStatement(); resultSet = statement.executeQuery("select * from message"); } finally { closeQuietly(resultSet); closeQuietly(statement); closeQuietly(connection); } }
Connection connection = getConnection(); try { setConfig(connection, "CACHE_SIZE", config.getCacheSize()); setConfig(connection, "COMPRESS_LOB", config.getCompressLob()); setConfig(connection, "MAX_MEMORY_ROWS", config.getMaxMemoryRows()); setConfig(connection, "MAX_LENGTH_INPLACE_LOB", config.getMaxLengthInplaceLob()); setConfig(connection, "DB_CLOSE_DELAY ", "-1"); ScriptReader scriptReader = new ScriptReader(reader); for (String statement = scriptReader.readStatement(); statement != null; statement = scriptReader.readStatement()) { executeCommand(connection, statement); setConfig(connection, "ALLOW_LITERALS", config.getAllowLiterals()); closeQuietly(connection);
@Test(groups = "requiresTempFile") public void testCorrectConfigurationPrefix() { final String expectedPrefix = "expected"; final String otherPrefix = "additional"; final String propertySuffixToTest = ".db.connections.max"; final int expectedValue = 1234; // Required properties for construction Map<String, String> properties = createDefaultConfigurationProperties(expectedPrefix, temporaryFile.getAbsolutePath()); // Optional property added with two different prefixes, two different values properties.put(otherPrefix + propertySuffixToTest, Integer.toString(expectedValue + 5678)); properties.put(expectedPrefix + propertySuffixToTest, Integer.toString(expectedValue)); Injector injector = createInjector(properties, new H2EmbeddedDataSourceModule(expectedPrefix, MainBinding.class)); ObjectHolder objectHolder = injector.getInstance(ObjectHolder.class); // Make sure we picked up the value with the expected prefix assertInstanceOf(objectHolder.dataSource, H2EmbeddedDataSource.class); H2EmbeddedDataSource created = (H2EmbeddedDataSource) objectHolder.dataSource; assertEquals(created.getMaxConnections(), expectedValue, "Property value not loaded from correct prefix"); }
@Test public void testInitFromFile() throws Exception { File initScript = File.createTempFile("initscript", ".ddl"); Connection connection = null; Statement statement = null; ResultSet resultSet = null; try { URL url = Resources.getResource("io/airlift/dbpool/h2.ddl"); Resources.asByteSource(url).copyTo(Files.asByteSink(initScript)); H2EmbeddedDataSourceConfig config = new H2EmbeddedDataSourceConfig() .setFilename(file.getAbsolutePath()) .setInitScript(initScript.getAbsolutePath()) .setCipher(Cipher.AES) .setFilePassword("filePassword"); H2EmbeddedDataSource dataSource = new H2EmbeddedDataSource(config); connection = dataSource.getConnection(); statement = connection.createStatement(); resultSet = statement.executeQuery("select * from message"); } finally { initScript.delete(); closeQuietly(resultSet); closeQuietly(statement); closeQuietly(connection); } }
@Override public H2EmbeddedDataSource get() { H2EmbeddedDataSourceConfig config = injector.getInstance(Key.get(H2EmbeddedDataSourceConfig.class, annotation)); try { return new H2EmbeddedDataSource(config); } catch (Exception e) { throw new ProvisionException("Error creating a H2EmbeddedDataSource", e); } } }
private static void executeCommand(Connection connection, String command) throws SQLException { Statement statement = connection.createStatement(); try { statement.executeUpdate(command); } finally { closeQuietly(statement); } }
@Test public void testInitFromFile() throws Exception { File initScript = File.createTempFile("initscript", ".ddl"); Connection connection = null; Statement statement = null; ResultSet resultSet = null; try { URL url = Resources.getResource("io/airlift/dbpool/h2.ddl"); Resources.asByteSource(url).copyTo(Files.asByteSink(initScript)); H2EmbeddedDataSourceConfig config = new H2EmbeddedDataSourceConfig() .setFilename(file.getAbsolutePath()) .setInitScript(initScript.getAbsolutePath()) .setCipher(Cipher.AES) .setFilePassword("filePassword"); H2EmbeddedDataSource dataSource = new H2EmbeddedDataSource(config); connection = dataSource.getConnection(); statement = connection.createStatement(); resultSet = statement.executeQuery("select * from message"); } finally { initScript.delete(); closeQuietly(resultSet); closeQuietly(statement); closeQuietly(connection); } }
@Test(expectedExceptions = FileNotFoundException.class) public void testCantFindInitScript() throws Exception { H2EmbeddedDataSourceConfig config = new H2EmbeddedDataSourceConfig() .setFilename(file.getAbsolutePath()) .setInitScript("foo") .setCipher(Cipher.AES) .setFilePassword("filePassword"); new H2EmbeddedDataSource(config); }
private static void setConfig(Connection connection, String name, Object value) throws SQLException { Statement statement = connection.createStatement(); try { String command = String.format("SET %s %s", name, value); int count = statement.executeUpdate(command); if (count != 0) { throw new SQLException("Failed to execute command: " + command); } } finally { closeQuietly(statement); } }
@Test(expectedExceptions = NullPointerException.class) public void testNullFilenameThrows() throws Exception { H2EmbeddedDataSourceConfig config = new H2EmbeddedDataSourceConfig() // Filename left as null .setInitScript("io/airlift/dbpool/h2.ddl") .setCipher(Cipher.AES) .setFilePassword("filePassword"); new H2EmbeddedDataSource(config); }
private static void setConfig(Connection connection, String name, Object value) throws SQLException { Statement statement = connection.createStatement(); try { String command = String.format("SET %s %s", name, value); int count = statement.executeUpdate(command); if (count != 0) { throw new SQLException("Failed to execute command: " + command); } } finally { closeQuietly(statement); } }
@Test(expectedExceptions = FileNotFoundException.class) public void testCantFindInitScript() throws Exception { H2EmbeddedDataSourceConfig config = new H2EmbeddedDataSourceConfig() .setFilename(file.getAbsolutePath()) .setInitScript("foo") .setCipher(Cipher.AES) .setFilePassword("filePassword"); new H2EmbeddedDataSource(config); }
@Test(expectedExceptions = NullPointerException.class) public void testNullFilenameThrows() throws Exception { H2EmbeddedDataSourceConfig config = new H2EmbeddedDataSourceConfig() // Filename left as null .setInitScript("io/airlift/dbpool/h2.ddl") .setCipher(Cipher.AES) .setFilePassword("filePassword"); new H2EmbeddedDataSource(config); }
@Test(expectedExceptions = JdbcSQLException.class) public void testInitFromInvalidDdlThrows() throws Exception { File initScript = File.createTempFile("initscript", ".ddl"); try { String invalidDdl = "This isn't valid SQL"; asCharSink(initScript, UTF_8).write(invalidDdl); H2EmbeddedDataSourceConfig config = new H2EmbeddedDataSourceConfig() .setFilename(file.getAbsolutePath()) .setInitScript(initScript.getAbsolutePath()) .setCipher(Cipher.AES) .setFilePassword("filePassword"); new H2EmbeddedDataSource(config); } finally { initScript.delete(); } }
@Test(expectedExceptions = IllegalArgumentException.class) public void testEmptyFilenameThrows() throws Exception { H2EmbeddedDataSourceConfig config = new H2EmbeddedDataSourceConfig() .setFilename("") .setInitScript("io/airlift/dbpool/h2.ddl") .setCipher(Cipher.AES) .setFilePassword("filePassword"); new H2EmbeddedDataSource(config); }