@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); }
super(config.getMaxConnections(), config.getMaxConnectionWait()); requireNonNull(config.getFilename()); if (config.getFilename().isEmpty()) { throw new IllegalArgumentException("filename is empty"); .append("jdbc:h2:").append(config.getFilename()) .append(";MVCC=").append(config.isMvccEnabled()); if (config.getCipher() != Cipher.NONE) { jdbcUrlBuilder.append(";CIPHER=").append(config.getCipher()); dataSource.setURL(jdbcUrl); dataSource.setUser("sa"); if (config.getCipher() != Cipher.NONE) { dataSource.setPassword(config.getFilePassword() + " "); dataSource.setLoginTimeout(Ints.checkedCast(config.getMaxConnectionWait().roundTo(SECONDS))); 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"); String fileName = config.getInitScript(); if (fileName != null) { setConfig(connection, "ALLOW_LITERALS", config.getAllowLiterals());
.build(); H2EmbeddedDataSourceConfig expected = new H2EmbeddedDataSourceConfig() .setAllowLiterals(AllowLiterals.NONE) .setCacheSize(4096) .setCipher(Cipher.AES) .setCompressLob(CompressLob.NO) .setFilename("TestData") .setFilePassword("test123") .setInitScript("init.sql") .setMaxLengthInplaceLob(8192) .setMaxMemoryRows(5000) .setMvccEnabled(false) .setMaxConnections(12) .setMaxConnectionWait(new Duration(42, TimeUnit.SECONDS));
@Test public void testValidations() { assertFailsValidation(new H2EmbeddedDataSourceConfig(), "filename", "may not be null", NotNull.class); } }
.build(); H2EmbeddedDataSourceConfig expected = new H2EmbeddedDataSourceConfig() .setAllowLiterals(AllowLiterals.NONE) .setCacheSize(4096) .setCipher(Cipher.AES) .setCompressLob(CompressLob.NO) .setFilename("TestData") .setFilePassword("test123") .setInitScript("init.sql") .setMaxLengthInplaceLob(8192) .setMaxMemoryRows(5000) .setMvccEnabled(false) .setMaxConnections(12) .setMaxConnectionWait(new Duration(42, TimeUnit.SECONDS));
@Test public void testValidations() { assertFailsValidation(new H2EmbeddedDataSourceConfig(), "filename", "may not be null", NotNull.class); } }
@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); }
super(config.getMaxConnections(), config.getMaxConnectionWait()); requireNonNull(config.getFilename()); if (config.getFilename().isEmpty()) { throw new IllegalArgumentException("filename is empty"); .append("jdbc:h2:").append(config.getFilename()) .append(";MVCC=").append(config.isMvccEnabled()); if (config.getCipher() != Cipher.NONE) { jdbcUrlBuilder.append(";CIPHER=").append(config.getCipher()); dataSource.setURL(jdbcUrl); dataSource.setUser("sa"); if (config.getCipher() != Cipher.NONE) { dataSource.setPassword(config.getFilePassword() + " "); dataSource.setLoginTimeout(Ints.checkedCast(config.getMaxConnectionWait().roundTo(SECONDS))); 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"); String fileName = config.getInitScript(); if (fileName != null) { setConfig(connection, "ALLOW_LITERALS", config.getAllowLiterals());
@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 = 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 = 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 = 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 = 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 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); } }
@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); } }
@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 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); } }