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 197a08f102 WICKET-6981 session attributes have to be reset 197a08f102 is described below commit 197a08f102d520cef1c289e668f6778fd3cfa856 Author: Sven Meier <svenme...@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 1c1480dc6b..ad294be7a1 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 50ae0afe87..b8c432a558 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); + } +}