@Test public void testWriteNoTransactionNoItems() throws Exception { when(fileSystem.createNewFile(new Path(fileName))).thenReturn(true); when(fileSystem.create(new Path(fileName))).thenReturn(fsDataOutputStream); writer.open(null); writer.write(new ArrayList<String>()); verifyZeroInteractions(fsDataOutputStream); }
@Test @SuppressWarnings("serial") public void testWriteNoTransaction() throws Exception { List<String> items = new ArrayList<String>() { { add(new String("one")); add(new String("two")); } }; when(fileSystem.createNewFile(new Path(fileName))).thenReturn(true); when(fileSystem.create(new Path(fileName))).thenReturn(fsDataOutputStream); writer.open(null); writer.write(items); ByteArrayOutputStream stream = new ByteArrayOutputStream(); getBytes(items.get(0), stream); getBytes(items.get(1), stream); verify(fsDataOutputStream).write(stream.toByteArray()); }
/** * A pointless use case but validates that the flag is still honored. * * @throws Exception */ @Test @SuppressWarnings("serial") public void testWriteTransactionReadOnly() throws Exception { final List<String> items = new ArrayList<String>() { { add(new String("one")); add(new String("two")); } }; when(fileSystem.createNewFile(new Path(fileName))).thenReturn(true); when(fileSystem.create(new Path(fileName))).thenReturn(fsDataOutputStream); writer.open(null); try { TransactionTemplate transactionTemplate = new TransactionTemplate(transactionManager); transactionTemplate.setReadOnly(true); transactionTemplate.execute( new TransactionCallback<Object>() { @Override public Object doInTransaction(TransactionStatus status) { try { writer.write(items); } catch (Exception e) { fail("An exception was thrown while writing: " + e.getMessage()); } return null; } }); } catch (Throwable t) { fail("Unexpected exception was thrown: " + t.getMessage()); } verifyZeroInteractions(fsDataOutputStream); }
@Test @SuppressWarnings("serial") public void testWriteTransaction() throws Exception { final List<String> items = new ArrayList<String>() { { add(new String("one")); add(new String("two")); } }; when(fileSystem.createNewFile(new Path(fileName))).thenReturn(true); when(fileSystem.create(new Path(fileName))).thenReturn(fsDataOutputStream); writer.open(null); new TransactionTemplate(transactionManager) .execute( new TransactionCallback<Object>() { @Override public Object doInTransaction(TransactionStatus status) { try { writer.write(items); } catch (Exception e) { fail("An exception was thrown while writing: " + e.getMessage()); } return null; } }); ByteArrayOutputStream stream = new ByteArrayOutputStream(); getBytes(items.get(0), stream); getBytes(items.get(1), stream); verify(fsDataOutputStream).write(stream.toByteArray()); }