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/12/15 07:49:45 UTC

[sling-org-apache-sling-models-impl] branch master updated: Add additional tests verifying default interface method behavior (#30)

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

kwin pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-models-impl.git


The following commit(s) were added to refs/heads/master by this push:
     new 89fcb87  Add additional tests verifying default interface method behavior (#30)
89fcb87 is described below

commit 89fcb8702e29ad3322ee2a023ae6b24e5d2f83ab
Author: paul-bjorkstrand <pa...@perficient.com>
AuthorDate: Wed Dec 15 01:49:37 2021 -0600

    Add additional tests verifying default interface method behavior (#30)
---
 .../models/impl/DefaultInterfaceMethodTest.java    | 66 ++++++++++++++++++++++
 .../interfaces/ModelWithDefaultMethods.java        | 33 +++++++++++
 2 files changed, 99 insertions(+)

diff --git a/src/test/java/org/apache/sling/models/impl/DefaultInterfaceMethodTest.java b/src/test/java/org/apache/sling/models/impl/DefaultInterfaceMethodTest.java
new file mode 100644
index 0000000..5798c67
--- /dev/null
+++ b/src/test/java/org/apache/sling/models/impl/DefaultInterfaceMethodTest.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.impl;
+
+import org.apache.sling.api.resource.Resource;
+import org.apache.sling.api.resource.ValueMap;
+import org.apache.sling.api.wrappers.ValueMapDecorator;
+import org.apache.sling.models.impl.injectors.ValueMapInjector;
+import org.apache.sling.models.testmodels.interfaces.ModelWithDefaultMethods;
+import org.junit.Before;
+import org.junit.Test;
+
+import java.util.Collections;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNull;
+import static org.mockito.Mockito.lenient;
+import static org.mockito.Mockito.mock;
+
+public class DefaultInterfaceMethodTest {
+
+    private ModelAdapterFactory factory;
+
+    @Before
+    public void setup() {
+        factory = AdapterFactoryTest.createModelAdapterFactory();
+        factory.bindInjector(new ValueMapInjector(), new ServicePropertiesMap(0, 0));
+        factory.adapterImplementations.addClassesAsAdapterAndImplementation(ModelWithDefaultMethods.class);
+    }
+
+    @Test
+    public void testDefaultInterfaceMethodsCanBeInjected() {
+        ValueMap vm = new ValueMapDecorator(Collections.singletonMap("prop", "the prop"));
+        Resource res = mock(Resource.class);
+        lenient().when(res.adaptTo(ValueMap.class)).thenReturn(vm);
+
+        ModelWithDefaultMethods model = factory.getAdapter(res,ModelWithDefaultMethods.class);
+
+        assertEquals("the prop", model.getProp());
+    }
+
+    @Test
+    public void testDefaultInterfaceMethodsDefaultImplementationsAreIgnored() {
+        ValueMap vm = new ValueMapDecorator(Collections.<String, Object>emptyMap());
+        Resource res = mock(Resource.class);
+        lenient().when(res.adaptTo(ValueMap.class)).thenReturn(vm);
+
+        ModelWithDefaultMethods model = factory.getAdapter(res,ModelWithDefaultMethods.class);
+
+        assertNull(model.getProp());
+    }
+}
diff --git a/src/test/java/org/apache/sling/models/testmodels/interfaces/ModelWithDefaultMethods.java b/src/test/java/org/apache/sling/models/testmodels/interfaces/ModelWithDefaultMethods.java
new file mode 100644
index 0000000..6c254e8
--- /dev/null
+++ b/src/test/java/org/apache/sling/models/testmodels/interfaces/ModelWithDefaultMethods.java
@@ -0,0 +1,33 @@
+/*
+ * 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.testmodels.interfaces;
+
+import org.apache.sling.api.resource.Resource;
+import org.apache.sling.models.annotations.Model;
+import org.apache.sling.models.annotations.Optional;
+
+import javax.inject.Inject;
+
+@Model(adaptables = Resource.class)
+public interface ModelWithDefaultMethods {
+
+    @Inject @Optional
+    default public String getProp() {
+        return "default";
+    }
+
+}