(no commit message)
authorerik <erik@77661180-640e-0410-b3a8-9f9b13e6d0e0>
Mon, 26 Jul 2010 17:20:56 +0000 (17:20 +0000)
committererik <erik@77661180-640e-0410-b3a8-9f9b13e6d0e0>
Mon, 26 Jul 2010 17:20:56 +0000 (17:20 +0000)
wicket/components/src/test/java/org/wamblee/wicket/page/CompositePageBehaviorTest.java [new file with mode: 0644]
wicket/components/src/test/java/org/wamblee/wicket/page/WebApplicationBasePageTest.java [new file with mode: 0644]

diff --git a/wicket/components/src/test/java/org/wamblee/wicket/page/CompositePageBehaviorTest.java b/wicket/components/src/test/java/org/wamblee/wicket/page/CompositePageBehaviorTest.java
new file mode 100644 (file)
index 0000000..37e7c1f
--- /dev/null
@@ -0,0 +1,68 @@
+/*
+ * 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.page;
+
+import static org.mockito.Matchers.*;
+import static org.mockito.Mockito.*;
+import junit.framework.TestCase;
+
+import org.apache.wicket.markup.html.WebPage;
+import org.apache.wicket.protocol.http.WebResponse;
+import org.wamblee.wicket.page.CompositePageBehavior;
+import org.wamblee.wicket.page.PageBehavior;
+
+public class CompositePageBehaviorTest extends TestCase {
+
+    private CompositePageBehavior composite;
+    private PageBehavior behavior1;
+    private PageBehavior behavior2;
+    private WebPage page;
+
+    @Override
+    protected void setUp() throws Exception {
+        composite = new CompositePageBehavior();
+        behavior1 = mock(PageBehavior.class);
+        behavior2 = mock(PageBehavior.class);
+        page = mock(WebPage.class);
+        composite.add(behavior1);
+        composite.add(behavior2);
+    }
+
+    public void testOnBeforeRender() {
+        composite.onBeforeRender(page);
+        verify(behavior1).onBeforeRender(same(page));
+        verify(behavior2).onBeforeRender(same(page));
+        verifyNoMoreInteractions(behavior1);
+        verifyNoMoreInteractions(behavior2);
+    }
+
+    public void testSetHeaders() {
+        WebResponse response = mock(WebResponse.class);
+        composite.setHeaders(page, response);
+        verify(behavior1).setHeaders(same(page), same(response));
+        verify(behavior2).setHeaders(same(page), same(response));
+        verifyNoMoreInteractions(behavior1);
+        verifyNoMoreInteractions(behavior2);
+    }
+
+    public void testOnAfterRender() {
+        composite.onAfterRender(page);
+        verify(behavior1).onAfterRender(same(page));
+        verify(behavior2).onAfterRender(same(page));
+        verifyNoMoreInteractions(behavior1);
+        verifyNoMoreInteractions(behavior2);
+    }
+}
diff --git a/wicket/components/src/test/java/org/wamblee/wicket/page/WebApplicationBasePageTest.java b/wicket/components/src/test/java/org/wamblee/wicket/page/WebApplicationBasePageTest.java
new file mode 100644 (file)
index 0000000..8d97be9
--- /dev/null
@@ -0,0 +1,194 @@
+/*
+ * 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.page;
+
+import static junit.framework.Assert.*;
+import static org.mockito.Matchers.*;
+import static org.mockito.Mockito.*;
+
+import java.lang.reflect.Field;
+
+import javax.persistence.EntityManager;
+import javax.transaction.Status;
+import javax.transaction.UserTransaction;
+
+import org.apache.wicket.IPageMap;
+import org.apache.wicket.PageParameters;
+import org.apache.wicket.model.IModel;
+import org.apache.wicket.protocol.http.WebResponse;
+import org.apache.wicket.util.tester.WicketTester;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.wamblee.wicket.page.CompositePageBehavior;
+import org.wamblee.wicket.page.DisableCachingBehavior;
+import org.wamblee.wicket.page.ExpirePageImmediatelyBehavior;
+import org.wamblee.wicket.page.FlushEntityManagerBehavior;
+import org.wamblee.wicket.page.PageBehavior;
+import org.wamblee.wicket.page.WebApplicationBasePage;
+
+public class WebApplicationBasePageTest {
+    private WicketTester wicket;
+
+    @Before
+    public void setUp() {
+        wicket = new WicketTester();
+    }
+
+    @After
+    public void tearDown() {
+        wicket.destroy();
+    }
+
+    private void checkBehaviorsInitialized(WebApplicationBasePage aPage)
+        throws Exception {
+        Field behaviorsField = WebApplicationBasePage.class
+            .getDeclaredField("behaviors");
+        behaviorsField.setAccessible(true);
+        CompositePageBehavior behaviors = (CompositePageBehavior) behaviorsField
+            .get(aPage);
+        assertNotNull(behaviors);
+    }
+
+    @Test
+    public void testRenderBasePage() throws Exception {
+        WebApplicationBasePage page = new WebApplicationBasePage();
+        wicket.startPage(page);
+        wicket.assertRenderedPage(WebApplicationBasePage.class);
+        checkBehaviorsInitialized(page);
+    }
+
+    @Test
+    public void testRenderBasePageWithModel() throws Exception {
+        WebApplicationBasePage page = new WebApplicationBasePage(
+            mock(IModel.class));
+        wicket.startPage(page);
+        wicket.assertRenderedPage(WebApplicationBasePage.class);
+        checkBehaviorsInitialized(page);
+    }
+
+    @Test
+    public void testRenderBasePageWithPageMap() throws Exception {
+        WebApplicationBasePage page = new WebApplicationBasePage(
+            mock(IPageMap.class));
+        wicket.startPage(page);
+        wicket.assertRenderedPage(WebApplicationBasePage.class);
+        checkBehaviorsInitialized(page);
+    }
+
+    @Test
+    public void testRenderBasePageWithPageMapAndModel() throws Exception {
+        WebApplicationBasePage page = new WebApplicationBasePage(
+            mock(IPageMap.class), mock(IModel.class));
+        wicket.startPage(page);
+        wicket.assertRenderedPage(WebApplicationBasePage.class);
+        checkBehaviorsInitialized(page);
+    }
+
+    @Test
+    public void testRenderBasePageWithPageParameters() throws Exception {
+        WebApplicationBasePage page = new WebApplicationBasePage(
+            new PageParameters());
+        wicket.startPage(page);
+        wicket.assertRenderedPage(WebApplicationBasePage.class);
+        checkBehaviorsInitialized(page);
+    }
+
+    @Test
+    public void testRenderBasePageWithPageMapAndPageParameters()
+        throws Exception {
+        WebApplicationBasePage page = new WebApplicationBasePage(
+            mock(IPageMap.class), new PageParameters());
+        wicket.startPage(page);
+        wicket.assertRenderedPage(WebApplicationBasePage.class);
+        checkBehaviorsInitialized(page);
+    }
+
+    public static final class TestPage extends WebApplicationBasePage {
+
+        private WebResponse response;
+
+        public TestPage(WebResponse aResponse) {
+            response = aResponse;
+        }
+
+        public void callAfterRender() {
+            onAfterRender();
+        }
+
+        public void callSetHeaders() {
+            setHeaders(response);
+        }
+
+        public void callBeforeRender() {
+            onBeforeRender();
+        }
+    }
+
+    @Test
+    public void testAddBehavior() throws Exception {
+        final WebResponse response = mock(WebResponse.class);
+        TestPage page = new TestPage(response);
+        PageBehavior behavior = mock(PageBehavior.class);
+        page.addBehavior(behavior);
+
+        reset(behavior);
+        page.callBeforeRender();
+        verify(behavior).onBeforeRender(same(page));
+        verifyNoMoreInteractions(behavior);
+
+        reset(behavior);
+        page.callAfterRender();
+        verify(behavior).onAfterRender(same(page));
+        verifyNoMoreInteractions(behavior);
+
+        reset(behavior);
+        page.callSetHeaders();
+        verify(behavior).setHeaders(same(page), same(response));
+        verifyNoMoreInteractions(behavior);
+    }
+
+    @Test
+    public void testSmokeBehaviorViewOnlyOnce() throws Exception {
+        WebApplicationBasePage page = new WebApplicationBasePage(
+            mock(IPageMap.class), new PageParameters());
+        page.addBehavior(new ExpirePageImmediatelyBehavior());
+        wicket.startPage(page);
+        wicket.assertRenderedPage(WebApplicationBasePage.class);
+    }
+
+    @Test
+    public void testSmokeBehaviorDisableCaching() throws Exception {
+        WebApplicationBasePage page = new WebApplicationBasePage(
+            mock(IPageMap.class), new PageParameters());
+        page.addBehavior(new DisableCachingBehavior());
+        wicket.startPage(page);
+        wicket.assertRenderedPage(WebApplicationBasePage.class);
+    }
+
+    @Test
+    public void testSmokeBehaviorFlushEntityManager() throws Exception {
+        WebApplicationBasePage page = new WebApplicationBasePage(
+            mock(IPageMap.class), new PageParameters());
+        EntityManager entityManager = mock(EntityManager.class);
+        when(entityManager.isOpen()).thenReturn(true);
+        PageBehavior behavior = new FlushEntityManagerBehavior(entityManager);
+        page.addBehavior(behavior);
+        wicket.startPage(page);
+        wicket.assertRenderedPage(WebApplicationBasePage.class);
+        verify(entityManager).flush();
+    }
+}