You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by cz...@apache.org on 2021/10/29 12:00:15 UTC

[sling-org-apache-sling-adapter] branch master updated: SLING-10885 : Improve adapter factory handling - remove mock class, use mockito

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

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


The following commit(s) were added to refs/heads/master by this push:
     new 7c4421f  SLING-10885 : Improve adapter factory handling - remove mock class, use mockito
7c4421f is described below

commit 7c4421f10f2f451c18d2fb4267e752637eb6ca48
Author: Carsten Ziegeler <cz...@apache.org>
AuthorDate: Fri Oct 29 14:00:10 2021 +0200

    SLING-10885 : Improve adapter factory handling - remove mock class, use mockito
---
 .../sling/adapter/internal/AdapterManagerTest.java | 33 ++++++++++----
 .../sling/adapter/mock/MockAdapterFactory.java     | 53 ----------------------
 2 files changed, 23 insertions(+), 63 deletions(-)

diff --git a/src/test/java/org/apache/sling/adapter/internal/AdapterManagerTest.java b/src/test/java/org/apache/sling/adapter/internal/AdapterManagerTest.java
index 4b047b2..4dcab05 100644
--- a/src/test/java/org/apache/sling/adapter/internal/AdapterManagerTest.java
+++ b/src/test/java/org/apache/sling/adapter/internal/AdapterManagerTest.java
@@ -26,11 +26,11 @@ import static org.junit.Assert.assertTrue;
 import java.util.Map;
 
 import org.apache.sling.adapter.Adaption;
-import org.apache.sling.adapter.mock.MockAdapterFactory;
 import org.apache.sling.api.adapter.AdapterFactory;
 import org.apache.sling.api.adapter.SlingAdaptable;
 import org.junit.Before;
 import org.junit.Test;
+import org.mockito.Mock;
 import org.mockito.Mockito;
 import org.osgi.framework.Bundle;
 import org.osgi.framework.BundleContext;
