Refine search
public static void testLoggingSuppressor() throws IOException { TestLogHandler logHandler = new TestLogHandler(); Closeables.logger.addHandler(logHandler); try { Closer closer = new Closer(new Closer.LoggingSuppressor()); TestCloseable c1 = closer.register(TestCloseable.throwsOnClose(new IOException())); TestCloseable c2 = closer.register(TestCloseable.throwsOnClose(new RuntimeException())); try { throw closer.rethrow(new IOException("thrown"), IOException.class); } catch (IOException expected) { } assertTrue(logHandler.getStoredLogRecords().isEmpty()); closer.close(); assertEquals(2, logHandler.getStoredLogRecords().size()); LogRecord record = logHandler.getStoredLogRecords().get(0); assertEquals("Suppressing exception thrown when closing " + c2, record.getMessage()); record = logHandler.getStoredLogRecords().get(1); assertEquals("Suppressing exception thrown when closing " + c1, record.getMessage()); } finally { Closeables.logger.removeHandler(logHandler); } }
private static MappedByteBuffer mapInternal(File file, MapMode mode, long size) throws IOException { checkNotNull(file); checkNotNull(mode); Closer closer = Closer.create(); try { RandomAccessFile raf = closer.register(new RandomAccessFile(file, mode == MapMode.READ_ONLY ? "r" : "rw")); FileChannel channel = closer.register(raf.getChannel()); return channel.map(mode, 0, size == -1 ? channel.size() : size); } catch (Throwable e) { throw closer.rethrow(e); } finally { closer.close(); } }
public void testExceptionThrown_fromTryBlock() throws IOException { Closer closer = new Closer(suppressor); TestCloseable c1 = closer.register(TestCloseable.normal()); TestCloseable c2 = closer.register(TestCloseable.normal()); IOException exception = new IOException(); try { try { throw exception; } catch (Throwable e) { throw closer.rethrow(e); } finally { closer.close(); } } catch (Throwable expected) { assertSame(exception, expected); } assertTrue(c1.isClosed()); assertTrue(c2.isClosed()); assertTrue(suppressor.suppressions.isEmpty()); }
@Override public long copyTo(CharSink sink) throws IOException { checkNotNull(sink); Closer closer = Closer.create(); try { Writer writer = closer.register(sink.openStream()); writer.write((String) seq); return seq.length(); } catch (Throwable e) { throw closer.rethrow(e); } finally { closer.close(); } } }
public void testExceptionThrown_whenCreatingCloseables() throws IOException { Closer closer = new Closer(suppressor); TestCloseable c1 = null; TestCloseable c2 = null; TestCloseable c3 = null; try { try { c1 = closer.register(TestCloseable.normal()); c2 = closer.register(TestCloseable.normal()); c3 = closer.register(TestCloseable.throwsOnCreate()); } catch (Throwable e) { throw closer.rethrow(e); } finally { closer.close(); } } catch (Throwable expected) { assertThat(expected).isInstanceOf(IOException.class); } assertTrue(c1.isClosed()); assertTrue(c2.isClosed()); assertNull(c3); assertTrue(suppressor.suppressions.isEmpty()); }
public void testExceptionThrown_whileClosingLastCloseable() throws IOException { Closer closer = new Closer(suppressor); IOException exception = new IOException(); // c1 is added first, closed last TestCloseable c1 = closer.register(TestCloseable.throwsOnClose(exception)); TestCloseable c2 = closer.register(TestCloseable.normal()); try { closer.close(); } catch (Throwable expected) { assertSame(exception, expected); } assertTrue(c1.isClosed()); assertTrue(c2.isClosed()); assertTrue(suppressor.suppressions.isEmpty()); }
/** * Reads the contents of this source as a string. * * @throws IOException if an I/O error occurs while reading from this source */ public String read() throws IOException { Closer closer = Closer.create(); try { Reader reader = closer.register(openStream()); return CharStreams.toString(reader); } catch (Throwable e) { throw closer.rethrow(e); } finally { closer.close(); } }
public void testRuntimeExceptions() throws IOException { Closer closer = new Closer(suppressor); RuntimeException tryException = new RuntimeException(); RuntimeException c1Exception = new RuntimeException(); RuntimeException c2Exception = new RuntimeException(); TestCloseable c1 = closer.register(TestCloseable.throwsOnClose(c1Exception)); TestCloseable c2 = closer.register(TestCloseable.throwsOnClose(c2Exception)); try { try { throw tryException; } catch (Throwable e) { throw closer.rethrow(e); } finally { closer.close(); } } catch (Throwable expected) { assertSame(tryException, expected); } assertTrue(c1.isClosed()); assertTrue(c2.isClosed()); assertSuppressed( new Suppression(c2, tryException, c2Exception), new Suppression(c1, tryException, c1Exception)); }
public void testExceptionThrown_whileClosingFirstCloseable() throws IOException { Closer closer = new Closer(suppressor); IOException exception = new IOException(); // c2 is added last, closed first TestCloseable c1 = closer.register(TestCloseable.normal()); TestCloseable c2 = closer.register(TestCloseable.throwsOnClose(exception)); try { closer.close(); } catch (Throwable expected) { assertSame(exception, expected); } assertTrue(c1.isClosed()); assertTrue(c2.isClosed()); assertTrue(suppressor.suppressions.isEmpty()); }
/** * Writes all the given bytes to this sink. * * @throws IOException if an I/O occurs while writing to this sink */ public void write(byte[] bytes) throws IOException { checkNotNull(bytes); Closer closer = Closer.create(); try { OutputStream out = closer.register(openStream()); out.write(bytes); out.flush(); // https://code.google.com/p/guava-libraries/issues/detail?id=1330 } catch (Throwable e) { throw closer.rethrow(e); } finally { closer.close(); } }