Author: mgrigorov
Date: Thu Nov 11 19:44:27 2010
New Revision: 1034078

URL: http://svn.apache.org/viewvc?rev=1034078&view=rev
Log:
WICKET-3138 Wicket 1.5 and GAE

Add IDataStore implementation that stores the pages in the HttpSession.
With two default eviction strategies:
 - PageNumberEvictionStrategy - sets maximum number of pages in the session
 - MemorySizeEvictionStrategy - sets maximum memory size for the pages in the 
session.

Added:
    wicket/trunk/wicket/src/main/java/org/apache/wicket/pageStore/memory/
    
wicket/trunk/wicket/src/main/java/org/apache/wicket/pageStore/memory/DataStoreEvictionStrategy.java
   (with props)
    
wicket/trunk/wicket/src/main/java/org/apache/wicket/pageStore/memory/HttpSessionDataStore.java
   (with props)
    
wicket/trunk/wicket/src/main/java/org/apache/wicket/pageStore/memory/MemorySizeEvictionStrategy.java
   (with props)
    
wicket/trunk/wicket/src/main/java/org/apache/wicket/pageStore/memory/PageNumberEvictionStrategy.java
   (with props)
    
wicket/trunk/wicket/src/main/java/org/apache/wicket/pageStore/memory/PageTable.java
   (with props)
    
wicket/trunk/wicket/src/main/java/org/apache/wicket/pageStore/memory/PageTableCleaner.java
   (with props)
    wicket/trunk/wicket/src/test/java/org/apache/wicket/pageStore/memory/
    
wicket/trunk/wicket/src/test/java/org/apache/wicket/pageStore/memory/DummyPageManagerContext.java
   (with props)
    
wicket/trunk/wicket/src/test/java/org/apache/wicket/pageStore/memory/HttpSessionDataStoreTest.java
   (with props)
    
wicket/trunk/wicket/src/test/java/org/apache/wicket/pageStore/memory/MemorySizeEvictionStrategyTest.java
   (with props)
    
wicket/trunk/wicket/src/test/java/org/apache/wicket/pageStore/memory/PageNumberEvictionStrategyTest.java
   (with props)
    
wicket/trunk/wicket/src/test/java/org/apache/wicket/pageStore/memory/PageTableTest.java
   (with props)

Added: 
wicket/trunk/wicket/src/main/java/org/apache/wicket/pageStore/memory/DataStoreEvictionStrategy.java
URL: 
http://svn.apache.org/viewvc/wicket/trunk/wicket/src/main/java/org/apache/wicket/pageStore/memory/DataStoreEvictionStrategy.java?rev=1034078&view=auto
==============================================================================
--- 
wicket/trunk/wicket/src/main/java/org/apache/wicket/pageStore/memory/DataStoreEvictionStrategy.java
 (added)
+++ 
wicket/trunk/wicket/src/main/java/org/apache/wicket/pageStore/memory/DataStoreEvictionStrategy.java
 Thu Nov 11 19:44:27 2010
@@ -0,0 +1,34 @@
+/*
+ * 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.pageStore.memory;
+
+
+/**
+ * An eviction strategy that decides whether the in-memory data structure used 
as page store should
+ * be compacted
+ */
+public interface DataStoreEvictionStrategy
+{
+
+       /**
+        * Called after each {...@link DataStore#storeData()} call.
+        * 
+        * @param pageTable
+        *            the in-memory data store with <strong>all</strong> pages
+        */
+       void evict(PageTable pageTable);
+}

Propchange: 
wicket/trunk/wicket/src/main/java/org/apache/wicket/pageStore/memory/DataStoreEvictionStrategy.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: 
wicket/trunk/wicket/src/main/java/org/apache/wicket/pageStore/memory/HttpSessionDataStore.java
URL: 
http://svn.apache.org/viewvc/wicket/trunk/wicket/src/main/java/org/apache/wicket/pageStore/memory/HttpSessionDataStore.java?rev=1034078&view=auto
==============================================================================
--- 
wicket/trunk/wicket/src/main/java/org/apache/wicket/pageStore/memory/HttpSessionDataStore.java
 (added)
