This is an automated email from the ASF dual-hosted git repository.
sseifert pushed a commit to branch master
in repository
https://gitbox.apache.org/repos/asf/sling-org-apache-sling-testing-osgi-mock.git
The following commit(s) were added to refs/heads/master by this push:
new a9d4ee8 SLING-12413: update/improve code
a9d4ee8 is described below
commit a9d4ee81e84cedc2d770fdcea180aa8b8aba36e7
Author: Bart Thierens <[email protected]>
AuthorDate: Mon Sep 2 10:19:09 2024 +0200
SLING-12413: update/improve code
---
.../sling/testing/mock/osgi/MockBundleContext.java | 20 +++++------------
.../sling/testing/mock/osgi/OsgiServiceUtil.java | 26 +++++-----------------
.../mock/osgi/MockServiceReferencesSortTest.java | 14 ++++++------
3 files changed, 18 insertions(+), 42 deletions(-)
diff --git
a/core/src/main/java/org/apache/sling/testing/mock/osgi/MockBundleContext.java
b/core/src/main/java/org/apache/sling/testing/mock/osgi/MockBundleContext.java
index e4a373c..4b10057 100644
---
a/core/src/main/java/org/apache/sling/testing/mock/osgi/MockBundleContext.java
+++
b/core/src/main/java/org/apache/sling/testing/mock/osgi/MockBundleContext.java
@@ -116,7 +116,6 @@ class MockBundleContext implements BundleContext {
}
}
- @SuppressWarnings("unchecked")
@Override
public ServiceRegistration registerService(final String clazz, final
Object service, final Dictionary properties) {
String[] clazzes;
@@ -165,9 +164,7 @@ class MockBundleContext implements BundleContext {
* Check for already registered services that may be affected by the
service registration - either
* adding by additional optional references, or creating a conflict in the
dependencies.
* @param registration Service registration
- * @param bundleContext Bundle context
*/
- @SuppressWarnings("unchecked")
private void handleRefsUpdateOnRegister(MockServiceRegistration<?>
registration) {
// handle DYNAMIC references to this registration
@@ -194,11 +191,11 @@ class MockBundleContext implements BundleContext {
OsgiServiceUtil.invokeBindMethod(
reference,
referenceInfo.getServiceRegistration().getService(),
- new ServiceInfo(registration),
+ new ServiceInfo<>(registration),
this);
break;
default:
- throw new RuntimeException("Unepxected cardinality: "
+ reference.getCardinality());
+ throw new RuntimeException("Unexpected cardinality: "
+ reference.getCardinality());
}
}
}
@@ -220,7 +217,7 @@ class MockBundleContext implements BundleContext {
servicesToRestart.add(referenceInfo.getServiceRegistration());
break;
default:
- throw new RuntimeException("Unepxected cardinality: "
+ reference.getCardinality());
+ throw new RuntimeException("Unexpected cardinality: "
+ reference.getCardinality());
}
}
}
@@ -269,9 +266,7 @@ class MockBundleContext implements BundleContext {
* Check for already registered services that may be affected by the
service unregistration - either
* adding by removing optional references, or creating a conflict in the
dependencies.
* @param registration Service registration
- * @param bundleContext Bundle context
*/
- @SuppressWarnings("unchecked")
private void handleRefsUpdateOnUnregister(MockServiceRegistration<?>
registration) {
// handle DYNAMIC references to this registration
@@ -290,11 +285,11 @@ class MockBundleContext implements BundleContext {
OsgiServiceUtil.invokeUnbindMethod(
reference,
referenceInfo.getServiceRegistration().getService(),
- new ServiceInfo(registration),
+ new ServiceInfo<>(registration),
this);
break;
default:
- throw new RuntimeException("Unepxected cardinality: "
+ reference.getCardinality());
+ throw new RuntimeException("Unexpected cardinality: "
+ reference.getCardinality());
}
}
}
@@ -314,14 +309,13 @@ class MockBundleContext implements BundleContext {
servicesToRestart.add(referenceInfo.getServiceRegistration());
break;
default:
- throw new RuntimeException("Unepxected cardinality: "
+ reference.getCardinality());
+ throw new RuntimeException("Unexpected cardinality: "
+ reference.getCardinality());
}
}
}
servicesToRestart.forEach(this::restartService);
}
- @SuppressWarnings("unchecked")
@Override
public ServiceReference getServiceReference(final String clazz) {
try {
@@ -341,7 +335,6 @@ class MockBundleContext implements BundleContext {
return getServiceReference(clazz.getName());
}
- @SuppressWarnings("unchecked")
@Override
public ServiceReference[] getServiceReferences(final String clazz, final
String filter)
throws InvalidSyntaxException {
@@ -375,7 +368,6 @@ class MockBundleContext implements BundleContext {
}
}
- @SuppressWarnings("unchecked")
@Override
public ServiceReference[] getAllServiceReferences(final String clazz,
final String filter)
throws InvalidSyntaxException {
diff --git
a/core/src/main/java/org/apache/sling/testing/mock/osgi/OsgiServiceUtil.java
b/core/src/main/java/org/apache/sling/testing/mock/osgi/OsgiServiceUtil.java
index 05e656d..9d4b813 100644
--- a/core/src/main/java/org/apache/sling/testing/mock/osgi/OsgiServiceUtil.java
+++ b/core/src/main/java/org/apache/sling/testing/mock/osgi/OsgiServiceUtil.java
@@ -431,12 +431,7 @@ final class OsgiServiceUtil {
try {
field.setAccessible(true);
field.set(target, value);
- } catch (IllegalAccessException ex) {
- throw new RuntimeException(
- "Unable to set field '" + field.getName() + "' for class "
- + target.getClass().getName(),
- ex);
- } catch (IllegalArgumentException ex) {
+ } catch (IllegalAccessException | IllegalArgumentException ex) {
throw new RuntimeException(
"Unable to set field '" + field.getName() + "' for class "
+ target.getClass().getName(),
@@ -785,21 +780,11 @@ final class OsgiServiceUtil {
case SERVICE:
case REFERENCE:
case SERVICEOBJECTS:
- Object item = null;
- if (serviceInfo != null) {
- item = serviceInfo.getService();
- if (reference.getFieldCollectionType() ==
FieldCollectionType.REFERENCE) {
- item = serviceInfo.getServiceReference();
- } else if (reference.getFieldCollectionType() ==
FieldCollectionType.SERVICEOBJECTS) {
- item = serviceInfo;
- }
- }
Field field = getCollectionField(targetClass,
fieldName);
if (field != null) {
// to make sure components are consistently sorted
(according to Felix sorting)
// we (re-)bind the entire collection field every
time a reference is added or removed
bindCollectionReference(reference, bundleContext,
target, field);
- return;
}
break;
default:
@@ -839,7 +824,6 @@ final class OsgiServiceUtil {
field = getField(targetClass, fieldName, Optional.class);
if (field != null) {
setField(target, field, servicePresent ?
Optional.of(serviceInfo.getService()) : Optional.empty());
- return;
}
}
}
@@ -1074,12 +1058,12 @@ final class OsgiServiceUtil {
Object otherRankObj = o.get(Constants.SERVICE_RANKING);
// If no rank, then spec says it defaults to zero.
- rankObj = (rankObj == null) ? new Integer(0) : rankObj;
- otherRankObj = (otherRankObj == null) ? new Integer(0) :
otherRankObj;
+ rankObj = (rankObj == null) ? Integer.valueOf(0) : rankObj;
+ otherRankObj = (otherRankObj == null) ? Integer.valueOf(0) :
otherRankObj;
// If rank is not Integer, then spec says it defaults to zero.
- Integer rank = (rankObj instanceof Integer) ? (Integer) rankObj :
new Integer(0);
- Integer otherRank = (otherRankObj instanceof Integer) ? (Integer)
otherRankObj : new Integer(0);
+ Integer rank = (rankObj instanceof Integer) ? (Integer) rankObj :
Integer.valueOf(0);
+ Integer otherRank = (otherRankObj instanceof Integer) ? (Integer)
otherRankObj : Integer.valueOf(0);
// Sort by rank in ascending order.
if (rank.compareTo(otherRank) < 0) {
diff --git
a/core/src/test/java/org/apache/sling/testing/mock/osgi/MockServiceReferencesSortTest.java
b/core/src/test/java/org/apache/sling/testing/mock/osgi/MockServiceReferencesSortTest.java
index b3491b8..cc04ec8 100644
---
a/core/src/test/java/org/apache/sling/testing/mock/osgi/MockServiceReferencesSortTest.java
+++
b/core/src/test/java/org/apache/sling/testing/mock/osgi/MockServiceReferencesSortTest.java
@@ -124,8 +124,8 @@ public class MockServiceReferencesSortTest {
// random order of instantiation and registration
RankedService serviceRanking0 = Mockito.mock(RankedService.class);
bundleContext.registerService(RankedService.class, serviceRanking0,
createServiceRankingDictionary(0));
- RankedService serviceRanking_5 = Mockito.mock(RankedService.class);
- bundleContext.registerService(RankedService.class, serviceRanking_5,
createServiceRankingDictionary(-5));
+ RankedService serviceRankingMin5 = Mockito.mock(RankedService.class);
+ bundleContext.registerService(RankedService.class, serviceRankingMin5,
createServiceRankingDictionary(-5));
RankedService serviceRanking5 = Mockito.mock(RankedService.class);
bundleContext.registerService(RankedService.class, serviceRanking5,
createServiceRankingDictionary(5));
@@ -134,20 +134,20 @@ public class MockServiceReferencesSortTest {
List<RankedService> referencedServices = service.getRankedServices();
assertNotNull(referencedServices);
assertEquals(3, referencedServices.size());
- assertEquals(serviceRanking_5, referencedServices.get(0));
+ assertEquals(serviceRankingMin5, referencedServices.get(0));
assertEquals(serviceRanking0, referencedServices.get(1));
assertEquals(serviceRanking5, referencedServices.get(2));
// register some more services
- RankedService serviceRanking_999 = Mockito.mock(RankedService.class);
- bundleContext.registerService(RankedService.class, serviceRanking_999,
createServiceRankingDictionary(-999));
+ RankedService serviceRankingMin999 = Mockito.mock(RankedService.class);
+ bundleContext.registerService(RankedService.class,
serviceRankingMin999, createServiceRankingDictionary(-999));
RankedService serviceRanking1 = Mockito.mock(RankedService.class);
bundleContext.registerService(RankedService.class, serviceRanking1,
createServiceRankingDictionary(1));
// check that the collection has been updated and sorted correctly
assertEquals(5, referencedServices.size());
- assertEquals(serviceRanking_999, referencedServices.get(0));
- assertEquals(serviceRanking_5, referencedServices.get(1));
+ assertEquals(serviceRankingMin999, referencedServices.get(0));
+ assertEquals(serviceRankingMin5, referencedServices.get(1));
assertEquals(serviceRanking0, referencedServices.get(2));
assertEquals(serviceRanking1, referencedServices.get(3));
assertEquals(serviceRanking5, referencedServices.get(4));