This is an automated email from the ASF dual-hosted git repository.
enapps-enorman 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 f376250 SLING-13192 Migrate to junit 5 (#9)
f376250 is described below
commit f376250fe96b67cf05bb2d82fdb45f203e61815f
Author: Eric Norman <[email protected]>
AuthorDate: Tue May 5 12:38:01 2026 -0700
SLING-13192 Migrate to junit 5 (#9)
---
pom.xml | 16 +-
.../sling/adapter/internal/AdapterManagerTest.java | 213 ++++++++++-----------
.../internal/AdapterWebConsolePluginTest.java | 89 ++++-----
.../sling/adapter/internal/PackageNameTest.java | 38 ++--
.../sling/adapter/internal/ReflectionTools.java | 2 +-
5 files changed, 174 insertions(+), 184 deletions(-)
diff --git a/pom.xml b/pom.xml
index f7e4bb5..67bcc4a 100644
--- a/pom.xml
+++ b/pom.xml
@@ -114,8 +114,18 @@
<scope>provided</scope>
</dependency>
<dependency>
- <groupId>junit</groupId>
- <artifactId>junit</artifactId>
+ <groupId>org.junit.jupiter</groupId>
+ <artifactId>junit-jupiter-api</artifactId>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.junit.jupiter</groupId>
+ <artifactId>junit-jupiter-engine</artifactId>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.junit.jupiter</groupId>
+ <artifactId>junit-jupiter-params</artifactId>
<scope>test</scope>
</dependency>
<dependency>
@@ -136,7 +146,7 @@
</dependency>
<dependency>
<groupId>org.apache.sling</groupId>
- <artifactId>org.apache.sling.testing.sling-mock.junit4</artifactId>
+ <artifactId>org.apache.sling.testing.sling-mock.junit5</artifactId>
<version>4.0.4</version>
<scope>test</scope>
</dependency>
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 334c298..ef4c3a5 100644
--- a/src/test/java/org/apache/sling/adapter/internal/AdapterManagerTest.java
+++ b/src/test/java/org/apache/sling/adapter/internal/AdapterManagerTest.java
@@ -23,8 +23,8 @@ import java.util.Map;
import org.apache.sling.adapter.Adaption;
import org.apache.sling.api.adapter.AdapterFactory;
import org.apache.sling.api.adapter.SlingAdaptable;
-import org.junit.Before;
-import org.junit.Test;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
@@ -34,17 +34,18 @@ import org.osgi.framework.ServiceRegistration;
import org.osgi.service.packageadmin.ExportedPackage;
import org.osgi.service.packageadmin.PackageAdmin;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.assertNull;
-import static org.junit.Assert.assertTrue;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.assertTrue;
-public class AdapterManagerTest {
+class AdapterManagerTest {
private AdapterManagerImpl am;
- @Before
- public void setUp() throws Exception {
+ @SuppressWarnings("deprecation")
+ @BeforeEach
+ void setUp() {
final PackageAdmin pa = Mockito.mock(PackageAdmin.class);
final ExportedPackage ep = Mockito.mock(ExportedPackage.class);
Mockito.when(pa.getExportedPackage(Mockito.anyString())).thenReturn(ep);
@@ -65,6 +66,7 @@ public class AdapterManagerTest {
*/
protected ServiceReference<AdapterFactory> createServiceReference(
final int ranking, final String[] adaptables, final String[]
adapters) {
+ @SuppressWarnings("unchecked")
final ServiceReference<AdapterFactory> ref =
Mockito.mock(ServiceReference.class);
Mockito.when(ref.getProperty(Constants.SERVICE_RANKING)).thenReturn(ranking);
Mockito.when(ref.getProperty(AdapterFactory.ADAPTABLE_CLASSES)).thenReturn(adaptables);
@@ -88,35 +90,36 @@ public class AdapterManagerTest {
}
@Test
- public void testInitialized() throws Exception {
- assertNotNull("AdapterFactoryDescriptors must not be null",
am.getFactories());
- assertTrue("AdapterFactoryDescriptors must be empty",
am.getFactories().isEmpty());
- assertTrue("AdapterFactory cache must be empty",
am.getFactoryCache().isEmpty());
+ void testInitialized() {
+ assertNotNull(am.getFactories(), "AdapterFactoryDescriptors must not
be null");
+ assertTrue(am.getFactories().isEmpty(), "AdapterFactoryDescriptors
must be empty");
+ assertTrue(am.getFactoryCache().isEmpty(), "AdapterFactory cache must
be empty");
}
@Test
- public void testInvalidRegistrations() throws Exception {
+ void testInvalidRegistrations() {
ServiceReference<AdapterFactory> ref =
createServiceReference(0, null, new String[]
{TestAdapter.class.getName()});
am.bindAdapterFactory(Mockito.mock(AdapterFactory.class), ref);
- assertTrue("AdapterFactoryDescriptors must be empty",
am.getFactories().isEmpty());
+ assertTrue(am.getFactories().isEmpty(), "AdapterFactoryDescriptors
must be empty");
ref = createServiceReference(0, new String[0], new String[]
{TestAdapter.class.getName()});
am.bindAdapterFactory(Mockito.mock(AdapterFactory.class), ref);
- assertTrue("AdapterFactoryDescriptors must be empty",
am.getFactories().isEmpty());
+ assertTrue(am.getFactories().isEmpty(), "AdapterFactoryDescriptors
must be empty");
ref = createServiceReference(0, new String[]
{TestSlingAdaptable.class.getName()}, null);
am.bindAdapterFactory(Mockito.mock(AdapterFactory.class), ref);
- assertTrue("AdapterFactoryDescriptors must be empty",
am.getFactories().isEmpty());
+ assertTrue(am.getFactories().isEmpty(), "AdapterFactoryDescriptors
must be empty");
ref = createServiceReference(0, new String[]
{TestSlingAdaptable.class.getName()}, new String[0]);
am.bindAdapterFactory(Mockito.mock(AdapterFactory.class), ref);
- assertTrue("AdapterFactoryDescriptors must be empty",
am.getFactories().isEmpty());
+ assertTrue(am.getFactories().isEmpty(), "AdapterFactoryDescriptors
must be empty");
}
@Test
- public void testBindUnbind() throws Exception {
+ void testBindUnbind() {
final ServiceReference<AdapterFactory> ref = createServiceReference();
+ @SuppressWarnings("unchecked")
final ServiceRegistration<Adaption> registration =
Mockito.mock(ServiceRegistration.class);
Mockito.when(ref.getBundle()
.getBundleContext()
@@ -129,14 +132,11 @@ public class AdapterManagerTest {
.registerService(Mockito.eq(Adaption.class),
Mockito.eq(AdaptionImpl.INSTANCE), Mockito.any());
// expect the factory, but cache is empty
- assertNotNull("AdapterFactoryDescriptors must not be null",
am.getFactories());
- assertEquals(
- "AdapterFactoryDescriptors must contain one entry",
- 1,
- am.getFactories().size());
+ assertNotNull(am.getFactories(), "AdapterFactoryDescriptors must not
be null");
+ assertEquals(1, am.getFactories().size(), "AdapterFactoryDescriptors
must contain one entry");
assertEquals(
1,
am.getFactories().get(TestSlingAdaptable.class.getName()).size());
- assertTrue("AdapterFactory cache must be empty",
am.getFactoryCache().isEmpty());
+ assertTrue(am.getFactoryCache().isEmpty(), "AdapterFactory cache must
be empty");
Map<String, AdapterFactoryDescriptorMap> f = am.getFactories();
AdapterFactoryDescriptorMap afdm =
f.get(TestSlingAdaptable.class.getName());
@@ -155,12 +155,13 @@ public class AdapterManagerTest {
am.unbindAdapterFactory(ref);
Mockito.verify(registration).unregister();
assertTrue(am.getFactories().get(TestSlingAdaptable.class.getName()).isEmpty());
- assertTrue("AdapterFactory cache must be empty",
am.getFactoryCache().isEmpty());
+ assertTrue(am.getFactoryCache().isEmpty(), "AdapterFactory cache must
be empty");
}
@Test
- public void testBindModifiedUnbind() throws Exception {
+ void testBindModifiedUnbind() {
ServiceReference<AdapterFactory> ref = createServiceReference();
+ @SuppressWarnings("unchecked")
final ServiceRegistration<Adaption> registration =
Mockito.mock(ServiceRegistration.class);
Mockito.when(ref.getBundle()
.getBundleContext()
@@ -168,20 +169,14 @@ public class AdapterManagerTest {
.thenReturn(registration);
am.bindAdapterFactory(Mockito.mock(AdapterFactory.class), ref);
- assertEquals(
- "AdapterFactoryDescriptors must contain one entry",
- 1,
- am.getFactories().size());
+ assertEquals(1, am.getFactories().size(), "AdapterFactoryDescriptors
must contain one entry");
assertEquals(
1,
am.getFactories().get(TestSlingAdaptable.class.getName()).size());
Mockito.when(ref.getProperty(AdapterFactory.ADAPTABLE_CLASSES))
.thenReturn(new String[]
{TestSlingAdaptable2.class.getName()});
am.updatedAdapterFactory(Mockito.mock(AdapterFactory.class), ref);
- assertEquals(
- "AdapterFactoryDescriptors must contain two entries",
- 2,
- am.getFactories().size());
+ assertEquals(2, am.getFactories().size(), "AdapterFactoryDescriptors
must contain two entries");
assertEquals(
0,
am.getFactories().get(TestSlingAdaptable.class.getName()).size());
assertEquals(
@@ -189,9 +184,9 @@ public class AdapterManagerTest {
}
@Test
- public void testAdaptBase() throws Exception {
+ void testAdaptBase() {
TestSlingAdaptable data = new TestSlingAdaptable();
- assertNull("Expect no adapter", am.getAdapter(data,
ITestAdapter.class));
+ assertNull(am.getAdapter(data, ITestAdapter.class), "Expect no
adapter");
final ServiceReference<AdapterFactory> ref = createServiceReference();
final AdapterFactory af = Mockito.mock(AdapterFactory.class);
@@ -204,9 +199,9 @@ public class AdapterManagerTest {
}
@Test
- public void testAdaptExtended() throws Exception {
+ void testAdaptExtended() {
TestSlingAdaptable2 data = new TestSlingAdaptable2();
- assertNull("Expect no adapter", am.getAdapter(data,
ITestAdapter.class));
+ assertNull(am.getAdapter(data, ITestAdapter.class), "Expect no
adapter");
final ServiceReference<AdapterFactory> ref = createServiceReference();
final AdapterFactory af = Mockito.mock(AdapterFactory.class);
@@ -219,9 +214,9 @@ public class AdapterManagerTest {
}
@Test
- public void testAdaptBase2() throws Exception {
+ void testAdaptBase2() {
TestSlingAdaptable data = new TestSlingAdaptable();
- assertNull("Expect no adapter", am.getAdapter(data,
ITestAdapter.class));
+ assertNull(am.getAdapter(data, ITestAdapter.class), "Expect no
adapter");
final ServiceReference<AdapterFactory> ref = createServiceReference();
final AdapterFactory af = Mockito.mock(AdapterFactory.class);
@@ -237,7 +232,7 @@ public class AdapterManagerTest {
}
@Test
- public void testAdaptExtended2() throws Exception {
+ void testAdaptExtended2() {
TestSlingAdaptable data = new TestSlingAdaptable();
TestSlingAdaptable2 data2 = new TestSlingAdaptable2();
@@ -268,7 +263,7 @@ public class AdapterManagerTest {
}
@Test
- public void testAdaptMultipleAdapterFactories() throws Exception {
+ void testAdaptMultipleAdapterFactories() {
final ServiceReference<AdapterFactory> firstAdaptable =
this.createServiceReference(1, new String[]
{AdapterObject.class.getName()}, new String[] {
ParentInterface.class.getName(),
FirstImplementation.class.getName()
@@ -281,41 +276,41 @@ public class AdapterManagerTest {
Mockito.when(secondAdaptable.compareTo(firstAdaptable)).thenReturn(1);
AdapterObject first = new AdapterObject(Want.FIRST_IMPL);
- assertNull("Expect no adapter", am.getAdapter(first,
ParentInterface.class));
+ assertNull(am.getAdapter(first, ParentInterface.class), "Expect no
adapter");
AdapterObject second = new AdapterObject(Want.SECOND_IMPL);
- assertNull("Expect no adapter", am.getAdapter(second,
ParentInterface.class));
+ assertNull(am.getAdapter(second, ParentInterface.class), "Expect no
adapter");
am.bindAdapterFactory(new FirstImplementationAdapterFactory(),
firstAdaptable);
am.bindAdapterFactory(new SecondImplementationAdapterFactory(),
secondAdaptable);
Object adapter = am.getAdapter(first, ParentInterface.class);
- assertNotNull("Did not get an adapter back for first implementation,
service ranking 1", adapter);
+ assertNotNull(adapter, "Did not get an adapter back for first
implementation, service ranking 1");
assertTrue(
- "Did not get the correct adaptable back for first
implementation, service ranking 1, ",
- adapter instanceof FirstImplementation);
+ adapter instanceof FirstImplementation,
+ "Did not get the correct adaptable back for first
implementation, service ranking 1, ");
adapter = am.getAdapter(second, ParentInterface.class);
- assertNotNull("Did not get an adapter back for second implementation,
service ranking 2", adapter);
+ assertNotNull(adapter, "Did not get an adapter back for second
implementation, service ranking 2");
assertTrue(
- "Did not get the correct adaptable back for second
implementation, service ranking 2, ",
- adapter instanceof SecondImplementation);
+ adapter instanceof SecondImplementation,
+ "Did not get the correct adaptable back for second
implementation, service ranking 2, ");
adapter = am.getAdapter(first, FirstImplementation.class);
- assertNotNull("Did not get an adapter back for first implementation,
service ranking 1", adapter);
+ assertNotNull(adapter, "Did not get an adapter back for first
implementation, service ranking 1");
assertTrue(
- "Did not get the correct adaptable back for first
implementation, service ranking 1, ",
- adapter instanceof FirstImplementation);
+ adapter instanceof FirstImplementation,
+ "Did not get the correct adaptable back for first
implementation, service ranking 1, ");
adapter = am.getAdapter(second, SecondImplementation.class);
- assertNotNull("Did not get an adapter back for second implementation,
service ranking 2", adapter);
+ assertNotNull(adapter, "Did not get an adapter back for second
implementation, service ranking 2");
assertTrue(
- "Did not get the correct adaptable back for second
implementation, service ranking 2, ",
- adapter instanceof SecondImplementation);
+ adapter instanceof SecondImplementation,
+ "Did not get the correct adaptable back for second
implementation, service ranking 2, ");
}
@Test
- public void testAdaptMultipleAdapterFactoriesReverseOrder() throws
Exception {
+ void testAdaptMultipleAdapterFactoriesReverseOrder() {
final ServiceReference<AdapterFactory> firstAdaptable =
this.createServiceReference(
2, new String[] {AdapterObject.class.getName()}, new String[]
{ParentInterface.class.getName()});
final ServiceReference<AdapterFactory> secondAdaptable =
this.createServiceReference(
@@ -324,23 +319,23 @@ public class AdapterManagerTest {
Mockito.when(secondAdaptable.compareTo(firstAdaptable)).thenReturn(-1);
AdapterObject first = new AdapterObject(Want.FIRST_IMPL);
- assertNull("Expect no adapter", am.getAdapter(first,
ParentInterface.class));
+ assertNull(am.getAdapter(first, ParentInterface.class), "Expect no
adapter");
AdapterObject second = new AdapterObject(Want.SECOND_IMPL);
- assertNull("Expect no adapter", am.getAdapter(second,
ParentInterface.class));
+ assertNull(am.getAdapter(second, ParentInterface.class), "Expect no
adapter");
am.bindAdapterFactory(new FirstImplementationAdapterFactory(),
firstAdaptable);
am.bindAdapterFactory(new SecondImplementationAdapterFactory(),
secondAdaptable);
Object adapter = am.getAdapter(first, ParentInterface.class);
- assertNotNull("Did not get an adapter back for first implementation,
service ranking 2", adapter);
+ assertNotNull(adapter, "Did not get an adapter back for first
implementation, service ranking 2");
assertTrue(
- "Did not get the correct adaptable back for first
implementation, service ranking 2, ",
- adapter instanceof FirstImplementation);
+ adapter instanceof FirstImplementation,
+ "Did not get the correct adaptable back for first
implementation, service ranking 2, ");
}
@Test
- public void testAdaptMultipleAdapterFactoriesServiceRanking() throws
Exception {
+ void testAdaptMultipleAdapterFactoriesServiceRanking() {
final ServiceReference<AdapterFactory> firstAdaptable =
createServiceReference(1, new String[]
{AdapterObject.class.getName()}, new String[] {
ParentInterface.class.getName(),
FirstImplementation.class.getName()
@@ -353,37 +348,37 @@ public class AdapterManagerTest {
Mockito.when(secondAdaptable.compareTo(firstAdaptable)).thenReturn(1);
AdapterObject first = new AdapterObject(Want.INDIFFERENT);
- assertNull("Expect no adapter", am.getAdapter(first,
ParentInterface.class));
+ assertNull(am.getAdapter(first, ParentInterface.class), "Expect no
adapter");
AdapterObject second = new AdapterObject(Want.INDIFFERENT);
- assertNull("Expect no adapter", am.getAdapter(second,
ParentInterface.class));
+ assertNull(am.getAdapter(second, ParentInterface.class), "Expect no
adapter");
am.bindAdapterFactory(new FirstImplementationAdapterFactory(),
firstAdaptable);
am.bindAdapterFactory(new SecondImplementationAdapterFactory(),
secondAdaptable);
Object adapter = am.getAdapter(first, ParentInterface.class);
assertNotNull(
- "Did not get an adapter back for first implementation (from
ParentInterface), service ranking 1",
- adapter);
+ adapter,
+ "Did not get an adapter back for first implementation (from
ParentInterface), service ranking 1");
assertTrue(
- "Did not get the correct adaptable back for first
implementation, service ranking 1, ",
- adapter instanceof FirstImplementation);
+ adapter instanceof FirstImplementation,
+ "Did not get the correct adaptable back for first
implementation, service ranking 1, ");
adapter = am.getAdapter(first, FirstImplementation.class);
- assertNotNull("Did not get an adapter back for first implementation,
service ranking 1", adapter);
+ assertNotNull(adapter, "Did not get an adapter back for first
implementation, service ranking 1");
assertTrue(
- "Did not get the correct adaptable back for first
implementation, service ranking 1, ",
- adapter instanceof FirstImplementation);
+ adapter instanceof FirstImplementation,
+ "Did not get the correct adaptable back for first
implementation, service ranking 1, ");
adapter = am.getAdapter(second, SecondImplementation.class);
- assertNotNull("Did not get an adapter back for second implementation,
service ranking 2", adapter);
+ assertNotNull(adapter, "Did not get an adapter back for second
implementation, service ranking 2");
assertTrue(
- "Did not get the correct adaptable back for second
implementation, service ranking 2, ",
- adapter instanceof SecondImplementation);
+ adapter instanceof SecondImplementation,
+ "Did not get the correct adaptable back for second
implementation, service ranking 2, ");
}
@Test
- public void
testAdaptMultipleAdapterFactoriesServiceRankingSecondHigherOrder() throws
Exception {
+ void testAdaptMultipleAdapterFactoriesServiceRankingSecondHigherOrder() {
final ServiceReference<AdapterFactory> firstAdaptable =
createServiceReference(2, new String[]
{AdapterObject.class.getName()}, new String[] {
ParentInterface.class.getName(),
FirstImplementation.class.getName()
@@ -396,37 +391,37 @@ public class AdapterManagerTest {
Mockito.when(secondAdaptable.compareTo(firstAdaptable)).thenReturn(-1);
AdapterObject first = new AdapterObject(Want.INDIFFERENT);
- assertNull("Expect no adapter", am.getAdapter(first,
ParentInterface.class));
+ assertNull(am.getAdapter(first, ParentInterface.class), "Expect no
adapter");
AdapterObject second = new AdapterObject(Want.INDIFFERENT);
- assertNull("Expect no adapter", am.getAdapter(second,
ParentInterface.class));
+ assertNull(am.getAdapter(second, ParentInterface.class), "Expect no
adapter");
am.bindAdapterFactory(new FirstImplementationAdapterFactory(),
firstAdaptable);
am.bindAdapterFactory(new SecondImplementationAdapterFactory(),
secondAdaptable);
Object adapter = am.getAdapter(first, ParentInterface.class);
assertNotNull(
- "Did not get an adapter back for second implementation (from
ParentInterface), service ranking 1",
- adapter);
+ adapter,
+ "Did not get an adapter back for second implementation (from
ParentInterface), service ranking 1");
assertTrue(
- "Did not get the correct adaptable back for second
implementation, service ranking 1, ",
- adapter instanceof SecondImplementation);
+ adapter instanceof SecondImplementation,
+ "Did not get the correct adaptable back for second
implementation, service ranking 1, ");
adapter = am.getAdapter(first, FirstImplementation.class);
- assertNotNull("Did not get an adapter back for first implementation,
service ranking 1", adapter);
+ assertNotNull(adapter, "Did not get an adapter back for first
implementation, service ranking 1");
assertTrue(
- "Did not get the correct adaptable back for first
implementation, service ranking 1, ",
- adapter instanceof FirstImplementation);
+ adapter instanceof FirstImplementation,
+ "Did not get the correct adaptable back for first
implementation, service ranking 1, ");
adapter = am.getAdapter(second, SecondImplementation.class);
- assertNotNull("Did not get an adapter back for second implementation,
service ranking 2", adapter);
+ assertNotNull(adapter, "Did not get an adapter back for second
implementation, service ranking 2");
assertTrue(
- "Did not get the correct adaptable back for second
implementation, service ranking 2, ",
- adapter instanceof SecondImplementation);
+ adapter instanceof SecondImplementation,
+ "Did not get the correct adaptable back for second
implementation, service ranking 2, ");
}
@Test
- public void testAdaptMultipleAdapterFactoriesServiceRankingReverse()
throws Exception {
+ void testAdaptMultipleAdapterFactoriesServiceRankingReverse() {
final ServiceReference<AdapterFactory> firstAdaptable =
this.createServiceReference(1, new String[]
{AdapterObject.class.getName()}, new String[] {
ParentInterface.class.getName(),
FirstImplementation.class.getName()
@@ -439,10 +434,10 @@ public class AdapterManagerTest {
Mockito.when(secondAdaptable.compareTo(firstAdaptable)).thenReturn(1);
AdapterObject first = new AdapterObject(Want.INDIFFERENT);
- assertNull("Expect no adapter", am.getAdapter(first,
ParentInterface.class));
+ assertNull(am.getAdapter(first, ParentInterface.class), "Expect no
adapter");
AdapterObject second = new AdapterObject(Want.INDIFFERENT);
- assertNull("Expect no adapter", am.getAdapter(second,
ParentInterface.class));
+ assertNull(am.getAdapter(second, ParentInterface.class), "Expect no
adapter");
// bind these in reverse order from the non-reverse test
am.bindAdapterFactory(new SecondImplementationAdapterFactory(),
secondAdaptable);
@@ -450,23 +445,23 @@ public class AdapterManagerTest {
Object adapter = am.getAdapter(first, ParentInterface.class);
assertNotNull(
- "Did not get an adapter back for first implementation (from
ParentInterface), service ranking 1",
- adapter);
+ adapter,
+ "Did not get an adapter back for first implementation (from
ParentInterface), service ranking 1");
assertTrue(
- "Did not get the correct adaptable back for first
implementation, service ranking 1, ",
- adapter instanceof FirstImplementation);
+ adapter instanceof FirstImplementation,
+ "Did not get the correct adaptable back for first
implementation, service ranking 1, ");
adapter = am.getAdapter(first, FirstImplementation.class);
- assertNotNull("Did not get an adapter back for first implementation,
service ranking 1", adapter);
+ assertNotNull(adapter, "Did not get an adapter back for first
implementation, service ranking 1");
assertTrue(
- "Did not get the correct adaptable back for first
implementation, service ranking 1, ",
- adapter instanceof FirstImplementation);
+ adapter instanceof FirstImplementation,
+ "Did not get the correct adaptable back for first
implementation, service ranking 1, ");
adapter = am.getAdapter(second, SecondImplementation.class);
- assertNotNull("Did not get an adapter back for second implementation,
service ranking 2", adapter);
+ assertNotNull(adapter, "Did not get an adapter back for second
implementation, service ranking 2");
assertTrue(
- "Did not get the correct adaptable back for second
implementation, service ranking 2, ",
- adapter instanceof SecondImplementation);
+ adapter instanceof SecondImplementation,
+ "Did not get the correct adaptable back for second
implementation, service ranking 2, ");
}
// ---------- Test Adaptable and Adapter Classes
---------------------------
@@ -483,13 +478,12 @@ public class AdapterManagerTest {
@Override
@SuppressWarnings("unchecked")
- public <AdapterType> AdapterType getAdapter(Object adaptable,
Class<AdapterType> type) {
+ public <T> T getAdapter(Object adaptable, Class<T> type) {
if (adaptable instanceof AdapterObject) {
AdapterObject adapterObject = (AdapterObject) adaptable;
switch (adapterObject.getWhatWeWant()) {
- case FIRST_IMPL:
- case INDIFFERENT:
- return (AdapterType) new FirstImplementation();
+ case FIRST_IMPL, INDIFFERENT:
+ return (T) new FirstImplementation();
case SECOND_IMPL:
return null;
}
@@ -502,13 +496,12 @@ public class AdapterManagerTest {
@Override
@SuppressWarnings("unchecked")
- public <AdapterType> AdapterType getAdapter(Object adaptable,
Class<AdapterType> type) {
+ public <T> T getAdapter(Object adaptable, Class<T> type) {
if (adaptable instanceof AdapterObject) {
AdapterObject adapterObject = (AdapterObject) adaptable;
switch (adapterObject.getWhatWeWant()) {
- case SECOND_IMPL:
- case INDIFFERENT:
- return (AdapterType) new SecondImplementation();
+ case SECOND_IMPL, INDIFFERENT:
+ return (T) new SecondImplementation();
case FIRST_IMPL:
return null;
}
diff --git
a/src/test/java/org/apache/sling/adapter/internal/AdapterWebConsolePluginTest.java
b/src/test/java/org/apache/sling/adapter/internal/AdapterWebConsolePluginTest.java
index f509eed..fb3290b 100644
---
a/src/test/java/org/apache/sling/adapter/internal/AdapterWebConsolePluginTest.java
+++
b/src/test/java/org/apache/sling/adapter/internal/AdapterWebConsolePluginTest.java
@@ -28,27 +28,28 @@ import java.util.List;
import jakarta.servlet.ServletException;
import org.apache.sling.api.adapter.AdapterFactory;
import org.apache.sling.testing.mock.sling.ResourceResolverType;
-import org.apache.sling.testing.mock.sling.junit.SlingContext;
+import org.apache.sling.testing.mock.sling.junit5.SlingContext;
+import org.apache.sling.testing.mock.sling.junit5.SlingContextExtension;
import
org.apache.sling.testing.mock.sling.servlet.MockSlingJakartaHttpServletRequest;
import
org.apache.sling.testing.mock.sling.servlet.MockSlingJakartaHttpServletResponse;
-import org.junit.Before;
-import org.junit.Rule;
-import org.junit.Test;
-import org.junit.Test.None;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mockito;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.BundleEvent;
-import org.osgi.framework.InvalidSyntaxException;
import org.osgi.framework.ServiceReference;
-import static org.junit.Assert.*;
+import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertNull;
/**
*/
-public class AdapterWebConsolePluginTest {
+@ExtendWith(SlingContextExtension.class)
+class AdapterWebConsolePluginTest {
- @Rule
public final SlingContext context = new
SlingContext(ResourceResolverType.JCR_MOCK);
private AdapterWebConsolePlugin plugin;
@@ -57,8 +58,8 @@ public class AdapterWebConsolePluginTest {
private org.osgi.service.packageadmin.PackageAdmin mockPackageAdmin;
@SuppressWarnings("deprecation")
- @Before
- public void beforeEach() {
+ @BeforeEach
+ void beforeEach() {
mockPackageAdmin = context.registerService(
org.osgi.service.packageadmin.PackageAdmin.class,
Mockito.mock(org.osgi.service.packageadmin.PackageAdmin.class));
@@ -69,18 +70,18 @@ public class AdapterWebConsolePluginTest {
/**
* Test method for {@link
org.apache.sling.adapter.internal.AdapterWebConsolePlugin#addingService(org.osgi.framework.ServiceReference)}.
*/
- @Test(expected = None.class)
- public void testAddingService() {
+ @Test
+ void testAddingService() {
// simulate serviceref with no config
@SuppressWarnings("unchecked")
ServiceReference<AdapterFactory> serviceRef1 =
Mockito.mock(ServiceReference.class);
- plugin.addingService(serviceRef1);
+ assertDoesNotThrow(() -> plugin.addingService(serviceRef1));
// simulate serviceref with empty config
@SuppressWarnings("unchecked")
ServiceReference<AdapterFactory> serviceRef2 =
Mockito.mock(ServiceReference.class);
Mockito.doReturn(new
String[0]).when(serviceRef2).getProperty(AdapterFactory.ADAPTER_CLASSES);
- plugin.addingService(serviceRef2);
+ assertDoesNotThrow(() -> plugin.addingService(serviceRef2));
// simulate serviceref with non-empty config
@SuppressWarnings("unchecked")
@@ -91,14 +92,14 @@ public class AdapterWebConsolePluginTest {
Mockito.doReturn(new String[] {"org.apache.sling.Adaptable1"})
.when(serviceRef3)
.getProperty(AdapterFactory.ADAPTABLE_CLASSES);
- plugin.addingService(serviceRef3);
+ assertDoesNotThrow(() -> plugin.addingService(serviceRef3));
}
/**
* Test method for {@link
org.apache.sling.adapter.internal.AdapterWebConsolePlugin#bundleChanged(org.osgi.framework.BundleEvent)}.
*/
- @Test(expected = None.class)
- public void testBundleChanged() {
+ @Test
+ void testBundleChanged() {
Bundle bundle1 = Mockito.mock(Bundle.class);
Mockito.doReturn(Bundle.INSTALLED).when(bundle1).getState();
@@ -106,23 +107,23 @@ public class AdapterWebConsolePluginTest {
BundleEvent event1 = Mockito.mock(BundleEvent.class);
Mockito.doReturn(bundle1).when(event1).getBundle();
Mockito.doReturn(BundleEvent.STOPPED).when(event1).getType();
- plugin.bundleChanged(event1);
+ assertDoesNotThrow(() -> plugin.bundleChanged(event1));
// bundle started event
BundleEvent event2 = Mockito.mock(BundleEvent.class);
Mockito.doReturn(bundle1).when(event2).getBundle();
Mockito.doReturn(BundleEvent.STARTED).when(event2).getType();
- plugin.bundleChanged(event2);
+ assertDoesNotThrow(() -> plugin.bundleChanged(event2));
// bundle other event
BundleEvent event3 = Mockito.mock(BundleEvent.class);
Mockito.doReturn(bundle1).when(event3).getBundle();
Mockito.doReturn(BundleEvent.UPDATED).when(event3).getType();
- plugin.bundleChanged(event3);
+ assertDoesNotThrow(() -> plugin.bundleChanged(event3));
}
- @Test(expected = None.class)
- public void testBundleChangedWithAdaptersJson() throws IOException {
+ @Test
+ void testBundleChangedWithAdaptersJson() throws IOException {
Bundle bundle1 = Mockito.mock(Bundle.class);
Mockito.doReturn(Bundle.ACTIVE).when(bundle1).getState();
@@ -135,11 +136,11 @@ public class AdapterWebConsolePluginTest {
BundleEvent event1 = Mockito.mock(BundleEvent.class);
Mockito.doReturn(bundle1).when(event1).getBundle();
Mockito.doReturn(BundleEvent.STARTED).when(event1).getType();
- plugin.bundleChanged(event1);
+ assertDoesNotThrow(() -> plugin.bundleChanged(event1));
}
- @Test(expected = None.class)
- public void testBundleChangedWithInvalidAdaptersJson() throws IOException {
+ @Test
+ void testBundleChangedWithInvalidAdaptersJson() throws IOException {
Bundle bundle1 = Mockito.mock(Bundle.class);
Mockito.doReturn(Bundle.ACTIVE).when(bundle1).getState();
@@ -152,14 +153,14 @@ public class AdapterWebConsolePluginTest {
BundleEvent event1 = Mockito.mock(BundleEvent.class);
Mockito.doReturn(bundle1).when(event1).getBundle();
Mockito.doReturn(BundleEvent.STARTED).when(event1).getType();
- plugin.bundleChanged(event1);
+ assertDoesNotThrow(() -> plugin.bundleChanged(event1));
}
/**
* Test method for {@link
org.apache.sling.adapter.internal.AdapterWebConsolePlugin#modifiedService(org.osgi.framework.ServiceReference,
java.lang.Object)}.
*/
- @Test(expected = None.class)
- public void testModifiedService() {
+ @Test
+ void testModifiedService() {
// simulate serviceref with non-empty config
@SuppressWarnings("unchecked")
ServiceReference<AdapterFactory> serviceRef3 =
Mockito.mock(ServiceReference.class);
@@ -170,14 +171,14 @@ public class AdapterWebConsolePluginTest {
.when(serviceRef3)
.getProperty(AdapterFactory.ADAPTABLE_CLASSES);
Object mockSvc = new Object();
- plugin.modifiedService(serviceRef3, mockSvc);
+ assertDoesNotThrow(() -> plugin.modifiedService(serviceRef3, mockSvc));
}
/**
* Test method for {@link
org.apache.sling.adapter.internal.AdapterWebConsolePlugin#removedService(org.osgi.framework.ServiceReference,
java.lang.Object)}.
*/
- @Test(expected = None.class)
- public void testRemovedService() {
+ @Test
+ void testRemovedService() {
// simulate serviceref with non-empty config
@SuppressWarnings("unchecked")
ServiceReference<AdapterFactory> serviceRef3 =
Mockito.mock(ServiceReference.class);
@@ -189,14 +190,14 @@ public class AdapterWebConsolePluginTest {
.getProperty(AdapterFactory.ADAPTABLE_CLASSES);
plugin.addingService(serviceRef3);
Object mockSvc = new Object();
- plugin.removedService(serviceRef3, mockSvc);
+ assertDoesNotThrow(() -> plugin.removedService(serviceRef3, mockSvc));
}
/**
- * Test method for {@link
org.apache.sling.adapter.internal.AdapterWebConsolePlugin#activate(org.osgi.framework.BundleContext)}.
+ * Test method for {@link
org.apache.sling.adapter.internal.AdapterWebConsolePlugin#AdapterWebConsolePlugin(org.osgi.framework.BundleContext)}.
*/
- @Test(expected = None.class)
- public void testActivate() throws InvalidSyntaxException {
+ @Test
+ void testActivate() {
final BundleContext bundleContext =
Mockito.spy(context.bundleContext());
// mock some deployed bundles
@@ -207,15 +208,15 @@ public class AdapterWebConsolePluginTest {
final Bundle[] mockBundles = new Bundle[] {bundle1, bundle2};
Mockito.doReturn(mockBundles).when(bundleContext).getBundles();
- new AdapterWebConsolePlugin(bundleContext);
+ assertDoesNotThrow(() -> new AdapterWebConsolePlugin(bundleContext));
}
/**
* Test method for {@link
org.apache.sling.adapter.internal.AdapterWebConsolePlugin#deactivate()}.
*/
- @Test(expected = None.class)
- public void testDeactivate() {
- plugin.deactivate();
+ @Test
+ void testDeactivate() {
+ assertDoesNotThrow(() -> plugin.deactivate());
}
/**
@@ -223,7 +224,7 @@ public class AdapterWebConsolePluginTest {
*/
@SuppressWarnings("deprecation")
@Test
- public void testDoGetWithHtmlOutput() throws ServletException, IOException
{
+ void testDoGetWithHtmlOutput() throws ServletException, IOException {
// simulate an exported package
org.osgi.service.packageadmin.ExportedPackage mockExportedPackage =
Mockito.mock(org.osgi.service.packageadmin.ExportedPackage.class);
@@ -234,7 +235,7 @@ public class AdapterWebConsolePluginTest {
}
@Test
- public void testDoGetWithJsonOutput() throws ServletException, IOException
{
+ void testDoGetWithJsonOutput() throws ServletException, IOException {
final String outputAsString = doGet("/data.json");
assertNotNull(outputAsString);
}
@@ -301,7 +302,7 @@ public class AdapterWebConsolePluginTest {
* Test method for {@link
org.apache.sling.adapter.internal.AdapterWebConsolePlugin#printConfiguration(java.io.PrintWriter)}.
*/
@Test
- public void testPrintConfiguration() {
+ void testPrintConfiguration() {
mockAdapters();
StringWriter sw = new StringWriter();
@@ -316,7 +317,7 @@ public class AdapterWebConsolePluginTest {
*
org.apache.sling.adapter.internal.AdapterWebConsolePlugin#getResource(java.lang.String)}.
*/
@Test
- public void testGetResource() throws SecurityException,
IllegalArgumentException {
+ void testGetResource() throws SecurityException, IllegalArgumentException {
final URL value1 = ReflectionTools.invokeMethodWithReflection(
plugin, "getResource", new Class[] {String.class}, URL.class,
new Object[] {"/invalid"});
assertNull(value1);
diff --git
a/src/test/java/org/apache/sling/adapter/internal/PackageNameTest.java
b/src/test/java/org/apache/sling/adapter/internal/PackageNameTest.java
index bb47358..2d70ebb 100644
--- a/src/test/java/org/apache/sling/adapter/internal/PackageNameTest.java
+++ b/src/test/java/org/apache/sling/adapter/internal/PackageNameTest.java
@@ -18,38 +18,24 @@
*/
package org.apache.sling.adapter.internal;
-import java.util.Arrays;
-import java.util.Collection;
+import java.util.stream.Stream;
-import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.junit.runners.Parameterized;
-import org.junit.runners.Parameterized.Parameters;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.Arguments;
+import org.junit.jupiter.params.provider.MethodSource;
-import static org.junit.Assert.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertEquals;
-@RunWith(Parameterized.class)
-public class PackageNameTest {
+class PackageNameTest {
- @Parameters
- public static Collection<Object[]> data() {
- return Arrays.asList(new Object[][] {
- {"java.lang.Foo", "java.lang"},
- {"noPackageName", ""},
- {"", ""}
- });
+ protected static Stream<Arguments> testPackageNameArgs() {
+ return Stream.of(
+ Arguments.of("java.lang.Foo", "java.lang"),
Arguments.of("noPackageName", ""), Arguments.of("", ""));
}
- private final String className;
- private final String packageName;
-
- public PackageNameTest(String className, String packageName) {
- this.className = className;
- this.packageName = packageName;
- }
-
- @Test
- public void testPackageName() {
+ @ParameterizedTest
+ @MethodSource(value = "testPackageNameArgs")
+ void testPackageName(String className, String packageName) {
assertEquals(packageName,
AdapterManagerImpl.getPackageName(className));
}
}
diff --git
a/src/test/java/org/apache/sling/adapter/internal/ReflectionTools.java
b/src/test/java/org/apache/sling/adapter/internal/ReflectionTools.java
index c38ab6e..511342b 100644
--- a/src/test/java/org/apache/sling/adapter/internal/ReflectionTools.java
+++ b/src/test/java/org/apache/sling/adapter/internal/ReflectionTools.java
@@ -22,7 +22,7 @@ import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
-import static org.junit.Assert.fail;
+import static org.junit.jupiter.api.Assertions.fail;
/**
* Reflection utilities to facilitate testing