method2testcases
stringlengths
118
3.08k
### Question: IpUtils { public static String getHostName() { try { return InetAddress.getLocalHost().getHostName(); } catch (final UnknownHostException ex) { return "unknown"; } } static String getIp(); static String getHostName(); static final String IP_REGEX; }### Answer: @Test public void assertGetHostName() { assertNotNull(IpUtils.getHostName()); }
### Question: TimeService { public long getCurrentMillis() { return System.currentTimeMillis(); } long getCurrentMillis(); }### Answer: @Test public void assertGetCurrentMillis() throws Exception { Assert.assertTrue(timeService.getCurrentMillis() <= System.currentTimeMillis()); } @Test public void assertGetCurrentMillis() { assertTrue(timeService.getCurrentMillis() <= System.currentTimeMillis()); } @Test public void testGetCurrentMillis() throws Exception { assertTrue(timeService.getCurrentMillis() <= System.currentTimeMillis()); }
### Question: CloudJobFacade implements JobFacade { @Override public void clearMisfire(final Collection<Integer> shardingItems) { } @Override JobRootConfiguration loadJobRootConfiguration(final boolean fromCache); @Override void checkJobExecutionEnvironment(); @Override void failoverIfNecessary(); @Override void registerJobBegin(final ShardingContexts shardingContexts); @Override void registerJobCompleted(final ShardingContexts shardingContexts); ShardingContexts getShardingContexts(); @Override boolean misfireIfRunning(final Collection<Integer> shardingItems); @Override void clearMisfire(final Collection<Integer> shardingItems); @Override boolean isExecuteMisfired(final Collection<Integer> shardingItems); @Override boolean isEligibleForJobRunning(); @Override boolean isNeedSharding(); @Override void beforeJobExecuted(final ShardingContexts shardingContexts); @Override void afterJobExecuted(final ShardingContexts shardingContexts); @Override void postJobExecutionEvent(final JobExecutionEvent jobExecutionEvent); @Override void postJobStatusTraceEvent(final String taskId, final JobStatusTraceEvent.State state, final String message); }### Answer: @Test public void assertClearMisfire() { jobFacade.clearMisfire(null); }
### Question: CloudJobFacade implements JobFacade { @Override public boolean isExecuteMisfired(final Collection<Integer> shardingItems) { return false; } @Override JobRootConfiguration loadJobRootConfiguration(final boolean fromCache); @Override void checkJobExecutionEnvironment(); @Override void failoverIfNecessary(); @Override void registerJobBegin(final ShardingContexts shardingContexts); @Override void registerJobCompleted(final ShardingContexts shardingContexts); ShardingContexts getShardingContexts(); @Override boolean misfireIfRunning(final Collection<Integer> shardingItems); @Override void clearMisfire(final Collection<Integer> shardingItems); @Override boolean isExecuteMisfired(final Collection<Integer> shardingItems); @Override boolean isEligibleForJobRunning(); @Override boolean isNeedSharding(); @Override void beforeJobExecuted(final ShardingContexts shardingContexts); @Override void afterJobExecuted(final ShardingContexts shardingContexts); @Override void postJobExecutionEvent(final JobExecutionEvent jobExecutionEvent); @Override void postJobStatusTraceEvent(final String taskId, final JobStatusTraceEvent.State state, final String message); }### Answer: @Test public void assertIsExecuteMisfired() { assertFalse(jobFacade.isExecuteMisfired(null)); }
### Question: TaskContext { public static String getIdForUnassignedSlave(final String id) { return id.replaceAll(TaskContext.from(id).getSlaveId(), UNASSIGNED_SLAVE_ID); } TaskContext(final String jobName, final List<Integer> shardingItem, final ExecutionType type); TaskContext(final String jobName, final List<Integer> shardingItem, final ExecutionType type, final String slaveId); private TaskContext(final String id, final MetaInfo metaInfo, final ExecutionType type, final String slaveId); static TaskContext from(final String id); static String getIdForUnassignedSlave(final String id); void setSlaveId(final String slaveId); String getTaskName(); String getExecutorId(final String appName); }### Answer: @Test public void assertGetIdForUnassignedSlave() { Assert.assertThat(TaskContext.getIdForUnassignedSlave("test_job@-@0@-@READY@-@slave-S0@-@0"), Is.is("test_job@-@0@-@READY@-@unassigned-slave@-@0")); } @Test public void assertGetIdForUnassignedSlave() { assertThat(TaskContext.getIdForUnassignedSlave("test_job@-@0@-@READY@-@slave-S0@-@0"), is("test_job@-@0@-@READY@-@unassigned-slave@-@0")); }
### Question: TaskContext { public void setSlaveId(final String slaveId) { id = id.replaceAll(this.slaveId, slaveId); this.slaveId = slaveId; } TaskContext(final String jobName, final List<Integer> shardingItem, final ExecutionType type); TaskContext(final String jobName, final List<Integer> shardingItem, final ExecutionType type, final String slaveId); private TaskContext(final String id, final MetaInfo metaInfo, final ExecutionType type, final String slaveId); static TaskContext from(final String id); static String getIdForUnassignedSlave(final String id); void setSlaveId(final String slaveId); String getTaskName(); String getExecutorId(final String appName); }### Answer: @Test public void assertSetSlaveId() { TaskContext actual = new TaskContext("test_job", Lists.newArrayList(0), ExecutionType.READY, "slave-S0"); Assert.assertThat(actual.getSlaveId(), Is.is("slave-S0")); actual.setSlaveId("slave-S1"); Assert.assertThat(actual.getSlaveId(), Is.is("slave-S1")); } @Test public void assertSetSlaveId() { TaskContext actual = new TaskContext("test_job", Collections.singletonList(0), ExecutionType.READY, "slave-S0"); assertThat(actual.getSlaveId(), is("slave-S0")); actual.setSlaveId("slave-S1"); assertThat(actual.getSlaveId(), is("slave-S1")); }
### Question: JobEventBus { public void post(final JobEvent event) { if (isRegistered && !executorServiceObject.isShutdown()) { eventBus.post(event); } } JobEventBus(); JobEventBus(final JobEventConfiguration jobEventConfig); void post(final JobEvent event); }### Answer: @Test public void assertPost() throws InterruptedException, NoSuchFieldException { jobEventBus = new JobEventBus(new TestJobEventConfiguration(jobEventCaller)); assertIsRegistered(true); jobEventBus.post(new JobExecutionEvent("fake_task_id", "test_event_bus_job", JobExecutionEvent.ExecutionSource.NORMAL_TRIGGER, 0)); while (!TestJobEventListener.isExecutionEventCalled()) { Thread.sleep(100L); } Mockito.verify(jobEventCaller).call(); } @Test public void assertPostWithoutListener() throws NoSuchFieldException { jobEventBus = new JobEventBus(); assertIsRegistered(false); ReflectionUtils.setFieldValue(jobEventBus, "eventBus", eventBus); jobEventBus.post(new JobExecutionEvent("fake_task_id", "test_event_bus_job", JobExecutionEvent.ExecutionSource.NORMAL_TRIGGER, 0)); Mockito.verify(eventBus, Mockito.times(0)).post(ArgumentMatchers.<JobEvent>any()); }
### Question: AbstractMuxStore extends AbstractBlobStore { protected AbstractMuxStore(URI id, BlobStore ... stores) { super(id); setBackingStores(Arrays.asList(stores)); } protected AbstractMuxStore(URI id, BlobStore ... stores); List<?extends BlobStore> getBackingStores(); void setBackingStores(List<?extends BlobStore> stores); }### Answer: @Test public void testAbstractMuxStore() { assertEquals(storeId, store.getId()); }
### Question: WWWBlob extends AbstractBlob { @Override public long getSize() throws IOException { if (exists != null && !exists) throw new MissingBlobException(id); if (size == null) connect(true, true); return size; } WWWBlob(URL url, BlobStoreConnection conn, StreamManager streamManager); @Override long getSize(); @Override InputStream openInputStream(); @Override OutputStream openOutputStream(long estimatedSize, boolean overwrite); @Override boolean exists(); @Override void delete(); @Override Blob moveTo(URI blobId, Map<String, String> hints); }### Answer: @Test public void testGetSize() { try { assertEquals(422279, blob.getSize()); } catch (IOException e) { fail("getSize() failed", e); } }
### Question: WWWBlob extends AbstractBlob { @Override public InputStream openInputStream() throws IOException { connect(true, false); return content; } WWWBlob(URL url, BlobStoreConnection conn, StreamManager streamManager); @Override long getSize(); @Override InputStream openInputStream(); @Override OutputStream openOutputStream(long estimatedSize, boolean overwrite); @Override boolean exists(); @Override void delete(); @Override Blob moveTo(URI blobId, Map<String, String> hints); }### Answer: @Test public void testOpenInputStream() { try { assertNotNull(blob.openInputStream()); } catch (IOException e) { fail("openInputStream() failed", e); } }
### Question: WWWBlob extends AbstractBlob { @Override public OutputStream openOutputStream(long estimatedSize, boolean overwrite) throws IOException { if (!overwrite && exists()) throw new DuplicateBlobException(id); URLConnection con = connect(false, false); OutputStream os = streamManager.manageOutputStream(owner, con.getOutputStream()); exists = true; return os; } WWWBlob(URL url, BlobStoreConnection conn, StreamManager streamManager); @Override long getSize(); @Override InputStream openInputStream(); @Override OutputStream openOutputStream(long estimatedSize, boolean overwrite); @Override boolean exists(); @Override void delete(); @Override Blob moveTo(URI blobId, Map<String, String> hints); }### Answer: @Test public void testOpenOutputStream() { try { assertNotNull(blob.openOutputStream(-1, true)); } catch (IOException e) { fail("openOutputStream() failed", e); } }
### Question: WWWConnection extends AbstractBlobStoreConnection { @Override public Blob getBlob(URI blobId, Map<String, String> hints) throws IOException, IllegalArgumentException { return getWWWBlob(blobId, true); } WWWConnection(WWWStore store, Map<String, URLStreamHandler> handlers, StreamManager streamManager); @Override void close(); @Override Blob getBlob(URI blobId, Map<String, String> hints); @Override Iterator<URI> listBlobIds(String filterPrefix); @Override void sync(); }### Answer: @Test public void testGetBlob() { try { Blob blob1 = con.getBlob(URI.create("http: assertNotNull(blob1); Blob blob2 = con.getBlob(URI.create("http: assertNotNull(blob2); assertEquals(blob1, blob2); } catch (IOException e) { fail("createBlob() failed", e); } }
### Question: WWWConnection extends AbstractBlobStoreConnection { @Override public Iterator<URI> listBlobIds(String filterPrefix) throws IOException { throw new UnsupportedOperationException("blob-id listing not supported"); } WWWConnection(WWWStore store, Map<String, URLStreamHandler> handlers, StreamManager streamManager); @Override void close(); @Override Blob getBlob(URI blobId, Map<String, String> hints); @Override Iterator<URI> listBlobIds(String filterPrefix); @Override void sync(); }### Answer: @Test(expectedExceptions = { UnsupportedOperationException.class }) public void testListBlobIds() throws IOException { con.listBlobIds(null); }
### Question: WWWConnection extends AbstractBlobStoreConnection { @Override public void close() { if (blobs != null) { for (WWWBlob blob : blobs.values()) blob.closed(); blobs.clear(); blobs = null; } super.close(); } WWWConnection(WWWStore store, Map<String, URLStreamHandler> handlers, StreamManager streamManager); @Override void close(); @Override Blob getBlob(URI blobId, Map<String, String> hints); @Override Iterator<URI> listBlobIds(String filterPrefix); @Override void sync(); }### Answer: @Test(expectedExceptions = { IllegalStateException.class }) public void testClose() throws IOException { closeableCon.close(); closeableCon.getBlob(URI.create("http: }
### Question: QuiescingStreamManager extends StreamManager { boolean isQuiescent() { stateLock.lock(); try { return quiescent; } finally { stateLock.unlock(); } } void lockUnquiesced(); void unlockState(); boolean setQuiescent(boolean quiescent); @Override OutputStream manageOutputStream(BlobStoreConnection con, OutputStream stream); }### Answer: @Test(groups={ "init" }) public void testInitialState() { assertFalse(manager.isQuiescent()); }
### Question: ManagedOutputStream extends FilterOutputStream { @Override public void close() throws IOException { if (!closed) { super.close(); closed = true; listener.notifyClosed(this); } } ManagedOutputStream(CloseListener listener, OutputStream stream, BlobStoreConnection con); BlobStoreConnection getConnection(); @Override void write(byte[] b, int off, int len); @Override void close(); }### Answer: @Test public void testCloseNotification() throws Exception { MockCloseListener listener = new MockCloseListener(); ManagedOutputStream managed = new ManagedOutputStream(listener, new ByteArrayOutputStream(), null); managed.close(); assertTrue(listener.getClosedSet().contains(managed)); }
### Question: ClientIterator implements Iterator<T> { public T next() { return load().next(); } ClientIterator(RemoteIterator<T> ri, int batchSize); boolean hasNext(); T next(); void remove(); }### Answer: @Test public void testNext() { for (String v : source) { assertEquals(v, ci.next()); } try { ci.next(); fail("Failed to get NoSuchElementException"); } catch (NoSuchElementException e) { } catch (Exception e) { fail("Failed to get NoSuchElementException"); } }
### Question: StreamManager { public OutputStream manageOutputStream(BlobStoreConnection con, OutputStream stream) throws IOException { ManagedOutputStream managed = new ManagedOutputStream(listener, stream, con); openOutputStreams.add(managed); return managed; } StreamManager(); OutputStream manageOutputStream(BlobStoreConnection con, OutputStream stream); InputStream manageInputStream(BlobStoreConnection con, InputStream stream); void connectionClosed(BlobStoreConnection con); }### Answer: @Test(dependsOnGroups = { "init" }) public void testManageOutputStream() throws Exception { OutputStream managed = manager.manageOutputStream(null, new ByteArrayOutputStream()); assertEquals(manager.getOpenOutputStreamCount(), 1); managed.close(); assertEquals(manager.getOpenOutputStreamCount(), 0); }
### Question: StreamManager { public InputStream manageInputStream(BlobStoreConnection con, InputStream stream) throws IOException { ManagedInputStream managed = new ManagedInputStream(listener, stream, con); openInputStreams.add(managed); return managed; } StreamManager(); OutputStream manageOutputStream(BlobStoreConnection con, OutputStream stream); InputStream manageInputStream(BlobStoreConnection con, InputStream stream); void connectionClosed(BlobStoreConnection con); }### Answer: @Test(dependsOnGroups = { "init" }) public void testManageInputStream() throws Exception { InputStream managed = manager.manageInputStream(null, new ByteArrayInputStream(new byte[0])); assertEquals(manager.getOpenInputStreamCount(), 1); managed.close(); assertEquals(manager.getOpenInputStreamCount(), 0); }
### Question: ClientIterator implements Iterator<T> { public void remove() { throw new UnsupportedOperationException("remove() is unsupported"); } ClientIterator(RemoteIterator<T> ri, int batchSize); boolean hasNext(); T next(); void remove(); }### Answer: @Test public void testRemove() { for (String v : source) { assertEquals(v, ci.next()); try { ci.remove(); fail("Failed to get UnsupportedOperationException"); } catch (UnsupportedOperationException e) { } catch (Exception e) { fail("Failed to get UnsupportedOperationException"); } } }
### Question: ClientConnection extends AbstractBlobStoreConnection { @Override public void close() { if (!isClosed()) { super.close(); try { remote.close(); } catch (IOException e) { log.warn("Failed to close connection on remote server.", e); } } } ClientConnection(BlobStore store, StreamManager strMgr, RemoteConnection con); @Override void close(); @Override Blob getBlob(URI blobId, Map<String, String> hints); @Override Blob getBlob(InputStream in, long estimatedSize, Map<String, String> hints); @Override Iterator<URI> listBlobIds(String filterPrefix); @Override void sync(); static int ITERATOR_BATCH_SIZE; }### Answer: @Test public void testClose() throws IOException { ServerConnection sc = new ServerConnection(con, exporter); ClientConnection cc = new ClientConnection(this.cc.getBlobStore(), new StreamManager(), (RemoteConnection) sc.getExported()); reset(con); con.close(); replay(con); cc.close(); verify(con); }
### Question: ClientConnection extends AbstractBlobStoreConnection { @Override public Iterator<URI> listBlobIds(String filterPrefix) throws IOException { ensureOpen(); RemoteIterator<URI> ri = remote.listBlobIds(filterPrefix); return new ClientIterator<URI>(ri, ITERATOR_BATCH_SIZE); } ClientConnection(BlobStore store, StreamManager strMgr, RemoteConnection con); @Override void close(); @Override Blob getBlob(URI blobId, Map<String, String> hints); @Override Blob getBlob(InputStream in, long estimatedSize, Map<String, String> hints); @Override Iterator<URI> listBlobIds(String filterPrefix); @Override void sync(); static int ITERATOR_BATCH_SIZE; }### Answer: @Test public void testListBlobIds() throws IOException { URI id = URI.create("foo:bar"); Iterator<URI> it = Arrays.asList(id).iterator(); reset(con); expect(con.listBlobIds(null)).andReturn(it); replay(con); Iterator<URI> ri = cc.listBlobIds(null); assertTrue(ri instanceof ClientIterator<?>); assertTrue(ri.hasNext()); assertEquals(id, ri.next()); assertFalse(ri.hasNext()); verify(con); }
### Question: ClientOutputStream extends OutputStream { public ClientOutputStream(RemoteOutputStream out) { this.remote = out; } ClientOutputStream(RemoteOutputStream out); @Override void close(); @Override void flush(); @Override void write(int b); @Override void write(byte[] buf); @Override void write(byte[] buf, int off, int len); }### Answer: @Test public void testClientOutputStream() throws IOException { }
### Question: ClientOutputStream extends OutputStream { @Override public void write(int b) throws IOException { remote.write(b); } ClientOutputStream(RemoteOutputStream out); @Override void close(); @Override void flush(); @Override void write(int b); @Override void write(byte[] buf); @Override void write(byte[] buf, int off, int len); }### Answer: @Test public void testWriteInt() throws IOException { bo.reset(); co.write('a'); byte[] b = bo.toByteArray(); assertNotNull(b); assertEquals(1, b.length); assertEquals('a', b[0]); } @Test public void testWriteByteArray() throws IOException { bo.reset(); byte[] in = "Hello World".getBytes(); co.write(in); byte[] b = bo.toByteArray(); assertNotNull(b); assertEquals(in, b); bo.reset(); in = new byte[0]; co.write(in); b = bo.toByteArray(); assertNotNull(b); assertEquals(in, b); } @Test public void testWriteByteArrayIntInt() throws IOException { bo.reset(); byte[] in = "Hello World".getBytes(); byte[] pb = new byte[in.length * 3]; System.arraycopy(in, 0, pb, in.length, in.length); co.write(pb, in.length, in.length); byte[] b = bo.toByteArray(); assertNotNull(b); assertEquals(in, b); byte[] r = new byte[100]; bogusIndex(r, -1, 10); bogusIndex(r, 10, -1); bogusIndex(r, -1, -1); bogusIndex(r, 50, 51); bogusIndex(r, 101, 1); bogusIndex(r, 0, 101); co.write(pb, in.length, 0); b = bo.toByteArray(); assertNotNull(b); assertEquals(in, b); }
### Question: AbstractMuxConnection extends AbstractBlobStoreConnection { @Override public void close() { super.close(); if (cons != null) { for (BlobStoreConnection con : cons.values()) con.close(); cons.clear(); cons = null; } } AbstractMuxConnection(BlobStore store, Transaction txn); abstract BlobStore getStore(URI blobId, Map<String, String> hints); Set<BlobStore> getStores(String prefix); @Override void close(); @Override Blob getBlob(URI blobId, Map<String, String> hints); @Override Iterator<URI> listBlobIds(String filterPrefix); @Override void sync(); }### Answer: @Test public void testClose() throws IOException { TestConnection con = (TestConnection) store.openConnection(null, null); assertFalse(con.isClosed()); con.getConnection(store1, null); con.getConnection(store2, null); assertEquals(2, con.getCons().size()); Collection<BlobStoreConnection> cons = con.getCons().values(); con.close(); assertTrue(con.isClosed()); for (BlobStoreConnection c : cons) assertTrue(c.isClosed()); }
### Question: ClientOutputStream extends OutputStream { @Override public void flush() throws IOException { remote.flush(); } ClientOutputStream(RemoteOutputStream out); @Override void close(); @Override void flush(); @Override void write(int b); @Override void write(byte[] buf); @Override void write(byte[] buf, int off, int len); }### Answer: @Test public void testFlush() throws IOException { final boolean[] flushed = new boolean[] { false }; ByteArrayOutputStream bo = new ByteArrayOutputStream() { @Override public void flush() { flushed[0] = true; } }; ServerOutputStream so = new ServerOutputStream(bo, exporter); ClientOutputStream co = new ClientOutputStream((RemoteOutputStream) so.getExported()); assertFalse(flushed[0]); co.flush(); assertTrue(flushed[0]); co.close(); }
### Question: ClientOutputStream extends OutputStream { @Override public void close() throws IOException { remote.close(); } ClientOutputStream(RemoteOutputStream out); @Override void close(); @Override void flush(); @Override void write(int b); @Override void write(byte[] buf); @Override void write(byte[] buf, int off, int len); }### Answer: @Test public void testClose() throws IOException { ServerOutputStream so = new ServerOutputStream(bo, exporter); ClientOutputStream co = new ClientOutputStream((RemoteOutputStream) so.getExported()); co.close(); try { for (int i = 0; i < 10; i++) { try { Thread.sleep(Exporter.RETRY_DELAY * i); } catch (InterruptedException e) { } co.write('a'); } fail("Failed to get expected exception"); } catch (NoSuchObjectException e) { } }
### Question: ClientInputStream extends InputStream { public ClientInputStream(RemoteInputStream remote) { this.remote = remote; } ClientInputStream(RemoteInputStream remote); @Override int read(); @Override int read(byte[] buf, int off, int len); @Override long skip(long n); @Override void close(); }### Answer: @Test public void testClientInputStream() throws IOException { assertFalse(ci.markSupported()); }
### Question: ClientInputStream extends InputStream { @Override public int read() throws IOException { return remote.read(); } ClientInputStream(RemoteInputStream remote); @Override int read(); @Override int read(byte[] buf, int off, int len); @Override long skip(long n); @Override void close(); }### Answer: @Test public void testRead() throws IOException { bi.reset(); for (byte b : buf) assertEquals(b, ci.read()); assertEquals(-1, ci.read()); } @Test public void testReadByteArray() throws IOException { bi.reset(); byte[] r = new byte[100]; assertEquals(buf.length, ci.read(r)); for (int i = 0; i < buf.length; i++) assertEquals(buf[i], r[i]); assertEquals(-1, ci.read()); bi.reset(); r = new byte[0]; assertEquals(0, ci.read(r)); } @Test public void testReadByteArrayIntInt() throws IOException { bi.reset(); byte[] r = new byte[100]; bogusIndex(r, -1, 10); bogusIndex(r, 10, -1); bogusIndex(r, -1, -1); bogusIndex(r, 50, 51); bogusIndex(r, 101, 1); bogusIndex(r, 0, 101); int off = r.length - buf.length; assertEquals(buf.length, ci.read(r, off, buf.length)); for (int i = 0; i < buf.length; i++) assertEquals(buf[i], r[off + i]); assertEquals(-1, ci.read()); }
### Question: ClientInputStream extends InputStream { @Override public long skip(long n) throws IOException { return remote.skip(n); } ClientInputStream(RemoteInputStream remote); @Override int read(); @Override int read(byte[] buf, int off, int len); @Override long skip(long n); @Override void close(); }### Answer: @Test public void testSkip() throws IOException { bi.reset(); int skip = buf.length - 1; int n = (int) ci.skip(skip); assertEquals(skip, n); for (int i = n; i < buf.length; i++) assertEquals(buf[i], ci.read()); assertEquals(-1, ci.read()); }
### Question: ClientInputStream extends InputStream { @Override public void close() throws IOException { remote.close(); } ClientInputStream(RemoteInputStream remote); @Override int read(); @Override int read(byte[] buf, int off, int len); @Override long skip(long n); @Override void close(); }### Answer: @Test public void testClose() throws IOException { ServerInputStream si = new ServerInputStream(bi, exporter); ClientInputStream ci = new ClientInputStream((RemoteInputStream) si.getExported()); bi.reset(); assertEquals(buf[0], ci.read()); ci.close(); try { for (int i = 0; i < 10; i++) { try { Thread.sleep(Exporter.RETRY_DELAY * i); } catch (InterruptedException e) { } ci.read(); } fail("Failed to get expected exception"); } catch (NoSuchObjectException e) { } }
### Question: ClientSynchronization implements Synchronization { public void afterCompletion(int status) { try { remote.afterCompletion(status); } catch (RemoteException e) { log.warn("Failed to execute afterCompletion() on remote", e); throw new RuntimeException("Failure to contact remote", e); } } ClientSynchronization(RemoteSynchronization remote); void afterCompletion(int status); void beforeCompletion(); }### Answer: @Test public void testAfterCompletion() { reset(sync); sync.afterCompletion(42); replay(sync); cs.afterCompletion(42); verify(sync); }
### Question: ClientSynchronization implements Synchronization { public void beforeCompletion() { try { remote.beforeCompletion(); } catch (RemoteException e) { log.warn("Failed to execute beforeCompletion on remote", e); throw new RuntimeException("Failure to contact remote", e); } } ClientSynchronization(RemoteSynchronization remote); void afterCompletion(int status); void beforeCompletion(); }### Answer: @Test public void testBeforeCompletion() { reset(sync); sync.beforeCompletion(); replay(sync); cs.beforeCompletion(); verify(sync); }
### Question: AbstractMuxConnection extends AbstractBlobStoreConnection { public AbstractMuxConnection(BlobStore store, Transaction txn) { super(store); this.txn = txn; } AbstractMuxConnection(BlobStore store, Transaction txn); abstract BlobStore getStore(URI blobId, Map<String, String> hints); Set<BlobStore> getStores(String prefix); @Override void close(); @Override Blob getBlob(URI blobId, Map<String, String> hints); @Override Iterator<URI> listBlobIds(String filterPrefix); @Override void sync(); }### Answer: @Test public void testAbstractMuxConnection() { assertEquals(store, con.getBlobStore()); }
### Question: ClientBlob extends AbstractBlob { @Override public InputStream openInputStream() throws IOException { ensureOpen(); return streamMgr.manageInputStream(getConnection(), new ClientInputStream(remote.openInputStream())); } ClientBlob(BlobStoreConnection con, StreamManager streamMgr, RemoteBlob remote); @Override URI getCanonicalId(); @Override InputStream openInputStream(); @Override OutputStream openOutputStream(long estSize, boolean overwrite); @Override long getSize(); @Override boolean exists(); @Override void delete(); @Override Blob moveTo(URI blobId, Map<String, String> hints); }### Answer: @Test public void testOpenInputStream() throws IOException { URI id = URI.create("foo:bar"); InputStream in = createMock(InputStream.class); makeThreadSafe(in, true); reset(blob); makeThreadSafe(blob, true); expect(blob.openInputStream()).andReturn(in); expect(blob.openInputStream()).andThrow(new MissingBlobException(id)); expect(in.read()).andReturn(42); in.close(); expectLastCall().atLeastOnce(); replay(blob); replay(in); InputStream ci = cb.openInputStream(); assertNotNull(ci); assertEquals(42, ci.read()); ci.close(); try { cb.openInputStream(); fail("Failed to rcv expected exception"); } catch (MissingBlobException e) { assertEquals(id, e.getBlobId()); } verify(blob); verify(in); }
### Question: ClientBlob extends AbstractBlob { @Override public OutputStream openOutputStream(long estSize, boolean overwrite) throws IOException { ensureOpen(); return streamMgr.manageOutputStream(getConnection(), new ClientOutputStream(remote.openOutputStream(estSize, overwrite))); } ClientBlob(BlobStoreConnection con, StreamManager streamMgr, RemoteBlob remote); @Override URI getCanonicalId(); @Override InputStream openInputStream(); @Override OutputStream openOutputStream(long estSize, boolean overwrite); @Override long getSize(); @Override boolean exists(); @Override void delete(); @Override Blob moveTo(URI blobId, Map<String, String> hints); }### Answer: @Test public void testOpenOutputStream() throws IOException { URI id = URI.create("foo:bar"); OutputStream out = createMock(OutputStream.class); makeThreadSafe(out, true); reset(blob); makeThreadSafe(blob, true); expect(blob.openOutputStream(42L, true)).andReturn(out); expect(blob.openOutputStream(-1L, true)).andThrow(new MissingBlobException(id)); out.write(42); out.close(); expectLastCall().atLeastOnce(); replay(blob); replay(out); OutputStream co = cb.openOutputStream(42L, true); assertNotNull(co); co.write(42); co.close(); try { cb.openOutputStream(-1L, true); fail("Failed to rcv expected exception"); } catch (MissingBlobException e) { assertEquals(id, e.getBlobId()); } verify(blob); verify(out); }
### Question: ClientBlob extends AbstractBlob { @Override public long getSize() throws IOException { ensureOpen(); return remote.getSize(); } ClientBlob(BlobStoreConnection con, StreamManager streamMgr, RemoteBlob remote); @Override URI getCanonicalId(); @Override InputStream openInputStream(); @Override OutputStream openOutputStream(long estSize, boolean overwrite); @Override long getSize(); @Override boolean exists(); @Override void delete(); @Override Blob moveTo(URI blobId, Map<String, String> hints); }### Answer: @Test public void testGetSize() throws IOException { reset(blob); expect(blob.getSize()).andReturn(42L); replay(blob); assertEquals(42L, cb.getSize()); verify(blob); }
### Question: ClientBlob extends AbstractBlob { @Override public boolean exists() throws IOException { ensureOpen(); return remote.exists(); } ClientBlob(BlobStoreConnection con, StreamManager streamMgr, RemoteBlob remote); @Override URI getCanonicalId(); @Override InputStream openInputStream(); @Override OutputStream openOutputStream(long estSize, boolean overwrite); @Override long getSize(); @Override boolean exists(); @Override void delete(); @Override Blob moveTo(URI blobId, Map<String, String> hints); }### Answer: @Test public void testExists() throws IOException { reset(blob); expect(blob.exists()).andReturn(true); replay(blob); assertTrue(cb.exists()); verify(blob); }
### Question: ClientBlob extends AbstractBlob { @Override public void delete() throws IOException { ensureOpen(); remote.delete(); } ClientBlob(BlobStoreConnection con, StreamManager streamMgr, RemoteBlob remote); @Override URI getCanonicalId(); @Override InputStream openInputStream(); @Override OutputStream openOutputStream(long estSize, boolean overwrite); @Override long getSize(); @Override boolean exists(); @Override void delete(); @Override Blob moveTo(URI blobId, Map<String, String> hints); }### Answer: @Test public void testDelete() throws IOException { reset(blob); blob.delete(); replay(blob); sb.delete(); verify(blob); }
### Question: ClientXAResource implements XAResource { public void commit(Xid xid, boolean onePhase) throws XAException { try { remote.commit(serialized(xid), onePhase); } catch (RemoteException e) { throw (XAException) new XAException("RMI faiure").initCause(e); } } ClientXAResource(RemoteXAResource xaRes, ClientTransactionListener txn); void commit(Xid xid, boolean onePhase); void end(Xid xid, int flags); void forget(Xid xid); int getTransactionTimeout(); boolean isSameRM(XAResource xa); int prepare(Xid xid); Xid[] recover(int flag); void rollback(Xid xid); boolean setTransactionTimeout(int seconds); void start(Xid xid, int flag); }### Answer: @Test public void testCommit() throws XAException { reset(res); res.commit(isA(SerializedXid.class), eq(false)); replay(res); cx.commit(xid, false); verify(res); }
### Question: ClientXAResource implements XAResource { public void end(Xid xid, int flags) throws XAException { try { remote.end(serialized(xid), flags); } catch (RemoteException e) { throw (XAException) new XAException("RMI faiure").initCause(e); } } ClientXAResource(RemoteXAResource xaRes, ClientTransactionListener txn); void commit(Xid xid, boolean onePhase); void end(Xid xid, int flags); void forget(Xid xid); int getTransactionTimeout(); boolean isSameRM(XAResource xa); int prepare(Xid xid); Xid[] recover(int flag); void rollback(Xid xid); boolean setTransactionTimeout(int seconds); void start(Xid xid, int flag); }### Answer: @Test public void testEnd() throws XAException { reset(res); res.end(isA(SerializedXid.class), eq(42)); replay(res); cx.end(xid, 42); verify(res); }
### Question: ClientXAResource implements XAResource { public void forget(Xid xid) throws XAException { try { remote.forget(serialized(xid)); } catch (RemoteException e) { throw (XAException) new XAException("RMI faiure").initCause(e); } } ClientXAResource(RemoteXAResource xaRes, ClientTransactionListener txn); void commit(Xid xid, boolean onePhase); void end(Xid xid, int flags); void forget(Xid xid); int getTransactionTimeout(); boolean isSameRM(XAResource xa); int prepare(Xid xid); Xid[] recover(int flag); void rollback(Xid xid); boolean setTransactionTimeout(int seconds); void start(Xid xid, int flag); }### Answer: @Test public void testForget() throws XAException { reset(res); res.forget(isA(SerializedXid.class)); replay(res); cx.forget(xid); verify(res); }
### Question: ClientXAResource implements XAResource { public int getTransactionTimeout() throws XAException { try { return remote.getTransactionTimeout(); } catch (RemoteException e) { throw (XAException) new XAException("RMI faiure").initCause(e); } } ClientXAResource(RemoteXAResource xaRes, ClientTransactionListener txn); void commit(Xid xid, boolean onePhase); void end(Xid xid, int flags); void forget(Xid xid); int getTransactionTimeout(); boolean isSameRM(XAResource xa); int prepare(Xid xid); Xid[] recover(int flag); void rollback(Xid xid); boolean setTransactionTimeout(int seconds); void start(Xid xid, int flag); }### Answer: @Test public void testGetTransactionTimeout() throws XAException { reset(res); expect(res.getTransactionTimeout()).andReturn(42); replay(res); assertEquals(42, cx.getTransactionTimeout()); verify(res); }
### Question: AbstractMuxConnection extends AbstractBlobStoreConnection { public Set<BlobStore> getStores(String prefix) { return new HashSet<BlobStore>(((AbstractMuxStore) getBlobStore()).getBackingStores()); } AbstractMuxConnection(BlobStore store, Transaction txn); abstract BlobStore getStore(URI blobId, Map<String, String> hints); Set<BlobStore> getStores(String prefix); @Override void close(); @Override Blob getBlob(URI blobId, Map<String, String> hints); @Override Iterator<URI> listBlobIds(String filterPrefix); @Override void sync(); }### Answer: @Test public void testGetStores() { Set<BlobStore> s = con.getStores(null); assertEquals(2, s.size()); assertTrue(s.contains(store1)); assertTrue(s.contains(store2)); }
### Question: ClientXAResource implements XAResource { public boolean isSameRM(XAResource xa) throws XAException { if (this == xa) return true; try { RemoteXAResource rxa = (xa == null) ? null : txn.getRemoteXAResource(xa); return (rxa == null) ? false : remote.isSameRM(rxa); } catch (RemoteException e) { throw (XAException) new XAException("RMI faiure").initCause(e); } } ClientXAResource(RemoteXAResource xaRes, ClientTransactionListener txn); void commit(Xid xid, boolean onePhase); void end(Xid xid, int flags); void forget(Xid xid); int getTransactionTimeout(); boolean isSameRM(XAResource xa); int prepare(Xid xid); Xid[] recover(int flag); void rollback(Xid xid); boolean setTransactionTimeout(int seconds); void start(Xid xid, int flag); }### Answer: @Test public void testIsSameRM() throws RemoteException, XAException, RollbackException, SystemException { reset(res); reset(txn); reset(stxn); expect(txn.getRemoteXAResource(res)).andReturn(sx); expect(stxn.getXAResource(sx)).andReturn(res); expect(res.isSameRM(res)).andReturn(true); replay(res); replay(txn); replay(stxn); assertTrue(cx.isSameRM(cx)); assertFalse(cx.isSameRM(null)); assertTrue(cx.isSameRM(res)); verify(res); verify(txn); verify(stxn); }
### Question: ClientXAResource implements XAResource { public int prepare(Xid xid) throws XAException { try { return remote.prepare(serialized(xid)); } catch (RemoteException e) { throw (XAException) new XAException("RMI faiure").initCause(e); } } ClientXAResource(RemoteXAResource xaRes, ClientTransactionListener txn); void commit(Xid xid, boolean onePhase); void end(Xid xid, int flags); void forget(Xid xid); int getTransactionTimeout(); boolean isSameRM(XAResource xa); int prepare(Xid xid); Xid[] recover(int flag); void rollback(Xid xid); boolean setTransactionTimeout(int seconds); void start(Xid xid, int flag); }### Answer: @Test public void testPrepare() throws XAException { reset(res); expect(res.prepare(isA(SerializedXid.class))).andReturn(XAResource.XA_OK); replay(res); assertEquals(XAResource.XA_OK, cx.prepare(xid)); verify(res); }
### Question: ClientXAResource implements XAResource { public Xid[] recover(int flag) throws XAException { try { return remote.recover(flag); } catch (RemoteException e) { throw (XAException) new XAException("RMI faiure").initCause(e); } } ClientXAResource(RemoteXAResource xaRes, ClientTransactionListener txn); void commit(Xid xid, boolean onePhase); void end(Xid xid, int flags); void forget(Xid xid); int getTransactionTimeout(); boolean isSameRM(XAResource xa); int prepare(Xid xid); Xid[] recover(int flag); void rollback(Xid xid); boolean setTransactionTimeout(int seconds); void start(Xid xid, int flag); }### Answer: @Test public void testRecover() throws XAException { reset(res); expect(res.recover(42)).andReturn(new Xid[] { xid }); replay(res); assertEquals(new Xid[] { new SerializedXid(xid) }, cx.recover(42)); verify(res); }
### Question: ClientXAResource implements XAResource { public void rollback(Xid xid) throws XAException { try { remote.rollback(serialized(xid)); } catch (RemoteException e) { throw (XAException) new XAException("RMI faiure").initCause(e); } } ClientXAResource(RemoteXAResource xaRes, ClientTransactionListener txn); void commit(Xid xid, boolean onePhase); void end(Xid xid, int flags); void forget(Xid xid); int getTransactionTimeout(); boolean isSameRM(XAResource xa); int prepare(Xid xid); Xid[] recover(int flag); void rollback(Xid xid); boolean setTransactionTimeout(int seconds); void start(Xid xid, int flag); }### Answer: @Test public void testRollback() throws XAException { reset(res); res.rollback(isA(SerializedXid.class)); replay(res); cx.rollback(xid); verify(res); }
### Question: ClientXAResource implements XAResource { public boolean setTransactionTimeout(int seconds) throws XAException { try { return remote.setTransactionTimeout(seconds); } catch (RemoteException e) { throw (XAException) new XAException("RMI faiure").initCause(e); } } ClientXAResource(RemoteXAResource xaRes, ClientTransactionListener txn); void commit(Xid xid, boolean onePhase); void end(Xid xid, int flags); void forget(Xid xid); int getTransactionTimeout(); boolean isSameRM(XAResource xa); int prepare(Xid xid); Xid[] recover(int flag); void rollback(Xid xid); boolean setTransactionTimeout(int seconds); void start(Xid xid, int flag); }### Answer: @Test public void testSetTransactionTimeout() throws XAException { reset(res); expect(res.setTransactionTimeout(42)).andReturn(true); replay(res); assertTrue(cx.setTransactionTimeout(42)); verify(res); }
### Question: ClientXAResource implements XAResource { public void start(Xid xid, int flag) throws XAException { try { remote.start(serialized(xid), flag); } catch (RemoteException e) { throw (XAException) new XAException("RMI faiure").initCause(e); } } ClientXAResource(RemoteXAResource xaRes, ClientTransactionListener txn); void commit(Xid xid, boolean onePhase); void end(Xid xid, int flags); void forget(Xid xid); int getTransactionTimeout(); boolean isSameRM(XAResource xa); int prepare(Xid xid); Xid[] recover(int flag); void rollback(Xid xid); boolean setTransactionTimeout(int seconds); void start(Xid xid, int flag); }### Answer: @Test public void testStart() throws XAException { reset(res); res.start(isA(SerializedXid.class), eq(42)); replay(res); cx.start(xid, 42); verify(res); }
### Question: ServerStore extends Exportable implements RemoteStore { public ServerStore(BlobStore store, Exporter exporter) throws RemoteException { super(exporter); this.store = store; } ServerStore(BlobStore store, Exporter exporter); @Override RemoteConnection openConnection(Map<String, String> hints); @Override RemoteTransactionListener startTransactionListener(Map<String, String> hints); }### Answer: @Test public void testServerStore() { assertTrue(ss.getExported() instanceof RemoteStore); }
### Question: ServerStore extends Exportable implements RemoteStore { @Override public RemoteConnection openConnection(Map<String, String> hints) throws IOException { return new ServerConnection(store.openConnection(null, hints), getExporter()); } ServerStore(BlobStore store, Exporter exporter); @Override RemoteConnection openConnection(Map<String, String> hints); @Override RemoteTransactionListener startTransactionListener(Map<String, String> hints); }### Answer: @Test public void testOpenConnection() throws Exception { BlobStoreConnection con = createMock(BlobStoreConnection.class); reset(store); expect(store.openConnection(null, null)).andReturn(con); expect(store.openConnection(null, null)).andThrow(new UnsupportedOperationException()); replay(store); RemoteConnection rc = ss.openConnection(null); assertTrue(rc instanceof ServerConnection); assertEquals(con, ((ServerConnection)rc).getConnection()); try { ss.openConnection(null); fail("Failed to rcv expected exception"); } catch (UnsupportedOperationException e) { } verify(store); }
### Question: ServerOutputStream extends UnicastExportable implements RemoteOutputStream, Closeable { public ServerOutputStream(OutputStream out, Exporter exporter) throws RemoteException { super(exporter); this.out = out; } ServerOutputStream(OutputStream out, Exporter exporter); void write(int b); void write(byte[] b); void write(PartialBuffer b); void flush(); void close(); @Override void unreferenced(); }### Answer: @Test public void testServerOutputStream() throws IOException { assertTrue(so.getExported() instanceof RemoteOutputStream); }
### Question: ServerOutputStream extends UnicastExportable implements RemoteOutputStream, Closeable { public void write(int b) throws IOException { out.write(b); } ServerOutputStream(OutputStream out, Exporter exporter); void write(int b); void write(byte[] b); void write(PartialBuffer b); void flush(); void close(); @Override void unreferenced(); }### Answer: @Test public void testWriteInt() throws IOException { bo.reset(); so.write('a'); byte[] b = bo.toByteArray(); assertNotNull(b); assertEquals(1, b.length); assertEquals('a', b[0]); } @Test public void testWriteByteArray() throws IOException { bo.reset(); byte[] in = "Hello World".getBytes(); so.write(in); byte[] b = bo.toByteArray(); assertNotNull(b); assertEquals(in, b); bo.reset(); in = new byte[0]; so.write(in); b = bo.toByteArray(); assertNotNull(b); assertEquals(in, b); } @Test public void testWritePartialBuffer() throws IOException { bo.reset(); byte[] in = "Hello World".getBytes(); PartialBuffer pb = new PartialBuffer(new byte[in.length * 3], in.length, in.length); System.arraycopy(in, 0, pb.getBuffer(), pb.getOffset(), pb.getLength()); so.write(pb); byte[] b = bo.toByteArray(); assertNotNull(b); assertEquals(in, b); }
### Question: AbstractMuxConnection extends AbstractBlobStoreConnection { protected BlobStoreConnection getConnection(BlobStore store, Map<String, String> hints) throws IOException { if (store == null) return null; if (cons == null) throw new IllegalStateException("Connection closed."); BlobStoreConnection con = cons.get(store.getId()); if (con == null) { con = store.openConnection(txn, hints); cons.put(store.getId(), con); } return con; } AbstractMuxConnection(BlobStore store, Transaction txn); abstract BlobStore getStore(URI blobId, Map<String, String> hints); Set<BlobStore> getStores(String prefix); @Override void close(); @Override Blob getBlob(URI blobId, Map<String, String> hints); @Override Iterator<URI> listBlobIds(String filterPrefix); @Override void sync(); }### Answer: @Test public void testGetConnection() throws IOException { BlobStoreConnection con1 = con.getConnection(store1, null); BlobStoreConnection con2 = con.getConnection(store2, null); assertEquals(2, con.getCons().size()); assertEquals(con.getCons().get(store1.getId()), con1); assertEquals(con.getCons().get(store2.getId()), con2); assertEquals(con1, con.getConnection(store1, null)); assertEquals(con2, con.getConnection(store2, null)); }
### Question: ServerOutputStream extends UnicastExportable implements RemoteOutputStream, Closeable { public void flush() throws IOException { out.flush(); } ServerOutputStream(OutputStream out, Exporter exporter); void write(int b); void write(byte[] b); void write(PartialBuffer b); void flush(); void close(); @Override void unreferenced(); }### Answer: @Test public void testFlush() throws IOException { final boolean[] flushed = new boolean[] { false }; OutputStream out = new OutputStream() { @Override public void flush() { flushed[0] = true; } @Override public void write(int b) throws IOException { } }; ServerOutputStream so = new ServerOutputStream(out, exporter); assertFalse(flushed[0]); so.flush(); assertTrue(flushed[0]); }
### Question: ServerOutputStream extends UnicastExportable implements RemoteOutputStream, Closeable { public void close() throws IOException { unExport(false); out.close(); } ServerOutputStream(OutputStream out, Exporter exporter); void write(int b); void write(byte[] b); void write(PartialBuffer b); void flush(); void close(); @Override void unreferenced(); }### Answer: @Test public void testClose() throws IOException { final boolean[] closed = new boolean[] { false, false }; OutputStream out = new OutputStream() { @Override public void close() throws IOException { closed[0] = true; } @Override public void write(int b) throws IOException { } }; ServerOutputStream so = new ServerOutputStream(out, exporter) { private static final long serialVersionUID = 1L; @Override public void unExport(boolean force) { closed[1] = true; super.unExport(force); } }; assertFalse(closed[0]); assertFalse(closed[1]); so.close(); assertTrue(closed[0]); assertTrue(closed[1]); }
### Question: ServerOutputStream extends UnicastExportable implements RemoteOutputStream, Closeable { @Override public void unreferenced() { try { close(); } catch (IOException e) { log.warn("Ignoring error in close of unreferenced OutputStream", e); } } ServerOutputStream(OutputStream out, Exporter exporter); void write(int b); void write(byte[] b); void write(PartialBuffer b); void flush(); void close(); @Override void unreferenced(); }### Answer: @Test public void testUnreferenced() throws IOException { final boolean[] closed = new boolean[] { false, false }; OutputStream out = new OutputStream() { @Override public void close() throws IOException { closed[0] = true; } @Override public void write(int b) throws IOException { } }; ServerOutputStream so = new ServerOutputStream(out, exporter) { private static final long serialVersionUID = 1L; @Override public void unExport(boolean force) { closed[1] = true; super.unExport(force); } }; assertFalse(closed[0]); assertFalse(closed[1]); so.unreferenced(); assertTrue(closed[0]); assertTrue(closed[1]); }
### Question: ServerInputStream extends UnicastExportable implements RemoteInputStream, Closeable { public ServerInputStream(InputStream in, Exporter exporter) throws RemoteException { super(exporter); this.in = in; } ServerInputStream(InputStream in, Exporter exporter); int read(); PartialBuffer read(int len); long skip(long n); void close(); @Override void unreferenced(); }### Answer: @Test public void testServerInputStream() throws IOException { ServerInputStream si = new ServerInputStream(bi, exporter); assertTrue(si.getExported() instanceof RemoteInputStream); si.close(); si = null; }
### Question: ServerInputStream extends UnicastExportable implements RemoteInputStream, Closeable { public int read() throws IOException { return in.read(); } ServerInputStream(InputStream in, Exporter exporter); int read(); PartialBuffer read(int len); long skip(long n); void close(); @Override void unreferenced(); }### Answer: @Test public void testRead() throws IOException { bi.reset(); try { si.read(-1); fail("Failed to throw exception for read with negative length"); } catch (Exception e) { } PartialBuffer pb = si.read(0); assertNotNull(pb); assertEquals(0, pb.getLength()); assertEquals(0, pb.getOffset()); pb = si.read(1); assertNotNull(pb); assertEquals(1, pb.getLength()); assertEquals(0, pb.getOffset()); assertEquals(buf[0], pb.getBuffer()[0]); pb = si.read(10); assertNotNull(pb); assertEquals(10, pb.getLength()); assertEquals(0, pb.getOffset()); for (int i = 0; i < 10; i++) assertEquals(buf[i + 1], pb.getBuffer()[i]); pb = si.read(10); assertNull(pb); } @Test public void testReadInt() throws IOException { bi.reset(); for (byte b : buf) assertEquals(b, si.read()); assertEquals(-1, si.read()); }
### Question: ServerInputStream extends UnicastExportable implements RemoteInputStream, Closeable { public long skip(long n) throws IOException { return in.skip(n); } ServerInputStream(InputStream in, Exporter exporter); int read(); PartialBuffer read(int len); long skip(long n); void close(); @Override void unreferenced(); }### Answer: @Test public void testSkip() throws IOException { bi.reset(); assertEquals(buf.length - 1, si.skip(buf.length - 1)); assertEquals(buf[buf.length - 1], si.read()); assertEquals(-1, si.read()); }
### Question: ServerInputStream extends UnicastExportable implements RemoteInputStream, Closeable { public void close() throws IOException { unExport(false); in.close(); } ServerInputStream(InputStream in, Exporter exporter); int read(); PartialBuffer read(int len); long skip(long n); void close(); @Override void unreferenced(); }### Answer: @Test public void testClose() throws IOException { final boolean[] closed = new boolean[] { false, false }; ByteArrayInputStream bi = new ByteArrayInputStream(buf) { @Override public void close() throws IOException { closed[0] = true; super.close(); } }; ServerInputStream si = new ServerInputStream(bi, exporter) { private static final long serialVersionUID = 1L; @Override public void unExport(boolean force) { closed[1] = true; super.unExport(force); } }; si.read(100); assertFalse(closed[0]); assertFalse(closed[1]); si.close(); assertTrue(closed[0]); assertTrue(closed[1]); }
### Question: AbstractMuxConnection extends AbstractBlobStoreConnection { @Override public Iterator<URI> listBlobIds(String filterPrefix) throws IOException { List<Iterator<URI>> iterators = new ArrayList<Iterator<URI>>(); for (BlobStore store : getStores(filterPrefix)) iterators.add(getConnection(store, null).listBlobIds(filterPrefix)); return Iterators.concat(iterators.iterator()); } AbstractMuxConnection(BlobStore store, Transaction txn); abstract BlobStore getStore(URI blobId, Map<String, String> hints); Set<BlobStore> getStores(String prefix); @Override void close(); @Override Blob getBlob(URI blobId, Map<String, String> hints); @Override Iterator<URI> listBlobIds(String filterPrefix); @Override void sync(); }### Answer: @Test public void testListBlobIds() throws IOException { for (Iterator<URI> it = con.listBlobIds(null); it.hasNext();) con.getBlob(it.next(), null).delete(); assertFalse(con.listBlobIds(null).hasNext()); Blob b1 = con.getBlob(null, s1Hint); Blob b2 = con.getBlob(null, s2Hint); b1.openOutputStream(0, true).close(); b2.openOutputStream(0, true).close(); Set<Blob> blobs = new HashSet<Blob>(); for (Iterator<URI> it = con.listBlobIds(null); it.hasNext();) blobs.add(con.getBlob(it.next(), null)); assertEquals(2, blobs.size()); assertTrue(blobs.contains(b1)); assertTrue(blobs.contains(b2)); for (Blob b : new Blob[] { b1, b2 }) { blobs = new HashSet<Blob>(); for (Iterator<URI> it = con.listBlobIds(b.getId().toString()); it.hasNext();) blobs.add(con.getBlob(it.next(), null)); assertEquals(1, blobs.size()); assertTrue(blobs.contains(b)); } }
### Question: ServerInputStream extends UnicastExportable implements RemoteInputStream, Closeable { @Override public void unreferenced() { try { close(); } catch (IOException e) { log.warn("Ignoring error in close of this unreferenced InputStream", e); } } ServerInputStream(InputStream in, Exporter exporter); int read(); PartialBuffer read(int len); long skip(long n); void close(); @Override void unreferenced(); }### Answer: @Test public void testUnreferenced() throws IOException { final boolean[] closed = new boolean[] { false, false }; ByteArrayInputStream bi = new ByteArrayInputStream(buf) { @Override public void close() throws IOException { closed[0] = true; super.close(); } }; ServerInputStream si = new ServerInputStream(bi, exporter) { private static final long serialVersionUID = 1L; @Override public void unExport(boolean force) { closed[1] = true; super.unExport(force); } }; si.read(100); assertFalse(closed[0]); assertFalse(closed[1]); si.unreferenced(); assertTrue(closed[0]); assertTrue(closed[1]); }
### Question: ServerConnection extends UnicastExportable implements RemoteConnection { public ServerConnection(BlobStoreConnection con, Exporter exporter) throws RemoteException { super(exporter); this.con = con; } ServerConnection(BlobStoreConnection con, Exporter exporter); @Override RemoteBlob getBlob(URI id, Map<String, String> hints); @Override RemoteBlobCreator getBlobCreator(long estimatedSize, Map<String, String> hints); @Override RemoteIterator<URI> listBlobIds(String filterPrefix); @Override void sync(); @Override void close(); @Override void unreferenced(); }### Answer: @Test public void testServerConnection() { assertTrue(sc.getExported() instanceof RemoteConnection); }
### Question: ServerConnection extends UnicastExportable implements RemoteConnection { @Override public RemoteBlob getBlob(URI id, Map<String, String> hints) throws IOException { return new ServerBlob(con.getBlob(id, hints), getExporter()); } ServerConnection(BlobStoreConnection con, Exporter exporter); @Override RemoteBlob getBlob(URI id, Map<String, String> hints); @Override RemoteBlobCreator getBlobCreator(long estimatedSize, Map<String, String> hints); @Override RemoteIterator<URI> listBlobIds(String filterPrefix); @Override void sync(); @Override void close(); @Override void unreferenced(); }### Answer: @Test public void testGetBlobURIMapOfStringString() throws IOException { URI id = URI.create("foo:bar"); Map<String, String> hints = new HashMap<String, String>(); hints.put("try harder?", "yes, of course!"); Blob blob = createMock(Blob.class); reset(con); expect(con.getBlob(id, hints)).andReturn(blob); expect(con.getBlob(id, hints)).andThrow(new UnsupportedIdException(id)); replay(con); RemoteBlob rb = sc.getBlob(id, hints); assertTrue(rb instanceof ServerBlob); assertEquals(blob, ((ServerBlob) rb).getBlob()); try { sc.getBlob(id, hints); fail("Failed to rcv expected exception"); } catch (UnsupportedIdException e) { assertEquals(id, e.getBlobId()); } verify(con); }
### Question: ServerConnection extends UnicastExportable implements RemoteConnection { @Override public RemoteBlobCreator getBlobCreator(long estimatedSize, Map<String, String> hints) throws IOException { return new ServerBlobCreator(con, estimatedSize, hints, getExporter()); } ServerConnection(BlobStoreConnection con, Exporter exporter); @Override RemoteBlob getBlob(URI id, Map<String, String> hints); @Override RemoteBlobCreator getBlobCreator(long estimatedSize, Map<String, String> hints); @Override RemoteIterator<URI> listBlobIds(String filterPrefix); @Override void sync(); @Override void close(); @Override void unreferenced(); }### Answer: @Test public void testGetBlobCreator() throws IOException { Map<String, String> hints = new HashMap<String, String>(); hints.put("try harder?", "yes, of course!"); Blob blob = createMock(Blob.class); reset(con); makeThreadSafe(con, true); expect(con.getBlob(isA(PipedInputStream.class), eq(42L), eq(hints))).andReturn(blob); expect(con.getBlob(isA(PipedInputStream.class), eq(-1L), eq(hints))) .andThrow(new UnsupportedOperationException()); replay(con); RemoteBlobCreator rbc = sc.getBlobCreator(42L, hints); assertTrue(rbc instanceof ServerBlobCreator); RemoteBlob rb = rbc.shutDown(false); assertTrue(rb instanceof ServerBlob); assertEquals(blob, ((ServerBlob)rb).getBlob()); try { sc.getBlobCreator(-1L, hints).shutDown(false); fail("Failed to rcv expected exception"); } catch (UnsupportedOperationException e) { } verify(con); }
### Question: ServerConnection extends UnicastExportable implements RemoteConnection { @Override public RemoteIterator<URI> listBlobIds(String filterPrefix) throws IOException { return new ServerIterator<URI>(con.listBlobIds(filterPrefix), getExporter()); } ServerConnection(BlobStoreConnection con, Exporter exporter); @Override RemoteBlob getBlob(URI id, Map<String, String> hints); @Override RemoteBlobCreator getBlobCreator(long estimatedSize, Map<String, String> hints); @Override RemoteIterator<URI> listBlobIds(String filterPrefix); @Override void sync(); @Override void close(); @Override void unreferenced(); }### Answer: @Test public void testListBlobIds() throws IOException { URI id = URI.create("foo:bar"); Iterator<URI> it = Arrays.asList(id).iterator(); reset(con); expect(con.listBlobIds(null)).andReturn(it); replay(con); RemoteIterator<URI> ri = sc.listBlobIds(null); assertTrue(ri instanceof ServerIterator<?>); assertEquals(it, ((ServerIterator<URI>) ri).getIterator()); verify(con); }
### Question: ServerConnection extends UnicastExportable implements RemoteConnection { @Override public void close() { unExport(false); con.close(); } ServerConnection(BlobStoreConnection con, Exporter exporter); @Override RemoteBlob getBlob(URI id, Map<String, String> hints); @Override RemoteBlobCreator getBlobCreator(long estimatedSize, Map<String, String> hints); @Override RemoteIterator<URI> listBlobIds(String filterPrefix); @Override void sync(); @Override void close(); @Override void unreferenced(); }### Answer: @Test public void testClose() throws RemoteException { ServerConnection sc = new ServerConnection(con, exporter); reset(con); con.close(); replay(con); assertNotNull(sc.getExported()); sc.close(); assertNull(sc.getExported()); verify(con); }
### Question: ServerConnection extends UnicastExportable implements RemoteConnection { @Override public void unreferenced() { close(); } ServerConnection(BlobStoreConnection con, Exporter exporter); @Override RemoteBlob getBlob(URI id, Map<String, String> hints); @Override RemoteBlobCreator getBlobCreator(long estimatedSize, Map<String, String> hints); @Override RemoteIterator<URI> listBlobIds(String filterPrefix); @Override void sync(); @Override void close(); @Override void unreferenced(); }### Answer: @Test public void testUnreferenced() throws RemoteException { ServerConnection sc = new ServerConnection(con, exporter); reset(con); con.close(); replay(con); assertNotNull(sc.getExported()); sc.unreferenced(); assertNull(sc.getExported()); verify(con); }
### Question: ServerBlob extends UnicastExportable implements RemoteBlob { public ServerBlob(Blob blob, Exporter exporter) throws RemoteException { super(exporter); this.blob = blob; } ServerBlob(Blob blob, Exporter exporter); @Override URI getId(); @Override URI getCanonicalId(); @Override boolean exists(); @Override long getSize(); @Override void delete(); @Override RemoteBlob moveTo(URI other, Map<String, String> hints); @Override RemoteInputStream openInputStream(); @Override RemoteOutputStream openOutputStream(long estimatedSize, boolean overwrite); }### Answer: @Test public void testServerBlob() { assertTrue(sb.getExported() instanceof RemoteBlob); }
### Question: ServerBlob extends UnicastExportable implements RemoteBlob { @Override public URI getId() { return blob.getId(); } ServerBlob(Blob blob, Exporter exporter); @Override URI getId(); @Override URI getCanonicalId(); @Override boolean exists(); @Override long getSize(); @Override void delete(); @Override RemoteBlob moveTo(URI other, Map<String, String> hints); @Override RemoteInputStream openInputStream(); @Override RemoteOutputStream openOutputStream(long estimatedSize, boolean overwrite); }### Answer: @Test public void testGetId() { URI uri = URI.create("urn:test:blob:id"); reset(blob); expect(blob.getId()).andReturn(uri); replay(blob); assertEquals(uri, sb.getId()); verify(blob); }
### Question: ServerBlob extends UnicastExportable implements RemoteBlob { @Override public boolean exists() throws IOException { return blob.exists(); } ServerBlob(Blob blob, Exporter exporter); @Override URI getId(); @Override URI getCanonicalId(); @Override boolean exists(); @Override long getSize(); @Override void delete(); @Override RemoteBlob moveTo(URI other, Map<String, String> hints); @Override RemoteInputStream openInputStream(); @Override RemoteOutputStream openOutputStream(long estimatedSize, boolean overwrite); }### Answer: @Test public void testExists() throws IOException { reset(blob); expect(blob.exists()).andReturn(true); replay(blob); assertTrue(sb.exists()); verify(blob); }
### Question: IdMappingBlob extends BlobWrapper { @Override public URI getCanonicalId() throws IOException { URI internalId = delegate.getCanonicalId(); if (internalId == null) return null; return mapper.getExternalId(internalId); } IdMappingBlob(BlobStoreConnection connection, Blob delegate, IdMapper mapper); @Override URI getCanonicalId(); @Override URI getId(); @Override Blob moveTo(URI blobId, Map<String, String> hints); }### Answer: @Test public void testGetCanonicalIdNotNull() throws Exception { assertNotNull(getTestBlob(blobId1, true).getCanonicalId()); } @Test public void testGetCanonicalIdNull() throws Exception { assertNull(getTestBlob(blobId1, false).getCanonicalId()); }
### Question: ServerBlob extends UnicastExportable implements RemoteBlob { @Override public long getSize() throws IOException { return blob.getSize(); } ServerBlob(Blob blob, Exporter exporter); @Override URI getId(); @Override URI getCanonicalId(); @Override boolean exists(); @Override long getSize(); @Override void delete(); @Override RemoteBlob moveTo(URI other, Map<String, String> hints); @Override RemoteInputStream openInputStream(); @Override RemoteOutputStream openOutputStream(long estimatedSize, boolean overwrite); }### Answer: @Test public void testGetSize() throws IOException { reset(blob); expect(blob.getSize()).andReturn(42L); replay(blob); assertEquals(42L, sb.getSize()); verify(blob); }
### Question: ServerBlob extends UnicastExportable implements RemoteBlob { @Override public void delete() throws IOException { blob.delete(); } ServerBlob(Blob blob, Exporter exporter); @Override URI getId(); @Override URI getCanonicalId(); @Override boolean exists(); @Override long getSize(); @Override void delete(); @Override RemoteBlob moveTo(URI other, Map<String, String> hints); @Override RemoteInputStream openInputStream(); @Override RemoteOutputStream openOutputStream(long estimatedSize, boolean overwrite); }### Answer: @Test public void testDelete() throws IOException { reset(blob); blob.delete(); replay(blob); sb.delete(); verify(blob); }
### Question: ServerBlob extends UnicastExportable implements RemoteBlob { @Override public RemoteBlob moveTo(URI other, Map<String, String> hints) throws IOException { return new ServerBlob(blob.moveTo(other, hints), getExporter()); } ServerBlob(Blob blob, Exporter exporter); @Override URI getId(); @Override URI getCanonicalId(); @Override boolean exists(); @Override long getSize(); @Override void delete(); @Override RemoteBlob moveTo(URI other, Map<String, String> hints); @Override RemoteInputStream openInputStream(); @Override RemoteOutputStream openOutputStream(long estimatedSize, boolean overwrite); }### Answer: @Test public void testMoveTo() throws IOException { URI id1 = URI.create("foo:1"); URI id2 = URI.create("foo:2"); BlobStoreConnection con = createMock(BlobStoreConnection.class); Blob blob2 = createMock(Blob.class); reset(blob); expect(blob.getConnection()).andStubReturn(con); expect(blob2.getConnection()).andStubReturn(con); expect(blob.getId()).andStubReturn(id1); expect(blob2.getId()).andStubReturn(id2); expect(con.getBlob(id1, null)).andStubReturn(blob); expect(con.getBlob(id2, null)).andStubReturn(blob2); expect(blob.moveTo(id1, null)).andStubThrow(new DuplicateBlobException(id1)); expect(blob.moveTo(id2, null)).andStubReturn(blob2); expect(blob.moveTo(null, null)).andStubThrow(new UnsupportedOperationException()); replay(blob); replay(blob2); replay(con); try { sb.moveTo(blob.getId(), null); fail("Failed to rcv expected exception"); } catch (DuplicateBlobException e) { } sb.moveTo(blob2.getId(), null); try { sb.moveTo(null, null); fail("Failed to rcv expected exception"); } catch (UnsupportedOperationException e) { } verify(blob); }
### Question: ServerBlob extends UnicastExportable implements RemoteBlob { @Override public RemoteInputStream openInputStream() throws IOException { return new ServerInputStream(blob.openInputStream(), getExporter()); } ServerBlob(Blob blob, Exporter exporter); @Override URI getId(); @Override URI getCanonicalId(); @Override boolean exists(); @Override long getSize(); @Override void delete(); @Override RemoteBlob moveTo(URI other, Map<String, String> hints); @Override RemoteInputStream openInputStream(); @Override RemoteOutputStream openOutputStream(long estimatedSize, boolean overwrite); }### Answer: @Test public void testOpenInputStream() throws IOException { URI id = URI.create("foo:bar"); InputStream in = createMock(InputStream.class); reset(blob); expect(blob.openInputStream()).andReturn(in); expect(blob.openInputStream()).andThrow(new MissingBlobException(id)); replay(blob); RemoteInputStream ri = sb.openInputStream(); assertTrue(ri instanceof ServerInputStream); assertEquals(((ServerInputStream) ri).getInputStream(), in); try { sb.openInputStream(); fail("Failed to rcv expected exception"); } catch (MissingBlobException e) { assertEquals(id, e.getBlobId()); } verify(blob); }
### Question: ServerBlob extends UnicastExportable implements RemoteBlob { @Override public RemoteOutputStream openOutputStream(long estimatedSize, boolean overwrite) throws IOException { return new ServerOutputStream(blob.openOutputStream(estimatedSize, overwrite), getExporter()); } ServerBlob(Blob blob, Exporter exporter); @Override URI getId(); @Override URI getCanonicalId(); @Override boolean exists(); @Override long getSize(); @Override void delete(); @Override RemoteBlob moveTo(URI other, Map<String, String> hints); @Override RemoteInputStream openInputStream(); @Override RemoteOutputStream openOutputStream(long estimatedSize, boolean overwrite); }### Answer: @Test public void testOpenOutputStream() throws IOException { URI id = URI.create("foo:bar"); OutputStream out = createMock(OutputStream.class); reset(blob); expect(blob.openOutputStream(42L, true)).andReturn(out); expect(blob.openOutputStream(-1L, true)).andThrow(new MissingBlobException(id)); replay(blob); RemoteOutputStream ro = sb.openOutputStream(42L, true); assertTrue(ro instanceof ServerOutputStream); assertEquals(((ServerOutputStream) ro).getOutputStream(), out); try { sb.openOutputStream(-1L, true); fail("Failed to rcv expected exception"); } catch (MissingBlobException e) { assertEquals(id, e.getBlobId()); } verify(blob); }
### Question: ServerTransactionListener extends UnicastExportable implements Transaction, RemoteTransactionListener { public void commit() throws RollbackException, HeuristicMixedException, HeuristicRollbackException, SecurityException, IllegalStateException, SystemException { throw new SecurityException("Cannot commit from this JVM. Transaction is on remote client."); } ServerTransactionListener(final BlobStore store, final Map<String, String> hints, Exporter exporter); Operation<?> getNextOperation(); void postResult(Result<?> result); void commit(); boolean delistResource(XAResource xaRes, int flags); boolean enlistResource(XAResource xaRes); int getStatus(); void registerSynchronization(Synchronization sync); void rollback(); void setRollbackOnly(); XAResource getXAResource(RemoteXAResource res); }### Answer: @Test(expectedExceptions = {SecurityException.class}) public void testCommit() throws SecurityException, IllegalStateException, RollbackException, HeuristicMixedException, HeuristicRollbackException, SystemException, RemoteException { st.commit(); }
### Question: ServerTransactionListener extends UnicastExportable implements Transaction, RemoteTransactionListener { public int getStatus() throws SystemException { try { return executeOnClient(new GetStatus()); } catch (ExecutionException e) { Throwable cause = e.getCause(); if (cause instanceof SystemException) throw (SystemException) cause; if (cause instanceof RuntimeException) throw (RuntimeException) cause; throw new RuntimeException("Error reported by server", cause); } } ServerTransactionListener(final BlobStore store, final Map<String, String> hints, Exporter exporter); Operation<?> getNextOperation(); void postResult(Result<?> result); void commit(); boolean delistResource(XAResource xaRes, int flags); boolean enlistResource(XAResource xaRes); int getStatus(); void registerSynchronization(Synchronization sync); void rollback(); void setRollbackOnly(); XAResource getXAResource(RemoteXAResource res); }### Answer: @Test public void testGetStatus() throws Exception { Future<Integer> future = executor.submit(new Callable<Integer>() { public Integer call() throws Exception { return st.getStatus(); } }); Operation<?> op = st.getNextOperation(); assertTrue(op instanceof GetStatus); st.postResult(new Result<Integer>(42)); assertEquals(new Integer(42), future.get()); }
### Question: ServerTransactionListener extends UnicastExportable implements Transaction, RemoteTransactionListener { public void registerSynchronization(Synchronization sync) throws RollbackException, IllegalStateException, SystemException { RemoteSynchronization rsync; try { rsync = new ServerSynchronization(sync, getExporter()); } catch (RemoteException e) { throw (SystemException) new SystemException("Failed to export Synchronization").initCause(e); } try { executeOnClient(new RegisterSynchronization(rsync)); } catch (ExecutionException e) { Throwable cause = e.getCause(); if (cause instanceof RollbackException) throw (RollbackException) cause; if (cause instanceof SystemException) throw (SystemException) cause; if (cause instanceof RuntimeException) throw (RuntimeException) cause; throw new RuntimeException("Error reported by server", cause); } } ServerTransactionListener(final BlobStore store, final Map<String, String> hints, Exporter exporter); Operation<?> getNextOperation(); void postResult(Result<?> result); void commit(); boolean delistResource(XAResource xaRes, int flags); boolean enlistResource(XAResource xaRes); int getStatus(); void registerSynchronization(Synchronization sync); void rollback(); void setRollbackOnly(); XAResource getXAResource(RemoteXAResource res); }### Answer: @Test public void testRegisterSynchronization() throws Exception { final Synchronization sync = createMock(Synchronization.class); Future<Void> future = executor.submit(new Callable<Void>() { public Void call() throws Exception { st.registerSynchronization(sync); return null; } }); Operation<?> op = st.getNextOperation(); assertTrue(op instanceof RegisterSynchronization); RemoteSynchronization rsync = ((RegisterSynchronization) op).getSynchronization(); assertTrue(rsync instanceof ServerSynchronization); assertEquals(sync, ((ServerSynchronization) rsync).getSynchronization()); st.postResult(new Result<Void>()); assertNull(future.get()); }
### Question: ServerTransactionListener extends UnicastExportable implements Transaction, RemoteTransactionListener { public void rollback() throws IllegalStateException, SystemException { try { executeOnClient(new Rollback()); } catch (ExecutionException e) { Throwable cause = e.getCause(); if (cause instanceof SystemException) throw (SystemException) cause; if (cause instanceof RuntimeException) throw (RuntimeException) cause; throw new RuntimeException("Error reported by server", cause); } } ServerTransactionListener(final BlobStore store, final Map<String, String> hints, Exporter exporter); Operation<?> getNextOperation(); void postResult(Result<?> result); void commit(); boolean delistResource(XAResource xaRes, int flags); boolean enlistResource(XAResource xaRes); int getStatus(); void registerSynchronization(Synchronization sync); void rollback(); void setRollbackOnly(); XAResource getXAResource(RemoteXAResource res); }### Answer: @Test public void testRollback() throws Exception { Future<Void> future = executor.submit(new Callable<Void>() { public Void call() throws Exception { st.rollback(); return null; } }); Operation<?> op = st.getNextOperation(); assertTrue(op instanceof Rollback); st.postResult(new Result<Void>()); assertNull(future.get()); }
### Question: ServerTransactionListener extends UnicastExportable implements Transaction, RemoteTransactionListener { public void setRollbackOnly() throws IllegalStateException, SystemException { throw new IllegalStateException("Cannot change the transaction state from this JVM. " + "Transaction is on remote client."); } ServerTransactionListener(final BlobStore store, final Map<String, String> hints, Exporter exporter); Operation<?> getNextOperation(); void postResult(Result<?> result); void commit(); boolean delistResource(XAResource xaRes, int flags); boolean enlistResource(XAResource xaRes); int getStatus(); void registerSynchronization(Synchronization sync); void rollback(); void setRollbackOnly(); XAResource getXAResource(RemoteXAResource res); }### Answer: @Test(expectedExceptions = {IllegalStateException.class}) public void testSetRollbackOnly() throws RemoteException, SystemException { st.setRollbackOnly(); }
### Question: ServerXAResource extends UnicastExportable implements RemoteXAResource { public ServerXAResource(XAResource xaRes, ServerTransactionListener txnListener, Exporter exporter) throws RemoteException { super(exporter); this.xaRes = xaRes; this.txnListener = txnListener; } ServerXAResource(XAResource xaRes, ServerTransactionListener txnListener, Exporter exporter); void commit(Xid xid, boolean onePhase); void end(Xid xid, int flags); void forget(Xid xid); int getTransactionTimeout(); boolean isSameRM(RemoteXAResource remote); int prepare(Xid xid); Xid[] recover(int flag); void rollback(Xid xid); boolean setTransactionTimeout(int seconds); void start(Xid xid, int flags); }### Answer: @Test public void testServerXAResource() { assertTrue(sx.getExported() instanceof RemoteXAResource); }
### Question: ServerXAResource extends UnicastExportable implements RemoteXAResource { public void commit(Xid xid, boolean onePhase) throws XAException { xaRes.commit(xid, onePhase); } ServerXAResource(XAResource xaRes, ServerTransactionListener txnListener, Exporter exporter); void commit(Xid xid, boolean onePhase); void end(Xid xid, int flags); void forget(Xid xid); int getTransactionTimeout(); boolean isSameRM(RemoteXAResource remote); int prepare(Xid xid); Xid[] recover(int flag); void rollback(Xid xid); boolean setTransactionTimeout(int seconds); void start(Xid xid, int flags); }### Answer: @Test public void testCommit() throws RemoteException, XAException { reset(res); res.commit(xid, false); replay(res); sx.commit(xid, false); verify(res); }
### Question: ServerXAResource extends UnicastExportable implements RemoteXAResource { public void end(Xid xid, int flags) throws XAException { xaRes.end(xid, flags); } ServerXAResource(XAResource xaRes, ServerTransactionListener txnListener, Exporter exporter); void commit(Xid xid, boolean onePhase); void end(Xid xid, int flags); void forget(Xid xid); int getTransactionTimeout(); boolean isSameRM(RemoteXAResource remote); int prepare(Xid xid); Xid[] recover(int flag); void rollback(Xid xid); boolean setTransactionTimeout(int seconds); void start(Xid xid, int flags); }### Answer: @Test public void testEnd() throws RemoteException, XAException { reset(res); res.end(xid, 42); replay(res); sx.end(xid, 42); verify(res); }
### Question: ServerXAResource extends UnicastExportable implements RemoteXAResource { public void forget(Xid xid) throws XAException { xaRes.forget(xid); } ServerXAResource(XAResource xaRes, ServerTransactionListener txnListener, Exporter exporter); void commit(Xid xid, boolean onePhase); void end(Xid xid, int flags); void forget(Xid xid); int getTransactionTimeout(); boolean isSameRM(RemoteXAResource remote); int prepare(Xid xid); Xid[] recover(int flag); void rollback(Xid xid); boolean setTransactionTimeout(int seconds); void start(Xid xid, int flags); }### Answer: @Test public void testForget() throws RemoteException, XAException { reset(res); res.forget(xid); replay(res); sx.forget(xid); verify(res); }
### Question: ServerXAResource extends UnicastExportable implements RemoteXAResource { public int getTransactionTimeout() throws XAException { return xaRes.getTransactionTimeout(); } ServerXAResource(XAResource xaRes, ServerTransactionListener txnListener, Exporter exporter); void commit(Xid xid, boolean onePhase); void end(Xid xid, int flags); void forget(Xid xid); int getTransactionTimeout(); boolean isSameRM(RemoteXAResource remote); int prepare(Xid xid); Xid[] recover(int flag); void rollback(Xid xid); boolean setTransactionTimeout(int seconds); void start(Xid xid, int flags); }### Answer: @Test public void testGetTransactionTimeout() throws RemoteException, XAException { reset(res); expect(res.getTransactionTimeout()).andReturn(42); replay(res); assertEquals(42, sx.getTransactionTimeout()); verify(res); }
### Question: ServerXAResource extends UnicastExportable implements RemoteXAResource { public boolean isSameRM(RemoteXAResource remote) throws XAException { XAResource local = (remote == null) ? null : txnListener.getXAResource(remote); return (local == null) ? false : xaRes.isSameRM(local); } ServerXAResource(XAResource xaRes, ServerTransactionListener txnListener, Exporter exporter); void commit(Xid xid, boolean onePhase); void end(Xid xid, int flags); void forget(Xid xid); int getTransactionTimeout(); boolean isSameRM(RemoteXAResource remote); int prepare(Xid xid); Xid[] recover(int flag); void rollback(Xid xid); boolean setTransactionTimeout(int seconds); void start(Xid xid, int flags); }### Answer: @Test public void testIsSameRM() throws RemoteException, XAException { reset(res); expect(res.isSameRM(res)).andReturn(true); expect(txn.getXAResource(sx)).andReturn(res); expect(txn.getXAResource(isA(ServerXAResource.class))).andReturn(null); replay(res); replay(txn); assertTrue(sx.isSameRM(sx)); assertFalse(sx.isSameRM(null)); assertFalse(sx.isSameRM(new ServerXAResource(res, txn, exporter))); verify(res); verify(txn); }
### Question: ServerXAResource extends UnicastExportable implements RemoteXAResource { public int prepare(Xid xid) throws XAException { return xaRes.prepare(xid); } ServerXAResource(XAResource xaRes, ServerTransactionListener txnListener, Exporter exporter); void commit(Xid xid, boolean onePhase); void end(Xid xid, int flags); void forget(Xid xid); int getTransactionTimeout(); boolean isSameRM(RemoteXAResource remote); int prepare(Xid xid); Xid[] recover(int flag); void rollback(Xid xid); boolean setTransactionTimeout(int seconds); void start(Xid xid, int flags); }### Answer: @Test public void testPrepare() throws RemoteException, XAException { reset(res); expect(res.prepare(xid)).andReturn(XAResource.XA_RDONLY); replay(res); assertEquals(XAResource.XA_RDONLY, sx.prepare(xid)); verify(res); }
### Question: ServerXAResource extends UnicastExportable implements RemoteXAResource { public Xid[] recover(int flag) throws XAException { Xid[] ids = xaRes.recover(flag); if (ids == null) return new Xid[0]; for (int i = 0; i < ids.length; i++) if ((ids[i] != null) && !(ids[i] instanceof Serializable)) ids[i] = new SerializedXid(ids[i]); return ids; } ServerXAResource(XAResource xaRes, ServerTransactionListener txnListener, Exporter exporter); void commit(Xid xid, boolean onePhase); void end(Xid xid, int flags); void forget(Xid xid); int getTransactionTimeout(); boolean isSameRM(RemoteXAResource remote); int prepare(Xid xid); Xid[] recover(int flag); void rollback(Xid xid); boolean setTransactionTimeout(int seconds); void start(Xid xid, int flags); }### Answer: @Test public void testRecover() throws RemoteException, XAException { reset(res); expect(res.recover(42)).andReturn(new Xid[] { xid }); replay(res); assertEquals(new Xid[] { xid }, sx.recover(42)); verify(res); }
### Question: ServerXAResource extends UnicastExportable implements RemoteXAResource { public void rollback(Xid xid) throws XAException { xaRes.rollback(xid); } ServerXAResource(XAResource xaRes, ServerTransactionListener txnListener, Exporter exporter); void commit(Xid xid, boolean onePhase); void end(Xid xid, int flags); void forget(Xid xid); int getTransactionTimeout(); boolean isSameRM(RemoteXAResource remote); int prepare(Xid xid); Xid[] recover(int flag); void rollback(Xid xid); boolean setTransactionTimeout(int seconds); void start(Xid xid, int flags); }### Answer: @Test public void testRollback() throws RemoteException, XAException { reset(res); res.rollback(xid); replay(res); sx.rollback(xid); verify(res); }
### Question: ServerXAResource extends UnicastExportable implements RemoteXAResource { public boolean setTransactionTimeout(int seconds) throws XAException { return xaRes.setTransactionTimeout(seconds); } ServerXAResource(XAResource xaRes, ServerTransactionListener txnListener, Exporter exporter); void commit(Xid xid, boolean onePhase); void end(Xid xid, int flags); void forget(Xid xid); int getTransactionTimeout(); boolean isSameRM(RemoteXAResource remote); int prepare(Xid xid); Xid[] recover(int flag); void rollback(Xid xid); boolean setTransactionTimeout(int seconds); void start(Xid xid, int flags); }### Answer: @Test public void testSetTransactionTimeout() throws RemoteException, XAException { reset(res); expect(res.setTransactionTimeout(42)).andReturn(true); replay(res); assertTrue(sx.setTransactionTimeout(42)); verify(res); }
### Question: ClientStore extends AbstractBlobStore { @Override public BlobStoreConnection openConnection(Transaction tx, Map<String, String> hints) throws UnsupportedOperationException, IOException { RemoteConnection con = (tx == null) ? server.openConnection(hints) : new ClientTransactionListener(server.startTransactionListener(hints), tx).getConnection(); return new ClientConnection(this, streamManager, con); } ClientStore(URI localId, RemoteStore server); @Override BlobStoreConnection openConnection(Transaction tx, Map<String, String> hints); }### Answer: @SuppressWarnings("unchecked") @Test public void testOpenConnection() throws IOException { BlobStoreConnection con = createMock(BlobStoreConnection.class); Transaction tx = createMock(Transaction.class); reset(store); makeThreadSafe(store, true); expect(store.openConnection(null, null)).andThrow(new UnsupportedOperationException()); expect(store.openConnection(isA(ServerTransactionListener.class), (Map) isNull())). andReturn(con); replay(store); try { cs.openConnection(null, null); fail("Failed to rcv expected exception"); } catch (UnsupportedOperationException e) { } BlobStoreConnection rc = cs.openConnection(tx, null); assertTrue(rc instanceof ClientConnection); verify(store); }
### Question: ServerXAResource extends UnicastExportable implements RemoteXAResource { public void start(Xid xid, int flags) throws XAException { xaRes.start(xid, flags); } ServerXAResource(XAResource xaRes, ServerTransactionListener txnListener, Exporter exporter); void commit(Xid xid, boolean onePhase); void end(Xid xid, int flags); void forget(Xid xid); int getTransactionTimeout(); boolean isSameRM(RemoteXAResource remote); int prepare(Xid xid); Xid[] recover(int flag); void rollback(Xid xid); boolean setTransactionTimeout(int seconds); void start(Xid xid, int flags); }### Answer: @Test public void testStart() throws RemoteException, XAException { reset(res); res.start(xid, 42); replay(res); sx.start(xid, 42); verify(res); }
### Question: ServerIterator extends UnicastExportable implements RemoteIterator<T> { public ServerIterator(Iterator<T> it, Exporter exporter) throws RemoteException { super(exporter); this.it = it; } ServerIterator(Iterator<T> it, Exporter exporter); List<T> next(int count); }### Answer: @Test public void testServerIterator() throws RemoteException { ServerIterator<URI> it = new ServerIterator<URI>(new ArrayList<URI>().iterator(), exporter); assertTrue(it.getExported() instanceof RemoteIterator<?>); }
### Question: ServerIterator extends UnicastExportable implements RemoteIterator<T> { public List<T> next(int count) throws RemoteException { if (count <= 0) throw new IllegalArgumentException("Invalid batch-size: " + count); List<T> items = new ArrayList<T>(count); while ((count-- > 0) && it.hasNext()) items.add(it.next()); if (items.isEmpty()) unExport(false); return items; } ServerIterator(Iterator<T> it, Exporter exporter); List<T> next(int count); }### Answer: @Test public void testNext() throws RemoteException { List<String> l = Arrays.asList("quick", "brown", "fox"); ServerIterator<String> it = new ServerIterator<String>(l.iterator(), exporter); for (int v : new int[] { -1, 0 }) { try { it.next(-1); fail("Failed to recieve expected exception for batch-size: " + v); } catch (Exception e) { } } List<String> n = it.next(10); assertEquals(l, n); n = it.next(10); assertTrue(n.isEmpty()); } @Test public void testUnExport() throws RemoteException { List<String> l = Arrays.asList("quick", "brown", "fox"); final boolean[] closed = new boolean[] { false }; ServerIterator<String> it = new ServerIterator<String>(l.iterator(), exporter) { private static final long serialVersionUID = 1L; @Override public void unExport(boolean force) { closed[0] = true; super.unExport(force); } }; assertFalse(closed[0]); for (String v : l) { assertEquals(v, it.next(1).get(0)); assertFalse(closed[0]); } assertTrue(it.next(1).isEmpty()); assertTrue(closed[0]); }
### Question: ServerSynchronization extends UnicastExportable implements RemoteSynchronization { public ServerSynchronization(Synchronization sync, Exporter exporter) throws RemoteException { super(exporter); this.sync = sync; } ServerSynchronization(Synchronization sync, Exporter exporter); void afterCompletion(int status); void beforeCompletion(); }### Answer: @Test public void testServerSynchronization() { assertTrue(ss.getExported() instanceof RemoteSynchronization); }
### Question: ServerSynchronization extends UnicastExportable implements RemoteSynchronization { public void afterCompletion(int status) { sync.afterCompletion(status); } ServerSynchronization(Synchronization sync, Exporter exporter); void afterCompletion(int status); void beforeCompletion(); }### Answer: @Test public void testAfterCompletion() throws RemoteException { reset(sync); sync.afterCompletion(42); replay(sync); ss.afterCompletion(42); verify(sync); }