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 2022/05/24 22:22:20 UTC

[wicket] branch WICKET-6981-session-attributes-not-set updated (95c0639dad -> fbce20096c)

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

svenmeier pushed a change to branch WICKET-6981-session-attributes-not-set
in repository https://gitbox.apache.org/repos/asf/wicket.git


 discard 95c0639dad WICKET-6981 session attributes have to be reset
     new fbce20096c WICKET-6981 session attributes have to be reset

This update added new revisions after undoing existing revisions.
That is to say, some revisions that were in the old version of the
branch are not in the new version.  This situation occurs
when a user --force pushes a change and generates a repository
containing something like this:

 * -- * -- B -- O -- O -- O   (95c0639dad)
            \
             N -- N -- N   refs/heads/WICKET-6981-session-attributes-not-set (fbce20096c)

You should already have received notification emails for all of the O
revisions, and so the following emails describe only the N revisions
from the common base, B.

Any revisions marked "omit" are not gone; other references still
refer to them.  Any revisions marked "discard" are gone forever.

The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 .../apache/wicket/pageStore/DefaultPageContextTest.java  | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)


[wicket] 01/01: WICKET-6981 session attributes have to be reset

Posted by sv...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

svenmeier pushed a commit to branch WICKET-6981-session-attributes-not-set
in repository https://gitbox.apache.org/repos/asf/wicket.git

commit fbce20096ce143f5ffd81d64b3f315a1dec09e19
Author: Sven Meier <sv...@apache.org>
AuthorDate: Tue May 24 23:57:15 2022 +0200

    WICKET-6981 session attributes have to be reset
    
    whenever it changes
---
 .../org/apache/wicket/mock/MockPageContext.java    |  12 +-
 .../pageStore/AbstractPersistentPageStore.java     |  11 +-
 .../wicket/pageStore/DefaultPageContext.java       |  25 ++--
 .../apache/wicket/pageStore/GroupingPageStore.java |  13 +-
 .../org/apache/wicket/pageStore/IPageContext.java  |   8 +-
 .../wicket/pageStore/InSessionPageStore.java       |  13 +-
 .../wicket/pageStore/AbstractPageStoreTest.java    |   8 +-
 .../wicket/pageStore/DefaultPageContextTest.java   | 146 +++++++++++++++++++++
 8 files changed, 181 insertions(+), 55 deletions(-)

