diff --git a/.github/workflows/maven-build.yml b/.github/workflows/maven-build.yml index bd99c91..33879bd 100644 --- a/.github/workflows/maven-build.yml +++ b/.github/workflows/maven-build.yml @@ -20,7 +20,7 @@ jobs: runs-on: ${{ matrix.os }} strategy: matrix: - java: [8, 11, 17] + java: [11, 17] os: [ubuntu-latest] distribution: [temurin] diff --git a/.github/workflows/maven-deploy.yml b/.github/workflows/maven-deploy.yml index 96d6b65..aef0ee4 100644 --- a/.github/workflows/maven-deploy.yml +++ b/.github/workflows/maven-deploy.yml @@ -1,4 +1,4 @@ -# Deploy snapshots to Sonatpe OSS repository and deploy site to GitHub Pages +# Deploy snapshots to Sonatype OSS repository and deploy site to GitHub Pages name: Deploy @@ -15,7 +15,7 @@ jobs: steps: - name: Checkout code - uses: actions/checkout@v2 + uses: actions/checkout@v3 - name: Configure GIT run: | @@ -23,11 +23,11 @@ jobs: git config --global user.name "${{ secrets.GH_SITE_DEPLOY_NAME }}" - name: Setup JDK - uses: actions/setup-java@v2 + uses: actions/setup-java@v3 with: distribution: temurin - java-version: 8 - cache: 'maven' + java-version: 11 + cache: maven - name: Build, verify, deploy, generate site env: diff --git a/.github/workflows/release-from-tag.yml b/.github/workflows/release-from-tag.yml index 8bc15d1..e517506 100644 --- a/.github/workflows/release-from-tag.yml +++ b/.github/workflows/release-from-tag.yml @@ -12,7 +12,7 @@ jobs: permissions: contents: write steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v3 - uses: ncipollo/release-action@v1 with: body: 'Changes: https://wcm.io/sling/commons/changes-report.html' diff --git a/.gitignore b/.gitignore index cb0de5f..e614605 100644 --- a/.gitignore +++ b/.gitignore @@ -32,3 +32,4 @@ npm-debug.log .vlt .vlt-sync* .brackets.json +dependency-reduced-pom.xml diff --git a/README.md b/README.md index 6b9669e..cceac23 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ Common Sling utility and helper functions. Documentation: https://wcm.io/sling/commons/
-Issues: https://wcm-io.atlassian.net/browse/WSLI
+Issues: https://github.com/wcm-io/io.wcm.sling.commons/issues
Wiki: https://wcm-io.atlassian.net/wiki/
Continuous Integration: https://github.com/wcm-io/io.wcm.sling.commons/actions
Commercial support: https://wcm.io/commercial-support.html diff --git a/changes.xml b/changes.xml index cdbe176..26fc3e5 100644 --- a/changes.xml +++ b/changes.xml @@ -23,6 +23,15 @@ xsi:schemaLocation="http://maven.apache.org/changes/1.0.0 http://maven.apache.org/plugins/maven-changes-plugin/xsd/changes-1.0.0.xsd"> + + + Switch to Java 11 as minimum version. + + + Eliminate dependency to Guava. Embed Caffeine as replacement for Guava Cache. + + + ContextAwareServiceResolver: Add ContextAwareServiceCollectionResolver that allows context-aware filtering of a pre-propulated service collections (e.g. managed by Declarative Services). diff --git a/pom.xml b/pom.xml index 97169f3..c5b62a8 100644 --- a/pom.xml +++ b/pom.xml @@ -25,13 +25,13 @@ io.wcm io.wcm.parent_toplevel - 2.1.0 + 2.2.0 io.wcm io.wcm.sling.commons - 1.6.0 + 1.6.2 jar Sling Commons @@ -49,7 +49,7 @@ sling/commons - 2022-12-05T12:35:44Z + 2023-04-19T11:06:55Z @@ -65,6 +65,13 @@ compile + + com.github.ben-manes.caffeine + caffeine + 3.1.5 + compile + + org.junit.jupiter junit-jupiter-api @@ -110,12 +117,6 @@ provided - - com.google.guava - guava - compile - - org.apache.commons commons-lang3 @@ -148,6 +149,14 @@ biz.aQute.bnd bnd-maven-plugin + + + + Import-Package: \ + !com.github.benmanes.caffeine.*, \ + * + + biz.aQute.bnd @@ -163,6 +172,42 @@ + + + org.apache.maven.plugins + maven-shade-plugin + + + + com.github.ben-manes.caffeine:caffeine + + + + + com.github.benmanes.caffeine + io.wcm.sling.commons.shaded.com.github.benmanes.caffeine + + + + + com.github.ben-manes.caffeine:caffeine + + module-info.class + META-INF/** + + + + + + + package + + shade + + + + + org.apache.maven.plugins maven-site-plugin diff --git a/src/main/java/io/wcm/sling/commons/caservice/impl/ContextAwareServiceCollectionResolverImpl.java b/src/main/java/io/wcm/sling/commons/caservice/impl/ContextAwareServiceCollectionResolverImpl.java index 9cfd156..8680f4b 100644 --- a/src/main/java/io/wcm/sling/commons/caservice/impl/ContextAwareServiceCollectionResolverImpl.java +++ b/src/main/java/io/wcm/sling/commons/caservice/impl/ContextAwareServiceCollectionResolverImpl.java @@ -32,10 +32,9 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import com.google.common.cache.CacheBuilder; -import com.google.common.cache.CacheLoader; -import com.google.common.cache.LoadingCache; -import com.google.common.cache.RemovalNotification; +import com.github.benmanes.caffeine.cache.Caffeine; +import com.github.benmanes.caffeine.cache.LoadingCache; +import com.github.benmanes.caffeine.cache.RemovalCause; import io.wcm.sling.commons.caservice.ContextAwareService; import io.wcm.sling.commons.caservice.ContextAwareServiceCollectionResolver; @@ -62,22 +61,19 @@ class ContextAwareServiceCollectionResolverImpl LoadingCache, CollectionItemDecoration> buildCache( @NotNull BiFunction<@NotNull ServiceReference, @Nullable S, @Nullable D> decorator, @NotNull BundleContext bundleContext) { - return CacheBuilder.newBuilder() + return Caffeine.newBuilder() // expire cached entry after 24h .expireAfterAccess(24, TimeUnit.HOURS) // unget service on removal - .removalListener((RemovalNotification, CollectionItemDecoration> notification) -> { - log.debug("Remove service {}", notification.getValue()); - bundleContext.ungetService(notification.getKey()); + .removalListener((ServiceReference key, CollectionItemDecoration value, RemovalCause cause) -> { + log.debug("Remove service {}", value); + bundleContext.ungetService(key); }) // build cache lazily - .build(new CacheLoader, CollectionItemDecoration>() { - @Override - public CollectionItemDecoration load(ServiceReference serviceReference) { - CollectionItemDecoration item = new CollectionItemDecoration<>(serviceReference, decorator, bundleContext); - log.debug("Add service {}", item); - return item; - } + .build((ServiceReference serviceReference) -> { + CollectionItemDecoration item = new CollectionItemDecoration<>(serviceReference, decorator, bundleContext); + log.debug("Add service {}", item); + return item; }); } @@ -112,7 +108,7 @@ public CollectionItemDecoration load(ServiceReference serviceReference) private @NotNull Stream> getMatching(@Nullable Adaptable adaptable) { String resourcePath = resourcePathResolver.get(adaptable); return serviceReferenceCollection.stream() - .map(decorationCache::getUnchecked) + .map(decorationCache::get) .filter(CollectionItemDecoration::isValid) .filter(item -> item.matches(resourcePath)); } diff --git a/src/main/java/io/wcm/sling/commons/caservice/impl/ContextAwareServiceResolverImpl.java b/src/main/java/io/wcm/sling/commons/caservice/impl/ContextAwareServiceResolverImpl.java index 245f479..9c2538f 100644 --- a/src/main/java/io/wcm/sling/commons/caservice/impl/ContextAwareServiceResolverImpl.java +++ b/src/main/java/io/wcm/sling/commons/caservice/impl/ContextAwareServiceResolverImpl.java @@ -21,7 +21,6 @@ import java.util.Collection; import java.util.concurrent.ConcurrentMap; -import java.util.concurrent.ExecutionException; import java.util.function.BiFunction; import java.util.function.Supplier; import java.util.stream.Collectors; @@ -42,11 +41,9 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import com.google.common.cache.CacheBuilder; -import com.google.common.cache.CacheLoader; -import com.google.common.cache.LoadingCache; -import com.google.common.cache.RemovalListener; -import com.google.common.cache.RemovalNotification; +import com.github.benmanes.caffeine.cache.Caffeine; +import com.github.benmanes.caffeine.cache.LoadingCache; +import com.github.benmanes.caffeine.cache.RemovalCause; import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; import io.wcm.sling.commons.caservice.ContextAwareService; @@ -85,20 +82,9 @@ private void deactivate() { private static LoadingCache> buildServiceTrackerCache( BundleContext bundleContext) { - return CacheBuilder.newBuilder() - .removalListener(new RemovalListener>() { - @SuppressWarnings("null") - @Override - public void onRemoval(RemovalNotification> notification) { - notification.getValue().dispose(); - } - }) - .build(new CacheLoader>() { - @Override - public ContextAwareServiceTracker load(String className) { - return new ContextAwareServiceTracker<>(className, bundleContext); - } - }); + return Caffeine.newBuilder() + .removalListener((String key, ContextAwareServiceTracker value, RemovalCause reason) -> value.dispose()) + .build(className -> new ContextAwareServiceTracker<>(className, bundleContext)); } @Override @@ -148,12 +134,7 @@ private Stream> getMatchingServic "unchecked" }) private ContextAwareServiceTracker getServiceTracker(Class serviceClass) { - try { - return (ContextAwareServiceTracker)serviceTrackerCache.get(serviceClass.getName()); - } - catch (ExecutionException ex) { - throw new RuntimeException("Error getting service tracker for " + serviceClass.getName() + " from cache.", ex); - } + return (ContextAwareServiceTracker)serviceTrackerCache.get(serviceClass.getName()); } ConcurrentMap> getContextAwareServiceTrackerMap() { diff --git a/src/main/java/io/wcm/sling/commons/resource/ImmutableValueMap.java b/src/main/java/io/wcm/sling/commons/resource/ImmutableValueMap.java index bee8a5e..11ca568 100644 --- a/src/main/java/io/wcm/sling/commons/resource/ImmutableValueMap.java +++ b/src/main/java/io/wcm/sling/commons/resource/ImmutableValueMap.java @@ -24,7 +24,7 @@ import java.util.HashMap; import java.util.Map; import java.util.Set; -import java.util.SortedMap; +import java.util.TreeMap; import org.apache.sling.api.resource.ValueMap; import org.apache.sling.api.wrappers.ValueMapDecorator; @@ -32,10 +32,6 @@ import org.jetbrains.annotations.Nullable; import org.osgi.annotation.versioning.ProviderType; -import com.google.common.base.Joiner; -import com.google.common.collect.ImmutableMap; -import com.google.common.collect.ImmutableSortedMap; - /** * {@link ValueMap} that does not support changing its content. *

