Skip to content

Commit

Permalink
separate class for unit tests with sling:alias, with having resource.…
Browse files Browse the repository at this point in the history
…resolver.optimize.alias.resolution disabled
  • Loading branch information
stefanseifert committed Dec 7, 2023
1 parent 97285e6 commit aa14ed0
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,13 @@
*/
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 static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;

import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceResolverFactory;
import org.apache.sling.testing.mock.sling.ResourceResolverType;
import org.apache.sling.testing.mock.sling.loader.ContentLoader;
import org.junit.After;
import org.junit.Before;
Expand Down Expand Up @@ -140,17 +137,4 @@ public void testSetCurrentPageViaSetResource_Resource() {
assertEquals(contentRoot + "/toolbar/jcr:content", context.currentResource().getPath());
}

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

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* #%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.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;

public class AemContextImpl_SlingAliasTest {

@Rule
public AemContext context = TestAemContext.newAemContextBuilder()
.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() {
if (context.resourceResolverType() == ResourceResolverType.RESOURCERESOLVER_MOCK) {
// sling:alias is not supported for RESOURCERESOLVER_MOCK
return;
}
Resource resource = context.create().resource(contentRoot + "/myresource",
JCR_PRIMARYTYPE, NT_UNSTRUCTURED,
"sling:alias", "myalias");
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 @@ -32,6 +32,7 @@

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import io.wcm.testing.mock.aem.junit.AemContext;
import io.wcm.testing.mock.aem.junit.AemContextBuilder;
import io.wcm.testing.mock.aem.junit.AemContextCallback;

public final class TestAemContext {
Expand Down Expand Up @@ -59,6 +60,15 @@ private TestAemContext() {
return new AemContext(new SetUpCallback(), resourceResolverTypes);
}

public static @NotNull AemContextBuilder newAemContextBuilder() {
return newAemContextBuilder(ALL_TYPES);
}

public static @NotNull AemContextBuilder newAemContextBuilder(ResourceResolverType... resourceResolverTypes) {
return new AemContextBuilder(resourceResolverTypes)
.afterSetUp(new SetUpCallback());
}

/**
* Custom set up rules required in all unit tests.
*/
Expand Down

0 comments on commit aa14ed0

Please sign in to comment.