diff --git a/wicket-core/src/main/java/org/apache/wicket/mock/MockPageContext.java b/wicket-core/src/main/java/org/apache/wicket/mock/MockPageContext.java
index 061b3770fd..12619c0b33 100644
--- a/wicket-core/src/main/java/org/apache/wicket/mock/MockPageContext.java
+++ b/wicket-core/src/main/java/org/apache/wicket/mock/MockPageContext.java
@@ -68,11 +68,9 @@ public class MockPageContext implements IPageContext
 	{
 		@SuppressWarnings("unchecked")
 		T value = (T)sessionAttributes.get(key);
-		if (value == null) {
+		if (value == null && defaultValue != null) {
 			value = defaultValue.get();
-			if (value != null) {
-				sessionAttributes.put(key, value);
-			}
+			sessionAttributes.put(key, value);
 		}
 		
 		return value;
@@ -82,11 +80,9 @@ public class MockPageContext implements IPageContext
 	public <T extends Serializable> T getSessionData(MetaDataKey<T> key, Supplier<T> defaultValue)
 	{
 		T value = key.get(sessionData);
-		if (value == null) {
+		if (value == null && defaultValue != null) {
 			value = defaultValue.get();
-			if (value != null) {
-				sessionData = key.set(sessionData, value);
-			}
+			sessionData = key.set(sessionData, value);
 		}
 		
 		return value;
diff --git a/wicket-core/src/main/java/org/apache/wicket/pageStore/AbstractPersistentPageStore.java b/wicket-core/src/main/java/org/apache/wicket/pageStore/AbstractPersistentPageStore.java
index 74df252c6f..15d3104619 100644
--- a/wicket-core/src/main/java/org/apache/wicket/pageStore/AbstractPersistentPageStore.java
+++ b/wicket-core/src/main/java/org/apache/wicket/pageStore/AbstractPersistentPageStore.java
@@ -146,14 +146,9 @@ public abstract class AbstractPersistentPageStore implements IPageStore
 	{
 		String key = KEY_PREFIX + Classes.simpleName(getClass());
 		
-		SessionAttribute attribute = context.getSessionAttribute(key, () -> {
-			if (create)
-			{
-				return new SessionAttribute(storeKey, createSessionIdentifier(context));
-			}
-			
-			return null;
-		});
+		SessionAttribute attribute = context.getSessionAttribute(key, create ? () -> {
+			return new SessionAttribute(storeKey, createSessionIdentifier(context));
+		} : null);
 		
 		if (attribute == null)
 		{
diff --git a/wicket-core/src/main/java/org/apache/wicket/pageStore/DefaultPageContext.java b/wicket-core/src/main/java/org/apache/wicket/pageStore/DefaultPageContext.java
index 780f440fb3..b0012e3f98 100644
--- a/wicket-core/src/main/java/org/apache/wicket/pageStore/DefaultPageContext.java
+++ b/wicket-core/src/main/java/org/apache/wicket/pageStore/DefaultPageContext.java
@@ -56,12 +56,13 @@ public class DefaultPageContext implements IPageContext
 		synchronized (session)
 		{
 			T value = (T)session.getAttribute(key);
-			if (value == null) {
-				value = defaultValue.get();
-				if (value != null) {
+			if (defaultValue != null) {
+				if (value == null) {
+					value = defaultValue.get();
 					session.bind();
-					session.setAttribute(key, value);
-				}
+				}				
+				
+				session.setAttribute(key, value);
 			}
 			
 			return value;
@@ -76,15 +77,15 @@ public class DefaultPageContext implements IPageContext
 		synchronized (session)
 		{
 			T value = session.getMetaData(key);
-			if (value != null) {
-				return value;
-			}
-			
-			value = defaultValue.get();
-			if (value != null) {
-				session.bind();
+			if (defaultValue != null) {
+				if (value == null) {
+					value = defaultValue.get();
+					session.bind();
+				}
+				
 				session.setMetaData(key, value);
 			}
+
 			return value;
 		}
 	}
diff --git a/wicket-core/src/main/java/org/apache/wicket/pageStore/GroupingPageStore.java b/wicket-core/src/main/java/org/apache/wicket/pageStore/GroupingPageStore.java
index 9eaaa7dc4d..13866504c3 100644
--- a/wicket-core/src/main/java/org/apache/wicket/pageStore/GroupingPageStore.java
+++ b/wicket-core/src/main/java/org/apache/wicket/pageStore/GroupingPageStore.java
@@ -140,16 +140,9 @@ public abstract class GroupingPageStore extends DelegatingPageStore
 
 	private SessionData getSessionData(IPageContext context, boolean create)
 	{
-		return context.getSessionData(KEY, () -> {
-			if (create)
-			{
-				return new SessionData();
-			}
-			else
-			{
-				return null;
-			}
-		});
+		return context.getSessionData(KEY, create ? () -> {
+			return new SessionData();
+		} : null);
 	}
 
 	/**
diff --git a/wicket-core/src/main/java/org/apache/wicket/pageStore/IPageContext.java b/wicket-core/src/main/java/org/apache/wicket/pageStore/IPageContext.java
index 0cdcd48132..98bf52d384 100644
--- a/wicket-core/src/main/java/org/apache/wicket/pageStore/IPageContext.java
+++ b/wicket-core/src/main/java/org/apache/wicket/pageStore/IPageContext.java
@@ -47,7 +47,8 @@ public interface IPageContext
 
 	/**
 	 * Get an attribute from the session. <br>
-	 * Binds the session if not already set and supplied default value is not <code>null</code>.
+	 * Binds the session if not already set <em>and</em> supplier is not <code>null</code>.
+	 * Sets the session attribute if supplier is not <code>null</code>.
 	 * 
 	 * @param key
 	 *            key
@@ -61,12 +62,13 @@ public interface IPageContext
 
 	/**
 	 * Get metadata from the session. <br>
-	 * Binds the session if not already set and supplied default value is not <code>null</code>.
+	 * Binds the session if not already set <em>and</em> supplier is not <code>null</code>.
+	 * Sets the session attribute if supplier is not <code>null</code>.
 	 *
 	 * @param key
 	 *            key
 	 * @param defaultValue
-	 *            default value to use if not present
+	 *            optional supplier of a default value to use if not present
 	 * @return value
 	 * 
 	 * @see Session#getMetaData(MetaDataKey)
diff --git a/wicket-core/src/main/java/org/apache/wicket/pageStore/InSessionPageStore.java b/wicket-core/src/main/java/org/apache/wicket/pageStore/InSessionPageStore.java
index d2b3653662..fa69b33a5b 100644
--- a/wicket-core/src/main/java/org/apache/wicket/pageStore/InSessionPageStore.java
+++ b/wicket-core/src/main/java/org/apache/wicket/pageStore/InSessionPageStore.java
@@ -149,16 +149,9 @@ public class InSessionPageStore implements IPageStore
 
 	private SessionData getSessionData(IPageContext context, boolean create)
 	{
-		SessionData data = context.getSessionData(getKey(), () -> {
-			if (create)
-			{
-				return dataCreator.get();
-			}
-			else
-			{
-				return null;
-			}
-		});
+		SessionData data = context.getSessionData(getKey(), create ? () -> {
+			return dataCreator.get();
+		} : null);
 
 		if (data != null && serializer != null)
 		{
diff --git a/wicket-core/src/test/java/org/apache/wicket/pageStore/AbstractPageStoreTest.java b/wicket-core/src/test/java/org/apache/wicket/pageStore/AbstractPageStoreTest.java
index 91f306693c..7b8cede0d4 100644
--- a/wicket-core/src/test/java/org/apache/wicket/pageStore/AbstractPageStoreTest.java
+++ b/wicket-core/src/test/java/org/apache/wicket/pageStore/AbstractPageStoreTest.java
@@ -123,7 +123,7 @@ public abstract class AbstractPageStoreTest
 		IPageContext context = new MockPageContext(sessionId) {
 			@Override
 			public <T extends Serializable> T getSessionAttribute(String key, Supplier<T> value) {
-				if (value.get() != null) {
+				if (value != null) {
 					fail();
 				}
 				
@@ -132,7 +132,7 @@ public abstract class AbstractPageStoreTest
 			
 			@Override
 			public <T extends Serializable> T getSessionData(MetaDataKey<T> key, Supplier<T> value) {
-				if (value.get() != null) {
+				if (value != null) {
 					return fail();
 				}
 				
@@ -149,7 +149,7 @@ public abstract class AbstractPageStoreTest
 		IPageContext context = new MockPageContext(sessionId) {
 			@Override
 			public <T extends Serializable> T getSessionAttribute(String key, Supplier<T> value) {
-				if (value.get() != null) {
+				if (value != null) {
 					fail();
 				}
 				
@@ -158,7 +158,7 @@ public abstract class AbstractPageStoreTest
 			
 			@Override
 			public <T extends Serializable> T getSessionData(MetaDataKey<T> key, Supplier<T> value) {
-				if (value.get() != null) {
+				if (value != null) {
 					return fail();
 				}
 				
diff --git a/wicket-core/src/test/java/org/apache/wicket/pageStore/DefaultPageContextTest.java b/wicket-core/src/test/java/org/apache/wicket/pageStore/DefaultPageContextTest.java
new file mode 100644
index 0000000000..dc3b783014
--- /dev/null
+++ b/wicket-core/src/test/java/org/apache/wicket/pageStore/DefaultPageContextTest.java
@@ -0,0 +1,146 @@
+/*
+ * 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;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+import java.io.Serializable;
+
+import org.apache.wicket.MetaDataKey;
+import org.apache.wicket.Session;
+import org.apache.wicket.mock.MockApplication;
+import org.apache.wicket.mock.MockSessionStore;
+import org.apache.wicket.protocol.http.WebApplication;
+import org.apache.wicket.request.Request;
+import org.apache.wicket.util.tester.WicketTestCase;
+import org.junit.jupiter.api.Test;
+
+class DefaultPageContextTest extends WicketTestCase
+{
+
+	private static final String KEY = "TEST";
+
+	private static final MetaDataKey<String> DATA_KEY = new MetaDataKey<String>()
+	{
+	};
+
+	MockSessionStore sessionStore = new MockSessionStore() {
+		public void setAttribute(Request request, String name, Serializable value) {
+			super.setAttribute(request, name, value);
+			
+			attributeSet = true;
+		}
+		
+		public void flushSession(Request request, Session session) {
+			super.flushSession(request, session);
+			
+			sessionFlushed = true;
+		}
+	};
+	
+	private boolean attributeSet;
+	
+	private boolean sessionFlushed;
+	
+	@Override
+	protected WebApplication newApplication()
+	{
+		return new MockApplication() {
+			@Override
+			protected void init()
+			{
+				setSessionStoreProvider(() -> {
+					return sessionStore;
+				});
+			}
+		};
+	}
+	
+	@Test
+	void testAttribute()
+	{
+		var context = new DefaultPageContext();
+		
+		assertEquals(null, context.<String>getSessionAttribute(KEY, null));
+		assertFalse(attributeSet);
+	}
+
+	@Test
+	void testAttributeWithDefault()
+	{
+		var context = new DefaultPageContext();
+		
+		assertEquals("FOO", context.<String>getSessionAttribute(KEY, () -> {
+			return "FOO";
+		}));
+		assertTrue(attributeSet);
+	}
+
+	@Test
+	void testAttributeExistsWithDefault()
+	{
+		tester.getSession().bind();
+		tester.getSession().setAttribute(KEY, "EXISTS");
+		
+		var context = new DefaultPageContext();
+		
+		assertEquals("EXISTS", context.<String>getSessionAttribute(KEY, () -> {
+			return "FOO";
+		}));
+		assertTrue(attributeSet);
+	}
+
+	@Test
+	void testData()
+	{
+		var context = new DefaultPageContext();
+		
+		assertEquals(null, context.<String>getSessionData(DATA_KEY, null));
+		
+		tester.getSession().internalDetach();
+		assertFalse(sessionFlushed);
+	}
+
+	@Test
+	void testDataWithDefault()
+	{
+		var context = new DefaultPageContext();
+		
+		assertEquals("FOO", context.<String>getSessionData(DATA_KEY, () -> {
+			return "FOO";
+		}));
+		tester.getSession().internalDetach();
+		assertTrue(sessionFlushed);
+	}
+
+	@Test
+	void testDataExistsWithDefault()
+	{
+		tester.getSession().setMetaData(DATA_KEY, "EXISTS");
+		tester.getSession().bind();
+		
+		var context = new DefaultPageContext();
+		
+		assertEquals("EXISTS", context.<String>getSessionData(DATA_KEY, () -> {
+			return "FOO";
+		}));
+		tester.getSession().internalDetach();
+		assertTrue(sessionFlushed);
+	}
+}