+++ 
wicket/trunk/wicket/src/main/java/org/apache/wicket/pageStore/memory/HttpSessionDataStore.java
 Thu Nov 11 19:44:27 2010
@@ -0,0 +1,140 @@
+/*
+ * 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.pageStore.memory;
+
+import javax.servlet.http.HttpSession;
+
+import org.apache.wicket.page.IPageManagerContext;
+import org.apache.wicket.pageStore.IDataStore;
+
+/**
+ * A {...@link DataStore} which stores the pages in the {...@link 
HttpSession}. Uses
+ * {...@link DataStoreEvictionStrategy} to keep the memory footprint 
reasonable.
+ * 
+ * <p>
+ * Usage:
+ * 
+ * <pre>
+ * <!...@formatter:off-->
+ * MyApp#init()
+ * {
+ * 
+ *     setPageManagerProvider(new IPageManagerProvider() 
+ *     {
+ *             public IPageManager get(IPageManagerContext pageManagerContext) 
+ *             { 
+ *                     IDataStore dataStore = new 
HttpSessionDataStore(pageManagerContext, new PageNumberEvictionStrategy(20)); 
+ *                     IPageStore pageStore = new 
DefaultPageStore(application.getName(), dataStore, getCacheSize()); 
+ *                     return new PersistentPageManager(application.getName(), 
pageStore, pageManagerContext);
+ *             }
+ *     }
+ * }
+ * <!...@formatter:on-->
+ * </pre>
+ */
+public class HttpSessionDataStore implements IDataStore
+{
+
+       /** the session attribute key. auto-prefixed with 
application.getSessionAttributePrefix() */
+       private static final String PAGE_TABLE_KEY = "page:store:memory";
+
+       private final IPageManagerContext pageManagerContext;
+
+       private final DataStoreEvictionStrategy evictionStrategy;
+
+       /**
+        * Construct.
+        * 
+        * @param pageManagerContext
+        * @param evictionStrategy
+        */
+       public HttpSessionDataStore(IPageManagerContext pageManagerContext,
+               DataStoreEvictionStrategy evictionStrategy)
+       {
+               this.pageManagerContext = pageManagerContext;
+               this.evictionStrategy = evictionStrategy;
+       }
+
+       /**
+        * @param sessionId
+        *            Ignored. Only pages from the current http session can be 
read
+        * @see 
org.apache.wicket.pageStore.IDataStore#getData(java.lang.String, int)
+        */
+       public byte[] getData(String sessionId, int pageId)
+       {
+               PageTable pageTable = getPageTable(false);
+               byte[] pageAsBytes = null;
+               if (pageTable != null)
+               {
+                       pageAsBytes = pageTable.getPage(pageId);
+               }
+               return pageAsBytes;
+       }
+
+       public void removeData(String sessionId, int pageId)
+       {
+               PageTable pageTable = getPageTable(false);
+               if (pageTable != null)
+               {
+                       pageTable.removePage(pageId);
+               }
+       }
+
+       public void removeData(String sessionId)
+       {
+               PageTable pageTable = getPageTable(false);
+               if (pageTable != null)
+               {
+                       pageTable.clear();
+               }
+       }
+
+       public void storeData(String sessionId, int pageId, byte[] pageAsBytes)
+       {
+               PageTable pageTable = getPageTable(true);
+               pageTable.storePage(pageId, pageAsBytes);
+
+               evictionStrategy.evict(pageTable);
+       }
+
+       public void destroy()
+       {
+               PageTable pageTable = getPageTable(false);
+               if (pageTable != null)
+               {
+                       pageTable.clear();
+               }
+       }
+
+       public boolean isReplicated()
+       {
+               return true;
+       }
+
+       private PageTable getPageTable(boolean create)
+       {
+               PageTable pageTable = 
(PageTable)pageManagerContext.getSessionAttribute(PAGE_TABLE_KEY);
+               if (pageTable == null && create)
+               {
+                       pageTable = new PageTable();
+                       pageManagerContext.setSessionAttribute(PAGE_TABLE_KEY, 
pageTable);
+               }
+
+               return pageTable;
+       }
+
+}

