@Test public void nonClosingInputStream() throws Exception { InputStream source = mock(InputStream.class); InputStream nonClosing = StreamUtils.nonClosing(source); nonClosing.read(); nonClosing.read(bytes); nonClosing.read(bytes, 1, 2); nonClosing.close(); InOrder ordered = inOrder(source); ordered.verify(source).read(); ordered.verify(source).read(bytes, 0, bytes.length); ordered.verify(source).read(bytes, 1, 2); ordered.verify(source, never()).close(); }
@Test public void nonClosingOutputStream() throws Exception { OutputStream source = mock(OutputStream.class); OutputStream nonClosing = StreamUtils.nonClosing(source); nonClosing.write(1); nonClosing.write(bytes); nonClosing.write(bytes, 1, 2); nonClosing.close(); InOrder ordered = inOrder(source); ordered.verify(source).write(1); ordered.verify(source).write(bytes, 0, bytes.length); ordered.verify(source).write(bytes, 1, 2); ordered.verify(source, never()).close(); } }
@Test public void marshalSaxResult() throws Exception { ContentHandler contentHandler = mock(ContentHandler.class); SAXResult result = new SAXResult(contentHandler); marshaller.marshal(flight, result); InOrder ordered = inOrder(contentHandler); ordered.verify(contentHandler).startDocument(); ordered.verify(contentHandler).startElement(eq(""), eq("flight"), eq("flight"), isA(Attributes.class)); ordered.verify(contentHandler).startElement(eq(""), eq("flightNumber"), eq("flightNumber"), isA(Attributes.class)); ordered.verify(contentHandler).characters(isA(char[].class), eq(0), eq(2)); ordered.verify(contentHandler).endElement("", "flightNumber", "flightNumber"); ordered.verify(contentHandler).endElement("", "flight", "flight"); ordered.verify(contentHandler).endDocument(); }
@Test public void testBatchUpdateWithInClause() throws Exception { @SuppressWarnings("unchecked") Map<String, Object>[] parameters = new Map[2]; parameters[0] = Collections.singletonMap("ids", Arrays.asList(1, 2)); parameters[1] = Collections.singletonMap("ids", Arrays.asList(3, 4)); final int[] rowsAffected = new int[] {1, 2}; given(preparedStatement.executeBatch()).willReturn(rowsAffected); given(connection.getMetaData()).willReturn(databaseMetaData); JdbcTemplate template = new JdbcTemplate(dataSource, false); namedParameterTemplate = new NamedParameterJdbcTemplate(template); int[] actualRowsAffected = namedParameterTemplate.batchUpdate( "delete sometable where id in (:ids)", parameters ); assertEquals("executed 2 updates", 2, actualRowsAffected.length); InOrder inOrder = inOrder(preparedStatement); inOrder.verify(preparedStatement).setObject(1, 1); inOrder.verify(preparedStatement).setObject(2, 2); inOrder.verify(preparedStatement).addBatch(); inOrder.verify(preparedStatement).setObject(1, 3); inOrder.verify(preparedStatement).setObject(2, 4); inOrder.verify(preparedStatement).addBatch(); inOrder.verify(preparedStatement, atLeastOnce()).close(); verify(connection, atLeastOnce()).close(); }
@Test public void marshalSAXResult() throws Exception { ContentHandler contentHandler = mock(ContentHandler.class); SAXResult result = new SAXResult(contentHandler); marshaller.marshal(flights, result); InOrder ordered = inOrder(contentHandler); ordered.verify(contentHandler).setDocumentLocator(isA(Locator.class)); ordered.verify(contentHandler).startDocument(); ordered.verify(contentHandler).startPrefixMapping("", "http://samples.springframework.org/flight"); ordered.verify(contentHandler).startElement(eq("http://samples.springframework.org/flight"), eq("flights"), eq("flights"), isA(Attributes.class)); ordered.verify(contentHandler).startElement(eq("http://samples.springframework.org/flight"), eq("flight"), eq("flight"), isA(Attributes.class)); ordered.verify(contentHandler).startElement(eq("http://samples.springframework.org/flight"), eq("number"), eq("number"), isA(Attributes.class)); ordered.verify(contentHandler).characters(isA(char[].class), eq(0), eq(2)); ordered.verify(contentHandler).endElement("http://samples.springframework.org/flight", "number", "number"); ordered.verify(contentHandler).endElement("http://samples.springframework.org/flight", "flight", "flight"); ordered.verify(contentHandler).endElement("http://samples.springframework.org/flight", "flights", "flights"); ordered.verify(contentHandler).endPrefixMapping(""); ordered.verify(contentHandler).endDocument(); }
@Test public void testTransactionWithExceptionOnRollback() throws Exception { given(con.getAutoCommit()).willReturn(true); willThrow(new SQLException("Cannot rollback")).given(con).rollback(); TransactionTemplate tt = new TransactionTemplate(tm); try { tt.execute(new TransactionCallbackWithoutResult() { @Override protected void doInTransactionWithoutResult(TransactionStatus status) throws RuntimeException { status.setRollbackOnly(); } }); fail("Should have thrown TransactionSystemException"); } catch (TransactionSystemException ex) { // expected } assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds)); InOrder ordered = inOrder(con); ordered.verify(con).setAutoCommit(false); ordered.verify(con).rollback(); ordered.verify(con).setAutoCommit(true); verify(con).close(); }
@Test public void testTransactionAwareDataSourceProxy() throws Exception { given(con.getAutoCommit()).willReturn(true); TransactionTemplate tt = new TransactionTemplate(tm); assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds)); tt.execute(new TransactionCallbackWithoutResult() { @Override protected void doInTransactionWithoutResult(TransactionStatus status) { // something transactional assertEquals(con, DataSourceUtils.getConnection(ds)); TransactionAwareDataSourceProxy dsProxy = new TransactionAwareDataSourceProxy(ds); try { assertEquals(con, ((ConnectionProxy) dsProxy.getConnection()).getTargetConnection()); // should be ignored dsProxy.getConnection().close(); } catch (SQLException ex) { throw new UncategorizedSQLException("", "", ex); } } }); assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds)); InOrder ordered = inOrder(con); ordered.verify(con).setAutoCommit(false); ordered.verify(con).commit(); ordered.verify(con).setAutoCommit(true); verify(con).close(); }
@Test public void testTransactionWithIsolationAndReadOnly() throws Exception { given(con.getTransactionIsolation()).willReturn(Connection.TRANSACTION_READ_COMMITTED); given(con.getAutoCommit()).willReturn(true); TransactionTemplate tt = new TransactionTemplate(tm); tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW); tt.setIsolationLevel(TransactionDefinition.ISOLATION_SERIALIZABLE); tt.setReadOnly(true); assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds)); tt.execute(new TransactionCallbackWithoutResult() { @Override protected void doInTransactionWithoutResult(TransactionStatus status) { assertTrue(TransactionSynchronizationManager.isCurrentTransactionReadOnly()); assertTrue(TransactionSynchronizationManager.isActualTransactionActive()); // something transactional } }); assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds)); InOrder ordered = inOrder(con); ordered.verify(con).setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE); ordered.verify(con).setAutoCommit(false); ordered.verify(con).commit(); ordered.verify(con).setAutoCommit(true); ordered.verify(con).setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED); verify(con).close(); }
@Test public void testTransactionWithEnforceReadOnly() throws Exception { tm.setEnforceReadOnly(true); given(con.getAutoCommit()).willReturn(true); Statement stmt = mock(Statement.class); given(con.createStatement()).willReturn(stmt); TransactionTemplate tt = new TransactionTemplate(tm); tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW); tt.setReadOnly(true); assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds)); tt.execute(new TransactionCallbackWithoutResult() { @Override protected void doInTransactionWithoutResult(TransactionStatus status) { assertTrue(TransactionSynchronizationManager.isCurrentTransactionReadOnly()); assertTrue(TransactionSynchronizationManager.isActualTransactionActive()); // something transactional } }); assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds)); InOrder ordered = inOrder(con, stmt); ordered.verify(con).setAutoCommit(false); ordered.verify(stmt).executeUpdate("SET TRANSACTION READ ONLY"); ordered.verify(stmt).close(); ordered.verify(con).commit(); ordered.verify(con).setAutoCommit(true); ordered.verify(con).close(); }
InOrder ordered = inOrder(con); ordered.verify(con).setAutoCommit(false); ordered.verify(con).commit();
InOrder ordered = inOrder(con); ordered.verify(con).setAutoCommit(false); ordered.verify(con).commit();
InOrder ordered = inOrder(con); ordered.verify(con).setAutoCommit(false); ordered.verify(con).commit();
InOrder ordered = inOrder(con); ordered.verify(con).setAutoCommit(false); ordered.verify(con).rollback();
verify(con).rollback(); InOrder ordered = inOrder(con); ordered.verify(con).setAutoCommit(false); ordered.verify(con).setAutoCommit(true);
private <Mock> Mock verify(Mock mock) { if (inOrder == null) { inOrder = BDDMockito.inOrder(mocks.toArray()); } return inOrder.verify(mock); }