@@ -129,8 +125,7 @@ public boolean equals(Object obj) { @Override public String toString() { - SortedMap sortedMap = ImmutableSortedMap.copyOf(this.map); - return "{" + Joiner.on(",").withKeyValueSeparator("=").join(sortedMap) + "}"; + return new TreeMap<>(map).toString(); } // mutable operations not supported @@ -190,8 +185,9 @@ public void clear() { * @param v1 Value 1 * @return ImmutableValueMap */ + @SuppressWarnings("null") public static @NotNull ImmutableValueMap of(@NotNull String k1, @NotNull Object v1) { - return new ImmutableValueMap(ImmutableMap.of(k1, v1)); + return new ImmutableValueMap(Map.of(k1, v1)); } /** @@ -203,9 +199,10 @@ public void clear() { * @return ImmutableValueMap * @throws IllegalArgumentException if duplicate keys are provided */ + @SuppressWarnings("null") public static @NotNull ImmutableValueMap of(@NotNull String k1, @NotNull Object v1, @NotNull String k2, @NotNull Object v2) { - return new ImmutableValueMap(ImmutableMap.of(k1, v1, k2, v2)); + return new ImmutableValueMap(Map.of(k1, v1, k2, v2)); } /** @@ -219,11 +216,12 @@ public void clear() { * @return ImmutableValueMap * @throws IllegalArgumentException if duplicate keys are provided */ + @SuppressWarnings("null") public static @NotNull ImmutableValueMap of( @NotNull String k1, @NotNull Object v1, @NotNull String k2, @NotNull Object v2, @NotNull String k3, @NotNull Object v3) { - return new ImmutableValueMap(ImmutableMap.of(k1, v1, k2, v2, k3, v3)); + return new ImmutableValueMap(Map.of(k1, v1, k2, v2, k3, v3)); } /** @@ -239,13 +237,13 @@ public void clear() { * @return ImmutableValueMap * @throws IllegalArgumentException if duplicate keys are provided */ - @SuppressWarnings({ "java:S107", "PMD.UseObjectForClearerAPI" }) + @SuppressWarnings({ "null", "java:S107", "PMD.UseObjectForClearerAPI" }) public static @NotNull ImmutableValueMap of( @NotNull String k1, @NotNull Object v1, @NotNull String k2, @NotNull Object v2, @NotNull String k3, @NotNull Object v3, @NotNull String k4, @NotNull Object v4) { - return new ImmutableValueMap(ImmutableMap.of(k1, v1, k2, v2, k3, v3, k4, v4)); + return new ImmutableValueMap(Map.of(k1, v1, k2, v2, k3, v3, k4, v4)); } /** @@ -263,14 +261,14 @@ public void clear() { * @return ImmutableValueMap * @throws IllegalArgumentException if duplicate keys are provided */ - @SuppressWarnings({ "java:S107", "PMD.UseObjectForClearerAPI" }) + @SuppressWarnings({ "null", "java:S107", "PMD.UseObjectForClearerAPI" }) public static ImmutableValueMap of( @NotNull String k1, @NotNull Object v1, @NotNull String k2, @NotNull Object v2, @NotNull String k3, @NotNull Object v3, @NotNull String k4, @NotNull Object v4, @NotNull String k5, @NotNull Object v5) { - return new ImmutableValueMap(ImmutableMap.of(k1, v1, k2, v2, k3, v3, k4, v4, k5, v5)); + return new ImmutableValueMap(Map.of(k1, v1, k2, v2, k3, v3, k4, v4, k5, v5)); } // looking for of() with > 5 entries? Use the builder instead. diff --git a/src/main/java/io/wcm/sling/commons/util/Escape.java b/src/main/java/io/wcm/sling/commons/util/Escape.java index 8d27ff2..8c7c470 100644 --- a/src/main/java/io/wcm/sling/commons/util/Escape.java +++ b/src/main/java/io/wcm/sling/commons/util/Escape.java @@ -30,15 +30,13 @@ import org.jetbrains.annotations.Nullable; import org.osgi.annotation.versioning.ProviderType; -import com.google.common.collect.ImmutableSet; - /** * Encoding utility functions. */ @ProviderType public final class Escape { private static final char LIKE_ESCAPE_CHARACTER = '\\'; - private static final Set LIKE_SPECIAL_CHARACTERS = ImmutableSet.of('%', '_'); + private static final Set LIKE_SPECIAL_CHARACTERS = Set.of('%', '_'); private Escape() { // Utility class - no instancing allowed diff --git a/src/site/markdown/context-aware-services.md b/src/site/markdown/context-aware-services.md index 96ca61b..b1d25e8 100644 --- a/src/site/markdown/context-aware-services.md +++ b/src/site/markdown/context-aware-services.md @@ -54,7 +54,7 @@ Define references to services via Declarative Services: ```java @Reference(cardinality = ReferenceCardinality.MULTIPLE, fieldOption = FieldOption.UPDATE, policy = ReferencePolicy.DYNAMIC, policyOption = ReferencePolicyOption.GREEDY) - private Collection> services; + private SortedSet> services = new ConcurrentSkipListSet<>(Collections.reverseOrder()); ``` Setup `ContextAwareServiceCollectionResolver` which get's a view filtered by resource context of these services: @@ -62,7 +62,7 @@ Setup `ContextAwareServiceCollectionResolver` which get's a view filtered by res ```java @Reference private ContextAwareServiceResolver serviceResolver; - private ContextAwareServiceCollectionResolver serviceCollectionResolver; + private ContextAwareServiceCollectionResolver serviceCollectionResolver; @Activate private void activate() { diff --git a/src/test/java/io/wcm/sling/commons/caservice/impl/ContextAwareServiceCollectionResolverImplTest.java b/src/test/java/io/wcm/sling/commons/caservice/impl/ContextAwareServiceCollectionResolverImplTest.java index 62c896e..d1e9fc4 100644 --- a/src/test/java/io/wcm/sling/commons/caservice/impl/ContextAwareServiceCollectionResolverImplTest.java +++ b/src/test/java/io/wcm/sling/commons/caservice/impl/ContextAwareServiceCollectionResolverImplTest.java @@ -23,6 +23,7 @@ import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertSame; +import java.util.List; import java.util.stream.Collectors; import org.apache.commons.lang3.StringUtils; @@ -30,8 +31,6 @@ import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; -import com.google.common.collect.ImmutableList; - import io.wcm.sling.commons.caservice.ContextAwareServiceCollectionResolver; import io.wcm.sling.commons.caservice.ContextAwareServiceResolver; import io.wcm.sling.commons.caservice.PathPreprocessor; @@ -75,7 +74,7 @@ void testWithDefaultImpl() { assertSame(contentDamImpl, underTest.resolve(context.create().resource("/content/dam/test1"))); assertSame(defaultImpl, underTest.resolve(context.create().resource("/etc/test1"))); - assertEquals(ImmutableList.of(contentDamImpl, contentImpl, defaultImpl), + assertEquals(List.of(contentDamImpl, contentImpl, defaultImpl), underTest.resolveAll(context.create().resource("/content/dam/test2")).collect(Collectors.toList())); } } @@ -93,7 +92,7 @@ void testWithDefaultImpl_Decorated() { assertSame(contentDamImpl, underTest.resolveDecorated(context.create().resource("/content/dam/test1")).getService()); assertSame(defaultImpl, underTest.resolveDecorated(context.create().resource("/etc/test1")).getService()); - assertEquals(ImmutableList.of(contentDamImpl, contentImpl, defaultImpl), + assertEquals(List.of(contentDamImpl, contentImpl, defaultImpl), underTest.resolveAllDecorated(context.create().resource("/content/dam/test2")) .map(DummySpiDecorator::getService).collect(Collectors.toList())); } @@ -109,13 +108,13 @@ void testWithDefaultImpl_DynamicListChange() { assertSame(contentImpl, underTest.resolve(context.create().resource("/content/sample/exclude/test1"))); assertSame(contentDamImpl, underTest.resolve(context.create().resource("/content/dam/test1"))); assertNull(underTest.resolve(context.create().resource("/etc/test1"))); - assertEquals(ImmutableList.of(contentDamImpl, contentImpl), + assertEquals(List.of(contentDamImpl, contentImpl), underTest.resolveAll(context.create().resource("/content/dam/test2")).collect(Collectors.toList())); DummySpi defaultImpl = testServices.addDefaultService(); assertSame(defaultImpl, underTest.resolve(context.create().resource("/etc/test2"))); - assertEquals(ImmutableList.of(contentDamImpl, contentImpl, defaultImpl), + assertEquals(List.of(contentDamImpl, contentImpl, defaultImpl), underTest.resolveAll(context.create().resource("/content/dam/test3")).collect(Collectors.toList())); } } @@ -131,9 +130,9 @@ void testWithoutDefaultImpl() { assertSame(contentDamImpl, underTest.resolve(context.create().resource("/content/dam/test1"))); assertNull(underTest.resolve(context.create().resource("/etc/test1"))); - assertEquals(ImmutableList.of(contentDamImpl, contentImpl), + assertEquals(List.of(contentDamImpl, contentImpl), underTest.resolveAll(context.create().resource("/content/dam/test2")).collect(Collectors.toList())); - assertEquals(ImmutableList.of(contentSampleImpl, contentImpl), + assertEquals(List.of(contentSampleImpl, contentImpl), underTest.resolveAll(context.create().resource("/content/sample/test2")).collect(Collectors.toList())); } } @@ -147,7 +146,7 @@ void testWithSlingHttpServletRequest() { context.currentResource(context.create().resource("/content/sample/test1")); assertSame(contentSampleImpl, underTest.resolve(context.request())); - assertEquals(ImmutableList.of(contentSampleImpl, contentImpl, defaultImpl), + assertEquals(List.of(contentSampleImpl, contentImpl, defaultImpl), underTest.resolveAll(context.request()).collect(Collectors.toList())); } } @@ -166,7 +165,7 @@ void testWithSlingHttpServletRequest_ResourceOtherContext() { context.currentResource(context.create().resource("/content/experience-fragments/test1")); assertSame(contentSampleImpl, underTest.resolve(context.request())); - assertEquals(ImmutableList.of(contentSampleImpl, contentImpl, defaultImpl), + assertEquals(List.of(contentSampleImpl, contentImpl, defaultImpl), underTest.resolveAll(context.request()).collect(Collectors.toList())); } } @@ -180,7 +179,7 @@ void testWithNull() { assertSame(defaultImpl, underTest.resolve(null)); - assertEquals(ImmutableList.of(defaultImpl), underTest.resolveAll(null).collect(Collectors.toList())); + assertEquals(List.of(defaultImpl), underTest.resolveAll(null).collect(Collectors.toList())); } } @@ -196,7 +195,7 @@ void testWithBundleHeader() { assertSame(contentDamImplWithBundleHeader, underTest.resolve(context.create().resource("/content/dam/test1"))); assertNull(underTest.resolve(context.create().resource("/etc/test1"))); - assertEquals(ImmutableList.of(contentDamImplWithBundleHeader, contentDamImpl, contentImpl), + assertEquals(List.of(contentDamImplWithBundleHeader, contentDamImpl, contentImpl), underTest.resolveAll(context.create().resource("/content/dam/test2")).collect(Collectors.toList())); } } @@ -214,7 +213,7 @@ void testWithPathPreProcessor() { assertSame(contentDamImpl, underTest.resolve(context.create().resource("/pathprefix/content/dam/test1"))); assertNull(underTest.resolve(context.create().resource("/pathprefix/etc/test1"))); - assertEquals(ImmutableList.of(contentDamImpl, contentImpl), + assertEquals(List.of(contentDamImpl, contentImpl), underTest.resolveAll(context.create().resource("/pathprefix/content/dam/test2")).collect(Collectors.toList())); } } diff --git a/src/test/java/io/wcm/sling/commons/caservice/impl/ContextAwareServiceResolverImplTest.java b/src/test/java/io/wcm/sling/commons/caservice/impl/ContextAwareServiceResolverImplTest.java index e83753e..289b764 100644 --- a/src/test/java/io/wcm/sling/commons/caservice/impl/ContextAwareServiceResolverImplTest.java +++ b/src/test/java/io/wcm/sling/commons/caservice/impl/ContextAwareServiceResolverImplTest.java @@ -24,6 +24,7 @@ import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertSame; +import java.util.List; import java.util.stream.Collectors; import org.apache.commons.lang3.StringUtils; @@ -31,8 +32,6 @@ import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; -import com.google.common.collect.ImmutableList; - import io.wcm.sling.commons.caservice.ContextAwareServiceResolver; import io.wcm.sling.commons.caservice.ContextAwareServiceResolver.ResolveAllResult; import io.wcm.sling.commons.caservice.PathPreprocessor; @@ -74,7 +73,7 @@ void testWithDefaultImpl() { assertSame(contentDamImpl, underTest.resolve(DummySpi.class, context.create().resource("/content/dam/test1"))); assertSame(defaultImpl, underTest.resolve(DummySpi.class, context.create().resource("/etc/test1"))); - assertEquals(ImmutableList.of(contentDamImpl, contentImpl, defaultImpl), + assertEquals(List.of(contentDamImpl, contentImpl, defaultImpl), underTest.resolveAll(DummySpi.class, context.create().resource("/content/dam/test2")).getServices().collect(Collectors.toList())); } @@ -86,9 +85,9 @@ void testWithoutDefaultImpl() { assertSame(contentDamImpl, underTest.resolve(DummySpi.class, context.create().resource("/content/dam/test1"))); assertNull(underTest.resolve(DummySpi.class, context.create().resource("/etc/test1"))); - assertEquals(ImmutableList.of(contentDamImpl, contentImpl), + assertEquals(List.of(contentDamImpl, contentImpl), underTest.resolveAll(DummySpi.class, context.create().resource("/content/dam/test2")).getServices().collect(Collectors.toList())); - assertEquals(ImmutableList.of(contentSampleImpl, contentImpl), + assertEquals(List.of(contentSampleImpl, contentImpl), underTest.resolveAll(DummySpi.class, context.create().resource("/content/sample/test2")).getServices().collect(Collectors.toList())); } @@ -99,7 +98,7 @@ void testWithSlingHttpServletRequest() { context.currentResource(context.create().resource("/content/sample/test1")); assertSame(contentSampleImpl, underTest.resolve(DummySpi.class, context.request())); - assertEquals(ImmutableList.of(contentSampleImpl, contentImpl, defaultImpl), + assertEquals(List.of(contentSampleImpl, contentImpl, defaultImpl), underTest.resolveAll(DummySpi.class, context.request()).getServices().collect(Collectors.toList())); } @@ -115,7 +114,7 @@ void testWithSlingHttpServletRequest_ResourceOtherContext() { context.currentResource(context.create().resource("/content/experience-fragments/test1")); assertSame(contentSampleImpl, underTest.resolve(DummySpi.class, context.request())); - assertEquals(ImmutableList.of(contentSampleImpl, contentImpl, defaultImpl), + assertEquals(List.of(contentSampleImpl, contentImpl, defaultImpl), underTest.resolveAll(DummySpi.class, context.request()).getServices().collect(Collectors.toList())); } @@ -125,7 +124,7 @@ void testWithNull() { assertSame(defaultImpl, underTest.resolve(DummySpi.class, null)); - assertEquals(ImmutableList.of(defaultImpl), underTest.resolveAll(DummySpi.class, null).getServices().collect(Collectors.toList())); + assertEquals(List.of(defaultImpl), underTest.resolveAll(DummySpi.class, null).getServices().collect(Collectors.toList())); } @Test @@ -148,7 +147,7 @@ void testWithBundleHeader() { assertSame(contentDamImplWithBundleHeader, underTest.resolve(DummySpi.class, context.create().resource("/content/dam/test1"))); assertNull(underTest.resolve(DummySpi.class, context.create().resource("/etc/test1"))); - assertEquals(ImmutableList.of(contentDamImplWithBundleHeader, contentDamImpl, contentImpl), + assertEquals(List.of(contentDamImplWithBundleHeader, contentDamImpl, contentImpl), underTest.resolveAll(DummySpi.class, context.create().resource("/content/dam/test2")).getServices().collect(Collectors.toList())); } @@ -163,7 +162,7 @@ void testWithPathPreProcessor() { assertSame(contentDamImpl, underTest.resolve(DummySpi.class, context.create().resource("/pathprefix/content/dam/test1"))); assertNull(underTest.resolve(DummySpi.class, context.create().resource("/pathprefix/etc/test1"))); - assertEquals(ImmutableList.of(contentDamImpl, contentImpl), + assertEquals(List.of(contentDamImpl, contentImpl), underTest.resolveAll(DummySpi.class, context.create().resource("/pathprefix/content/dam/test2")).getServices().collect(Collectors.toList())); } diff --git a/src/test/java/io/wcm/sling/commons/caservice/impl/TestServices.java b/src/test/java/io/wcm/sling/commons/caservice/impl/TestServices.java index 010108d..0b6ec4f 100644 --- a/src/test/java/io/wcm/sling/commons/caservice/impl/TestServices.java +++ b/src/test/java/io/wcm/sling/commons/caservice/impl/TestServices.java @@ -27,6 +27,7 @@ import java.util.Collection; import java.util.Comparator; import java.util.Dictionary; +import java.util.Map; import java.util.TreeSet; import org.apache.sling.testing.mock.osgi.MapUtil; @@ -34,8 +35,6 @@ import org.osgi.framework.ServiceReference; import org.osgi.framework.ServiceRegistration; -import com.google.common.collect.ImmutableMap; - import io.wcm.testing.mock.aem.junit5.AemContext; /** @@ -106,7 +105,7 @@ DummySpi addDefaultService() { @SuppressWarnings("null") DummySpi addContentDamImplWithBundleHeader() { // service gets path pattern from bundle header instead of service property - ((MockBundle)context.bundleContext().getBundle()).setHeaders(ImmutableMap.of( + ((MockBundle)context.bundleContext().getBundle()).setHeaders(Map.of( PROPERTY_CONTEXT_PATH_PATTERN, "^/content/dam(/.*)?$")); return register(new DummySpiImpl("/content/dam (bundle header)"), SERVICE_RANKING, 1000); diff --git a/src/test/java/io/wcm/sling/commons/osgi/RankedServicesTest.java b/src/test/java/io/wcm/sling/commons/osgi/RankedServicesTest.java index c8db53f..25586a2 100644 --- a/src/test/java/io/wcm/sling/commons/osgi/RankedServicesTest.java +++ b/src/test/java/io/wcm/sling/commons/osgi/RankedServicesTest.java @@ -26,26 +26,27 @@ import java.util.Map; +import org.apache.commons.collections4.IteratorUtils; import org.junit.jupiter.api.Test; import org.osgi.framework.Constants; -import com.google.common.collect.ImmutableMap; -import com.google.common.collect.Iterators; - import io.wcm.sling.commons.osgi.RankedServices.ChangeListener; @SuppressWarnings({ "deprecation", "null" }) class RankedServicesTest { private static final String SERVICE_1 = "service1"; - private static final Map SERVICE_1_PROPS = ImmutableMap.builder() - .put(Constants.SERVICE_RANKING, 50).put(Constants.SERVICE_ID, 1L).build(); + private static final Map SERVICE_1_PROPS = Map.of( + Constants.SERVICE_RANKING, 50, + Constants.SERVICE_ID, 1L); private static final String SERVICE_2 = "service2"; - private static final Map SERVICE_2_PROPS = ImmutableMap.builder() - .put(Constants.SERVICE_RANKING, 10).put(Constants.SERVICE_ID, 2L).build(); + private static final Map SERVICE_2_PROPS = Map.of( + Constants.SERVICE_RANKING, 10, + Constants.SERVICE_ID, 2L); private static final String SERVICE_3 = "service3"; - private static final Map SERVICE_3_PROPS = ImmutableMap.builder() - .put(Constants.SERVICE_RANKING, 100).put(Constants.SERVICE_ID, 3L).build(); + private static final Map SERVICE_3_PROPS = Map.of( + Constants.SERVICE_RANKING, 100, + Constants.SERVICE_ID, 3L); @Test void testSortedServices() { @@ -55,20 +56,20 @@ void testSortedServices() { underTest.bind(SERVICE_1, SERVICE_1_PROPS); assertEquals(1, underTest.get().size()); - Comparable[] services = Iterators.toArray(underTest.get().iterator(), Comparable.class); + Comparable[] services = IteratorUtils.toArray(underTest.get().iterator(), Comparable.class); assertSame(SERVICE_1, services[0]); underTest.bind(SERVICE_2, SERVICE_2_PROPS); underTest.bind(SERVICE_3, SERVICE_3_PROPS); assertEquals(3, underTest.get().size()); - services = Iterators.toArray(underTest.get().iterator(), Comparable.class); + services = IteratorUtils.toArray(underTest.get().iterator(), Comparable.class); assertSame(SERVICE_2, services[0]); assertSame(SERVICE_1, services[1]); assertSame(SERVICE_3, services[2]); underTest.unbind(SERVICE_2, SERVICE_2_PROPS); assertEquals(2, underTest.get().size()); - services = Iterators.toArray(underTest.get().iterator(), Comparable.class); + services = IteratorUtils.toArray(underTest.get().iterator(), Comparable.class); assertSame(SERVICE_1, services[0]); assertSame(SERVICE_3, services[1]); diff --git a/src/test/java/io/wcm/sling/commons/request/QueryStringBuilderTest.java b/src/test/java/io/wcm/sling/commons/request/QueryStringBuilderTest.java index 769bbbb..e83fac6 100644 --- a/src/test/java/io/wcm/sling/commons/request/QueryStringBuilderTest.java +++ b/src/test/java/io/wcm/sling/commons/request/QueryStringBuilderTest.java @@ -22,11 +22,13 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNull; -import org.junit.jupiter.api.Test; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.TreeMap; +import java.util.TreeSet; -import com.google.common.collect.ImmutableList; -import com.google.common.collect.ImmutableSortedMap; -import com.google.common.collect.ImmutableSortedSet; +import org.junit.jupiter.api.Test; import io.wcm.sling.commons.util.Escape; @@ -61,8 +63,8 @@ void testUrlEncoding() { void testMulti() { assertEquals("p1=value1&p1=value2&p1=&p2=1&p2=2&p3=false&p3=true&p4=abc", new QueryStringBuilder() .param("p1", new String[] { "value1", "value2", null }) - .param("p2", ImmutableList.of(1, 2)) - .param("p3", ImmutableSortedSet.of(false, true)) + .param("p2", List.of(1, 2)) + .param("p3", new TreeSet<>(Set.of(false, true))) .param("p4", "abc") .build()); } @@ -70,12 +72,12 @@ void testMulti() { @Test void testMap() { assertEquals("p1=value1&p1=value2&p1=&p2=1&p2=2&p3=false&p3=true&p4=abc", new QueryStringBuilder() - .params(ImmutableSortedMap.of( + .params(new TreeMap<>(Map.of( "p1", new String[] { "value1", "value2", null }, - "p2", ImmutableList.of(1, 2), - "p3", ImmutableSortedSet.of(false, true), + "p2", List.of(1, 2), + "p3", new TreeSet<>(Set.of(false, true)), "p4", "abc" - )) + ))) .build()); } diff --git a/src/test/java/io/wcm/sling/commons/request/RequestParamTest.java b/src/test/java/io/wcm/sling/commons/request/RequestParamTest.java index e3317fe..f1ea73e 100644 --- a/src/test/java/io/wcm/sling/commons/request/RequestParamTest.java +++ b/src/test/java/io/wcm/sling/commons/request/RequestParamTest.java @@ -33,8 +33,6 @@ import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; -import com.google.common.collect.ImmutableMap; - import io.wcm.testing.mock.aem.junit5.AemContext; import io.wcm.testing.mock.aem.junit5.AemContextExtension; @@ -73,34 +71,18 @@ void setUp() throws Exception { request.setParameterMap(paramMap); } + @SuppressWarnings("null") protected Map getParamMap() { - return ImmutableMap.builder() - .put(STRING_PARAM, new String[] { - STRING_VALUE - }) - .put(MULTI_STRING_PARAM, MULTI_STRING_VALUE) - .put(INTEGER_PARAM, new String[] { - Integer.toString(INTEGER_VALUE) - }) - .put(LONG_PARAM, new String[] { - Long.toString(LONG_VALUE) - }) - .put(FLOAT_PARAM, new String[] { - Float.toString(FLOAT_VALUE) - }) - .put(DOUBLE_PARAM, new String[] { - Double.toString(DOUBLE_VALUE) - }) - .put(BOOLEAN_PARAM, new String[] { - Boolean.toString(BOOLEAN_VALUE) - }) - .put(ENUM_PARAM, new String[] { - ENUM_VALUE.name() - }) - .put(RequestParam.PARAMETER_FORMENCODING, new String[] { - StandardCharsets.UTF_8.name() - }) - .build(); + return Map.of( + STRING_PARAM, new String[] { STRING_VALUE }, + MULTI_STRING_PARAM, MULTI_STRING_VALUE, + INTEGER_PARAM, new String[] { Integer.toString(INTEGER_VALUE) }, + LONG_PARAM, new String[] { Long.toString(LONG_VALUE) }, + FLOAT_PARAM, new String[] { Float.toString(FLOAT_VALUE) }, + DOUBLE_PARAM, new String[] { Double.toString(DOUBLE_VALUE) }, + BOOLEAN_PARAM, new String[] { Boolean.toString(BOOLEAN_VALUE) }, + ENUM_PARAM, new String[] { ENUM_VALUE.name() }, + RequestParam.PARAMETER_FORMENCODING, new String[] { StandardCharsets.UTF_8.name() }); } enum SAMPLE_ENUM { diff --git a/src/test/java/io/wcm/sling/commons/resource/ImmutableValueMapTest.java b/src/test/java/io/wcm/sling/commons/resource/ImmutableValueMapTest.java index b7aeea5..f8a8409 100644 --- a/src/test/java/io/wcm/sling/commons/resource/ImmutableValueMapTest.java +++ b/src/test/java/io/wcm/sling/commons/resource/ImmutableValueMapTest.java @@ -32,14 +32,11 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import com.google.common.collect.ImmutableMap; - class ImmutableValueMapTest { - private static final Map SAMPLE_PROPS = ImmutableMap.builder() - .put("prop1", "value1") - .put("prop2", 55) - .build(); + private static final Map SAMPLE_PROPS = Map.of( + "prop1", "value1", + "prop2", 55); private ValueMap underTest; @@ -95,10 +92,9 @@ void testMapPut() { @Test void testMapPutAll() { assertThrows(UnsupportedOperationException.class, () -> { - underTest.putAll(ImmutableMap.builder() - .put("prop4", 25) - .put("prop5", 33) - .build()); + underTest.putAll(Map.of( + "prop4", 25, + "prop5", 33)); }); } @@ -157,8 +153,8 @@ void testOfx5() { void testBuilder() { ValueMap map = ImmutableValueMap.builder() .put("p1", "v1") - .putAll(ImmutableMap.of("p2", "v2", "p3", "v3")) - .put(ImmutableMap.of("p4", "v4").entrySet().iterator().next()) + .putAll(Map.of("p2", "v2", "p3", "v3")) + .put(Map.of("p4", "v4").entrySet().iterator().next()) .put("p5", "v5") .build(); assertEquals(5, map.size()); @@ -210,7 +206,7 @@ void testToString() { .put("prop2", 55) .put("prop0", true) .build(); - assertEquals("{prop0=true,prop1=value1,prop2=55}", map.toString()); + assertEquals("{prop0=true, prop1=value1, prop2=55}", map.toString()); assertEquals("{}", ImmutableValueMap.of().toString()); }