Propchange: 
wicket/trunk/wicket/src/main/java/org/apache/wicket/pageStore/memory/HttpSessionDataStore.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: 
wicket/trunk/wicket/src/main/java/org/apache/wicket/pageStore/memory/MemorySizeEvictionStrategy.java
URL: 
http://svn.apache.org/viewvc/wicket/trunk/wicket/src/main/java/org/apache/wicket/pageStore/memory/MemorySizeEvictionStrategy.java?rev=1034078&view=auto
==============================================================================
--- 
wicket/trunk/wicket/src/main/java/org/apache/wicket/pageStore/memory/MemorySizeEvictionStrategy.java
 (added)
+++ 
wicket/trunk/wicket/src/main/java/org/apache/wicket/pageStore/memory/MemorySizeEvictionStrategy.java
 Thu Nov 11 19:44:27 2010
@@ -0,0 +1,63 @@
+/*
+ * 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.pageStore.memory;
+
+import org.apache.wicket.util.lang.Args;
+import org.apache.wicket.util.lang.Bytes;
+import org.apache.wicket.util.lang.WicketObjects;
+
+/**
+ * An eviction strategy that keeps the data store size up to configured bytes
+ */
+public class MemorySizeEvictionStrategy implements DataStoreEvictionStrategy
+{
+
+       private final Bytes maxBytes;
+
+       /**
+        * Construct.
+        * 
+        * @param maxBytes
+        *            the maximum size of the data store
+        */
+       public MemorySizeEvictionStrategy(Bytes maxBytes)
+       {
+               Args.notNull(maxBytes, "maxBytes");
+
+               this.maxBytes = maxBytes;
+       }
+
+       /**
+        * 
+        * @see 
org.apache.wicket.pageStore.memory.DataStoreEvictionStrategy#evict(org.apache.wicket.pageStore.memory.PageTable)
+        */
+       public void evict(PageTable pageTable)
+       {
+
+               long storeCurrentSize = WicketObjects.sizeof(pageTable);
+
+               if (storeCurrentSize > maxBytes.bytes())
+               {
+                       PageTableCleaner cleaner = new PageTableCleaner();
+                       cleaner.drop(pageTable, 1);
+
+                       // recurse until enough space is cleaned
+                       evict(pageTable);
+               }
+       }
+
+}

Propchange: 
wicket/trunk/wicket/src/main/java/org/apache/wicket/pageStore/memory/MemorySizeEvictionStrategy.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: 
wicket/trunk/wicket/src/main/java/org/apache/wicket/pageStore/memory/PageNumberEvictionStrategy.java
URL: 
http://svn.apache.org/viewvc/wicket/trunk/wicket/src/main/java/org/apache/wicket/pageStore/memory/PageNumberEvictionStrategy.java?rev=1034078&view=auto
==============================================================================
--- 
wicket/trunk/wicket/src/main/java/org/apache/wicket/pageStore/memory/PageNumberEvictionStrategy.java
 (added)
+++ 
wicket/trunk/wicket/src/main/java/org/apache/wicket/pageStore/memory/PageNumberEvictionStrategy.java
 Thu Nov 11 19:44:27 2010
@@ -0,0 +1,56 @@
+/*
+ * 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.pageStore.memory;
+
+
+/**
+ * An eviction strategy which decides whether to evict entries from the 
in-memory data store
+ * depending on the number of stored paged per session
+ */
+public class PageNumberEvictionStrategy implements DataStoreEvictionStrategy
+{
+
+       private final int pagesNumber;
+
+       /**
+        * Construct.
+        * 
+        * @param pagesNumber
+        *            the maximum number of pages the data store can hold
+        */
+       public PageNumberEvictionStrategy(int pagesNumber)
+       {
+               this.pagesNumber = pagesNumber;
+       }
+
+       /**
+        * 
+        * @see 
org.apache.wicket.pageStore.memory.DataStoreEvictionStrategy#evict(org.apache.wicket.pageStore.memory.PageTable)
+        */
+       public void evict(PageTable pageTable)
+       {
+               int size = pageTable.size();
+               int pagesToDrop = size - pagesNumber;
+
+               if (pagesToDrop > 0)
+               {
+                       PageTableCleaner cleaner = new PageTableCleaner();
+                       cleaner.drop(pageTable, pagesToDrop);
+               }
+       }
+
+}

