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-feature-analyser.git
The following commit(s) were added to refs/heads/master by this push:
new 40f8654 SLING-9536 : Add analyser checking for duplicate symbolic
names
40f8654 is described below
commit 40f86541b6c44263f87e9499b281ac6153dc7678
Author: Carsten Ziegeler <[email protected]>
AuthorDate: Sun Jun 21 13:03:02 2020 +0200
SLING-9536 : Add analyser checking for duplicate symbolic names
---
.../impl/CheckContentPackagesDependencies.java | 2 +-
.../task/impl/CheckDuplicateSymbolicName.java | 76 +++++++++++++++++
.../task/impl/CheckDuplicateSymbolicNameTest.java | 94 ++++++++++++++++++++++
3 files changed, 171 insertions(+), 1 deletion(-)
diff --git
a/src/main/java/org/apache/sling/feature/analyser/task/impl/CheckContentPackagesDependencies.java
b/src/main/java/org/apache/sling/feature/analyser/task/impl/CheckContentPackagesDependencies.java
index b3ee952..4091da5 100644
---
a/src/main/java/org/apache/sling/feature/analyser/task/impl/CheckContentPackagesDependencies.java
+++
b/src/main/java/org/apache/sling/feature/analyser/task/impl/CheckContentPackagesDependencies.java
@@ -51,7 +51,7 @@ public class CheckContentPackagesDependencies implements
AnalyserTask {
@Override
public String getName() {
- return "Content-packages dependencies checker";
+ return "Content packages dependencies checker";
}
@Override
diff --git
a/src/main/java/org/apache/sling/feature/analyser/task/impl/CheckDuplicateSymbolicName.java
b/src/main/java/org/apache/sling/feature/analyser/task/impl/CheckDuplicateSymbolicName.java
new file mode 100644
index 0000000..5c5987d
--- /dev/null
+++
b/src/main/java/org/apache/sling/feature/analyser/task/impl/CheckDuplicateSymbolicName.java
@@ -0,0 +1,76 @@
+/*
+ * 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.feature.analyser.task.impl;
+
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+import java.util.SortedMap;
+import java.util.TreeMap;
+
+import org.apache.sling.feature.ArtifactId;
+import org.apache.sling.feature.analyser.task.AnalyserTask;
+import org.apache.sling.feature.analyser.task.AnalyserTaskContext;
+import org.apache.sling.feature.scanner.BundleDescriptor;
+
+
+public class CheckDuplicateSymbolicName implements AnalyserTask {
+
+ @Override
+ public String getName() {
+ return "Duplicate Symbolic Name";
+ }
+
+ @Override
+ public String getId() {
+ return "duplicate-symbolic-names";
+ }
+
+ private SortedMap<String, Set<ArtifactId>> createBundleMap(final
AnalyserTaskContext ctx) {
+ // build a map of bundles by symbolic name
+
+ final SortedMap<String, Set<ArtifactId>> bundleMap = new TreeMap<>();
+ for(final BundleDescriptor desc :
ctx.getFeatureDescriptor().getBundleDescriptors()) {
+ final String key = desc.getBundleSymbolicName();
+ final Set<ArtifactId> set = bundleMap.computeIfAbsent(key, id ->
new HashSet<>());
+ set.add(desc.getArtifact().getId());
+ }
+ return bundleMap;
+ }
+
+ @Override
+ public void execute(final AnalyserTaskContext ctx) throws Exception {
+ final SortedMap<String, Set<ArtifactId>> bundleMap =
createBundleMap(ctx);
+
+ for(final Map.Entry<String, Set<ArtifactId>> entry :
bundleMap.entrySet()) {
+ if ( entry.getValue().size() > 1 ) {
+ final StringBuilder sb = new StringBuilder();
+ sb.append("Duplicate symbolic name ");
+ sb.append(entry.getKey());
+ sb.append(" : ");
+ boolean first = true;
+ for(final ArtifactId id : entry.getValue()) {
+ if ( first ) first = false; else sb.append(", ");
+ sb.append(id.toMvnId());
+ }
+ ctx.reportError(sb.toString());
+ }
+ }
+ }
+}
diff --git
a/src/test/java/org/apache/sling/feature/analyser/task/impl/CheckDuplicateSymbolicNameTest.java
b/src/test/java/org/apache/sling/feature/analyser/task/impl/CheckDuplicateSymbolicNameTest.java
new file mode 100644
index 0000000..9ffe513
--- /dev/null
+++
b/src/test/java/org/apache/sling/feature/analyser/task/impl/CheckDuplicateSymbolicNameTest.java
@@ -0,0 +1,94 @@
+/*
+ * 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.feature.analyser.task.impl;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+import static org.mockito.ArgumentMatchers.anyString;
+import static org.mockito.Mockito.doAnswer;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+import java.util.LinkedList;
+import java.util.List;
+
+import org.apache.sling.feature.Artifact;
+import org.apache.sling.feature.ArtifactId;
+import org.apache.sling.feature.Feature;
+import org.apache.sling.feature.analyser.task.AnalyserTask;
+import org.apache.sling.feature.analyser.task.AnalyserTaskContext;
+import org.apache.sling.feature.scanner.BundleDescriptor;
+import org.apache.sling.feature.scanner.FeatureDescriptor;
+import org.apache.sling.feature.scanner.impl.FeatureDescriptorImpl;
+import org.junit.Before;
+import org.junit.Test;
+
+
+public class CheckDuplicateSymbolicNameTest {
+
+ private List<String> errors = new LinkedList<>();
+
+ private AnalyserTaskContext ctx;
+
+ @Before public void setup() {
+ ctx = mock(AnalyserTaskContext.class);
+
+ final Feature feature = new Feature(ArtifactId.parse("g:a:1"));
+ FeatureDescriptor featureDescriptor = new
FeatureDescriptorImpl(feature);
+
+ when(ctx.getFeatureDescriptor()).thenReturn(featureDescriptor);
+
+ doAnswer(invocation -> {
+ String error = invocation.getArgument(0);
+ errors.add(error);
+ return null;
+ }).when(ctx).reportError(anyString());
+ }
+
+ private BundleDescriptor newDescriptor(final ArtifactId id, final String
symbolicName) {
+ final Artifact artifact = new Artifact(id);
+ final BundleDescriptor desc = mock(BundleDescriptor.class);
+ when(desc.getBundleSymbolicName()).thenReturn(symbolicName);
+ when(desc.getArtifact()).thenReturn(artifact);
+ return desc;
+ }
+
+ @Test public void testNoDuplicates() throws Exception {
+ final AnalyserTask task = new CheckDuplicateSymbolicName();
+
+
ctx.getFeatureDescriptor().getBundleDescriptors().add(newDescriptor(ArtifactId.parse("g:b1:1"),
"b1"));
+
ctx.getFeatureDescriptor().getBundleDescriptors().add(newDescriptor(ArtifactId.parse("g:b2:1"),
"b2"));
+
ctx.getFeatureDescriptor().getBundleDescriptors().add(newDescriptor(ArtifactId.parse("g:b3:1"),
"b3"));
+ task.execute(ctx);
+ assertTrue(errors.isEmpty());
+ }
+
+ @Test public void testDuplicates() throws Exception {
+ final AnalyserTask task = new CheckDuplicateSymbolicName();
+
+
ctx.getFeatureDescriptor().getBundleDescriptors().add(newDescriptor(ArtifactId.parse("g:b1:1"),
"b1"));
+
ctx.getFeatureDescriptor().getBundleDescriptors().add(newDescriptor(ArtifactId.parse("g:b2:1"),
"b1"));
+
ctx.getFeatureDescriptor().getBundleDescriptors().add(newDescriptor(ArtifactId.parse("g:b3:1"),
"b2"));
+
ctx.getFeatureDescriptor().getBundleDescriptors().add(newDescriptor(ArtifactId.parse("g:b4:1"),
"b3"));
+
ctx.getFeatureDescriptor().getBundleDescriptors().add(newDescriptor(ArtifactId.parse("g:b5:1"),
"b3"));
+ task.execute(ctx);
+ assertEquals(2, errors.size());
+ }
+
+}