@@ -173,7 +173,9 @@ public class AdapterManagerTest {
         assertNull("Expect no adapter", am.getAdapter(data, ITestAdapter.class));
 
         final ServiceReference<AdapterFactory> ref = createServiceReference();
-        am.bindAdapterFactory(new MockAdapterFactory(), ref);
+        final AdapterFactory af = Mockito.mock(AdapterFactory.class);
+        Mockito.when(af.getAdapter(data, ITestAdapter.class)).thenReturn(Mockito.mock(ITestAdapter.class));
+        am.bindAdapterFactory(af, ref);
 
         Object adapter = am.getAdapter(data, ITestAdapter.class);
         assertNotNull(adapter);
@@ -186,7 +188,9 @@ public class AdapterManagerTest {
         assertNull("Expect no adapter", am.getAdapter(data, ITestAdapter.class));
 
         final ServiceReference<AdapterFactory> ref = createServiceReference();
-        am.bindAdapterFactory(new MockAdapterFactory(), ref);
+        final AdapterFactory af = Mockito.mock(AdapterFactory.class);
+        Mockito.when(af.getAdapter(data, ITestAdapter.class)).thenReturn(Mockito.mock(ITestAdapter.class));
+        am.bindAdapterFactory(af, ref);
 
         Object adapter = am.getAdapter(data, ITestAdapter.class);
         assertNotNull(adapter);
@@ -199,10 +203,12 @@ public class AdapterManagerTest {
         assertNull("Expect no adapter", am.getAdapter(data, ITestAdapter.class));
 
         final ServiceReference<AdapterFactory> ref = createServiceReference();
-        am.bindAdapterFactory(new MockAdapterFactory(), ref);
+        final AdapterFactory af = Mockito.mock(AdapterFactory.class);
+        Mockito.when(af.getAdapter(data, ITestAdapter.class)).thenReturn(Mockito.mock(ITestAdapter.class));
+        am.bindAdapterFactory(af, ref);
 
         final ServiceReference<AdapterFactory> ref2 = createServiceReference2();
-        am.bindAdapterFactory(new MockAdapterFactory(), ref2);
+        am.bindAdapterFactory(af, ref2);
 
         Object adapter = am.getAdapter(data, ITestAdapter.class);
         assertNotNull(adapter);
@@ -211,20 +217,27 @@ public class AdapterManagerTest {
 
     @Test
     public void testAdaptExtended2() throws Exception {
-        final ServiceReference<AdapterFactory> ref = createServiceReference();
-        am.bindAdapterFactory(new MockAdapterFactory(), ref);
+        TestSlingAdaptable data = new TestSlingAdaptable();
+        TestSlingAdaptable2 data2 = new TestSlingAdaptable2();
 
+        final AdapterFactory af1 = Mockito.mock(AdapterFactory.class);
+        Mockito.when(af1.getAdapter(data, ITestAdapter.class)).thenReturn(Mockito.mock(ITestAdapter.class));
+        Mockito.when(af1.getAdapter(data2, ITestAdapter.class)).thenReturn(Mockito.mock(ITestAdapter.class));
+        final AdapterFactory af2 = Mockito.mock(AdapterFactory.class);
+        Mockito.when(af2.getAdapter(data2, TestAdapter.class)).thenReturn(Mockito.mock(TestAdapter.class));
+        final ServiceReference<AdapterFactory> ref1 = createServiceReference();
         final ServiceReference<AdapterFactory> ref2 = createServiceReference2();
-        am.bindAdapterFactory(new MockAdapterFactory(), ref2);
+        Mockito.when(ref1.compareTo(ref2)).thenReturn(-1);
+        Mockito.when(ref2.compareTo(ref1)).thenReturn(1);
+        am.bindAdapterFactory(af1, ref1);
+        am.bindAdapterFactory(af2, ref2);
 
-        TestSlingAdaptable data = new TestSlingAdaptable();
         Object adapter = am.getAdapter(data, ITestAdapter.class);
         assertNotNull(adapter);
         assertTrue(adapter instanceof ITestAdapter);
         adapter = am.getAdapter(data, TestAdapter.class);
         assertNull(adapter);
 
-        TestSlingAdaptable2 data2 = new TestSlingAdaptable2();
         adapter = am.getAdapter(data2, ITestAdapter.class);
         assertNotNull(adapter);
         assertTrue(adapter instanceof ITestAdapter);
diff --git a/src/test/java/org/apache/sling/adapter/mock/MockAdapterFactory.java b/src/test/java/org/apache/sling/adapter/mock/MockAdapterFactory.java
deleted file mode 100644
index f4f403c..0000000
--- a/src/test/java/org/apache/sling/adapter/mock/MockAdapterFactory.java
+++ /dev/null
@@ -1,53 +0,0 @@
-/*
- * 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.adapter.mock;
-
-import java.lang.reflect.InvocationHandler;
-import java.lang.reflect.Method;
-import java.lang.reflect.Proxy;
-
-import org.apache.sling.api.adapter.AdapterFactory;
-
-public class MockAdapterFactory implements AdapterFactory {
-
-    private static final InvocationHandler NOP_INVOCATION_HANDLER = new InvocationHandler() {
-        public Object invoke(Object proxy, Method method, Object[] args)
-                throws Throwable {
-            return null;
-        }
-    };
-
-    @SuppressWarnings("unchecked")
-    public <AdapterType> AdapterType getAdapter(Object adaptable,
-            Class<AdapterType> type) {
-
-        try {
-            if (type.isInterface()) {
-                return (AdapterType) Proxy.newProxyInstance(type.getClassLoader(),
-                    new Class[] { type }, NOP_INVOCATION_HANDLER);
-            }
-
-            return type.newInstance();
-        } catch (Exception e) {
-            // ignore
-        }
-
-        return null;
-    }
-}