Propchange: 
wicket/trunk/wicket/src/main/java/org/apache/wicket/pageStore/memory/PageNumberEvictionStrategy.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: 
wicket/trunk/wicket/src/main/java/org/apache/wicket/pageStore/memory/PageTable.java
URL: 
http://svn.apache.org/viewvc/wicket/trunk/wicket/src/main/java/org/apache/wicket/pageStore/memory/PageTable.java?rev=1034078&view=auto
==============================================================================
--- 
wicket/trunk/wicket/src/main/java/org/apache/wicket/pageStore/memory/PageTable.java
 (added)
+++ 
wicket/trunk/wicket/src/main/java/org/apache/wicket/pageStore/memory/PageTable.java
 Thu Nov 11 19:44:27 2010
@@ -0,0 +1,123 @@
+/*
+ * 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.pageStore.memory;
+
+import java.util.Queue;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ConcurrentLinkedQueue;
+import java.util.concurrent.ConcurrentMap;
+
+import org.apache.wicket.IClusterable;
+
+/**
+ * A structure that holds page id => pageAsBytes.
+ * 
+ * <p>
+ * Additionally it has an index of the least recently used pages
+ */
+class PageTable implements IClusterable
+{
+       private static final long serialVersionUID = 1L;
+
+
+       /**
+        * Holds the index of last/least recently used page ids. The most 
recently used page id is in
+        * the tail, the least recently used is in the head.
+        */
+       /*
+        * Can be replaced later with PriorityQueue to deal with lightweight 
(Ajax) and heavyweight
+        * pages
+        */
+       private final Queue<Integer> index;
+
+       /**
+        * The actual container for the pages.
+        * 
+        * <p>
+        * page id => page as bytes
+        */
+       private final ConcurrentMap<Integer, byte[]> pages;
+
+       public PageTable()
+       {
+               pages = new ConcurrentHashMap<Integer, byte[]>();
+               index = new ConcurrentLinkedQueue<Integer>();
+       }
+
+       void storePage(Integer pageId, byte[] pageAsBytes)
+       {
+               synchronized (index)
+               {
+                       pages.put(pageId, pageAsBytes);
+
+                       updateIndex(pageId);
+               }
+       }
+
+       byte[] getPage(final Integer pageId)
+       {
+               synchronized (index)
+               {
+                       updateIndex(pageId);
+
+                       return pages.get(pageId);
+               }
+       }
+
+       public byte[] removePage(Integer pageId)
+       {
+               synchronized (index)
+               {
+                       index.remove(pageId);
+
+                       return pages.remove(pageId);
+               }
+       }
+
+       public void clear()
+       {
+               synchronized (index)
+               {
+                       index.clear();
+                       pages.clear();
+               }
+       }
+
+       public int size()
+       {
+               return pages.size();
+       }
+
+       Integer getOldest()
+       {
+               return index.peek();
+       }
+
+       /**
+        * Updates the index of last/least recently used pages by removing the 
page id from the index
+        * (in case it is already in) and (re-)adding it at the head
+        * 
+        * @param pageId
+        *            the id of a recently used page
+        */
+       private void updateIndex(Integer pageId)
+       {
+               index.remove(pageId);
+               index.offer(pageId);
+       }
+
+}

Propchange: 
wicket/trunk/wicket/src/main/java/org/apache/wicket/pageStore/memory/PageTable.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: 
wicket/trunk/wicket/src/main/java/org/apache/wicket/pageStore/memory/PageTableCleaner.java
URL: 
http://svn.apache.org/viewvc/wicket/trunk/wicket/src/main/java/org/apache/wicket/pageStore/memory/PageTableCleaner.java?rev=1034078&view=auto
==============================================================================
--- 
wicket/trunk/wicket/src/main/java/org/apache/wicket/pageStore/memory/PageTableCleaner.java
 (added)
+++ 
wicket/trunk/wicket/src/main/java/org/apache/wicket/pageStore/memory/PageTableCleaner.java
 Thu Nov 11 19:44:27 2010
