This is an automated email from the ASF dual-hosted git repository. jsedding pushed a commit to branch resolver-2.x-backports in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-servlets-resolver.git
commit 3b528cd2938d3df91f932c5ce1c1990502c820e5 Author: Carsten Ziegeler <[email protected]> AuthorDate: Wed Apr 23 07:51:04 2025 +0200 SLING-12643 : Support Jakarta ServletAPI in Scripting (Initial support) (cherry picked from commit 7a1631e99467c2c810bb9cf343189f0ab464000c) --- .../bundle/BundledRenderUnitCapabilityImpl.java | 25 ++-- .../internal/bundle/BundledScriptTracker.java | 19 +-- .../BundledRenderUnitCapabilityImplTest.java | 163 +++++++++++++++++++++ 3 files changed, 183 insertions(+), 24 deletions(-) diff --git a/src/main/java/org/apache/sling/servlets/resolver/internal/bundle/BundledRenderUnitCapabilityImpl.java b/src/main/java/org/apache/sling/servlets/resolver/internal/bundle/BundledRenderUnitCapabilityImpl.java index 871836d..56a82d3 100644 --- a/src/main/java/org/apache/sling/servlets/resolver/internal/bundle/BundledRenderUnitCapabilityImpl.java +++ b/src/main/java/org/apache/sling/servlets/resolver/internal/bundle/BundledRenderUnitCapabilityImpl.java @@ -56,13 +56,14 @@ class BundledRenderUnitCapabilityImpl implements BundledRenderUnitCapability { @Nullable String scriptEngineName, @Nullable String scriptExtension) { this.resourceTypes = resourceTypes; - this.path = path; + this.path = path != null && path.isEmpty() ? null : path; this.selectors = selectors; - this.extension = extension; - this.method = method; - this.extendedResourceType = extendedResourceType; - this.scriptEngineName = scriptEngineName; - this.scriptExtension = scriptExtension; + this.extension = extension != null && extension.isEmpty() ? null : extension; + this.method = method != null && method.isEmpty() ? null : method; + this.extendedResourceType = + extendedResourceType != null && extendedResourceType.isEmpty() ? null : extendedResourceType; + this.scriptEngineName = scriptEngineName != null && scriptEngineName.isEmpty() ? null : scriptEngineName; + this.scriptExtension = scriptExtension != null && scriptExtension.isEmpty() ? null : scriptExtension; } @Override @@ -159,34 +160,34 @@ class BundledRenderUnitCapabilityImpl implements BundledRenderUnitCapability { .append("=") .append(selectors); } - if (extension != null && !extension.isEmpty()) { + if (extension != null) { sb.append("; ") .append(ServletResolverConstants.SLING_SERVLET_EXTENSIONS) .append("=") .append(extension); } - if (method != null && !method.isEmpty()) { + if (method != null) { sb.append("; ") .append(ServletResolverConstants.SLING_SERVLET_METHODS) .append("=") .append(method); } - if (path != null && !path.isEmpty()) { + if (path != null) { sb.append("; ") .append(ServletResolverConstants.SLING_SERVLET_PATHS) .append("=") .append(path); } - if (extendedResourceType != null && !extendedResourceType.isEmpty()) { + if (extendedResourceType != null) { sb.append("; ").append(BundledScriptTracker.AT_EXTENDS).append("=").append(extendedResourceType); } - if (scriptEngineName != null && !scriptEngineName.isEmpty()) { + if (scriptEngineName != null) { sb.append("; ") .append(BundledScriptTracker.AT_SCRIPT_ENGINE) .append("=") .append(scriptEngineName); } - if (scriptExtension != null && !scriptExtension.isEmpty()) { + if (scriptExtension != null) { sb.append("; ") .append(BundledScriptTracker.AT_SCRIPT_EXTENSION) .append("=") diff --git a/src/main/java/org/apache/sling/servlets/resolver/internal/bundle/BundledScriptTracker.java b/src/main/java/org/apache/sling/servlets/resolver/internal/bundle/BundledScriptTracker.java index b720475..05380df 100644 --- a/src/main/java/org/apache/sling/servlets/resolver/internal/bundle/BundledScriptTracker.java +++ b/src/main/java/org/apache/sling/servlets/resolver/internal/bundle/BundledScriptTracker.java @@ -236,7 +236,7 @@ public class BundledScriptTracker implements BundleTrackerCustomizer<List<Servic properties.put(ServletResolverConstants.SLING_SERVLET_RESOURCE_TYPES, resourceTypesRegistrationValue); String extension = bundledRenderUnitCapability.getExtension(); - if (extension != null && !extension.isEmpty()) { + if (extension != null) { properties.put(ServletResolverConstants.SLING_SERVLET_EXTENSIONS, extension); } @@ -246,13 +246,12 @@ public class BundledScriptTracker implements BundleTrackerCustomizer<List<Servic bundledRenderUnitCapability.getSelectors().toArray()); } - if (bundledRenderUnitCapability.getMethod() != null - && !bundledRenderUnitCapability.getMethod().isEmpty()) { + if (bundledRenderUnitCapability.getMethod() != null) { properties.put(ServletResolverConstants.SLING_SERVLET_METHODS, bundledRenderUnitCapability.getMethod()); } String extendedResourceTypeString = bundledRenderUnitCapability.getExtendedResourceType(); - if (extendedResourceTypeString != null && !extendedResourceTypeString.isEmpty()) { + if (extendedResourceTypeString != null) { collectInheritanceChain(inheritanceChain, bundleWiring, extendedResourceTypeString, cache); inheritanceChain.stream() .filter(typeProvider -> @@ -282,9 +281,7 @@ public class BundledScriptTracker implements BundleTrackerCustomizer<List<Servic executable = bundledRenderUnitFinder.findUnit(bundle.getBundleContext(), inheritanceChain, aggregate); } } else if (bundledRenderUnitCapability.getPath() != null - && !bundledRenderUnitCapability.getPath().isEmpty() - && bundledRenderUnitCapability.getScriptEngineName() != null - && !bundledRenderUnitCapability.getScriptEngineName().isEmpty()) { + && bundledRenderUnitCapability.getScriptEngineName() != null) { Set<TypeProvider> aggregate = Stream.concat(inheritanceChain.stream(), requiresChain.stream()) .collect(Collectors.toCollection(LinkedHashSet::new)); executable = bundledRenderUnitFinder.findUnit(bundle.getBundleContext(), baseTypeProvider, aggregate); @@ -299,10 +296,8 @@ public class BundledScriptTracker implements BundleTrackerCustomizer<List<Servic } else { if (!bundledRenderUnitCapability.getResourceTypes().isEmpty() && bundledRenderUnitCapability.getSelectors().isEmpty() - && (bundledRenderUnitCapability.getExtension() == null - || bundledRenderUnitCapability.getExtension().isEmpty()) - && (bundledRenderUnitCapability.getMethod() == null - || bundledRenderUnitCapability.getMethod().isEmpty())) { + && bundledRenderUnitCapability.getExtension() == null + && bundledRenderUnitCapability.getMethod() == null) { String scriptName = FilenameUtils.getName(executable.getPath()); String scriptNameNoExtension = scriptName.substring(0, scriptName.lastIndexOf('.')); boolean noMatch = bundledRenderUnitCapability.getResourceTypes().stream() @@ -759,7 +754,7 @@ public class BundledScriptTracker implements BundleTrackerCustomizer<List<Servic Bundle providingBundle = wire.getProvider().getBundle(); providers.add(new TypeProviderImpl(wiredCapability, providingBundle)); String wiredExtends = wiredCapability.getExtendedResourceType(); - if (wiredExtends != null && !wiredExtends.isEmpty()) { + if (wiredExtends != null) { collectInheritanceChain(providers, wire.getProviderWiring(), wiredExtends, cache); } } diff --git a/src/test/java/org/apache/sling/servlets/resolver/internal/bundle/BundledRenderUnitCapabilityImplTest.java b/src/test/java/org/apache/sling/servlets/resolver/internal/bundle/BundledRenderUnitCapabilityImplTest.java new file mode 100644 index 0000000..a02bdfb --- /dev/null +++ b/src/test/java/org/apache/sling/servlets/resolver/internal/bundle/BundledRenderUnitCapabilityImplTest.java @@ -0,0 +1,163 @@ +/* + * 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.sling.servlets.resolver.internal.bundle; + +import java.util.Collections; +import java.util.List; +import java.util.Set; + +import org.apache.sling.api.resource.type.ResourceType; +import org.apache.sling.scripting.spi.bundle.BundledRenderUnitCapability; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; + +public class BundledRenderUnitCapabilityImplTest { + + @Test + public void testNullValues() { + Set<ResourceType> resourceTypes = Collections.emptySet(); + List<String> selectors = Collections.emptyList(); + + BundledRenderUnitCapability capability = new BundledRenderUnitCapabilityImpl.Builder() + .withResourceTypes(resourceTypes) + .withPath(null) + .withSelectors(selectors) + .withExtension(null) + .withMethod(null) + .withExtendedResourceType(null) + .withScriptEngineName(null) + .withScriptEngineExtension(null) + .build(); + + assertNull("Path should be null when null is passed", capability.getPath()); + assertNull("Extension should be null when null is passed", capability.getExtension()); + assertNull("Method should be null when null is passed", capability.getMethod()); + assertNull("ExtendedResourceType should be null when null is passed", capability.getExtendedResourceType()); + assertNull("ScriptEngineName should be null when null is passed", capability.getScriptEngineName()); + assertNull("ScriptExtension should be null when null is passed", capability.getScriptExtension()); + } + + @Test + public void testEmptyStringsReturnNull() { + Set<ResourceType> resourceTypes = Collections.emptySet(); + List<String> selectors = Collections.emptyList(); + + BundledRenderUnitCapability capability = new BundledRenderUnitCapabilityImpl.Builder() + .withResourceTypes(resourceTypes) + .withPath("") + .withSelectors(selectors) + .withExtension("") + .withMethod("") + .withExtendedResourceType("") + .withScriptEngineName("") + .withScriptEngineExtension("") + .build(); + + assertNull("Path should be null when empty string is passed", capability.getPath()); + assertNull("Extension should be null when empty string is passed", capability.getExtension()); + assertNull("Method should be null when empty string is passed", capability.getMethod()); + assertNull( + "ExtendedResourceType should be null when empty string is passed", + capability.getExtendedResourceType()); + assertNull("ScriptEngineName should be null when empty string is passed", capability.getScriptEngineName()); + assertNull("ScriptExtension should be null when empty string is passed", capability.getScriptExtension()); + } + + @Test + public void testNonEmptyValues() { + Set<ResourceType> resourceTypes = Collections.singleton(ResourceType.parseResourceType("test/resourceType")); + List<String> selectors = List.of("selector1", "selector2"); + + BundledRenderUnitCapability capability = new BundledRenderUnitCapabilityImpl.Builder() + .withResourceTypes(resourceTypes) + .withPath("/test/path") + .withSelectors(selectors) + .withExtension("html") + .withMethod("GET") + .withExtendedResourceType("extended/type") + .withScriptEngineName("engineName") + .withScriptEngineExtension("scriptExt") + .build(); + + assertEquals("/test/path", capability.getPath()); + assertEquals("html", capability.getExtension()); + assertEquals("GET", capability.getMethod()); + assertEquals("extended/type", capability.getExtendedResourceType()); + assertEquals("engineName", capability.getScriptEngineName()); + assertEquals("scriptExt", capability.getScriptExtension()); + assertEquals(resourceTypes, capability.getResourceTypes()); + assertEquals(selectors, capability.getSelectors()); + } + + @Test + public void testEmptyCollections() { + Set<ResourceType> resourceTypes = Collections.emptySet(); + List<String> selectors = Collections.emptyList(); + + BundledRenderUnitCapability capability = new BundledRenderUnitCapabilityImpl.Builder() + .withResourceTypes(resourceTypes) + .withSelectors(selectors) + .build(); + + assertTrue( + "ResourceTypes should be empty", capability.getResourceTypes().isEmpty()); + assertTrue("Selectors should be empty", capability.getSelectors().isEmpty()); + } + + @Test + public void testEqualsAndHashCode() { + Set<ResourceType> resourceTypes = Collections.singleton(ResourceType.parseResourceType("test/resourceType")); + List<String> selectors = List.of("selector1"); + + BundledRenderUnitCapability capability1 = new BundledRenderUnitCapabilityImpl.Builder() + .withResourceTypes(resourceTypes) + .withPath("/test/path") + .withSelectors(selectors) + .build(); + + BundledRenderUnitCapability capability2 = new BundledRenderUnitCapabilityImpl.Builder() + .withResourceTypes(resourceTypes) + .withPath("/test/path") + .withSelectors(selectors) + .build(); + + assertEquals("Capabilities should be equal", capability1, capability2); + assertEquals("Hash codes should match", capability1.hashCode(), capability2.hashCode()); + } + + @Test + public void testToString() { + Set<ResourceType> resourceTypes = Collections.singleton(ResourceType.parseResourceType("test/resourceType")); + List<String> selectors = List.of("selector1"); + + BundledRenderUnitCapability capability = new BundledRenderUnitCapabilityImpl.Builder() + .withResourceTypes(resourceTypes) + .withPath("/test/path") + .withSelectors(selectors) + .build(); + + String toString = capability.toString(); + assertTrue("toString should contain resource type", toString.contains("test/resourceType")); + assertTrue("toString should contain path", toString.contains("/test/path")); + assertTrue("toString should contain selector", toString.contains("selector1")); + } +}
