From: Erik Brakkee Date: Fri, 23 Jul 2010 18:43:58 +0000 (+0000) Subject: (no commit message) X-Git-Tag: wamblee-utils-0.7~197 X-Git-Url: http://wamblee.org/gitweb/?a=commitdiff_plain;ds=inline;h=4da58769e6089ee240959884c50f611e1ed97db5;p=utils --- diff --git a/wicket/components/src/test/java/org/wamblee/wicket/behavior/CompositePageBehaviorTest.java b/wicket/components/src/test/java/org/wamblee/wicket/behavior/CompositePageBehaviorTest.java new file mode 100644 index 00000000..61d5c331 --- /dev/null +++ b/wicket/components/src/test/java/org/wamblee/wicket/behavior/CompositePageBehaviorTest.java @@ -0,0 +1,66 @@ +/* + * 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.behavior; + +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; + +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/behavior/WebApplicationBasePageTest.java b/wicket/components/src/test/java/org/wamblee/wicket/behavior/WebApplicationBasePageTest.java new file mode 100644 index 00000000..a89d6994 --- /dev/null +++ b/wicket/components/src/test/java/org/wamblee/wicket/behavior/WebApplicationBasePageTest.java @@ -0,0 +1,170 @@ +/* + * 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.behavior; + +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 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; + +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); + PageBehavior behavior = new FlushEntityManagerBehavior(entityManager); + page.addBehavior(behavior); + wicket.startPage(page); + wicket.assertRenderedPage(WebApplicationBasePage.class); + verify(entityManager).flush(); + } +}