--- /dev/null
+/*
+ * Copyright 2005-2010 the original author or authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.wamblee.wicket.transactions;
+
+import static junit.framework.Assert.*;
+import static org.mockito.Mockito.*;
+
+import javax.naming.InitialContext;
+import javax.transaction.Status;
+import javax.transaction.UserTransaction;
+
+import org.apache.wicket.Page;
+import org.apache.wicket.Request;
+import org.apache.wicket.RequestCycle;
+import org.apache.wicket.Response;
+import org.apache.wicket.WicketRuntimeException;
+import org.apache.wicket.protocol.http.WebApplication;
+import org.apache.wicket.protocol.http.WebRequest;
+import org.apache.wicket.util.tester.WicketTester;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.mockito.InOrder;
+import org.mockito.invocation.InvocationOnMock;
+import org.mockito.stubbing.Answer;
+import org.wamblee.test.jndi.StubInitialContextFactory;
+
+public class OpenTransactionInViewRequestCycleTest {
+
+ private UserTransaction userTransaction;
+ private WicketTester wicket;
+
+ public static Runnable BEHAVIOR;
+
+ @Before
+ public void setUp() throws Exception {
+ userTransaction = mock(UserTransaction.class);
+ StubInitialContextFactory.register();
+ InitialContext ctx = new InitialContext();
+ ctx.bind("java:comp/UserTransaction", userTransaction);
+ BEHAVIOR = mock(Runnable.class);
+ wicket = new WicketTester(new WebApplication() {
+
+ @Override
+ public Class<? extends Page> getHomePage() {
+ return MyPage.class;
+ }
+
+ public RequestCycle newRequestCycle(Request aRequest,
+ Response aResponse) {
+ return new OpenTransactionInViewRequestCycle(this,
+ (WebRequest) aRequest, aResponse);
+ }
+ });
+
+ }
+
+ @After
+ public void tearDown() {
+ StubInitialContextFactory.unregister();
+ }
+
+ private void stubTransactionBehavior(final int aStatus) throws Exception {
+ when(userTransaction.getStatus()).thenReturn(aStatus);
+ doAnswer(new Answer() {
+ @Override
+ public Object answer(InvocationOnMock aInvocation) throws Throwable {
+ when(userTransaction.getStatus()).thenReturn(
+ Status.STATUS_ROLLING_BACK);
+ return null;
+ }
+ }).when(userTransaction).rollback();
+
+ }
+
+ @Test
+ public void testCommittedTransaction() throws Exception {
+
+ stubTransactionBehavior(Status.STATUS_ACTIVE);
+
+ wicket.startPage(MyPage.class);
+ wicket.assertRenderedPage(MyPage.class);
+
+ InOrder inorder = inOrder(userTransaction);
+ inorder.verify(userTransaction).begin();
+ inorder.verify(userTransaction).commit();
+ verify(userTransaction, never()).rollback();
+ }
+
+ @Test
+ public void testRolledbackTransaction() throws Exception {
+ stubTransactionBehavior(Status.STATUS_ACTIVE);
+ doThrow(new RuntimeException("Page rendering fails")).when(BEHAVIOR)
+ .run();
+ try {
+ wicket.startPage(MyPage.class);
+ fail();
+ } catch (WicketRuntimeException e) {
+ InOrder inorder = inOrder(userTransaction);
+ inorder.verify(userTransaction).begin();
+ inorder.verify(userTransaction).rollback();
+ verify(userTransaction, never()).commit();
+ }
+ }
+
+ @Test
+ public void testMarkTransactionForRollback() throws Exception {
+ stubTransactionBehavior(Status.STATUS_MARKED_ROLLBACK);
+ wicket.startPage(MyPage.class);
+ InOrder inorder = inOrder(userTransaction);
+ inorder.verify(userTransaction).begin();
+ inorder.verify(userTransaction).rollback();
+ verify(userTransaction, never()).commit();
+ }
+
+}