--- /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.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);
+ }
+}
--- /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.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();
+ }
+}