@@ -0,0 +1,41 @@
+/*
+ * 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.pageStore.memory;
+
+/**
+ * Helper class that knows how to remove the nth oldest pages from {...@link 
PageTable}
+ */
+public class PageTableCleaner
+{
+
+       /**
+        * Removes {...@code pagesNumber} of pages from the {...@link PageTable 
pageTable}
+        * 
+        * @param pageTable
+        *            the {...@link PageTable} to clean
+        * @param pagesNumber
+        *            the number of pages to remove
+        */
+       public void drop(final PageTable pageTable, final int pagesNumber)
+       {
+               for (int i = 0; i < pagesNumber; i++)
+               {
+                       Integer pageIdOfTheOldest = pageTable.getOldest();
+                       pageTable.removePage(pageIdOfTheOldest);
+               }
+       }
+}

Propchange: 
wicket/trunk/wicket/src/main/java/org/apache/wicket/pageStore/memory/PageTableCleaner.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: 
wicket/trunk/wicket/src/test/java/org/apache/wicket/pageStore/memory/DummyPageManagerContext.java
URL: 
http://svn.apache.org/viewvc/wicket/trunk/wicket/src/test/java/org/apache/wicket/pageStore/memory/DummyPageManagerContext.java?rev=1034078&view=auto
==============================================================================
--- 
wicket/trunk/wicket/src/test/java/org/apache/wicket/pageStore/memory/DummyPageManagerContext.java
 (added)
+++ 
wicket/trunk/wicket/src/test/java/org/apache/wicket/pageStore/memory/DummyPageManagerContext.java
 Thu Nov 11 19:44:27 2010
