TransactionBackendHandlerFactory { public static TextProtocolBackendHandler newInstance(final String sql, final TCLStatement tclStatement, final BackendConnection backendConnection) { if (tclStatement instanceof BeginTransactionStatement) { return new TransactionBackendHandler(TransactionOperationType.BEGIN, backendConnection); } if (tclStatement instanceof SetAutoCommitStatement) { if (((SetAutoCommitStatement) tclStatement).isAutoCommit()) { return backendConnection.getTransactionStatus().isInTransaction() ? new TransactionBackendHandler(TransactionOperationType.COMMIT, backendConnection) : new SkipBackendHandler(); } return new TransactionBackendHandler(TransactionOperationType.BEGIN, backendConnection); } if (tclStatement instanceof CommitStatement) { return new TransactionBackendHandler(TransactionOperationType.COMMIT, backendConnection); } if (tclStatement instanceof RollbackStatement) { return new TransactionBackendHandler(TransactionOperationType.ROLLBACK, backendConnection); } return new BroadcastBackendHandler(sql, tclStatement, backendConnection); }  static TextProtocolBackendHandler newInstance(final String sql, final TCLStatement tclStatement, final BackendConnection backendConnection);  }
@Test public void assertTransactionBackendHandlerReturnedWhenTCLStatementInstanceOfCommitStatement() { BackendConnection backendConnection = mock(BackendConnection.class, Answers.RETURNS_DEEP_STUBS); TextProtocolBackendHandler textProtocolBackendHandler = TransactionBackendHandlerFactory.newInstance(null, mock(CommitStatement.class), backendConnection); assertThat(textProtocolBackendHandler, instanceOf(TransactionBackendHandler.class)); TransactionBackendHandler transactionBackendHandler = (TransactionBackendHandler) textProtocolBackendHandler; assertFieldOfInstance(transactionBackendHandler, "operationType", is(TransactionOperationType.COMMIT)); assertFieldOfInstance(getBackendTransactionManager(transactionBackendHandler), "connection", is(backendConnection)); }
@Test public void assertTransactionBackendHandlerReturnedWhenTCLStatementInstanceOfRollbackStatement() { BackendConnection backendConnection = mock(BackendConnection.class, Answers.RETURNS_DEEP_STUBS); TextProtocolBackendHandler textProtocolBackendHandler = TransactionBackendHandlerFactory.newInstance(null, mock(RollbackStatement.class), backendConnection); assertThat(textProtocolBackendHandler, instanceOf(TransactionBackendHandler.class)); TransactionBackendHandler transactionBackendHandler = (TransactionBackendHandler) textProtocolBackendHandler; assertFieldOfInstance(transactionBackendHandler, "operationType", is(TransactionOperationType.ROLLBACK)); assertFieldOfInstance(getBackendTransactionManager(transactionBackendHandler), "connection", is(backendConnection)); }
@Test public void assertBroadcastBackendHandlerReturnedWhenTCLStatementNotHit() { assertThat(TransactionBackendHandlerFactory.newInstance(null, mock(TCLStatement.class), null), instanceOf(BroadcastBackendHandler.class)); }