You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@wicket.apache.org by sv...@apache.org on 2019/03/13 11:51:21 UTC

[wicket] branch master updated: WICKET-6644 use null instead of -1

This is an automated email from the ASF dual-hosted git repository.

svenmeier pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/wicket.git


The following commit(s) were added to refs/heads/master by this push:
     new c7eecfe  WICKET-6644 use null instead of -1
c7eecfe is described below

commit c7eecfeb6b67edee89820aaba46434a9e5a838e3
Author: Sven Meier <sv...@apache.org>
AuthorDate: Wed Mar 13 12:26:11 2019 +0100

    WICKET-6644 use null instead of -1
    
    to indicate cache cleared
---
 .../markup/repeater/AbstractPageableView.java      | 41 ++-------
 .../markup/repeater/AbstractPageableViewTest.java  | 97 ++++++++++++++++++++++
 2 files changed, 103 insertions(+), 35 deletions(-)

diff --git a/wicket-core/src/main/java/org/apache/wicket/markup/repeater/AbstractPageableView.java b/wicket-core/src/main/java/org/apache/wicket/markup/repeater/AbstractPageableView.java
index 437d96d..2b56d12 100644
--- a/wicket-core/src/main/java/org/apache/wicket/markup/repeater/AbstractPageableView.java
+++ b/wicket-core/src/main/java/org/apache/wicket/markup/repeater/AbstractPageableView.java
@@ -64,7 +64,7 @@ public abstract class AbstractPageableView<T> extends RefreshingView<T> implemen
 	 * for the duration of the request because that call can potentially be expensive ( a select
 	 * count query ) and so we do not want to execute it multiple times.
 	 */
-	private transient long cachedItemCount;
+	private transient Long cachedItemCount;
 
 	/**
 	 * Constructor
@@ -76,14 +76,12 @@ public abstract class AbstractPageableView<T> extends RefreshingView<T> implemen
 	public AbstractPageableView(String id, IModel<? extends Collection<? extends T>> model)
 	{
 		super(id, model);
-		clearCachedItemCount();
 	}
 
 	/** @see org.apache.wicket.Component#Component(String) */
 	public AbstractPageableView(String id)
 	{
 		super(id);
-		clearCachedItemCount();
 	}
 
 	/**
@@ -132,26 +130,7 @@ public abstract class AbstractPageableView<T> extends RefreshingView<T> implemen
 
 	private void clearCachedItemCount()
 	{
-		cachedItemCount = -1;
-	}
-
-	private void setCachedItemCount(long itemCount)
-	{
-		cachedItemCount = itemCount;
-	}
-
-	private long getCachedItemCount()
-	{
-		if (cachedItemCount < 0)
-		{
-			throw new IllegalStateException("getItemCountCache() called when cache was not set");
-		}
-		return cachedItemCount;
-	}
-
-	private boolean isItemCountCached()
-	{
-		return cachedItemCount >= 0;
+		cachedItemCount = null;
 	}
 
 	// /////////////////////////////////////////////////////////////////////////
@@ -228,14 +207,14 @@ public abstract class AbstractPageableView<T> extends RefreshingView<T> implemen
 	@Override
 	public final long getItemCount()
 	{
-		if (isItemCountCached())
+		if (cachedItemCount != null)
 		{
-			return getCachedItemCount();
+			return cachedItemCount;
 		}
 
 		long count = internalGetItemCount();
 
-		setCachedItemCount(count);
+		cachedItemCount = count;
 		return count;
 	}
 
@@ -389,12 +368,4 @@ public abstract class AbstractPageableView<T> extends RefreshingView<T> implemen
 		clearCachedItemCount();
 		super.onDetach();
 	}
-
-	private void readObject(java.io.ObjectInputStream s) throws java.io.IOException,
-		ClassNotFoundException
-	{
-		// Read in all fields
-		s.defaultReadObject();
-		clearCachedItemCount();
-	}
-}
+}
\ No newline at end of file
diff --git a/wicket-core/src/test/java/org/apache/wicket/markup/repeater/AbstractPageableViewTest.java b/wicket-core/src/test/java/org/apache/wicket/markup/repeater/AbstractPageableViewTest.java
new file mode 100644
index 0000000..36af146
--- /dev/null
+++ b/wicket-core/src/test/java/org/apache/wicket/markup/repeater/AbstractPageableViewTest.java
@@ -0,0 +1,97 @@
+/*
+ * 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.markup.repeater;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+
+import org.apache.wicket.model.IModel;
+import org.apache.wicket.model.Model;
+import org.apache.wicket.util.tester.WicketTestCase;
+import org.junit.jupiter.api.Test;
+
+/**
+ * Test for {@link AbstractPageableView}. 
+ */
+public class AbstractPageableViewTest extends WicketTestCase
+{
+	private static int count = 5;
+	
+	/**
+	 */
+	@Test
+	public void cachedItemCount()
+	{
+		View view = new View("f");
+		
+		assertEquals(5, view.getItemCount());
+		
+		count = 6;
+
+		assertEquals(5, view.getItemCount(), "still 5 cached");
+		
+		view.beforeRender();
+
+		assertEquals(6, view.getItemCount(), "cached cleared before render");
+
+		byte[] bytes = tester.getApplication().getFrameworkSettings().getSerializer().serialize(view);
+		
+		view = (View)tester.getApplication().getFrameworkSettings().getSerializer().deserialize(bytes);
+		
+		count = 7;
+		
+		assertEquals(7, view.getItemCount(), "cached cleared when deserialized");
+		
+		view.detach();
+		
+		count = 8;
+		
+		assertEquals(8, view.getItemCount(), "cached cleared when detached");
+	}	
+
+	static class View extends AbstractPageableView<Integer>
+	{
+		public View(String id)
+		{
+			super(id);
+		}
+
+		@Override
+		protected void populateItem(Item<Integer> item)
+		{
+		}
+		
+		@Override
+		protected long internalGetItemCount()
+		{
+			return count;
+		}
+		
+		@Override
+		protected Iterator<IModel<Integer>> getItemModels(long offset, long size)
+		{
+			List<IModel<Integer>> models = new ArrayList<>();
+			for (int m = 0; m < count; m++) {
+				models.add(Model.of(m));
+			}
+			return models.iterator();
+		}
+	}
+}
\ No newline at end of file