Author: pedro Date: Sat Mar 19 20:56:59 2011 New Revision: 1083284 URL: http://svn.apache.org/viewvc?rev=1083284&view=rev Log: Test case preventing problems during the page serialization process outside Wicket lifecycle Issue: WICKET-3420
Added: wicket/trunk/wicket-core/src/test/java/org/apache/wicket/page/PersistentPageManagerTest.java Modified: wicket/trunk/wicket-core/src/main/java/org/apache/wicket/pageStore/DefaultPageStore.java wicket/trunk/wicket-core/src/test/java/org/apache/wicket/pageStore/memory/DummyPageManagerContext.java wicket/trunk/wicket-core/src/test/java/org/apache/wicket/versioning/InMemoryPageStore.java Modified: wicket/trunk/wicket-core/src/main/java/org/apache/wicket/pageStore/DefaultPageStore.java URL: http://svn.apache.org/viewvc/wicket/trunk/wicket-core/src/main/java/org/apache/wicket/pageStore/DefaultPageStore.java?rev=1083284&r1=1083283&r2=1083284&view=diff ============================================================================== --- wicket/trunk/wicket-core/src/main/java/org/apache/wicket/pageStore/DefaultPageStore.java (original) +++ wicket/trunk/wicket-core/src/main/java/org/apache/wicket/pageStore/DefaultPageStore.java Sat Mar 19 20:56:59 2011 @@ -22,7 +22,6 @@ import java.util.ArrayList; import java.util.Iterator; import java.util.List; -import org.apache.wicket.Page; import org.apache.wicket.page.IManageablePage; import org.apache.wicket.util.lang.Args; import org.apache.wicket.util.lang.Objects; @@ -275,7 +274,7 @@ public class DefaultPageStore implements { return null; } - else if (!storeAfterSessionReplication() || serializable instanceof Page) + else if (!storeAfterSessionReplication() || serializable instanceof IManageablePage) { return serializable; } Added: wicket/trunk/wicket-core/src/test/java/org/apache/wicket/page/PersistentPageManagerTest.java URL: http://svn.apache.org/viewvc/wicket/trunk/wicket-core/src/test/java/org/apache/wicket/page/PersistentPageManagerTest.java?rev=1083284&view=auto ============================================================================== --- wicket/trunk/wicket-core/src/test/java/org/apache/wicket/page/PersistentPageManagerTest.java (added) +++ wicket/trunk/wicket-core/src/test/java/org/apache/wicket/page/PersistentPageManagerTest.java Sat Mar 19 20:56:59 2011 @@ -0,0 +1,115 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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.apache.wicket.page; + +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.ObjectInputStream; +import java.io.Serializable; + +import junit.framework.TestCase; + +import org.apache.wicket.pageStore.DefaultPageStore; +import org.apache.wicket.pageStore.IDataStore; +import org.apache.wicket.pageStore.IPageStore; +import org.apache.wicket.pageStore.memory.DummyPageManagerContext; +import org.apache.wicket.util.lang.WicketObjects; +import org.apache.wicket.versioning.InMemoryPageStore; + +/** + * @author Pedro Santos + */ +public class PersistentPageManagerTest extends TestCase +{ + /** + * WICKET-3470 + * + * @throws ClassNotFoundException + * @throws IOException + */ + public void testSerializationOutsideWicketLifecyle() throws IOException, ClassNotFoundException + { + IPageManager pageManager = newPersistentPageManager("test_app"); + TestPage toSerializePage = new TestPage(); + pageManager.touchPage(toSerializePage); + pageManager.commitRequest(); + pageManager.destroy(); + // serializing the Wicket piece in servlet session + Serializable sessionEntry = pageManager.getContext().getSessionAttribute(null); + byte[] serializedSessionEntry = WicketObjects.objectToByteArray(sessionEntry); + assertTrue("ok, wicket was able to serialize the session entry", true); + + // testing if it is possible to deserialize the session entry + IPageManager newPageManager = newPersistentPageManager("test_app"); + ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream( + serializedSessionEntry)); + assertNull("Worker thread should be unaware of Wicket application", in.readObject()); + Serializable loadedSessionEntry = (Serializable)in.readObject(); + assertNotNull( + "Wicket needs to be able to deserialize the session entry regardless the application availability", + loadedSessionEntry); + + newPageManager.getContext().setSessionAttribute(null, loadedSessionEntry); + + + TestPage deserializedPage = (TestPage)newPageManager.getPage(toSerializePage.getPageId()); + assertNotNull(deserializedPage); + assertEquals(toSerializePage.instanceID, deserializedPage.instanceID); + + newPageManager.destroy(); + } + + private PersistentPageManager newPersistentPageManager(String appName) + { + IDataStore dataStore = new InMemoryPageStore(); + IPageStore pageStore = new DefaultPageStore(appName, dataStore, 4); + IPageManagerContext pageManagerContext = new DummyPageManagerContext(); + return new PersistentPageManager(appName, pageStore, pageManagerContext); + } + + private static class TestPage implements IManageablePage + { + /** */ + private static final long serialVersionUID = 1L; + private static int sequence; + private int instanceID; + + private TestPage() + { + instanceID = sequence++; + } + + public boolean isPageStateless() + { + return false; + } + + public int getPageId() + { + return instanceID; + } + + public void detach() + { + } + + public boolean setFreezePageId(boolean freeze) + { + return false; + } + } +} Modified: wicket/trunk/wicket-core/src/test/java/org/apache/wicket/pageStore/memory/DummyPageManagerContext.java URL: http://svn.apache.org/viewvc/wicket/trunk/wicket-core/src/test/java/org/apache/wicket/pageStore/memory/DummyPageManagerContext.java?rev=1083284&r1=1083283&r2=1083284&view=diff ============================================================================== --- wicket/trunk/wicket-core/src/test/java/org/apache/wicket/pageStore/memory/DummyPageManagerContext.java (original) +++ wicket/trunk/wicket-core/src/test/java/org/apache/wicket/pageStore/memory/DummyPageManagerContext.java Sat Mar 19 20:56:59 2011 @@ -20,18 +20,22 @@ import java.io.Serializable; import org.apache.wicket.page.IPageManagerContext; -class DummyPageManagerContext implements IPageManagerContext +/** + */ +public class DummyPageManagerContext implements IPageManagerContext { Serializable attribute = null; + Object requestData; public void setRequestData(Object data) { + requestData = data; } public Object getRequestData() { - return null; + return requestData; } public void setSessionAttribute(String key, Serializable value) @@ -50,7 +54,7 @@ class DummyPageManagerContext implements public String getSessionId() { - return null; + return "dummy_id"; } } \ No newline at end of file Modified: wicket/trunk/wicket-core/src/test/java/org/apache/wicket/versioning/InMemoryPageStore.java URL: http://svn.apache.org/viewvc/wicket/trunk/wicket-core/src/test/java/org/apache/wicket/versioning/InMemoryPageStore.java?rev=1083284&r1=1083283&r2=1083284&view=diff ============================================================================== --- wicket/trunk/wicket-core/src/test/java/org/apache/wicket/versioning/InMemoryPageStore.java (original) +++ wicket/trunk/wicket-core/src/test/java/org/apache/wicket/versioning/InMemoryPageStore.java Sat Mar 19 20:56:59 2011 @@ -27,7 +27,7 @@ import org.apache.wicket.pageStore.IData * * @author martin-g */ -class InMemoryPageStore implements IDataStore +public class InMemoryPageStore implements IDataStore { /** @@ -35,7 +35,7 @@ class InMemoryPageStore implements IData */ private final Map<String, Map<Integer, byte[]>> store; - InMemoryPageStore() + public InMemoryPageStore() { store = new HashMap<String, Map<Integer, byte[]>>(); }