Search in sources :

Example 26 with Closeable

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");
    }
}
Also used : Closeable(java.io.Closeable) IOException(java.io.IOException) Test(org.junit.Test)

Example 27 with Closeable

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();
}
Also used : JdbcUtils(com.alibaba.druid.util.JdbcUtils) SQLException(java.sql.SQLException) Closeable(java.io.Closeable) MockStatement(com.alibaba.druid.mock.MockStatement) MockResultSet(com.alibaba.druid.mock.MockResultSet) IOException(java.io.IOException) MockConnection(com.alibaba.druid.mock.MockConnection)

Example 28 with Closeable

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);
    }
}
Also used : Util.canonicalPath(org.neo4j.commandline.Util.canonicalPath) Path(java.nio.file.Path) Closeable(java.io.Closeable) CommandFailed(org.neo4j.commandline.admin.CommandFailed) StoreLockException(org.neo4j.kernel.StoreLockException) IOException(java.io.IOException)

Example 29 with Closeable

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?"));
        }
    }
}
Also used : DefaultFileSystemAbstraction(org.neo4j.io.fs.DefaultFileSystemAbstraction) FileSystemAbstraction(org.neo4j.io.fs.FileSystemAbstraction) DefaultFileSystemAbstraction(org.neo4j.io.fs.DefaultFileSystemAbstraction) StoreLocker(org.neo4j.kernel.internal.StoreLocker) Closeable(java.io.Closeable) CommandFailed(org.neo4j.commandline.admin.CommandFailed) Test(org.junit.Test)

Example 30 with Closeable

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());
    }
}
Also used : Path(java.nio.file.Path) AccessDeniedException(java.nio.file.AccessDeniedException) Closeable(java.io.Closeable) Test(org.junit.Test)

Aggregations

Closeable (java.io.Closeable)216 IOException (java.io.IOException)88 Test (org.junit.Test)56 ArrayList (java.util.ArrayList)29 File (java.io.File)26 HashMap (java.util.HashMap)12 VirtualFile (org.jboss.vfs.VirtualFile)12 URL (java.net.URL)9 Path (java.nio.file.Path)9 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)8 Map (java.util.Map)7 ISE (io.druid.java.util.common.ISE)6 InputStream (java.io.InputStream)6 MountHandle (org.jboss.as.server.deployment.module.MountHandle)6 ResourceRoot (org.jboss.as.server.deployment.module.ResourceRoot)6 ProgramController (co.cask.cdap.app.runtime.ProgramController)5 ProgramType (co.cask.cdap.proto.ProgramType)4 ProgramId (co.cask.cdap.proto.id.ProgramId)4 Pair (io.druid.java.util.common.Pair)4 FileOutputStream (java.io.FileOutputStream)4