This is an automated email from the ASF dual-hosted git repository. sseifert pushed a commit to branch feature/SLING-13050-integration-tests-single-pom in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-models-impl.git
commit 32e378412e9cf193d9e6d93a5f05570ff82e4ce3 Author: Stefan Seifert <[email protected]> AuthorDate: Fri Jan 23 11:32:26 2026 +0100 SLING-13050 add ITs --- pom.xml | 24 +- .../launcher-repoinit.txt | 0 src/test/{features => it-features}/launcher.json | 0 .../customizers/SM_TeleporterCustomizer.java | 60 +++ .../apache/sling/models/testing/DecoratedIT.java | 96 ++++ .../sling/models/testing/ImplementsExtendsIT.java | 150 +++++++ .../testing/InjectorSpecificAnnotationIT.java | 72 +++ .../sling/models/testing/ModelFactorySimpleIT.java | 130 ++++++ .../sling/models/testing/PathBoundServletIT.java | 66 +++ .../ServiceInjectionWithDifferentRankingIT.java | 147 ++++++ .../org/apache/sling/models/testing/SimpleIT.java | 111 +++++ .../org/apache/sling/models/testing/ViaIT.java | 77 ++++ .../models/testing/delegate/DelegateRequestIT.java | 114 +++++ .../testing/delegate/DelegateResourceIT.java | 105 +++++ .../ViaOriginalResourceDelegationIT.java | 139 ++++++ .../sling/models/testing/helper/FakeRequest.java | 400 +++++++++++++++++ .../sling/models/testing/helper/FakeResponse.java | 191 ++++++++ .../sling/models/testing/rtbound/FakeRequest.java | 498 +++++++++++++++++++++ .../testing/rtbound/ResourceTypeBindingIT.java | 146 ++++++ .../rtboundpicker/ResourceTypePickerIT.java | 91 ++++ 20 files changed, 2616 insertions(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 1f93553..94116ec 100644 --- a/pom.xml +++ b/pom.xml @@ -391,8 +391,8 @@ <artifactId>slingfeature-maven-plugin</artifactId> <version>1.9.2</version> <extensions>true</extensions> - <configuration> + <features>src/test/it-features</features> <replacePropertyVariables>models.api.version,it.models.log.level</replacePropertyVariables> <skipAddFeatureDependencies>true</skipAddFeatureDependencies> <aggregates> @@ -487,6 +487,28 @@ </executions> </plugin> + <plugin> + <artifactId>maven-failsafe-plugin</artifactId> + <configuration> + <systemPropertyVariables> + <launchpad.http.server.url>http://localhost:${http.port}</launchpad.http.server.url> + <starter.http.test.ports>false:${http.port}</starter.http.test.ports> + <starter.min.bundles.count>${starter.min.bundles.count}</starter.min.bundles.count> + <!-- Comma-separated list of paths to check for 200 status --> + <starter.check.paths>/system/console/bundles,</starter.check.paths> + </systemPropertyVariables> + </configuration> + <executions> + <execution> + <id>integration-test</id> + <goals> + <goal>integration-test</goal> + <goal>verify</goal> + </goals> + </execution> + </executions> + </plugin> + </plugins> </build> diff --git a/src/test/features/launcher-repoinit.txt b/src/test/it-features/launcher-repoinit.txt similarity index 100% rename from src/test/features/launcher-repoinit.txt rename to src/test/it-features/launcher-repoinit.txt diff --git a/src/test/features/launcher.json b/src/test/it-features/launcher.json similarity index 100% rename from src/test/features/launcher.json rename to src/test/it-features/launcher.json diff --git a/src/test/java/org/apache/sling/junit/teleporter/customizers/SM_TeleporterCustomizer.java b/src/test/java/org/apache/sling/junit/teleporter/customizers/SM_TeleporterCustomizer.java new file mode 100644 index 0000000..ed04105 --- /dev/null +++ b/src/test/java/org/apache/sling/junit/teleporter/customizers/SM_TeleporterCustomizer.java @@ -0,0 +1,60 @@ +/* + * 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.junit.teleporter.customizers; + +import java.io.IOException; +import java.net.URI; +import java.util.concurrent.TimeoutException; + +import org.apache.sling.junit.rules.TeleporterRule; +import org.apache.sling.models.impl.ModelAdapterFactory; +import org.apache.sling.testing.clients.ClientException; +import org.apache.sling.testing.clients.osgi.OsgiConsoleClient; +import org.apache.sling.testing.serversetup.instance.SlingTestBase; +import org.apache.sling.testing.teleporter.client.ClientSideTeleporter; +import org.apache.sling.testing.timeouts.TimeoutsProvider; + +/** This is required by the TeleporterRule, to setup the client-side + * teleporter with (at least) the test server URL. + */ +public class SM_TeleporterCustomizer implements TeleporterRule.Customizer { + + private static final SlingTestBase S = new SlingTestBase(); + + private static final Class[] EXPECTED_COMPONENTS = new Class[] {ModelAdapterFactory.class}; + + @Override + public void customize(TeleporterRule t, String options) { + final ClientSideTeleporter cst = (ClientSideTeleporter) t; + cst.setBaseUrl(S.getServerBaseUrl()); + cst.setServerCredentials(S.getServerUsername(), S.getServerPassword()); + cst.setTestReadyTimeoutSeconds(TimeoutsProvider.getInstance().getTimeout(5)); + cst.includeDependencyPrefix("org.apache.sling.models.testing"); + + // additionally check for the registration of mandatory sling models components + try (OsgiConsoleClient osgiClient = + new OsgiConsoleClient(URI.create(S.getServerBaseUrl()), S.getServerUsername(), S.getServerPassword())) { + for (Class clazz : EXPECTED_COMPONENTS) { + osgiClient.waitComponentRegistered(clazz.getName(), 20000, 200); + } + } catch (ClientException | TimeoutException | InterruptedException | IOException ex) { + throw new RuntimeException("Error waiting for expected components.", ex); + } + } +} diff --git a/src/test/java/org/apache/sling/models/testing/DecoratedIT.java b/src/test/java/org/apache/sling/models/testing/DecoratedIT.java new file mode 100644 index 0000000..8c0a39b --- /dev/null +++ b/src/test/java/org/apache/sling/models/testing/DecoratedIT.java @@ -0,0 +1,96 @@ +/* + * 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.models.testing; + +import javax.jcr.Node; +import javax.jcr.Session; + +import org.apache.commons.lang3.RandomStringUtils; +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.api.resource.ResourceWrapper; +import org.apache.sling.junit.rules.TeleporterRule; +import org.apache.sling.models.factory.ModelFactory; +import org.apache.sling.models.it.models.SelfModel; +import org.junit.After; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; + +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +public class DecoratedIT { + + @Rule + public final TeleporterRule teleporter = TeleporterRule.forClass(getClass(), "SM_Teleporter"); + + private ModelFactory modelFactory; + + private ResourceResolver resolver; + private Resource resourceWithDefaultWrapperBehavior; + private Resource resourceWithCustomAdaptToWrapper; + + @Before + @SuppressWarnings("null") + public void setUp() throws Exception { + ResourceResolverFactory rrFactory = teleporter.getService(ResourceResolverFactory.class); + modelFactory = teleporter.getService(ModelFactory.class); + resolver = rrFactory.getServiceResourceResolver(null); + Session session = resolver.adaptTo(Session.class); + Node rootNode = session.getRootNode(); + Node createdNode = rootNode.addNode("test_" + RandomStringUtils.randomAlphanumeric(10)); + createdNode.setProperty("decorate", true); + session.save(); + + resourceWithDefaultWrapperBehavior = resolver.getResource(createdNode.getPath()); + + createdNode = rootNode.addNode("test_" + RandomStringUtils.randomAlphanumeric(10)); + createdNode.setProperty("decorate", "customAdaptTo"); + session.save(); + + resourceWithCustomAdaptToWrapper = resolver.getResource(createdNode.getPath()); + } + + @After + public void tearDown() throws Exception { + resolver.delete(resourceWithDefaultWrapperBehavior); + resolver.delete(resourceWithCustomAdaptToWrapper); + resolver.close(); + } + + @Test + public void testInjectDecoratedResourceUsingCreateModel() { + assertTrue("Resource is not wrapped", resourceWithDefaultWrapperBehavior instanceof ResourceWrapper); + SelfModel model = modelFactory.createModel(resourceWithDefaultWrapperBehavior, SelfModel.class); + + assertNotNull("Model is null", model); + assertTrue("Model is not wrapped", model.getResource() instanceof ResourceWrapper); + } + + @Test + public void testInjectDecoratedResourceUsingAdaptTo() { + assertTrue("Resource is not wrapped", resourceWithCustomAdaptToWrapper instanceof ResourceWrapper); + SelfModel model = resourceWithCustomAdaptToWrapper.adaptTo(SelfModel.class); + + assertNotNull("Model is null", model); + assertTrue("Model is not wrapped", model.getResource() instanceof ResourceWrapper); + } +} diff --git a/src/test/java/org/apache/sling/models/testing/ImplementsExtendsIT.java b/src/test/java/org/apache/sling/models/testing/ImplementsExtendsIT.java new file mode 100644 index 0000000..cc42661 --- /dev/null +++ b/src/test/java/org/apache/sling/models/testing/ImplementsExtendsIT.java @@ -0,0 +1,150 @@ +/* + * 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.models.testing; + +import javax.jcr.Node; +import javax.jcr.RepositoryException; +import javax.jcr.Session; + +import org.apache.commons.lang3.RandomStringUtils; +import org.apache.sling.api.adapter.AdapterManager; +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.junit.rules.TeleporterRule; +import org.apache.sling.models.it.implpicker.CustomLastImplementationPicker; +import org.apache.sling.models.it.models.implextend.ImplementsInterfacePropertyModel; +import org.apache.sling.models.it.models.implextend.ImplementsInterfacePropertyModel2; +import org.apache.sling.models.it.models.implextend.InvalidSampleServiceInterface; +import org.apache.sling.models.it.models.implextend.SampleServiceInterface; +import org.apache.sling.models.it.models.implextend.SimplePropertyModel; +import org.junit.After; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; + +public class ImplementsExtendsIT { + + @Rule + public final TeleporterRule teleporter = TeleporterRule.forClass(getClass(), "SM_Teleporter"); + + private AdapterManager adapterManager; + + private String firstValue; + private String secondValue; + private String thirdValue; + private ResourceResolver resolver; + private Resource resource; + private Node createdNode; + + @Before + @SuppressWarnings("null") + public void setUp() throws Exception { + ResourceResolverFactory rrFactory = teleporter.getService(ResourceResolverFactory.class); + adapterManager = teleporter.getService(AdapterManager.class); + firstValue = RandomStringUtils.randomAlphanumeric(10); + thirdValue = RandomStringUtils.randomAlphanumeric(10); + + resolver = rrFactory.getServiceResourceResolver(null); + Session session = resolver.adaptTo(Session.class); + Node rootNode = session.getRootNode(); + createdNode = rootNode.addNode("test_" + RandomStringUtils.randomAlphanumeric(10)); + createdNode.setProperty("first", firstValue); + createdNode.setProperty("third", thirdValue); + session.save(); + + resource = resolver.getResource(createdNode.getPath()); + } + + @After + public void after() throws Exception { + if (resolver != null) { + resolver.close(); + } + } + + /** + * Try to adapt to interface, with an different implementation class that has the @Model annotation + */ + @Test + public void testImplementsInterfaceModel() { + SampleServiceInterface model = adapterManager.getAdapter(resource, SampleServiceInterface.class); + assertNotNull(model); + assertEquals(ImplementsInterfacePropertyModel.class, model.getClass()); + assertEquals(firstValue + "|" + secondValue + "|" + thirdValue, model.getAllProperties()); + } + + /** + * Ensure that the implementation class itself can be adapted to, even if it is not part of the "adapter" property in the annotation. + */ + @Test + public void testImplementsInterfaceModel_ImplClassImplicitlyMapped() { + ImplementsInterfacePropertyModel model = + adapterManager.getAdapter(resource, ImplementsInterfacePropertyModel.class); + assertNotNull(model); + } + + /** + * Test implementation class with a mapping that is not valid (an interface that is not implemented). + */ + @Test + public void testInvalidImplementsInterfaceModel() { + InvalidSampleServiceInterface model = adapterManager.getAdapter(resource, InvalidSampleServiceInterface.class); + assertNull(model); + } + + /** + * Test to adapt to a superclass of the implementation class with the appropriate mapping in the @Model annotation. + */ + @Test + public void testExtendsClassModel() { + SimplePropertyModel model = adapterManager.getAdapter(resource, SimplePropertyModel.class); + assertNotNull(model); + assertEquals("!" + firstValue + "|" + secondValue + "|" + thirdValue + "!", model.getAllProperties()); + } + + /** + * Try to adapt to interface, with an different implementation class that has the @Model annotation + */ + @Test + @SuppressWarnings("null") + public void testImplementsInterfaceModelWithPickLastImplementationPicker() throws RepositoryException { + + Session session = resolver.adaptTo(Session.class); + Node node = resource.adaptTo(Node.class); + Node childNode = node.addNode(CustomLastImplementationPicker.CUSTOM_NAME); + childNode.setProperty("first", firstValue); + childNode.setProperty("third", thirdValue); + session.save(); + + Resource childResource = resolver.getResource(childNode.getPath()); + + SampleServiceInterface model = adapterManager.getAdapter(childResource, SampleServiceInterface.class); + assertNotNull(model); + assertEquals(ImplementsInterfacePropertyModel2.class, model.getClass()); + assertEquals(firstValue + "|" + secondValue + "|" + thirdValue, model.getAllProperties()); + + childNode.remove(); + session.save(); + } +} diff --git a/src/test/java/org/apache/sling/models/testing/InjectorSpecificAnnotationIT.java b/src/test/java/org/apache/sling/models/testing/InjectorSpecificAnnotationIT.java new file mode 100644 index 0000000..3764dbc --- /dev/null +++ b/src/test/java/org/apache/sling/models/testing/InjectorSpecificAnnotationIT.java @@ -0,0 +1,72 @@ +/* + * 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.models.testing; + +import javax.jcr.Node; +import javax.jcr.Session; + +import org.apache.commons.lang3.RandomStringUtils; +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.junit.rules.TeleporterRule; +import org.apache.sling.models.it.models.SlingPropertyAnnotationTestModel; +import org.junit.Rule; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +public class InjectorSpecificAnnotationIT { + + @Rule + public final TeleporterRule teleporter = TeleporterRule.forClass(getClass(), "SM_Teleporter"); + + @Test + @SuppressWarnings("null") + public void test() throws Exception { + ResourceResolverFactory rrFactory = teleporter.getService(ResourceResolverFactory.class); + String value = RandomStringUtils.randomAlphanumeric(10); + + ResourceResolver resolver = null; + Node createdNode = null; + try { + resolver = rrFactory.getServiceResourceResolver(null); + Session session = resolver.adaptTo(Session.class); + Node rootNode = session.getRootNode(); + createdNode = rootNode.addNode("test_" + RandomStringUtils.randomAlphanumeric(10)); + createdNode.setProperty("testProperty", value); + session.save(); + + Resource resource = resolver.getResource(createdNode.getPath()); + + SlingPropertyAnnotationTestModel model = resource.adaptTo(SlingPropertyAnnotationTestModel.class); + + assertNotNull("Model is null", model); + assertEquals("Test Property is not set correctly", value, model.getTestProperty()); + } finally { + if (createdNode != null) { + createdNode.remove(); + } + if (resolver != null) { + resolver.close(); + } + } + } +} diff --git a/src/test/java/org/apache/sling/models/testing/ModelFactorySimpleIT.java b/src/test/java/org/apache/sling/models/testing/ModelFactorySimpleIT.java new file mode 100644 index 0000000..5468113 --- /dev/null +++ b/src/test/java/org/apache/sling/models/testing/ModelFactorySimpleIT.java @@ -0,0 +1,130 @@ +/* + * 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.models.testing; + +import javax.jcr.Node; +import javax.jcr.Session; + +import org.apache.commons.lang3.RandomStringUtils; +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.junit.rules.TeleporterRule; +import org.apache.sling.models.factory.ModelFactory; +import org.apache.sling.models.it.models.ConstructorInjectionTestModel; +import org.apache.sling.models.it.models.FieldInjectionTestModel; +import org.apache.sling.models.it.models.implextend.InvalidImplementsInterfacePropertyModel; +import org.apache.sling.models.it.models.implextend.SampleServiceInterface; +import org.junit.After; +import org.junit.Before; +import org.junit.Ignore; +import org.junit.Rule; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertSame; +import static org.junit.Assert.assertTrue; + +public class ModelFactorySimpleIT { + + @Rule + public final TeleporterRule teleporter = TeleporterRule.forClass(getClass(), "SM_Teleporter"); + + private ModelFactory modelFactory; + + private String value; + private ResourceResolver resolver; + private Resource resource; + private Node createdNode; + + @Before + @SuppressWarnings("null") + public void setUp() throws Exception { + ResourceResolverFactory rrFactory = teleporter.getService(ResourceResolverFactory.class); + modelFactory = teleporter.getService(ModelFactory.class); + value = RandomStringUtils.randomAlphanumeric(10); + + resolver = rrFactory.getServiceResourceResolver(null); + Session session = resolver.adaptTo(Session.class); + Node rootNode = session.getRootNode(); + createdNode = rootNode.addNode("test_" + RandomStringUtils.randomAlphanumeric(10)); + createdNode.setProperty("testProperty", value); + session.save(); + + resource = resolver.getResource(createdNode.getPath()); + } + + @After + public void tearDown() throws Exception { + if (createdNode != null) { + createdNode.remove(); + } + if (resolver != null) { + resolver.close(); + } + } + + @Test + public void testCreateModel() { + FieldInjectionTestModel model = modelFactory.createModel(resource, FieldInjectionTestModel.class); + + assertNotNull("Model is null", model); + assertEquals("Test Property is not set correctly", value, model.getTestProperty()); + assertNotNull("Filters is null", model.getFilters()); + assertSame("Adaptable is not injected", resource, model.getResource()); + } + + @Ignore + private static final class DummyClass {} + + @Test + public void testIsModelClass() { + assertTrue("Model is not detected as such", modelFactory.isModelClass(ConstructorInjectionTestModel.class)); + assertFalse("Dummy class incorrectly detected as model class", modelFactory.isModelClass(DummyClass.class)); + assertFalse( + "Model with invalid adaptable incorrectly detected as model class", + modelFactory.isModelClass(InvalidImplementsInterfacePropertyModel.class)); + assertTrue( + "Model is not detected as such", + modelFactory.isModelClass(SampleServiceInterface.class)); // being provided by two adapters + } + + @Test + public void testCanCreateFromAdaptable() { + assertTrue( + "Model is not detected as such", + modelFactory.canCreateFromAdaptable(resource, ConstructorInjectionTestModel.class)); + assertTrue( + "Model is not detected as such", + modelFactory.canCreateFromAdaptable(resource, SampleServiceInterface.class)); + assertFalse( + "Model is incorrectly detected", + modelFactory.canCreateFromAdaptable( + new String(), ConstructorInjectionTestModel.class)); // invalid adaptable + } + + @Test() + public void testCanCreateFromAdaptableWithModelExceptin() { + assertFalse( + "Model is incorrectly detected", + modelFactory.canCreateFromAdaptable(resource, DummyClass.class)); // no model class + } +} diff --git a/src/test/java/org/apache/sling/models/testing/PathBoundServletIT.java b/src/test/java/org/apache/sling/models/testing/PathBoundServletIT.java new file mode 100644 index 0000000..bec53f8 --- /dev/null +++ b/src/test/java/org/apache/sling/models/testing/PathBoundServletIT.java @@ -0,0 +1,66 @@ +/* + * 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.models.testing; + +import org.apache.sling.api.resource.ResourceResolver; +import org.apache.sling.api.resource.ResourceResolverFactory; +import org.apache.sling.engine.SlingRequestProcessor; +import org.apache.sling.junit.rules.TeleporterRule; +import org.apache.sling.models.testing.helper.FakeRequest; +import org.apache.sling.models.testing.helper.FakeResponse; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; + +public class PathBoundServletIT { + + @Rule + public final TeleporterRule teleporter = TeleporterRule.forClass(getClass(), "SM_Teleporter"); + + private ResourceResolverFactory rrFactory; + private SlingRequestProcessor slingRequestProcessor; + + @Before + @SuppressWarnings("null") + public void setup() { + rrFactory = teleporter.getService(ResourceResolverFactory.class); + slingRequestProcessor = teleporter.getService(SlingRequestProcessor.class); + } + + @Test + public void testDoubledServlets() throws Exception { + try (ResourceResolver resolver = rrFactory.getServiceResourceResolver(null); ) { + FakeResponse response = new FakeResponse(); + slingRequestProcessor.processRequest(new FakeRequest("/apps/rtpickerrequest"), response, resolver); + + Assert.assertEquals(200, response.getStatus()); + } + } + + @Test + public void testDoubledServletsJakarta() throws Exception { + try (ResourceResolver resolver = rrFactory.getServiceResourceResolver(null); ) { + FakeResponse response = new FakeResponse(); + slingRequestProcessor.processRequest(new FakeRequest("/apps/rtpickerrequest-jakarta"), response, resolver); + + Assert.assertEquals(200, response.getStatus()); + } + } +} diff --git a/src/test/java/org/apache/sling/models/testing/ServiceInjectionWithDifferentRankingIT.java b/src/test/java/org/apache/sling/models/testing/ServiceInjectionWithDifferentRankingIT.java new file mode 100644 index 0000000..7a9b1a0 --- /dev/null +++ b/src/test/java/org/apache/sling/models/testing/ServiceInjectionWithDifferentRankingIT.java @@ -0,0 +1,147 @@ +/* + * 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.models.testing; + +import javax.jcr.Node; +import javax.jcr.Session; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Dictionary; +import java.util.Hashtable; + +import org.apache.commons.lang3.RandomStringUtils; +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.junit.Activator; +import org.apache.sling.junit.rules.TeleporterRule; +import org.apache.sling.models.factory.ModelFactory; +import org.apache.sling.models.it.models.ServiceInjectionTestModel; +import org.apache.sling.models.it.services.SimpleService; +import org.apache.sling.models.it.services.SimpleServiceWithCustomRanking; +import org.junit.After; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.osgi.framework.BundleContext; +import org.osgi.framework.Constants; +import org.osgi.framework.ServiceRegistration; + +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +public class ServiceInjectionWithDifferentRankingIT { + + @Rule + public final TeleporterRule teleporter = TeleporterRule.forClass(getClass(), "SM_Teleporter"); + + private ModelFactory modelFactory; + + private String value; + private ResourceResolver resolver; + private Resource resource; + private Node createdNode; + private BundleContext bundleContext; + private Collection<ServiceRegistration> serviceRegistrations; + + @Before + @SuppressWarnings("null") + public void setUp() throws Exception { + ResourceResolverFactory rrFactory = teleporter.getService(ResourceResolverFactory.class); + modelFactory = teleporter.getService(ModelFactory.class); + value = RandomStringUtils.randomAlphanumeric(10); + + resolver = rrFactory.getServiceResourceResolver(null); + Session session = resolver.adaptTo(Session.class); + Node rootNode = session.getRootNode(); + createdNode = rootNode.addNode("test_" + RandomStringUtils.randomAlphanumeric(10)); + createdNode.setProperty("testProperty", value); + session.save(); + + resource = resolver.getResource(createdNode.getPath()); + + bundleContext = Activator.getBundleContext(); + serviceRegistrations = new ArrayList<ServiceRegistration>(); + } + + @After + public void tearDown() throws Exception { + if (createdNode != null) { + createdNode.remove(); + } + if (resolver != null) { + resolver.close(); + } + + for (ServiceRegistration serviceRegistration : serviceRegistrations) { + serviceRegistration.unregister(); + } + } + + @SuppressWarnings("unchecked") + private void registerSimpleService(int ranking) { + @SuppressWarnings("rawtypes") + Dictionary serviceProps = new Hashtable(); + serviceProps.put(Constants.SERVICE_RANKING, new Integer(ranking)); + ServiceRegistration serviceRegistration = bundleContext.registerService( + SimpleService.class.getName(), new SimpleServiceWithCustomRanking(ranking), serviceProps); + serviceRegistrations.add(serviceRegistration); + } + + @Test + public void testServiceInjectionConsideringRankingWithResource() throws IOException { + + registerSimpleService(0); + // cannot use adaptTo due to adaptersCache + ServiceInjectionTestModel model = modelFactory.createModel(resource, ServiceInjectionTestModel.class); + assertNotNull("Model is null", model); + // only the default service with ranking 0 is known + assertEquals( + "The service with the highest ranking was not returned", + 0, + model.getSimpleService().getRanking()); + assertArrayEquals("Order on injected services is wrong", model.getSimpleServicesRankings(), new Integer[] {0}); + + registerSimpleService(-1000); + model = modelFactory.createModel(resource, ServiceInjectionTestModel.class); + assertNotNull("Model is null", model); + // ranking 0 is still the highest one + assertEquals( + "The service with the highest ranking was not returned", + 0, + model.getSimpleService().getRanking()); + assertArrayEquals( + "Order on injected services is wrong", model.getSimpleServicesRankings(), new Integer[] {0, -1000}); + + registerSimpleService(1000); + model = modelFactory.createModel(resource, ServiceInjectionTestModel.class); + assertNotNull("Model is null", model); + // now ranking 1000 is the highest + assertEquals( + "The service with the highest ranking was not returned", + 1000, + model.getSimpleService().getRanking()); + assertArrayEquals( + "Order on injected services is wrong", model.getSimpleServicesRankings(), new Integer[] {1000, 0, -1000 + }); + } +} diff --git a/src/test/java/org/apache/sling/models/testing/SimpleIT.java b/src/test/java/org/apache/sling/models/testing/SimpleIT.java new file mode 100644 index 0000000..7ab3c7f --- /dev/null +++ b/src/test/java/org/apache/sling/models/testing/SimpleIT.java @@ -0,0 +1,111 @@ +/* + * 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.models.testing; + +import javax.jcr.Node; +import javax.jcr.Session; + +import org.apache.commons.lang3.RandomStringUtils; +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.junit.rules.TeleporterRule; +import org.apache.sling.models.it.models.ConstructorInjectionTestModel; +import org.apache.sling.models.it.models.FieldInjectionTestModel; +import org.apache.sling.models.it.models.InterfaceInjectionTestModel; +import org.junit.After; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertSame; + +public class SimpleIT { + + @Rule + public final TeleporterRule teleporter = TeleporterRule.forClass(getClass(), "SM_Teleporter"); + + private String value; + private String childValue; + private ResourceResolver resolver; + private Resource resource; + private Node createdNode; + + @Before + @SuppressWarnings("null") + public void setUp() throws Exception { + ResourceResolverFactory rrFactory = teleporter.getService(ResourceResolverFactory.class); + value = RandomStringUtils.randomAlphanumeric(10); + childValue = RandomStringUtils.randomAlphanumeric(10); + + resolver = rrFactory.getServiceResourceResolver(null); + Session session = resolver.adaptTo(Session.class); + Node rootNode = session.getRootNode(); + createdNode = rootNode.addNode("test_" + RandomStringUtils.randomAlphanumeric(10)); + createdNode.setProperty("testProperty", value); + Node child = createdNode.addNode("child"); + child.setProperty("childProperty", childValue); + session.save(); + + resource = resolver.getResource(createdNode.getPath()); + } + + @After + public void tearDown() throws Exception { + if (createdNode != null) { + createdNode.remove(); + } + if (resolver != null) { + resolver.close(); + } + } + + @Test + public void testFieldInjection() { + FieldInjectionTestModel model = resource.adaptTo(FieldInjectionTestModel.class); + + assertNotNull("Model is null", model); + assertEquals("Test Property is not set correctly", value, model.getTestProperty()); + assertEquals("Child Test Property is not set correctly", childValue, model.getChildProperty()); + assertNotNull("Filters is null", model.getFilters()); + assertSame("Adaptable is not injected", resource, model.getResource()); + } + + @Test + public void testInterfaceInjection() { + InterfaceInjectionTestModel model = resource.adaptTo(InterfaceInjectionTestModel.class); + + assertNotNull("Model is null", model); + assertEquals("Test Property is not set correctly", value, model.getTestProperty()); + assertNotNull("Filters is null", model.getFilters()); + assertSame("Adaptable is not injected", resource, model.getResource()); + } + + @Test + public void testConstructorInjection() { + ConstructorInjectionTestModel model = resource.adaptTo(ConstructorInjectionTestModel.class); + + assertNotNull("Model is null", model); + assertEquals("Test Property is not set correctly", value, model.getTestProperty()); + assertNotNull("Filters is null", model.getFilters()); + assertSame("Adaptable is not injected", resource, model.getResource()); + } +} diff --git a/src/test/java/org/apache/sling/models/testing/ViaIT.java b/src/test/java/org/apache/sling/models/testing/ViaIT.java new file mode 100644 index 0000000..07f973e --- /dev/null +++ b/src/test/java/org/apache/sling/models/testing/ViaIT.java @@ -0,0 +1,77 @@ +/* + * 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.models.testing; + +import javax.jcr.Node; +import javax.jcr.Session; + +import org.apache.commons.lang3.RandomStringUtils; +import org.apache.sling.api.adapter.AdapterManager; +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.junit.rules.TeleporterRule; +import org.apache.sling.models.it.models.SourceObject; +import org.apache.sling.models.it.models.ViaModel; +import org.junit.Rule; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +public class ViaIT { + + @Rule + public final TeleporterRule teleporter = TeleporterRule.forClass(getClass(), "SM_Teleporter"); + + @Test + @SuppressWarnings("null") + public void test() throws Exception { + ResourceResolverFactory rrFactory = teleporter.getService(ResourceResolverFactory.class); + AdapterManager adapterManager = teleporter.getService(AdapterManager.class); + + String value = RandomStringUtils.randomAlphanumeric(10); + + ResourceResolver resolver = null; + Node createdNode = null; + try { + resolver = rrFactory.getServiceResourceResolver(null); + Session session = resolver.adaptTo(Session.class); + Node rootNode = session.getRootNode(); + createdNode = rootNode.addNode("test_" + RandomStringUtils.randomAlphanumeric(10)); + createdNode.setProperty("testProperty", value); + session.save(); + + Resource resource = resolver.getResource(createdNode.getPath()); + SourceObject obj = new SourceObject(resource); + + ViaModel model = adapterManager.getAdapter(obj, ViaModel.class); + + assertNotNull("Model is null", model); + assertEquals("Test Property is not set correctly", value, model.getTestProperty()); + } finally { + if (createdNode != null) { + createdNode.remove(); + } + if (resolver != null) { + resolver.close(); + } + } + } +} diff --git a/src/test/java/org/apache/sling/models/testing/delegate/DelegateRequestIT.java b/src/test/java/org/apache/sling/models/testing/delegate/DelegateRequestIT.java new file mode 100644 index 0000000..a6b494a --- /dev/null +++ b/src/test/java/org/apache/sling/models/testing/delegate/DelegateRequestIT.java @@ -0,0 +1,114 @@ +/* + * 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.models.testing.delegate; + +import java.util.HashMap; +import java.util.Map; + +import org.apache.sling.api.SlingConstants; +import org.apache.sling.api.resource.LoginException; +import org.apache.sling.api.resource.PersistenceException; +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.api.resource.ResourceUtil; +import org.apache.sling.junit.rules.TeleporterRule; +import org.apache.sling.models.factory.ModelFactory; +import org.apache.sling.models.it.delegate.request.DelegateBaseModel; +import org.apache.sling.models.it.delegate.request.DelegateExtendedModel; +import org.apache.sling.models.it.delegate.request.DelegateInterface; +import org.apache.sling.models.testing.rtbound.FakeRequest; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +public class DelegateRequestIT { + + @Rule + public final TeleporterRule teleporter = TeleporterRule.forClass(getClass(), "SM_Teleporter"); + + private ResourceResolverFactory rrFactory; + + private ModelFactory modelFactory; + + private final String baseComponentPath = "/content/delegate/baseComponent"; + private final String extendedComponentPath = "/content/delegate/extendedComponent"; + + @Before + @SuppressWarnings("null") + public void setup() throws LoginException, PersistenceException { + rrFactory = teleporter.getService(ResourceResolverFactory.class); + modelFactory = teleporter.getService(ModelFactory.class); + try (ResourceResolver adminResolver = rrFactory.getServiceResourceResolver(null); ) { + + Map<String, Object> properties = new HashMap<String, Object>(); + properties.put("text", "baseTESTValue"); + properties.put("other", "baseOther"); + properties.put( + SlingConstants.NAMESPACE_PREFIX + ":" + SlingConstants.PROPERTY_RESOURCE_TYPE, + "sling/delegate/base"); + ResourceUtil.getOrCreateResource(adminResolver, baseComponentPath, properties, null, false); + properties.clear(); + + properties.put("text", "extendedTESTValue"); + properties.put("other", "extendedOther"); + properties.put( + SlingConstants.NAMESPACE_PREFIX + ":" + SlingConstants.PROPERTY_RESOURCE_TYPE, + "sling/delegate/extended"); + ResourceUtil.getOrCreateResource(adminResolver, extendedComponentPath, properties, null, false); + properties.clear(); + + properties.put( + SlingConstants.NAMESPACE_PREFIX + ":" + SlingConstants.PROPERTY_RESOURCE_SUPER_TYPE, + "sling/delegate/base"); + ResourceUtil.getOrCreateResource(adminResolver, "/apps/sling/delegate/extended", properties, null, false); + properties.clear(); + + adminResolver.commit(); + } + } + + @Test + public void testCreateDelegateModel() throws LoginException { + try (ResourceResolver resolver = rrFactory.getServiceResourceResolver(null); ) { + final Resource baseComponentResource = resolver.getResource(baseComponentPath); + assertNotNull(baseComponentResource); + final FakeRequest baseRequest = new FakeRequest(baseComponentResource); + final DelegateInterface modelFromBase = modelFactory.createModel(baseRequest, DelegateInterface.class); + assertNotNull("Base Model should not be null", modelFromBase); + assertTrue("Model should be DelegateBaseModel", modelFromBase instanceof DelegateBaseModel); + assertEquals("baseTESTValue", modelFromBase.getText()); + assertEquals("baseOther", modelFromBase.getOther()); + + final Resource extendedComponentResource = resolver.getResource(extendedComponentPath); + assertNotNull(extendedComponentResource); + final FakeRequest extendedRequest = new FakeRequest(extendedComponentResource); + final DelegateInterface modelFromExtended = + modelFactory.createModel(extendedRequest, DelegateInterface.class); + assertNotNull("Extended Model should not be null", modelFromExtended); + assertTrue("Model should be DelegateExtendedModel", modelFromExtended instanceof DelegateExtendedModel); + assertEquals("EXTENDEDTESTVALUE", modelFromExtended.getText()); + assertEquals("extendedOther", modelFromExtended.getOther()); + } + } +} diff --git a/src/test/java/org/apache/sling/models/testing/delegate/DelegateResourceIT.java b/src/test/java/org/apache/sling/models/testing/delegate/DelegateResourceIT.java new file mode 100644 index 0000000..97f8094 --- /dev/null +++ b/src/test/java/org/apache/sling/models/testing/delegate/DelegateResourceIT.java @@ -0,0 +1,105 @@ +/* + * 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.models.testing.delegate; + +import java.util.HashMap; +import java.util.Map; + +import org.apache.sling.api.SlingConstants; +import org.apache.sling.api.resource.LoginException; +import org.apache.sling.api.resource.PersistenceException; +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.api.resource.ResourceUtil; +import org.apache.sling.junit.rules.TeleporterRule; +import org.apache.sling.models.it.delegate.resource.DelegateBaseModel; +import org.apache.sling.models.it.delegate.resource.DelegateExtendedModel; +import org.apache.sling.models.it.delegate.resource.DelegateInterface; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +public class DelegateResourceIT { + + @Rule + public final TeleporterRule teleporter = TeleporterRule.forClass(getClass(), "SM_Teleporter"); + + private ResourceResolverFactory rrFactory; + + private final String baseComponentPath = "/content/delegate/baseComponent"; + private final String extendedComponentPath = "/content/delegate/extendedComponent"; + + @Before + @SuppressWarnings("null") + public void setup() throws LoginException, PersistenceException { + rrFactory = teleporter.getService(ResourceResolverFactory.class); + try (ResourceResolver adminResolver = rrFactory.getServiceResourceResolver(null); ) { + Map<String, Object> properties = new HashMap<String, Object>(); + properties.put("text", "baseTESTValue"); + properties.put("other", "baseOther"); + properties.put( + SlingConstants.NAMESPACE_PREFIX + ":" + SlingConstants.PROPERTY_RESOURCE_TYPE, + "sling/delegate/base"); + ResourceUtil.getOrCreateResource(adminResolver, baseComponentPath, properties, null, false); + properties.clear(); + + properties.put("text", "extendedTESTValue"); + properties.put("other", "extendedOther"); + properties.put( + SlingConstants.NAMESPACE_PREFIX + ":" + SlingConstants.PROPERTY_RESOURCE_TYPE, + "sling/delegate/extended"); + ResourceUtil.getOrCreateResource(adminResolver, extendedComponentPath, properties, null, false); + properties.clear(); + + properties.put( + SlingConstants.NAMESPACE_PREFIX + ":" + SlingConstants.PROPERTY_RESOURCE_SUPER_TYPE, + "sling/delegate/base"); + ResourceUtil.getOrCreateResource(adminResolver, "/apps/sling/delegate/extended", properties, null, false); + properties.clear(); + + adminResolver.commit(); + } + } + + @Test + public void testCreateDelegateModel() throws LoginException { + try (ResourceResolver resolver = rrFactory.getServiceResourceResolver(null); ) { + final Resource baseComponentResource = resolver.getResource(baseComponentPath); + assertNotNull(baseComponentResource); + final DelegateInterface modelFromBase = baseComponentResource.adaptTo(DelegateInterface.class); + assertNotNull("Model should not be null", modelFromBase); + assertTrue("Model should be DelegateBaseModel", modelFromBase instanceof DelegateBaseModel); + assertEquals("baseTESTValue", modelFromBase.getText()); + assertEquals("baseOther", modelFromBase.getOther()); + + final Resource extendedComponentResource = resolver.getResource(extendedComponentPath); + assertNotNull(extendedComponentResource); + final DelegateInterface modelFromExtended = extendedComponentResource.adaptTo(DelegateInterface.class); + assertNotNull("Model should not be null", modelFromExtended); + assertTrue("Model should be DelegateExtendedModel", modelFromExtended instanceof DelegateExtendedModel); + assertEquals("EXTENDEDTESTVALUE", modelFromExtended.getText()); + assertEquals("extendedOther", modelFromExtended.getOther()); + } + } +} diff --git a/src/test/java/org/apache/sling/models/testing/delegate/viaoriginalresource/ViaOriginalResourceDelegationIT.java b/src/test/java/org/apache/sling/models/testing/delegate/viaoriginalresource/ViaOriginalResourceDelegationIT.java new file mode 100644 index 0000000..ee119b7 --- /dev/null +++ b/src/test/java/org/apache/sling/models/testing/delegate/viaoriginalresource/ViaOriginalResourceDelegationIT.java @@ -0,0 +1,139 @@ +/* + * 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.models.testing.delegate.viaoriginalresource; + +import java.util.HashMap; +import java.util.Map; + +import org.apache.sling.api.resource.LoginException; +import org.apache.sling.api.resource.PersistenceException; +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.api.resource.ResourceUtil; +import org.apache.sling.junit.rules.TeleporterRule; +import org.apache.sling.models.factory.ModelFactory; +import org.apache.sling.models.it.delegate.viaoriginalresource.A; +import org.apache.sling.models.it.delegate.viaoriginalresource.models.A1Impl; +import org.apache.sling.models.it.delegate.viaoriginalresource.models.AImpl; +import org.apache.sling.models.it.delegate.viaoriginalresource.models.B1Impl; +import org.apache.sling.models.it.delegate.viaoriginalresource.models.BImpl; +import org.apache.sling.models.testing.rtbound.FakeRequest; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; + +import static org.junit.Assert.assertSame; +import static org.junit.Assert.assertTrue; + +public class ViaOriginalResourceDelegationIT { + + @Rule + public final TeleporterRule teleporter = TeleporterRule.forClass(getClass(), "SM_Teleporter"); + + private ResourceResolverFactory rrFactory; + + private ModelFactory modelFactory; + + private final String genericComponent = "/apps/delegate/nestedrtbound/generic"; + private final String specificComponent = "/apps/delegate/nestedrtbound/specific"; + private final String genericContent = "/content/delegate/nestedrtbound/generic"; + private final String specificContent = "/content/delegate/nestedrtbound/specific"; + + @Before + @SuppressWarnings("null") + public void setup() throws LoginException, PersistenceException { + rrFactory = teleporter.getService(ResourceResolverFactory.class); + modelFactory = teleporter.getService(ModelFactory.class); + try (ResourceResolver adminResolver = rrFactory.getServiceResourceResolver(null); ) { + + Map<String, Object> properties = new HashMap<String, Object>(); + ResourceUtil.getOrCreateResource(adminResolver, genericComponent, properties, null, false); + properties.clear(); + + properties.put("sling:resourceSuperType", "delegate/nestedrtbound/generic"); + ResourceUtil.getOrCreateResource(adminResolver, specificComponent, properties, null, false); + properties.clear(); + + properties.put("sling:resourceType", "delegate/nestedrtbound/generic"); + ResourceUtil.getOrCreateResource(adminResolver, genericContent, properties, null, false); + properties.clear(); + + properties.put("sling:resourceType", "delegate/nestedrtbound/specific"); + ResourceUtil.getOrCreateResource(adminResolver, specificContent, properties, null, false); + properties.clear(); + + adminResolver.commit(); + } + } + + @Test + public void testGenericModelFromRequest() throws LoginException { + try (ResourceResolver resolver = rrFactory.getServiceResourceResolver(null); ) { + final Resource content = resolver.getResource(genericContent); + final FakeRequest baseRequest = new FakeRequest(content); + + final A model = modelFactory.createModel(baseRequest, A.class); + assertTrue(model instanceof AImpl); + assertTrue(((AImpl) model).other instanceof BImpl); + } + } + + @Test + public void testSpecificModelFromRequest() throws LoginException { + try (ResourceResolver resolver = rrFactory.getServiceResourceResolver(null); ) { + final Resource content = resolver.getResource(specificContent); + final FakeRequest baseRequest = new FakeRequest(content); + + final A model = modelFactory.createModel(baseRequest, A.class); + assertTrue(model instanceof A1Impl); + assertTrue(((A1Impl) model).other instanceof B1Impl); + assertTrue(((A1Impl) model).delegate instanceof AImpl); + assertTrue(((AImpl) ((A1Impl) model).delegate).other instanceof B1Impl); + // Since SLING-11133 and cache = true + assertSame(((A1Impl) model).other, ((AImpl) ((A1Impl) model).delegate).other); + } + } + + @Test + public void testGenericModelFromResource() throws LoginException { + try (ResourceResolver resolver = rrFactory.getServiceResourceResolver(null); ) { + final Resource content = resolver.getResource(genericContent); + + final A model = modelFactory.createModel(content, A.class); + assertTrue(model instanceof AImpl); + assertTrue(((AImpl) model).other instanceof BImpl); + } + } + + @Test + public void testSpecificModelFromResource() throws LoginException { + try (ResourceResolver resolver = rrFactory.getServiceResourceResolver(null); ) { + final Resource content = resolver.getResource(specificContent); + + final A model = modelFactory.createModel(content, A.class); + assertTrue(model instanceof A1Impl); + assertTrue(((A1Impl) model).other instanceof B1Impl); + assertTrue(((A1Impl) model).delegate instanceof AImpl); + assertTrue(((AImpl) ((A1Impl) model).delegate).other instanceof B1Impl); + // Since SLING-11133 and cache = true + assertSame(((A1Impl) model).other, ((AImpl) ((A1Impl) model).delegate).other); + } + } +} diff --git a/src/test/java/org/apache/sling/models/testing/helper/FakeRequest.java b/src/test/java/org/apache/sling/models/testing/helper/FakeRequest.java new file mode 100644 index 0000000..1290f8a --- /dev/null +++ b/src/test/java/org/apache/sling/models/testing/helper/FakeRequest.java @@ -0,0 +1,400 @@ +/* + * 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.models.testing.helper; + +import javax.servlet.AsyncContext; +import javax.servlet.DispatcherType; +import javax.servlet.RequestDispatcher; +import javax.servlet.ServletContext; +import javax.servlet.ServletException; +import javax.servlet.ServletInputStream; +import javax.servlet.ServletRequest; +import javax.servlet.ServletResponse; +import javax.servlet.http.Cookie; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import javax.servlet.http.HttpSession; +import javax.servlet.http.HttpUpgradeHandler; +import javax.servlet.http.Part; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.UnsupportedEncodingException; +import java.security.Principal; +import java.util.Collection; +import java.util.Collections; +import java.util.Enumeration; +import java.util.HashMap; +import java.util.Locale; +import java.util.Map; + +public class FakeRequest implements HttpServletRequest { + + private final String path; + + private final StringBuffer requestUrl; + + private final Map<String, Object> attributes = new HashMap<String, Object>(); + + public FakeRequest(String path) { + this.path = path; + this.requestUrl = new StringBuffer("http://notarealhost").append(path); + } + + @Override + public String getAuthType() { + return null; + } + + @Override + public Cookie[] getCookies() { + return new Cookie[0]; + } + + @Override + public long getDateHeader(String name) { + return 0; + } + + @Override + public String getHeader(String name) { + return null; + } + + @Override + public Enumeration<String> getHeaders(String name) { + return null; + } + + @Override + public Enumeration<String> getHeaderNames() { + return null; + } + + @Override + public int getIntHeader(String name) { + return 0; + } + + @Override + public String getMethod() { + return "GET"; + } + + @Override + public String getPathInfo() { + return path; + } + + @Override + public String getPathTranslated() { + return null; + } + + @Override + public String getContextPath() { + return null; + } + + @Override + public String getQueryString() { + return null; + } + + @Override + public String getRemoteUser() { + return null; + } + + @Override + public boolean isUserInRole(String role) { + return false; + } + + @Override + public Principal getUserPrincipal() { + return null; + } + + @Override + public String getRequestedSessionId() { + return null; + } + + @Override + public String getRequestURI() { + return null; + } + + @Override + public StringBuffer getRequestURL() { + return requestUrl; + } + + @Override + public String getServletPath() { + return ""; + } + + @Override + public HttpSession getSession(boolean create) { + return null; + } + + @Override + public HttpSession getSession() { + return null; + } + + @Override + public boolean isRequestedSessionIdValid() { + return false; + } + + @Override + public boolean isRequestedSessionIdFromCookie() { + return false; + } + + @Override + public boolean isRequestedSessionIdFromURL() { + return false; + } + + @Override + public boolean isRequestedSessionIdFromUrl() { + return false; + } + + @Override + public Object getAttribute(String name) { + return attributes.get(name); + } + + @Override + public Enumeration<String> getAttributeNames() { + return null; + } + + @Override + public String getCharacterEncoding() { + return null; + } + + @Override + public void setCharacterEncoding(String env) throws UnsupportedEncodingException {} + + @Override + public int getContentLength() { + return 0; + } + + @Override + public String getContentType() { + return null; + } + + @Override + public ServletInputStream getInputStream() throws IOException { + return null; + } + + @Override + public String getParameter(String name) { + return null; + } + + @Override + public Enumeration<String> getParameterNames() { + return null; + } + + @Override + public String[] getParameterValues(String name) { + return new String[0]; + } + + @Override + public Map<String, String[]> getParameterMap() { + return Collections.emptyMap(); + } + + @Override + public String getProtocol() { + return null; + } + + @Override + public String getScheme() { + return null; + } + + @Override + public String getServerName() { + return null; + } + + @Override + public int getServerPort() { + return 0; + } + + @Override + public BufferedReader getReader() throws IOException { + return null; + } + + @Override + public String getRemoteAddr() { + return null; + } + + @Override + public String getRemoteHost() { + return null; + } + + @Override + public void setAttribute(String name, Object o) { + attributes.put(name, o); + } + + @Override + public void removeAttribute(String name) { + attributes.remove(name); + } + + @Override + public Locale getLocale() { + return null; + } + + @Override + public Enumeration<Locale> getLocales() { + return Collections.emptyEnumeration(); + } + + @Override + public boolean isSecure() { + return false; + } + + @Override + public RequestDispatcher getRequestDispatcher(String path) { + return null; + } + + @Override + public String getRealPath(String path) { + return null; + } + + @Override + public int getRemotePort() { + return 0; + } + + @Override + public String getLocalName() { + return null; + } + + @Override + public String getLocalAddr() { + return null; + } + + @Override + public int getLocalPort() { + return 0; + } + + @Override + public long getContentLengthLong() { + return 0; + } + + @Override + public ServletContext getServletContext() { + return null; + } + + @Override + public AsyncContext startAsync() throws IllegalStateException { + return null; + } + + @Override + public AsyncContext startAsync(ServletRequest servletRequest, ServletResponse servletResponse) + throws IllegalStateException { + return null; + } + + @Override + public boolean isAsyncStarted() { + return false; + } + + @Override + public boolean isAsyncSupported() { + return false; + } + + @Override + public AsyncContext getAsyncContext() { + return null; + } + + @Override + public DispatcherType getDispatcherType() { + return null; + } + + @Override + public String changeSessionId() { + return null; + } + + @Override + public boolean authenticate(HttpServletResponse response) throws IOException, ServletException { + return false; + } + + @Override + public void login(String username, String password) throws ServletException {} + + @Override + public void logout() throws ServletException {} + + @Override + public Collection<Part> getParts() throws IOException, ServletException { + return null; + } + + @Override + public Part getPart(String name) throws IOException, ServletException { + return null; + } + + @Override + @SuppressWarnings("null") + public <T extends HttpUpgradeHandler> T upgrade(Class<T> handlerClass) throws IOException, ServletException { + return null; + } +} diff --git a/src/test/java/org/apache/sling/models/testing/helper/FakeResponse.java b/src/test/java/org/apache/sling/models/testing/helper/FakeResponse.java new file mode 100644 index 0000000..fa474aa --- /dev/null +++ b/src/test/java/org/apache/sling/models/testing/helper/FakeResponse.java @@ -0,0 +1,191 @@ +/* + * 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.models.testing.helper; + +import javax.servlet.ServletOutputStream; +import javax.servlet.http.Cookie; +import javax.servlet.http.HttpServletResponse; + +import java.io.IOException; +import java.io.PrintWriter; +import java.io.StringWriter; +import java.util.Collection; +import java.util.Locale; + +public class FakeResponse implements HttpServletResponse { + + private StringWriter stringWriter = new StringWriter(); + private String contentType = null; + private String characterEncoding = null; + private int status = -1; + + @Override + public void addCookie(Cookie cookie) {} + + @Override + public boolean containsHeader(String name) { + return false; + } + + @Override + public String encodeURL(String url) { + return null; + } + + @Override + public String encodeRedirectURL(String url) { + return null; + } + + @Override + public String encodeUrl(String url) { + return null; + } + + @Override + public String encodeRedirectUrl(String url) { + return null; + } + + @Override + public void sendError(int sc, String msg) throws IOException {} + + @Override + public void sendError(int sc) throws IOException {} + + @Override + public void sendRedirect(String location) throws IOException {} + + @Override + public void setDateHeader(String name, long date) {} + + @Override + public void addDateHeader(String name, long date) {} + + @Override + public void setHeader(String name, String value) {} + + @Override + public void addHeader(String name, String value) {} + + @Override + public void setIntHeader(String name, int value) {} + + @Override + public void addIntHeader(String name, int value) {} + + @Override + public void setStatus(int sc) { + this.status = sc; + } + + @Override + public void setStatus(int sc, String sm) { + setStatus(sc); + } + + @Override + public String getCharacterEncoding() { + return characterEncoding; + } + + @Override + public String getContentType() { + return contentType; + } + + @Override + public ServletOutputStream getOutputStream() throws IOException { + return null; + } + + @Override + public PrintWriter getWriter() throws IOException { + return new PrintWriter(stringWriter); + } + + @Override + public void setCharacterEncoding(String charset) { + this.characterEncoding = charset; + } + + @Override + public void setContentLength(int len) {} + + @Override + public void setContentType(String type) { + this.contentType = type; + } + + @Override + public void setBufferSize(int size) {} + + @Override + public int getBufferSize() { + return 0; + } + + @Override + public void flushBuffer() throws IOException {} + + @Override + public void resetBuffer() {} + + @Override + public boolean isCommitted() { + return false; + } + + @Override + public void reset() {} + + @Override + public void setLocale(Locale loc) {} + + @Override + public Locale getLocale() { + return null; + } + + public StringWriter getStringWriter() { + return stringWriter; + } + + public int getStatus() { + return status; + } + + @Override + public void setContentLengthLong(long len) {} + + @Override + public String getHeader(String name) { + return null; + } + + @Override + public Collection<String> getHeaders(String name) { + return null; + } + + @Override + public Collection<String> getHeaderNames() { + return null; + } +} diff --git a/src/test/java/org/apache/sling/models/testing/rtbound/FakeRequest.java b/src/test/java/org/apache/sling/models/testing/rtbound/FakeRequest.java new file mode 100644 index 0000000..56a8a78 --- /dev/null +++ b/src/test/java/org/apache/sling/models/testing/rtbound/FakeRequest.java @@ -0,0 +1,498 @@ +/* + * 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.models.testing.rtbound; + +import javax.servlet.AsyncContext; +import javax.servlet.DispatcherType; +import javax.servlet.RequestDispatcher; +import javax.servlet.ServletContext; +import javax.servlet.ServletException; +import javax.servlet.ServletInputStream; +import javax.servlet.ServletRequest; +import javax.servlet.ServletResponse; +import javax.servlet.http.Cookie; +import javax.servlet.http.HttpServletResponse; +import javax.servlet.http.HttpSession; +import javax.servlet.http.HttpUpgradeHandler; +import javax.servlet.http.Part; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.UnsupportedEncodingException; +import java.security.Principal; +import java.util.Collection; +import java.util.Enumeration; +import java.util.HashMap; +import java.util.List; +import java.util.Locale; +import java.util.Map; +import java.util.ResourceBundle; + +import org.apache.sling.api.SlingHttpServletRequest; +import org.apache.sling.api.request.RequestDispatcherOptions; +import org.apache.sling.api.request.RequestParameter; +import org.apache.sling.api.request.RequestParameterMap; +import org.apache.sling.api.request.RequestPathInfo; +import org.apache.sling.api.request.RequestProgressTracker; +import org.apache.sling.api.resource.Resource; +import org.apache.sling.api.resource.ResourceResolver; +import org.jetbrains.annotations.NotNull; + +public class FakeRequest implements SlingHttpServletRequest { + + private final Resource resource; + private final Map<String, Object> attributes = new HashMap<>(); + + public FakeRequest(Resource r) { + this.resource = r; + } + + @Override + public @NotNull Resource getResource() { + return resource; + } + + @Override + @SuppressWarnings("null") + public @NotNull ResourceResolver getResourceResolver() { + return null; + } + + @Override + @SuppressWarnings("null") + public @NotNull RequestPathInfo getRequestPathInfo() { + return null; + } + + @Override + public RequestParameter getRequestParameter(@NotNull String s) { + return null; + } + + @Override + public RequestParameter[] getRequestParameters(@NotNull String s) { + return new RequestParameter[0]; + } + + @Override + @SuppressWarnings("null") + public @NotNull RequestParameterMap getRequestParameterMap() { + return null; + } + + @Override + public RequestDispatcher getRequestDispatcher( + @NotNull String s, RequestDispatcherOptions requestDispatcherOptions) { + return null; + } + + @Override + public RequestDispatcher getRequestDispatcher( + @NotNull Resource resource, RequestDispatcherOptions requestDispatcherOptions) { + return null; + } + + @Override + public RequestDispatcher getRequestDispatcher(@NotNull Resource resource) { + return null; + } + + @Override + public Cookie getCookie(String s) { + return null; + } + + @Override + public String getResponseContentType() { + return null; + } + + @Override + @SuppressWarnings("null") + public @NotNull Enumeration<String> getResponseContentTypes() { + return null; + } + + @Override + public ResourceBundle getResourceBundle(Locale locale) { + return null; + } + + @Override + public ResourceBundle getResourceBundle(String s, Locale locale) { + return null; + } + + @Override + @SuppressWarnings("null") + public RequestProgressTracker getRequestProgressTracker() { + return null; + } + + @Override + public String getAuthType() { + return null; + } + + @Override + public Cookie[] getCookies() { + return new Cookie[0]; + } + + @Override + public long getDateHeader(String s) { + return 0; + } + + @Override + public String getHeader(String s) { + return null; + } + + @Override + public Enumeration<String> getHeaders(String s) { + return null; + } + + @Override + public Enumeration<String> getHeaderNames() { + return null; + } + + @Override + public int getIntHeader(String s) { + return 0; + } + + @Override + public String getMethod() { + return null; + } + + @Override + public String getPathInfo() { + return null; + } + + @Override + public String getPathTranslated() { + return null; + } + + @Override + public String getContextPath() { + return null; + } + + @Override + public String getQueryString() { + return null; + } + + @Override + public String getRemoteUser() { + return null; + } + + @Override + public boolean isUserInRole(String s) { + return false; + } + + @Override + public Principal getUserPrincipal() { + return null; + } + + @Override + public String getRequestedSessionId() { + return null; + } + + @Override + public String getRequestURI() { + return null; + } + + @Override + public StringBuffer getRequestURL() { + return null; + } + + @Override + public String getServletPath() { + return null; + } + + @Override + public HttpSession getSession(boolean b) { + return null; + } + + @Override + public HttpSession getSession() { + return null; + } + + @Override + public boolean isRequestedSessionIdValid() { + return false; + } + + @Override + public boolean isRequestedSessionIdFromCookie() { + return false; + } + + @Override + public boolean isRequestedSessionIdFromURL() { + return false; + } + + @Override + public boolean isRequestedSessionIdFromUrl() { + return false; + } + + @Override + public Object getAttribute(String s) { + return attributes.get(s); + } + + @Override + public Enumeration<String> getAttributeNames() { + return null; + } + + @Override + public String getCharacterEncoding() { + return null; + } + + @Override + public void setCharacterEncoding(String s) throws UnsupportedEncodingException {} + + @Override + public int getContentLength() { + return 0; + } + + @Override + public String getContentType() { + return null; + } + + @Override + public ServletInputStream getInputStream() throws IOException { + return null; + } + + @Override + public String getParameter(String s) { + return null; + } + + @Override + public Enumeration<String> getParameterNames() { + return null; + } + + @Override + public String[] getParameterValues(String s) { + return new String[0]; + } + + @Override + public Map<String, String[]> getParameterMap() { + return null; + } + + @Override + public String getProtocol() { + return null; + } + + @Override + public String getScheme() { + return null; + } + + @Override + public String getServerName() { + return null; + } + + @Override + public int getServerPort() { + return 0; + } + + @Override + public BufferedReader getReader() throws IOException { + return null; + } + + @Override + public String getRemoteAddr() { + return null; + } + + @Override + public String getRemoteHost() { + return null; + } + + @Override + public void setAttribute(String s, Object o) { + attributes.put(s, o); + } + + @Override + public void removeAttribute(String s) {} + + @Override + public Locale getLocale() { + return null; + } + + @Override + public Enumeration<Locale> getLocales() { + return null; + } + + @Override + public boolean isSecure() { + return false; + } + + @Override + public RequestDispatcher getRequestDispatcher(String s) { + return null; + } + + @Override + public String getRealPath(String s) { + return null; + } + + @Override + public int getRemotePort() { + return 0; + } + + @Override + public String getLocalName() { + return null; + } + + @Override + public String getLocalAddr() { + return null; + } + + @Override + public int getLocalPort() { + return 0; + } + + @Override + @SuppressWarnings("null") + public <AdapterType> AdapterType adaptTo(@NotNull Class<AdapterType> aClass) { + return null; + } + + @Override + public String changeSessionId() { + return null; + } + + @Override + public boolean authenticate(HttpServletResponse response) throws IOException, ServletException { + return false; + } + + @Override + public void login(String username, String password) throws ServletException {} + + @Override + public void logout() throws ServletException {} + + @Override + public Collection<Part> getParts() throws IOException, ServletException { + return null; + } + + @Override + public Part getPart(String name) throws IOException, ServletException { + return null; + } + + @Override + @SuppressWarnings("null") + public <T extends HttpUpgradeHandler> T upgrade(Class<T> handlerClass) throws IOException, ServletException { + return null; + } + + @Override + public long getContentLengthLong() { + return 0; + } + + @Override + public ServletContext getServletContext() { + return null; + } + + @Override + public AsyncContext startAsync() throws IllegalStateException { + return null; + } + + @Override + public AsyncContext startAsync(ServletRequest servletRequest, ServletResponse servletResponse) + throws IllegalStateException { + return null; + } + + @Override + public boolean isAsyncStarted() { + return false; + } + + @Override + public boolean isAsyncSupported() { + return false; + } + + @Override + public AsyncContext getAsyncContext() { + return null; + } + + @Override + public DispatcherType getDispatcherType() { + return null; + } + + @Override + @SuppressWarnings("null") + public List<RequestParameter> getRequestParameterList() { + return null; + } +} diff --git a/src/test/java/org/apache/sling/models/testing/rtbound/ResourceTypeBindingIT.java b/src/test/java/org/apache/sling/models/testing/rtbound/ResourceTypeBindingIT.java new file mode 100644 index 0000000..74ec330 --- /dev/null +++ b/src/test/java/org/apache/sling/models/testing/rtbound/ResourceTypeBindingIT.java @@ -0,0 +1,146 @@ +/* + * 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.models.testing.rtbound; + +import java.util.HashMap; +import java.util.Map; + +import org.apache.sling.api.SlingConstants; +import org.apache.sling.api.resource.LoginException; +import org.apache.sling.api.resource.PersistenceException; +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.api.resource.ResourceUtil; +import org.apache.sling.junit.rules.TeleporterRule; +import org.apache.sling.models.factory.ModelFactory; +import org.apache.sling.models.it.rtbound.BaseComponent; +import org.apache.sling.models.it.rtbound.ExtendedComponent; +import org.apache.sling.models.it.rtbound.FromRequestComponent; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; + +public class ResourceTypeBindingIT { + + @Rule + public final TeleporterRule teleporter = TeleporterRule.forClass(getClass(), "SM_Teleporter"); + + private ResourceResolverFactory rrFactory; + + private ModelFactory modelFactory; + + private final String baseComponentPath = "/content/rt/baseComponent"; + private final String childComponentPath = "/content/rt/childComponent"; + private final String child2ComponentPath = "/content/rt/child2Component"; + private final String extendedComponentPath = "/content/rt/extendedComponent"; + private final String fromRequestComponentPath = "/content/rt/fromRequest"; + + @Before + @SuppressWarnings("null") + public void setup() throws LoginException, PersistenceException { + rrFactory = teleporter.getService(ResourceResolverFactory.class); + modelFactory = teleporter.getService(ModelFactory.class); + + try (ResourceResolver adminResolver = rrFactory.getServiceResourceResolver(null); ) { + Map<String, Object> properties = new HashMap<String, Object>(); + properties.put("sampleValue", "baseTESTValue"); + properties.put( + SlingConstants.NAMESPACE_PREFIX + ":" + SlingConstants.PROPERTY_RESOURCE_TYPE, "sling/rt/base"); + ResourceUtil.getOrCreateResource(adminResolver, baseComponentPath, properties, null, false); + properties.clear(); + + properties.put("sampleValue", "childTESTValue"); + properties.put( + SlingConstants.NAMESPACE_PREFIX + ":" + SlingConstants.PROPERTY_RESOURCE_TYPE, "sling/rt/child"); + properties.put( + SlingConstants.NAMESPACE_PREFIX + ":" + SlingConstants.PROPERTY_RESOURCE_SUPER_TYPE, + "sling/rt/base"); + ResourceUtil.getOrCreateResource(adminResolver, childComponentPath, properties, null, false); + properties.clear(); + + properties.put("sampleValue", "childTESTValue2"); + properties.put( + SlingConstants.NAMESPACE_PREFIX + ":" + SlingConstants.PROPERTY_RESOURCE_TYPE, "sling/rt/child"); + ResourceUtil.getOrCreateResource(adminResolver, child2ComponentPath, properties, null, false); + properties.clear(); + + properties.put( + SlingConstants.NAMESPACE_PREFIX + ":" + SlingConstants.PROPERTY_RESOURCE_SUPER_TYPE, + "sling/rt/base"); + ResourceUtil.getOrCreateResource(adminResolver, "/apps/sling/rt/child", properties, null, false); + properties.clear(); + + properties.put("sampleValue", "extendedTESTValue"); + properties.put( + SlingConstants.NAMESPACE_PREFIX + ":" + SlingConstants.PROPERTY_RESOURCE_TYPE, "sling/rt/extended"); + ResourceUtil.getOrCreateResource(adminResolver, extendedComponentPath, properties, null, false); + + properties.clear(); + properties.put( + SlingConstants.NAMESPACE_PREFIX + ":" + SlingConstants.PROPERTY_RESOURCE_TYPE, + "sling/rt/fromRequest"); + ResourceUtil.getOrCreateResource(adminResolver, fromRequestComponentPath, properties, null, false); + + adminResolver.commit(); + } + } + + @Test + public void testClientModelCreateFromResource() throws LoginException { + try (ResourceResolver resolver = rrFactory.getServiceResourceResolver(null); ) { + final Resource baseComponentResource = resolver.getResource(baseComponentPath); + Assert.assertNotNull(baseComponentResource); + final Object baseModel = modelFactory.getModelFromResource(baseComponentResource); + Assert.assertNotNull("Model should not be null", baseModel); + Assert.assertTrue("Model should be a BaseComponent", baseModel instanceof BaseComponent); + + final Resource childComponentResource = resolver.getResource(childComponentPath); + Assert.assertNotNull(childComponentResource); + final Object childModel = modelFactory.getModelFromResource(childComponentResource); + Assert.assertNotNull("Model should not be null", childModel); + Assert.assertTrue("Model should be a BaseComponent", childModel instanceof BaseComponent); + + final Resource child2ComponentResource = resolver.getResource(child2ComponentPath); + Assert.assertNotNull(child2ComponentResource); + final Object child2Model = modelFactory.getModelFromResource(child2ComponentResource); + Assert.assertNotNull("Model should not be null", child2Model); + Assert.assertTrue("Model should be a BaseComponent", child2Model instanceof BaseComponent); + + final Resource extendedComponentResource = resolver.getResource(extendedComponentPath); + Assert.assertNotNull(extendedComponentResource); + final Object extendedModel = modelFactory.getModelFromResource(extendedComponentResource); + Assert.assertNotNull("Model should not be null", extendedModel); + Assert.assertTrue("Model should be a BaseComponent", extendedModel instanceof BaseComponent); + Assert.assertTrue("Model should be an ExtendedComponent", extendedModel instanceof ExtendedComponent); + } + } + + @Test + public void testClientModelCreateFromRequest() throws LoginException { + try (ResourceResolver resolver = rrFactory.getServiceResourceResolver(null); ) { + final Resource baseComponentResource = resolver.getResource(fromRequestComponentPath); + Assert.assertNotNull(baseComponentResource); + final Object baseModel = modelFactory.getModelFromRequest(new FakeRequest(baseComponentResource)); + Assert.assertNotNull("Model should not be null", baseModel); + Assert.assertTrue("Model should be a FromRequestComponent", baseModel instanceof FromRequestComponent); + } + } +} diff --git a/src/test/java/org/apache/sling/models/testing/rtboundpicker/ResourceTypePickerIT.java b/src/test/java/org/apache/sling/models/testing/rtboundpicker/ResourceTypePickerIT.java new file mode 100644 index 0000000..3faa6e9 --- /dev/null +++ b/src/test/java/org/apache/sling/models/testing/rtboundpicker/ResourceTypePickerIT.java @@ -0,0 +1,91 @@ +/* + * 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.models.testing.rtboundpicker; + +import java.util.HashMap; +import java.util.Map; + +import org.apache.sling.api.SlingConstants; +import org.apache.sling.api.resource.LoginException; +import org.apache.sling.api.resource.PersistenceException; +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.api.resource.ResourceUtil; +import org.apache.sling.junit.rules.TeleporterRule; +import org.apache.sling.models.it.rtboundpicker.BaseComponent; +import org.apache.sling.models.it.rtboundpicker.SubRTComponent; +import org.apache.sling.models.it.rtboundpicker.TestComponent; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; + +public class ResourceTypePickerIT { + + @Rule + public final TeleporterRule teleporter = TeleporterRule.forClass(getClass(), "SM_Teleporter"); + + private ResourceResolverFactory rrFactory; + + private final String baseComponentPath = "/content/rtpicker/baseComponent"; + private final String childComponentPath = "/content/rtpicker/childComponent"; + + @Before + @SuppressWarnings("null") + public void setup() throws LoginException, PersistenceException { + rrFactory = teleporter.getService(ResourceResolverFactory.class); + try (ResourceResolver adminResolver = rrFactory.getServiceResourceResolver(null); ) { + Map<String, Object> properties = new HashMap<String, Object>(); + properties.put( + SlingConstants.NAMESPACE_PREFIX + ":" + SlingConstants.PROPERTY_RESOURCE_TYPE, + "sling/rtpicker/base"); + ResourceUtil.getOrCreateResource(adminResolver, baseComponentPath, properties, null, false); + properties.clear(); + + properties.put( + SlingConstants.NAMESPACE_PREFIX + ":" + SlingConstants.PROPERTY_RESOURCE_TYPE, + "sling/rtpicker/sub"); + properties.put( + SlingConstants.NAMESPACE_PREFIX + ":" + SlingConstants.PROPERTY_RESOURCE_SUPER_TYPE, + "sling/rtpicker/base"); + ResourceUtil.getOrCreateResource(adminResolver, childComponentPath, properties, null, false); + properties.clear(); + + adminResolver.commit(); + } + } + + @Test + public void testClientModelCreateFromResource() throws LoginException { + try (ResourceResolver resolver = rrFactory.getServiceResourceResolver(null); ) { + final Resource baseComponentResource = resolver.getResource(baseComponentPath); + Assert.assertNotNull(baseComponentResource); + TestComponent baseModel = baseComponentResource.adaptTo(TestComponent.class); + Assert.assertNotNull("Model should not be null", baseModel); + Assert.assertTrue("Model should be a BaseComponent", baseModel instanceof BaseComponent); + + final Resource childComponentResource = resolver.getResource(childComponentPath); + Assert.assertNotNull(childComponentResource); + baseModel = childComponentResource.adaptTo(TestComponent.class); + Assert.assertNotNull("Model should not be null", baseModel); + Assert.assertTrue("Model should be a SubRTComponent", baseModel instanceof SubRTComponent); + } + } +}