@@ -0,0 +1,56 @@
+/*
+ * 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.pageStore.memory;
+
+import java.io.Serializable;
+
+import org.apache.wicket.page.IPageManagerContext;
+
+class DummyPageManagerContext implements IPageManagerContext
+{
+
+       Serializable attribute = null;
+
+       public void setRequestData(Object data)
+       {
+       }
+
+       public Object getRequestData()
+       {
+               return null;
+       }
+
+       public void setSessionAttribute(String key, Serializable value)
+       {
+               attribute = value;
+       }
+
+       public Serializable getSessionAttribute(String key)
+       {
+               return attribute;
+       }
+
+       public void bind()
+       {
+       }
+
+       public String getSessionId()
+       {
+               return null;
+       }
+
+}
\ No newline at end of file

Propchange: 
wicket/trunk/wicket/src/test/java/org/apache/wicket/pageStore/memory/DummyPageManagerContext.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: 
wicket/trunk/wicket/src/test/java/org/apache/wicket/pageStore/memory/HttpSessionDataStoreTest.java
URL: 
http://svn.apache.org/viewvc/wicket/trunk/wicket/src/test/java/org/apache/wicket/pageStore/memory/HttpSessionDataStoreTest.java?rev=1034078&view=auto
==============================================================================
--- 
wicket/trunk/wicket/src/test/java/org/apache/wicket/pageStore/memory/HttpSessionDataStoreTest.java
 (added)
+++ 
wicket/trunk/wicket/src/test/java/org/apache/wicket/pageStore/memory/HttpSessionDataStoreTest.java
 Thu Nov 11 19:44:27 2010
@@ -0,0 +1,95 @@
+/*
+ * 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.pageStore.memory;
+
+import static org.junit.Assert.assertArrayEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+public class HttpSessionDataStoreTest
+{
+       final String sessionId = "anything";
+
+       final int pageId = 1;
+
+       final byte[] PAGE1 = new byte[] { 1 };
+       final byte[] PAGE2 = new byte[] { 2 };
+
+       HttpSessionDataStore store;
+
+       @Before
+       public void before()
+       {
+               store = new HttpSessionDataStore(new DummyPageManagerContext(), 
new NoopEvictionStrategy());
+       }
+
+       @After
+       public void after()
+       {
+               store.destroy();
+       }
+
+       @Test
+       public void storePage()
+       {
+               assertNull(store.getData(sessionId, pageId));
+
+               store.storeData(sessionId, pageId, PAGE1);
+               assertArrayEquals(PAGE1, store.getData(sessionId, pageId));
+       }
+
+       @Test
+       public void removePage1()
+       {
+               assertNull(store.getData(sessionId, pageId));
+
+               store.storeData(sessionId, pageId, PAGE1);
+
+               assertNotNull(store.getData(sessionId, pageId));
+
+               store.removeData(sessionId, pageId);
+
+               assertNull(store.getData(sessionId, pageId));
+       }
+
+       @Test
+       public void removePage2()
+       {
+               assertNull(store.getData(sessionId, pageId));
+
+               store.storeData(sessionId, pageId, PAGE1);
+
+               assertNotNull(store.getData(sessionId, pageId));
+
+               store.removeData(sessionId);
+
+               assertNull(store.getData(sessionId, pageId));
+       }
+
+
+       private static final class NoopEvictionStrategy implements 
DataStoreEvictionStrategy
+       {
+
+               public void evict(PageTable pageTable)
+               {
+               }
+       }
+}

Propchange: 
wicket/trunk/wicket/src/test/java/org/apache/wicket/pageStore/memory/HttpSessionDataStoreTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: 
wicket/trunk/wicket/src/test/java/org/apache/wicket/pageStore/memory/MemorySizeEvictionStrategyTest.java
URL: 
http://svn.apache.org/viewvc/wicket/trunk/wicket/src/test/java/org/apache/wicket/pageStore/memory/MemorySizeEvictionStrategyTest.java?rev=1034078&view=auto
==============================================================================
--- 
wicket/trunk/wicket/src/test/java/org/apache/wicket/pageStore/memory/MemorySizeEvictionStrategyTest.java
 (added)
+++ 
wicket/trunk/wicket/src/test/java/org/apache/wicket/pageStore/memory/MemorySizeEvictionStrategyTest.java
 Thu Nov 11 19:44:27 2010
@@ -0,0 +1,65 @@
+/*
+ * 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.pageStore.memory;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+import org.apache.wicket.util.lang.Bytes;
+import org.apache.wicket.util.lang.WicketObjects;
+import org.junit.Test;
+
+/***/
+public class MemorySizeEvictionStrategyTest
+{
+       final byte[] PAGE1 = new byte[] { 1 };
+       final byte[] PAGE2 = new byte[] { 2, 3 };
+
+       /***/
+       @Test
+       public void evict()
+       {
+               PageTable pageTable = new PageTable();
+
+               long sizeOfEmptyPageTable = WicketObjects.sizeof(pageTable);
+
+               // evict to empty page table
+               MemorySizeEvictionStrategy strategy = new 
MemorySizeEvictionStrategy(
+                       Bytes.bytes(sizeOfEmptyPageTable));
+               pageTable.storePage(PAGE1.length, PAGE1);
+               assertEquals(1, pageTable.size());
+               strategy.evict(pageTable);
+               assertEquals(0, pageTable.size());
+               long currentSize = WicketObjects.sizeof(pageTable);
+               assertTrue("Current size: |" + currentSize + "|, strategy size: 
|" + sizeOfEmptyPageTable +
+                       "|", currentSize <= sizeOfEmptyPageTable);
+
+               // evict to page table with size: empty + PAGE2
+               pageTable.storePage(PAGE2.length, PAGE2);
+               long sizeOfWithPage2 = WicketObjects.sizeof(pageTable);
+               strategy = new 
MemorySizeEvictionStrategy(Bytes.bytes(sizeOfWithPage2));
+               pageTable.storePage(PAGE1.length, PAGE1);
+               assertEquals(2, pageTable.size());
+               strategy.evict(pageTable);
+               // the following assertion depends on the fact that PAGE2 has
+               // bigger size than PAGE1
+               assertEquals(1, pageTable.size());
+               currentSize = WicketObjects.sizeof(pageTable);
+               assertTrue("Current size: |" + currentSize + "|, strategy size: 
|" + sizeOfWithPage2 + "|",
+                       currentSize <= sizeOfWithPage2);
+       }
+}

