You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by en...@apache.org on 2026/04/06 21:02:39 UTC
(sling-org-apache-sling-jcr-contentloader) branch master updated: SLING-13153 migrate to jakarta servlet (#33)
This is an automated email from the ASF dual-hosted git repository.
enorman pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-jcr-contentloader.git
The following commit(s) were added to refs/heads/master by this push:
new 8c3360c SLING-13153 migrate to jakarta servlet (#33)
8c3360c is described below
commit 8c3360ce2b713a68a327bf248c3aabb357e7fccd
Author: Eric Norman <en...@apache.org>
AuthorDate: Mon Apr 6 14:02:33 2026 -0700
SLING-13153 migrate to jakarta servlet (#33)
---
pom.xml | 25 +++-
.../internal/ContentLoaderWebConsolePlugin.java | 17 +--
.../ContentLoaderWebConsolePluginTest.java | 160 +++++++++++++++++++++
.../contentloader/internal/ReflectionTools.java | 38 +++++
4 files changed, 228 insertions(+), 12 deletions(-)
diff --git a/pom.xml b/pom.xml
index 95b6e28..006fac8 100644
--- a/pom.xml
+++ b/pom.xml
@@ -222,8 +222,8 @@
</dependency>
<!-- for Web Console plugin -->
<dependency>
- <groupId>javax.servlet</groupId>
- <artifactId>javax.servlet-api</artifactId>
+ <groupId>jakarta.servlet</groupId>
+ <artifactId>jakarta.servlet-api</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
@@ -402,5 +402,26 @@
</plugin>
</plugins>
</reporting>
+ <profiles>
+ <profile>
+ <id>jacoco-report</id>
+ <build>
+ <plugins>
+ <plugin>
+ <groupId>org.jacoco</groupId>
+ <artifactId>jacoco-maven-plugin</artifactId>
+ <configuration>
+ <excludes>
+ <!-- skip binary packages included by bnd -->
+ <exclude>org/kxml2/**</exclude>
+ <exclude>org/xmlpull/**</exclude>
+ <exclude>org/apache/sling/jcr/contentparser/impl/**</exclude>
+ </excludes>
+ </configuration>
+ </plugin>
+ </plugins>
+ </build>
+ </profile>
+ </profiles>
</project>
diff --git a/src/main/java/org/apache/sling/jcr/contentloader/internal/ContentLoaderWebConsolePlugin.java b/src/main/java/org/apache/sling/jcr/contentloader/internal/ContentLoaderWebConsolePlugin.java
index fea75e5..f7fbd9d 100644
--- a/src/main/java/org/apache/sling/jcr/contentloader/internal/ContentLoaderWebConsolePlugin.java
+++ b/src/main/java/org/apache/sling/jcr/contentloader/internal/ContentLoaderWebConsolePlugin.java
@@ -20,9 +20,6 @@ package org.apache.sling.jcr.contentloader.internal;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
-import javax.servlet.GenericServlet;
-import javax.servlet.ServletRequest;
-import javax.servlet.ServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
@@ -36,6 +33,9 @@ import java.util.Spliterators;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;
+import jakarta.servlet.GenericServlet;
+import jakarta.servlet.ServletRequest;
+import jakarta.servlet.ServletResponse;
import org.apache.sling.api.request.ResponseUtil;
import org.apache.sling.commons.osgi.PropertiesUtil;
import org.apache.sling.jcr.api.SlingRepository;
@@ -48,7 +48,7 @@ import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
@Component(
- service = javax.servlet.Servlet.class,
+ service = jakarta.servlet.Servlet.class,
property = {
Constants.SERVICE_VENDOR + "=The Apache Software Foundation",
Constants.SERVICE_DESCRIPTION + "=Apache Sling JCR Content Loader Web Console Plugin",
@@ -162,7 +162,7 @@ public class ContentLoaderWebConsolePlugin extends GenericServlet {
sb.append("<table class='nicetable nested'><thead>");
sb.append("<tr><th>Path</th><th>")
.append(ResponseUtil.escapeXml(entry.getPath()))
- .append("</th><tr>");
+ .append("</th></tr>");
sb.append("</thead><tbody>");
printPathEntryTableRow(sb, "Target Path", ResponseUtil.escapeXml(entry.getTarget()), row++);
// most important directives
@@ -195,11 +195,8 @@ public class ContentLoaderWebConsolePlugin extends GenericServlet {
private URL getResource(final String path) {
if (path.startsWith("/" + RES_LOC)) {
// strip label
- int index = path.indexOf('/', 1);
- if (index <= 0) {
- throw new IllegalStateException("The relativeResourcePrefix must contain at least one '/'");
- }
- return this.getClass().getResource(path.substring(index));
+ String resPath = path.substring(LABEL.length() + 1);
+ return this.getClass().getResource(resPath);
}
return null;
}
diff --git a/src/test/java/org/apache/sling/jcr/contentloader/internal/ContentLoaderWebConsolePluginTest.java b/src/test/java/org/apache/sling/jcr/contentloader/internal/ContentLoaderWebConsolePluginTest.java
new file mode 100644
index 0000000..742ec1f
--- /dev/null
+++ b/src/test/java/org/apache/sling/jcr/contentloader/internal/ContentLoaderWebConsolePluginTest.java
@@ -0,0 +1,160 @@
+/*
+ * 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.jcr.contentloader.internal;
+
+import javax.jcr.RepositoryException;
+import javax.jcr.Session;
+
+import java.io.IOException;
+import java.net.URL;
+import java.util.Calendar;
+import java.util.Dictionary;
+import java.util.Hashtable;
+import java.util.Map;
+
+import org.apache.sling.jcr.contentloader.PathEntry;
+import org.apache.sling.testing.mock.sling.ResourceResolverType;
+import org.apache.sling.testing.mock.sling.junit.SlingContext;
+import org.apache.sling.testing.mock.sling.servlet.MockSlingJakartaHttpServletRequest;
+import org.apache.sling.testing.mock.sling.servlet.MockSlingJakartaHttpServletResponse;
+import org.jetbrains.annotations.NotNull;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.mockito.Mockito;
+import org.osgi.framework.Bundle;
+
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.eq;
+
+/**
+ *
+ */
+public class ContentLoaderWebConsolePluginTest {
+
+ @Rule
+ public final SlingContext context = new SlingContext(ResourceResolverType.JCR_MOCK);
+
+ private ContentLoaderWebConsolePlugin plugin;
+ private BundleHelper mockBundleHelper;
+
+ @Before
+ public void beforeEach() {
+ mockBundleHelper = context.registerService(BundleHelper.class, Mockito.mock(BundleHelper.class));
+
+ plugin = context.registerInjectActivateService(ContentLoaderWebConsolePlugin.class);
+
+ // for link prefix
+ context.jakartaRequest().setAttribute("felix.webconsole.appRoot", "/console");
+ }
+
+ /**
+ * Test method for {@link org.apache.sling.jcr.contentloader.internal.ContentLoaderWebConsolePlugin#service(jakarta.servlet.ServletRequest, jakarta.servlet.ServletResponse)}.
+ */
+ @Test
+ public void testService() throws IOException, RepositoryException {
+ // simulate deployed bundles
+ plugin.context = Mockito.spy(plugin.context);
+ Bundle mockBundle1 = mockBundle(1, "test.bundle1", Map.of());
+ // simulate some content to check for in the output
+ String contentHeader2 = "test-initial-content;path:=/test-content/test.bundle2";
+ Bundle mockBundle2 = mockBundle(2, "test.bundle2", Map.of(PathEntry.CONTENT_HEADER, contentHeader2));
+ String contentHeader3 = "test-initial-content;path:=/test-content/test.bundle3";
+ Bundle mockBundle3 = mockBundle(3, "test.bundle3", Map.of(PathEntry.CONTENT_HEADER, contentHeader3));
+ Bundle[] bundles = new Bundle[] {mockBundle1, mockBundle2, mockBundle3};
+ Mockito.doReturn(bundles).when(plugin.context).getBundles();
+
+ // simulate various values in mockBundleHelper BundleContentInfo for code coverage
+ Map<String, Object> contentInfoMap2 = Map.of();
+ Mockito.doReturn(contentInfoMap2)
+ .when(mockBundleHelper)
+ .getBundleContentInfo(any(Session.class), eq(mockBundle2), eq(false));
+ Map<String, Object> contentInfoMap3 = Map.of(
+ BundleContentLoaderListener.PROPERTY_UNINSTALL_PATHS,
+ new String[] {"default:/test-content/test.bundle2"},
+ BundleContentLoaderListener.PROPERTY_CONTENT_LOADED_AT,
+ Calendar.getInstance(),
+ BundleContentLoaderListener.PROPERTY_CONTENT_LOADED_BY,
+ "testuser1",
+ BundleContentLoaderListener.PROPERTY_CONTENT_LOADED,
+ true);
+ Mockito.doReturn(contentInfoMap3)
+ .when(mockBundleHelper)
+ .getBundleContentInfo(any(Session.class), eq(mockBundle3), eq(false));
+
+ final @NotNull MockSlingJakartaHttpServletRequest req = context.jakartaRequest();
+ final @NotNull MockSlingJakartaHttpServletResponse resp = context.jakartaResponse();
+ plugin.service(req, resp);
+ final String outputAsString = resp.getOutputAsString();
+ assertNotNull(outputAsString);
+ assertTrue(outputAsString.contains("<a href=\"/console/bundles/2\">test.bundle2 (2)</a>"));
+ }
+
+ /**
+ * Mock a deployed bundle
+ */
+ private Bundle mockBundle(long bundleId, String symbolicName, Map<String, String> headersMap) {
+ Bundle mockBundle = Mockito.mock(Bundle.class);
+ Mockito.doReturn(symbolicName).when(mockBundle).getSymbolicName();
+ Dictionary<String, String> headers = new Hashtable<>(headersMap);
+ Mockito.doReturn(headers).when(mockBundle).getHeaders();
+ Mockito.doReturn(bundleId).when(mockBundle).getBundleId();
+ return mockBundle;
+ }
+
+ @Test
+ public void testServiceWithRepositoryException() throws IOException, RepositoryException {
+ // simulate exception thrown during login
+ plugin.repository = Mockito.spy(plugin.repository);
+ Mockito.doThrow(RepositoryException.class).when(plugin.repository).loginService(null, null);
+
+ final @NotNull MockSlingJakartaHttpServletRequest req = context.jakartaRequest();
+ final @NotNull MockSlingJakartaHttpServletResponse resp = context.jakartaResponse();
+ plugin.service(req, resp);
+ final String outputAsString = resp.getOutputAsString();
+ assertTrue(
+ "Expected logged error message", outputAsString.contains("Error accessing the underlying repository"));
+ }
+
+ /**
+ * Test method for {@link
+ * org.apache.sling.jcr.contentloader.internal.ContentLoaderWebConsolePlugin#getResource(java.lang.String)}.
+ */
+ @Test
+ public void testGetResource() throws SecurityException, IllegalArgumentException {
+ final URL value1 = ReflectionTools.invokeMethodWithReflection(
+ plugin, "getResource", new Class[] {String.class}, URL.class, new Object[] {"/invalid"});
+ assertNull(value1);
+
+ final URL value2 = ReflectionTools.invokeMethodWithReflection(
+ plugin, "getResource", new Class[] {String.class}, URL.class, new Object[] {
+ "/jcr-content-loader/res/ui/main.css"
+ });
+ assertNotNull(value2);
+
+ final URL value3 = ReflectionTools.invokeMethodWithReflection(
+ plugin, "getResource", new Class[] {String.class}, URL.class, new Object[] {
+ "/jcr-content-loader/res/ui/invalid.css"
+ });
+ assertNull(value3);
+ }
+}
diff --git a/src/test/java/org/apache/sling/jcr/contentloader/internal/ReflectionTools.java b/src/test/java/org/apache/sling/jcr/contentloader/internal/ReflectionTools.java
index ff5c53a..00570a6 100644
--- a/src/test/java/org/apache/sling/jcr/contentloader/internal/ReflectionTools.java
+++ b/src/test/java/org/apache/sling/jcr/contentloader/internal/ReflectionTools.java
@@ -19,6 +19,8 @@
package org.apache.sling.jcr.contentloader.internal;
import java.lang.reflect.Field;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import static org.junit.Assert.fail;
@@ -32,6 +34,42 @@ public class ReflectionTools {
// hide the public constructor
}
+ public static <T> T invokeMethodWithReflection(
+ Object obj, String methodName, Class<?>[] parameterTypes, Class<T> expectedType, Object[] methodArgs) {
+ Object result = null;
+ try {
+ Class<?> clazz = obj.getClass();
+ Method method = null;
+ do {
+ try { // NOSONAR
+ method = clazz.getDeclaredMethod(methodName, parameterTypes);
+ } catch (NoSuchMethodException nsfe) {
+ clazz = clazz.getSuperclass();
+ }
+ } while (method == null && clazz != null);
+ if (method != null) {
+ if (Modifier.isStatic(method.getModifiers())) {
+ if (!method.canAccess(null)) {
+ method.setAccessible(true);
+ }
+ result = method.invoke(null, methodArgs);
+ } else {
+ if (!method.canAccess(obj)) {
+ method.setAccessible(true);
+ }
+ result = method.invoke(obj, methodArgs);
+ }
+ } else {
+ fail("Failed to find method via reflection: " + methodName);
+ }
+ } catch (IllegalArgumentException | IllegalAccessException | SecurityException e) {
+ fail("Failed to get method via reflection. Reason: " + e.getMessage());
+ } catch (InvocationTargetException e) {
+ fail("Failed to invoke method via reflection. Reason: " + e.getMessage());
+ }
+ return expectedType == null ? null : expectedType.cast(result);
+ }
+
public static <T> T getFieldWithReflection(Object obj, String fieldName, Class<T> expectedType) {
Object result = null;
try {