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); } }
@Test public void testGetWorkUnitsAndExtractor() throws IOException, DataRecordException { OldApiHadoopFileInputSource<String, Text, LongWritable, Text> fileInputSource = new TestHadoopFileInputSource(); List<WorkUnit> workUnitList = fileInputSource.getWorkunits(this.sourceState); Assert.assertEquals(workUnitList.size(), 1); WorkUnitState workUnitState = new WorkUnitState(workUnitList.get(0)); Closer closer = Closer.create(); try { OldApiHadoopFileInputExtractor<String, Text, LongWritable, Text> extractor = (OldApiHadoopFileInputExtractor<String, Text, LongWritable, Text>) fileInputSource.getExtractor( workUnitState); Text text = extractor.readRecord(null); Assert.assertEquals(text.toString(), TEXT); Assert.assertNull(extractor.readRecord(null)); } catch (Throwable t) { throw closer.rethrow(t); } finally { closer.close(); } }
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()); }
@Test public void testGetWorkUnitsAndExtractor() throws IOException, DataRecordException { HadoopFileInputSource<String, Text, LongWritable, Text> fileInputSource = new TestHadoopFileInputSource(); List<WorkUnit> workUnitList = fileInputSource.getWorkunits(this.sourceState); Assert.assertEquals(workUnitList.size(), 1); WorkUnitState workUnitState = new WorkUnitState(workUnitList.get(0)); Closer closer = Closer.create(); try { HadoopFileInputExtractor<String, Text, LongWritable, Text> extractor = (HadoopFileInputExtractor<String, Text, LongWritable, Text>) fileInputSource.getExtractor( workUnitState); Text text = extractor.readRecord(null); Assert.assertEquals(text.toString(), TEXT); Assert.assertNull(extractor.readRecord(null)); } catch (Throwable t) { throw closer.rethrow(t); } finally { closer.close(); } }
@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()); }
/** * 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(); } }
long start = System.nanoTime(); discoveryServer = new TestingDiscoveryServer(environment); closer.register(() -> closeUnchecked(discoveryServer)); log.info("Created TestingDiscoveryServer in %s", nanosSince(start).convertToMostSuccinctTimeUnit()); TestingPrestoServer worker = closer.register(createTestingPrestoServer(discoveryServer.getBaseUrl(), false, extraProperties, parserOptions, environment)); servers.add(worker); extraCoordinatorProperties.putAll(extraProperties); extraCoordinatorProperties.putAll(coordinatorProperties); coordinator = closer.register(createTestingPrestoServer(discoveryServer.getBaseUrl(), true, extraCoordinatorProperties, parserOptions, environment)); servers.add(coordinator); throw closer.rethrow(e, Exception.class); closer.close();
/** * 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(); } }
public void testCloseExceptionsSuppressed_whenExceptionThrownFromTryBlock() throws IOException { Closer closer = new Closer(suppressor); IOException tryException = new IOException(); IOException c1Exception = new IOException(); IOException c2Exception = new IOException(); 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)); }
/** * Writes the given character sequence to this sink. * * @throws IOException if an I/O error while writing to this sink */ public void write(CharSequence charSequence) throws IOException { checkNotNull(charSequence); Closer closer = Closer.create(); try { Writer out = closer.register(openStream()); out.append(charSequence); out.flush(); // https://code.google.com/p/guava-libraries/issues/detail?id=1330 } 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)); }
private static MappedByteBuffer map(RandomAccessFile raf, MapMode mode, long size) throws IOException { Closer closer = Closer.create(); try { FileChannel channel = closer.register(raf.getChannel()); return channel.map(mode, 0, size); } catch (Throwable e) { throw closer.rethrow(e); } finally { closer.close(); } }
public static void testSuppressingSuppressorIfPossible() throws IOException { // can't test the JDK7 suppressor when not running on JDK7 if (!Closer.SuppressingSuppressor.isAvailable()) { return; } Closer closer = new Closer(new Closer.SuppressingSuppressor()); IOException thrownException = new IOException(); IOException c1Exception = new IOException(); RuntimeException c2Exception = new RuntimeException(); TestCloseable c1 = closer.register(TestCloseable.throwsOnClose(c1Exception)); TestCloseable c2 = closer.register(TestCloseable.throwsOnClose(c2Exception)); try { try { throw thrownException; } catch (Throwable e) { throw closer.rethrow(thrownException, IOException.class); } finally { assertThat(getSuppressed(thrownException)).isEmpty(); closer.close(); } } catch (IOException expected) { assertSame(thrownException, expected); } assertTrue(c1.isClosed()); assertTrue(c2.isClosed()); ImmutableSet<Throwable> suppressed = ImmutableSet.copyOf(getSuppressed(thrownException)); assertEquals(2, suppressed.size()); assertEquals(ImmutableSet.of(c1Exception, c2Exception), suppressed); }
/** * Reads the full contents of this byte source as a byte array. * * @throws IOException if an I/O error occurs while reading from this source */ public byte[] read() throws IOException { Closer closer = Closer.create(); try { InputStream in = closer.register(openStream()); Optional<Long> size = sizeIfKnown(); return size.isPresent() ? ByteStreams.toByteArray(in, size.get()) : ByteStreams.toByteArray(in); } catch (Throwable e) { throw closer.rethrow(e); } finally { closer.close(); } }