use of java.io.Closeable in project sonarqube by SonarSource.
the class System2Test method close_throws_exception_on_error.
@Test
public void close_throws_exception_on_error() {
Closeable closeable = new Closeable() {
@Override
public void close() throws IOException {
throw new IOException("expected");
}
};
try {
System2.INSTANCE.close(closeable);
fail();
} catch (IllegalStateException e) {
assertThat(e.getCause().getMessage()).isEqualTo("expected");
}
}
use of java.io.Closeable in project druid by alibaba.
the class JdbcUtilsTest method test_close.
public void test_close() throws Exception {
JdbcUtils.close((Connection) null);
JdbcUtils.close((Statement) null);
JdbcUtils.close((ResultSet) null);
JdbcUtils.close(new MockConnection() {
@Override
public void close() throws SQLException {
throw new SQLException();
}
});
JdbcUtils.close(new MockStatement(null) {
@Override
public void close() throws SQLException {
throw new SQLException();
}
});
JdbcUtils.close(new MockResultSet(null) {
@Override
public void close() throws SQLException {
throw new SQLException();
}
});
JdbcUtils.close(new Closeable() {
@Override
public void close() throws IOException {
throw new IOException();
}
});
JdbcUtils.close(new Closeable() {
@Override
public void close() throws IOException {
}
});
JdbcUtils.close((Closeable) null);
new JdbcUtils();
}
use of java.io.Closeable in project neo4j by neo4j.
the class DumpCommand method execute.
@Override
public void execute(String[] args) throws IncorrectUsage, CommandFailed {
String database = arguments.parse("database", args);
Path archive = calculateArchive(database, arguments.parseMandatoryPath("to", args));
Path databaseDirectory = canonicalPath(toDatabaseDirectory(database));
try {
Validators.CONTAINS_EXISTING_DATABASE.validate(databaseDirectory.toFile());
} catch (IllegalArgumentException e) {
throw new CommandFailed("database does not exist: " + database, e);
}
try (Closeable ignored = StoreLockChecker.check(databaseDirectory)) {
dump(database, databaseDirectory, archive);
} catch (StoreLockException e) {
throw new CommandFailed("the database is in use -- stop Neo4j and try again", e);
} catch (IOException e) {
wrapIOException(e);
} catch (CannotWriteException e) {
throw new CommandFailed("you do not have permission to dump the database -- is Neo4j running as a " + "different user?", e);
}
}
use of java.io.Closeable in project neo4j by neo4j.
the class DumpCommandTest method shouldReportAHelpfulErrorIfWeDontHaveWritePermissionsForLock.
@Test
public void shouldReportAHelpfulErrorIfWeDontHaveWritePermissionsForLock() throws Exception {
assumeFalse("We haven't found a way to reliably tests permissions on Windows", SystemUtils.IS_OS_WINDOWS);
try (FileSystemAbstraction fileSystem = new DefaultFileSystemAbstraction();
StoreLocker storeLocker = new StoreLocker(fileSystem)) {
storeLocker.checkLock(databaseDirectory.toFile());
try (Closeable ignored = withPermissions(databaseDirectory.resolve(StoreLocker.STORE_LOCK_FILENAME), emptySet())) {
execute("foo.db");
fail("expected exception");
} catch (CommandFailed e) {
assertThat(e.getMessage(), equalTo("you do not have permission to dump the database -- is Neo4j " + "running as a different user?"));
}
}
}
use of java.io.Closeable in project neo4j by neo4j.
the class DumperTest method shouldGiveAClearErrorMessageIfTheArchivesParentDirectoryIsNotWritable.
@Test
public void shouldGiveAClearErrorMessageIfTheArchivesParentDirectoryIsNotWritable() throws IOException {
assumeFalse("We haven't found a way to reliably tests permissions on Windows", SystemUtils.IS_OS_WINDOWS);
Path directory = testDirectory.directory("a-directory").toPath();
Path archive = testDirectory.file("subdir/the-archive.dump").toPath();
Files.createDirectories(archive.getParent());
try (Closeable ignored = TestUtils.withPermissions(archive.getParent(), emptySet())) {
new Dumper().dump(directory, archive, Predicates.alwaysFalse());
fail("Expected an exception");
} catch (AccessDeniedException e) {
assertEquals(archive.getParent().toString(), e.getMessage());
}
}
Aggregations