Skip to content

Commit

Permalink
enable resource.resolver.enable.vanitypath, separate unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanseifert committed Dec 7, 2023
1 parent 3e69338 commit 94c666b
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 13 deletions.
3 changes: 2 additions & 1 deletion changes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
Update to latest Sling Mock.
</action>
<action type="update" dev="sseifert" issue="29">
Resource Resolver Factory: Switch back to resource.resolver.optimize.alias.resolution=true (which is default) to avoid warning.
Resource Resolver Factory: Set resource.resolver.optimize.alias.resolution=true and resource.resolver.enable.vanitypath=true to use the optimized sling:alias resolution.
As a result, sling:alias resolution is only supported with JCR_OAK resource resolver type.
</action>
</release>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,6 @@ protected final Map<String, Object> resourceResolverFactoryActivatorPropsMergeWi
});
props.put("resource.resolver.map.location", "/etc/map");
props.put("resource.resolver.default.vanity.redirect.status", "302");
props.put("resource.resolver.enable.vanitypath", false);
props.put("resource.resolver.vanitypath.maxEntries", -1);
props.put("resource.resolver.vanitypath.bloomfilter.maxBytes", 1024000);
props.put(ResourceResolverFactoryConfigPropertyNames.getVanityPathAllowListPropertyName(), new String[] {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* #%L
* wcm.io
* %%
* Copyright (C) 2023 wcm.io
* %%
* 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.
* #L%
*/
package io.wcm.testing.mock.aem.context;

import static com.day.cq.commons.jcr.JcrConstants.JCR_PRIMARYTYPE;
import static com.day.cq.commons.jcr.JcrConstants.NT_UNSTRUCTURED;
import static org.junit.Assert.assertEquals;

import java.util.Map;

import org.apache.sling.api.resource.PersistenceException;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.testing.mock.sling.ResourceResolverType;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;

import io.wcm.testing.mock.aem.junit.AemContext;
import io.wcm.testing.mock.aem.junit.AemContextBuilder;

public class SlingAlias_JcrMockTest {

@Rule
public AemContext context = new AemContextBuilder(ResourceResolverType.JCR_MOCK)
/*
* The optimized alias resolution is based on ResourceChangeListener, which is not supported with JCR_MOCK,
* because JCR_MOCK does not support JCR Observation events. So, to use sling:alias with JCR_MOCK
* we have to disabled the optimized alias resolution.
* But be warned: This old code path is deprecated and likely to be removed in future Sling versions.
* See SLING-12054 and SLING-12025 as references.
*/
.resourceResolverFactoryActivatorProps(Map.of("resource.resolver.optimize.alias.resolution", false))
.build();

private String contentRoot;

@Before
public void setUp() throws Exception {
contentRoot = context.uniqueRoot().content() + "/sample";
}

@Test
public void testSlingAlias() throws PersistenceException {
Resource resource = context.create().resource(contentRoot + "/myresource",
JCR_PRIMARYTYPE, NT_UNSTRUCTURED,
"sling:alias", "myalias");
context.resourceResolver().commit();

assertEquals(contentRoot + "/myresource", resource.getPath());
assertEquals(contentRoot + "/myalias", context.resourceResolver().map(resource.getPath()));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@
import static com.day.cq.commons.jcr.JcrConstants.NT_UNSTRUCTURED;
import static org.junit.Assert.assertEquals;

import java.util.Map;

import org.apache.sling.api.resource.PersistenceException;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.testing.mock.sling.ResourceResolverType;
import org.junit.Before;
Expand All @@ -33,12 +32,10 @@

import io.wcm.testing.mock.aem.junit.AemContext;

public class AemContextImpl_SlingAliasTest {
public class SlingAlias_JcrOakTest {

@Rule
public AemContext context = TestAemContext.newAemContextBuilder()
.resourceResolverFactoryActivatorProps(Map.of("resource.resolver.optimize.alias.resolution", false))
.build();
public AemContext context = new AemContext(ResourceResolverType.JCR_OAK);

private String contentRoot;

Expand All @@ -48,14 +45,12 @@ public void setUp() throws Exception {
}

@Test
public void testSlingAlias() {
if (context.resourceResolverType() == ResourceResolverType.RESOURCERESOLVER_MOCK) {
// sling:alias is not supported for RESOURCERESOLVER_MOCK
return;
}
public void testSlingAlias() throws PersistenceException {
Resource resource = context.create().resource(contentRoot + "/myresource",
JCR_PRIMARYTYPE, NT_UNSTRUCTURED,
"sling:alias", "myalias");
context.resourceResolver().commit();

assertEquals(contentRoot + "/myresource", resource.getPath());
assertEquals(contentRoot + "/myalias", context.resourceResolver().map(resource.getPath()));
}
Expand Down

0 comments on commit 94c666b

Please sign in to comment.