You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by kw...@apache.org on 2021/11/25 08:22:07 UTC

[sling-org-apache-sling-testing-sling-mock] 01/01: SLING-10937 add failing test for comparing resource inside and outside Sling Model

This is an automated email from the ASF dual-hosted git repository.

kwin pushed a commit to branch SLING-10937-unwrapped-adaptable-in-sling-model
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-testing-sling-mock.git

commit ee2ecf53bd9108853a47d6c288266546b09e2caa
Author: Konrad Windszus <kw...@apache.org>
AuthorDate: Thu Nov 25 09:21:50 2021 +0100

    SLING-10937 add failing test for comparing resource inside and outside
    Sling Model
---
 .../AbstractModelAdapterFactoryUtilTest.java       | 10 ++++++
 .../ModelAdapterFactoryUtilClassesTest.java        |  4 ++-
 .../mock/sling/context/models/ResourceModel.java   | 37 ++++++++++++++++++++++
 3 files changed, 50 insertions(+), 1 deletion(-)

diff --git a/core/src/test/java/org/apache/sling/testing/mock/sling/context/AbstractModelAdapterFactoryUtilTest.java b/core/src/test/java/org/apache/sling/testing/mock/sling/context/AbstractModelAdapterFactoryUtilTest.java
index d8bd6ae..d21dc72 100644
--- a/core/src/test/java/org/apache/sling/testing/mock/sling/context/AbstractModelAdapterFactoryUtilTest.java
+++ b/core/src/test/java/org/apache/sling/testing/mock/sling/context/AbstractModelAdapterFactoryUtilTest.java
@@ -22,9 +22,11 @@ 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.commons.mime.MimeTypeService;
 import org.apache.sling.testing.mock.sling.context.models.OsgiServiceModel;
 import org.apache.sling.testing.mock.sling.context.models.RequestAttributeModel;
+import org.apache.sling.testing.mock.sling.context.models.ResourceModel;
 import org.apache.sling.testing.mock.sling.context.models.ServiceInterface;
 import org.apache.sling.testing.mock.sling.junit.SlingContext;
 import org.apache.sling.testing.mock.sling.services.MockMimeTypeService;
@@ -67,4 +69,12 @@ public abstract class AbstractModelAdapterFactoryUtilTest {
         assertEquals("myValue", model.getPropValue());
     }
 
+    @Test
+    public void testResourceInsideAndOutsideModel() {
+        Resource resource = context().currentResource("/");
+        assertNotNull(resource);
+        ResourceModel model = resource.adaptTo(ResourceModel.class);
+        assertNotNull(model);
+        assertEquals(resource, model.getAdaptable());
+    }
 }
diff --git a/core/src/test/java/org/apache/sling/testing/mock/sling/context/ModelAdapterFactoryUtilClassesTest.java b/core/src/test/java/org/apache/sling/testing/mock/sling/context/ModelAdapterFactoryUtilClassesTest.java
index f3b6c44..ab1bb21 100644
--- a/core/src/test/java/org/apache/sling/testing/mock/sling/context/ModelAdapterFactoryUtilClassesTest.java
+++ b/core/src/test/java/org/apache/sling/testing/mock/sling/context/ModelAdapterFactoryUtilClassesTest.java
@@ -20,6 +20,7 @@ package org.apache.sling.testing.mock.sling.context;
 
 import org.apache.sling.testing.mock.sling.context.models.OsgiServiceModel;
 import org.apache.sling.testing.mock.sling.context.models.RequestAttributeModel;
+import org.apache.sling.testing.mock.sling.context.models.ResourceModel;
 import org.apache.sling.testing.mock.sling.context.models.ServiceInterfaceImpl;
 import org.apache.sling.testing.mock.sling.junit.SlingContext;
 import org.junit.Before;
@@ -45,7 +46,8 @@ public class ModelAdapterFactoryUtilClassesTest extends AbstractModelAdapterFact
                 );
         context.addModelsForClasses(
                 RequestAttributeModel.class,
-                ServiceInterfaceImpl.class
+                ServiceInterfaceImpl.class,
+                ResourceModel.class
                 );
     }
 
diff --git a/core/src/test/java/org/apache/sling/testing/mock/sling/context/models/ResourceModel.java b/core/src/test/java/org/apache/sling/testing/mock/sling/context/models/ResourceModel.java
new file mode 100644
index 0000000..4ffb667
--- /dev/null
+++ b/core/src/test/java/org/apache/sling/testing/mock/sling/context/models/ResourceModel.java
@@ -0,0 +1,37 @@
+/*
+ * 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.testing.mock.sling.context.models;
+
+import org.apache.sling.api.resource.Resource;
+import org.apache.sling.models.annotations.Model;
+
+@Model(adaptables = Resource.class)
+public class ResourceModel {
+    
+    private final Resource resource;
+
+    public ResourceModel(Resource resource) {
+        this.resource = resource;
+    }
+
+    public Resource getAdaptable() {
+        return resource;
+    }
+
+}