Propchange: 
wicket/trunk/wicket/src/test/java/org/apache/wicket/pageStore/memory/MemorySizeEvictionStrategyTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: 
wicket/trunk/wicket/src/test/java/org/apache/wicket/pageStore/memory/PageNumberEvictionStrategyTest.java
URL: 
http://svn.apache.org/viewvc/wicket/trunk/wicket/src/test/java/org/apache/wicket/pageStore/memory/PageNumberEvictionStrategyTest.java?rev=1034078&view=auto
==============================================================================
--- 
wicket/trunk/wicket/src/test/java/org/apache/wicket/pageStore/memory/PageNumberEvictionStrategyTest.java
 (added)
+++ 
wicket/trunk/wicket/src/test/java/org/apache/wicket/pageStore/memory/PageNumberEvictionStrategyTest.java
 Thu Nov 11 19:44:27 2010
@@ -0,0 +1,54 @@
+/*
+ * 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.pageStore.memory;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+
+import org.junit.Test;
+
+/***/
+public class PageNumberEvictionStrategyTest
+{
+
+       final byte[] PAGE1 = new byte[] { 1 };
+       final byte[] PAGE2 = new byte[] { 2, 3 };
+
+       /***/
+       @Test
+       public void evict()
+       {
+               // evict to page table with one page only
+               PageNumberEvictionStrategy strategy = new 
PageNumberEvictionStrategy(1);
+
+               PageTable pageTable = new PageTable();
+
+               pageTable.storePage(PAGE1.length, PAGE1);
+               assertEquals(1, pageTable.size());
+               strategy.evict(pageTable);
+               assertEquals(1, pageTable.size());
+               assertNotNull(pageTable.getPage(PAGE1.length));
+
+               pageTable.storePage(PAGE2.length, PAGE2);
+               assertEquals(2, pageTable.size());
+               strategy.evict(pageTable);
+               assertEquals(1, pageTable.size());
+               assertNotNull(pageTable.getPage(PAGE2.length));
+               assertNull(pageTable.getPage(PAGE1.length));
+       }
+}

Propchange: 
wicket/trunk/wicket/src/test/java/org/apache/wicket/pageStore/memory/PageNumberEvictionStrategyTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: 
wicket/trunk/wicket/src/test/java/org/apache/wicket/pageStore/memory/PageTableTest.java
URL: 
http://svn.apache.org/viewvc/wicket/trunk/wicket/src/test/java/org/apache/wicket/pageStore/memory/PageTableTest.java?rev=1034078&view=auto
==============================================================================
--- 
wicket/trunk/wicket/src/test/java/org/apache/wicket/pageStore/memory/PageTableTest.java
 (added)
+++ 
wicket/trunk/wicket/src/test/java/org/apache/wicket/pageStore/memory/PageTableTest.java
 Thu Nov 11 19:44:27 2010
@@ -0,0 +1,58 @@
+/*
+ * 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.pageStore.memory;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNull;
+
+import org.junit.Test;
+
+/***/
+public class PageTableTest
+{
+
+       final byte[] data = new byte[] { 1 };
+
+       /***/
+       @Test
+       public void getOldest()
+       {
+               PageTable pageTable = new PageTable();
+
+               assertNull(pageTable.getOldest());
+
+               pageTable.storePage(1, data);
+               // index: 1
+               assertEquals(Integer.valueOf(1), pageTable.getOldest());
+
+               pageTable.storePage(2, data);
+               // index: 2, 1
+               assertEquals(Integer.valueOf(1), pageTable.getOldest());
+
+               pageTable.storePage(3, data);
+               // index: 3, 2, 1
+               assertEquals(Integer.valueOf(1), pageTable.getOldest());
+
+               pageTable.getPage(1);
+               // index: 1, 3, 2
+               assertEquals(Integer.valueOf(2), pageTable.getOldest());
+
+               pageTable.removePage(2);
+               // index: 1, 3
+               assertEquals(Integer.valueOf(3), pageTable.getOldest());
+       }
+}

Propchange: 
wicket/trunk/wicket/src/test/java/org/apache/wicket/pageStore/memory/PageTableTest.java
------------------------------------------------------------------------------
    svn:eol-style = native


Reply via email to