private void input() throws IOException { final InputShop<E> is = getArchiveDriver() .newInputShop(model, getArchiveInputSocket()); try { check(is); final Closeable[] streams = new Closeable[getNumEntries()]; try { for (int i = 0; i < streams.length; i++) { input(is, i).close(); // first attempt streams[i] = input(is, i); // second attempt close(streams); trigger(TestCloseable.class, expected); try { throw got; } finally { clear(TestCloseable.class);
private void output() throws IOException { final OutputShop<E> os = getArchiveDriver() .newOutputShop(model, getArchiveOutputSocket(), null); try { final Closeable[] streams = new Closeable[getNumEntries()]; try { for (int i = 0; i < streams.length; i++) streams[i] = output(os, i); } finally { close(streams); check(os); } finally { final IOException expected = new IOException(); trigger(TestCloseable.class, expected); try { throw got; } finally { clear(TestCloseable.class);
private int getMaxArchiveLength() { return getNumEntries() * getDataLength() * 4 / 3; // account for archive type specific overhead }
@Test(expected = NullPointerException.class) public void testNewInputShopMustNotTolerateNullModel() throws IOException { getArchiveDriver().newInputShop(null, getArchiveInputSocket()); }
@Test(expected = NullPointerException.class) public void testNewOutputShopMustNotTolerateNullModel() throws IOException { getArchiveDriver().newOutputShop(null, getArchiveOutputSocket(), null); }
private InputStream input(final InputShop<E> shop, final int i) throws IOException { final InputSocket<? extends E> input = shop.getInputSocket(name(i)); final byte[] buf = new byte[getDataLength()]; ReadOnlyFile rof; try { assertTrue(Arrays.equals(getData(), buf)); final byte[] buf = new byte[getDataLength()]; SeekableByteChannel channel; try { readFully(channel, ByteBuffer.wrap(buf)); assertEquals(-1, channel.read(ByteBuffer.wrap(buf))); } finally { assertTrue(Arrays.equals(getData(), buf)); final byte[] buf = new byte[getDataLength()]; boolean failure = true; final DataInputStream try { in.readFully(buf); assertTrue(Arrays.equals(getData(), buf)); assertEquals(-1, in.read()); failure = false;
@Override public void setUp() throws IOException { super.setUp(); // Order is important here! final TestConfig config = getTestConfig(); config.setDataSize(getMaxArchiveLength()); config.setIOPoolProvider(null); // reset parent = newController(model.getParent()); }
@CreatesObligation private OutputStream output(final OutputShop<E> shop, final int i) throws IOException { final String name = name(i); final E entry = newEntry(name); final OutputSocket<? extends E> output = shop.getOutputSocket(entry); assertSame(entry, output.getLocalTarget()); assertNull(shop.getEntry(name)); assertEquals(i, shop.getSize()); boolean failure = true; final OutputStream out = output.newOutputStream(); try { assertSame(entry, shop.getEntry(name)); assertEquals(i + 1, shop.getSize()); out.write(getData()); failure = false; } finally { if (failure) out.close(); } return out; }
private <E extends FsArchiveEntry> void check( final EntryContainer<E> container) { final int numEntries = getNumEntries(); assertEquals(numEntries, container.getSize()); final Iterator<E> it = container.iterator(); final E e = it.next(); assertNotNull(e); assertEquals(name(i), e.getName()); assertSame(FILE, e.getType()); assertEquals(getDataLength(), e.getSize(DATA)); final long storage = e.getSize(STORAGE); assertTrue(UNKNOWN == storage || getDataLength() <= storage); // random data is not compressible! assertTrue(UNKNOWN != e.getTime(WRITE)); try {
private OutputSocket<?> getArchiveOutputSocket() { return getArchiveDriver().getOutputSocket(parent, name, FsOutputOptions.NONE, null); }
@Test public void testGetInputSocketMustForwardTheCallToTheGivenController() { final Throwable expected = new RuntimeException(); trigger(MockController.class, expected); try { getArchiveInputSocket(); fail(); } catch (final RuntimeException got) { if (!contains(got, expected)) throw got; } }
@Test public void testGetOutputSocketMustForwardTheCallToTheGivenController() { final Throwable expected = new RuntimeException(); trigger(MockController.class, expected); try { getArchiveOutputSocket(); fail(); } catch (final RuntimeException got) { if (!contains(got, expected)) throw got; } }
@Test public void testIOPoolShouldBeConstant() { final IOPool<?> p1 = getArchiveDriver().getPool(); final IOPool<?> p2 = getArchiveDriver().getPool(); if (p1 != p2) logger.log(Level.WARNING, "{0} returns different I/O buffer pools upon multiple invocations of getPool()!", getArchiveDriver().getClass()); }
private InputSocket<?> getArchiveInputSocket() { return getArchiveDriver().getInputSocket(parent, name, FsInputOptions.NONE); }
@Test(expected = NullPointerException.class) public void testNewInputShopMustNotTolerateNullInputSocket() throws IOException { getArchiveDriver().newInputShop(model, null); }
@Test(expected = NullPointerException.class) public void testNewOutputShopMustNotTolerateNullInputSocket() throws IOException { getArchiveDriver().newOutputShop(model, null, null); }
@Test public void testIOPoolMustNotBeNull() { assertNotNull(getArchiveDriver().getPool()); }
@Test public void testArchiveDriverMustBeFederated() { assertTrue(getArchiveDriver().isFederated()); }
private E newEntry(final String name) throws CharConversionException { final E e = getArchiveDriver().newEntry(name, Entry.Type.FILE, null); assertNotNull(e); assertEquals(name, e.getName()); assertSame(FILE, e.getType()); assertTrue(UNKNOWN == e.getSize(DATA)); assertTrue(UNKNOWN == e.getSize(STORAGE)); assertTrue(UNKNOWN == e.getTime(WRITE)); assertTrue(UNKNOWN == e.getTime(READ)); assertTrue(UNKNOWN == e.getTime(CREATE)); return e; }