You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tapestry.apache.org by th...@apache.org on 2023/07/12 02:54:30 UTC
[tapestry-5] 01/02: TAP5-2756: adding ReferenceType.forPages(String...)
This is an automated email from the ASF dual-hosted git repository.
thiagohp pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/tapestry-5.git
commit d6c4324215337b3bfb9ed8e62dd7174e0427410d
Author: Thiago H. de Paula Figueiredo <th...@arsmachina.com.br>
AuthorDate: Tue Jul 11 21:41:36 2023 -0300
TAP5-2756: adding ReferenceType.forPages(String...)
---
.../pageload/PageCachingReferenceTypeService.java | 2 +-
.../tapestry5/services/pageload/ReferenceType.java | 21 ++++++++-
.../tapestry5/services/ReferenceTypeTest.java | 50 ++++++++++++++++++++++
3 files changed, 71 insertions(+), 2 deletions(-)
diff --git a/tapestry-core/src/main/java/org/apache/tapestry5/services/pageload/PageCachingReferenceTypeService.java b/tapestry-core/src/main/java/org/apache/tapestry5/services/pageload/PageCachingReferenceTypeService.java
index d58d8f912..46728d8e7 100644
--- a/tapestry-core/src/main/java/org/apache/tapestry5/services/pageload/PageCachingReferenceTypeService.java
+++ b/tapestry-core/src/main/java/org/apache/tapestry5/services/pageload/PageCachingReferenceTypeService.java
@@ -19,7 +19,7 @@ import org.apache.tapestry5.ioc.annotations.UsesOrderedConfiguration;
/**
* A service which defines whether cache page instances should have soft references pointing to
* it (the default) or strong, non-garbage-collectable ones. This service is a chain of command
- * of itself
+ * of itself.
*
* @see ReferenceType
* @since 5.8.3
diff --git a/tapestry-core/src/main/java/org/apache/tapestry5/services/pageload/ReferenceType.java b/tapestry-core/src/main/java/org/apache/tapestry5/services/pageload/ReferenceType.java
index 979931110..5df36ca75 100644
--- a/tapestry-core/src/main/java/org/apache/tapestry5/services/pageload/ReferenceType.java
+++ b/tapestry-core/src/main/java/org/apache/tapestry5/services/pageload/ReferenceType.java
@@ -14,6 +14,10 @@
package org.apache.tapestry5.services.pageload;
import java.lang.ref.SoftReference;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.Set;
+import java.util.stream.Collectors;
/**
* Defines the types of reference the page cache can use.
@@ -30,5 +34,20 @@ public enum ReferenceType
/**
* Use a strong (normal) reference to cached page instances.
*/
- STRONG
+ STRONG;
+
+ /**
+ * Utility method that returns {@linkplain PageCachingReferenceTypeService}
+ * matching page names in a case-insensitive way.
+ * @return a {@linkplain PageCachingReferenceTypeService} instance.
+ */
+ public PageCachingReferenceTypeService forPages(String... canonicalPageNames)
+ {
+ final Set<String> pageNames = Collections.unmodifiableSet(
+ Arrays.stream(canonicalPageNames).
+ map(String::toLowerCase).
+ collect(Collectors.toSet()));
+ return (p) -> pageNames.contains(p.toLowerCase()) ? this : null;
+ }
+
}
\ No newline at end of file
diff --git a/tapestry-core/src/test/java/org/apache/tapestry5/services/ReferenceTypeTest.java b/tapestry-core/src/test/java/org/apache/tapestry5/services/ReferenceTypeTest.java
new file mode 100644
index 000000000..aa427ca77
--- /dev/null
+++ b/tapestry-core/src/test/java/org/apache/tapestry5/services/ReferenceTypeTest.java
@@ -0,0 +1,50 @@
+// Copyright 2023 The Apache Software Foundation
+//
+// Licensed 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.tapestry5.services;
+
+import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.assertNull;
+
+import org.apache.tapestry5.services.pageload.PageCachingReferenceTypeService;
+import org.apache.tapestry5.services.pageload.ReferenceType;
+import org.testng.annotations.Test;
+
+public class ReferenceTypeTest
+{
+ private static final String PAGE1 = "lib1/Something";
+ private static final String PAGE2 = "lib2/Else";
+ private static final String ANOTHER_PAGE = "another";
+
+ @Test
+ public void forPages()
+ {
+
+ final PageCachingReferenceTypeService instance = ReferenceType.STRONG.forPages(PAGE1, PAGE2);
+
+ assertNull(instance.get(ANOTHER_PAGE));
+
+ assertEquals(instance.get(PAGE1), ReferenceType.STRONG);
+ assertEquals(instance.get(PAGE2), ReferenceType.STRONG);
+ assertEquals(instance.get(PAGE1.toLowerCase()), ReferenceType.STRONG);
+ assertEquals(instance.get(PAGE2.toLowerCase()), ReferenceType.STRONG);
+ assertEquals(instance.get(PAGE1.toUpperCase()), ReferenceType.STRONG);
+ assertEquals(instance.get(PAGE2.toUpperCase()), ReferenceType.STRONG);
+
+ assertNull(instance.get(PAGE1 + "a"));
+ assertNull(instance.get(PAGE2 + "b"));
+
+ }
+
+}