(no commit message)
authorErik Brakkee <erik@brakkee.org>
Sat, 17 Jul 2010 14:11:49 +0000 (14:11 +0000)
committerErik Brakkee <erik@brakkee.org>
Sat, 17 Jul 2010 14:11:49 +0000 (14:11 +0000)
wicket/joe/src/test/java/org/wamblee/wicket/transactions/MyPage.java [new file with mode: 0644]
wicket/joe/src/test/java/org/wamblee/wicket/transactions/OpenTransactionInViewRequestCycleTest.java [new file with mode: 0644]

diff --git a/wicket/joe/src/test/java/org/wamblee/wicket/transactions/MyPage.java b/wicket/joe/src/test/java/org/wamblee/wicket/transactions/MyPage.java
new file mode 100644 (file)
index 0000000..99af484
--- /dev/null
@@ -0,0 +1,12 @@
+/**
+ * 
+ */
+package org.wamblee.wicket.transactions;
+
+import org.apache.wicket.markup.html.WebPage;
+
+public class MyPage extends WebPage { 
+    public MyPage() { 
+        OpenTransactionInViewRequestCycleTest.BEHAVIOR.run();
+    }
+}
\ No newline at end of file
diff --git a/wicket/joe/src/test/java/org/wamblee/wicket/transactions/OpenTransactionInViewRequestCycleTest.java b/wicket/joe/src/test/java/org/wamblee/wicket/transactions/OpenTransactionInViewRequestCycleTest.java
new file mode 100644 (file)
index 0000000..ca9ede1
--- /dev/null
@@ -0,0 +1,129 @@
+/*
+ * 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();
+    }
+
+}