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 c72870b  SLING-12407 : Add analyser to check correct type of service 
ranking
c72870b is described below

commit c72870be956fbe591b763adcaeb3fd63a6010593
Author: Carsten Ziegeler <[email protected]>
AuthorDate: Sun Aug 11 11:22:31 2024 +0200

    SLING-12407 : Add analyser to check correct type of service ranking
---
 .../analyser/task/impl/CheckConfigurations.java    | 48 ++++++++++++++++
 .../task/impl/CheckConfigurationsTest.java         | 65 ++++++++++++++++++++++
 2 files changed, 113 insertions(+)

diff --git 
a/src/main/java/org/apache/sling/feature/analyser/task/impl/CheckConfigurations.java
 
b/src/main/java/org/apache/sling/feature/analyser/task/impl/CheckConfigurations.java
new file mode 100644
index 0000000..9768406
--- /dev/null
+++ 
b/src/main/java/org/apache/sling/feature/analyser/task/impl/CheckConfigurations.java
@@ -0,0 +1,48 @@
+/*
+ * 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 org.apache.sling.feature.Configuration;
+import org.apache.sling.feature.analyser.task.AnalyserTask;
+import org.apache.sling.feature.analyser.task.AnalyserTaskContext;
+import org.osgi.framework.Constants;
+
+public class CheckConfigurations implements AnalyserTask {
+
+    @Override
+    public String getId() {
+        return "configurations-basic";
+    }
+
+    @Override
+    public String getName() {
+        return "Basic checks for Configurations";
+    }
+
+    @Override
+    public void execute(AnalyserTaskContext ctx) throws Exception {
+        for(final Configuration cfg : ctx.getFeature().getConfigurations()) {
+            // check that service ranking is of type integer
+            final Object val = 
cfg.getProperties().get(Constants.SERVICE_RANKING);
+            if ( val != null && !(val instanceof Integer) ) {
+                ctx.reportConfigurationError(cfg, "Service.ranking is not of 
type Integer. Use 'service.ranking:Integer' as the key.");
+            }
+        }
+    }
+}
diff --git 
a/src/test/java/org/apache/sling/feature/analyser/task/impl/CheckConfigurationsTest.java
 
b/src/test/java/org/apache/sling/feature/analyser/task/impl/CheckConfigurationsTest.java
new file mode 100644
index 0000000..ab903af
--- /dev/null
+++ 
b/src/test/java/org/apache/sling/feature/analyser/task/impl/CheckConfigurationsTest.java
@@ -0,0 +1,65 @@
+/*
+ * 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 org.apache.sling.feature.Configuration;
+import org.apache.sling.feature.analyser.task.AnalyserTask;
+import org.junit.Before;
+import org.junit.Test;
+import org.osgi.framework.Constants;
+
+public class CheckConfigurationsTest {
+    
+    private AnalyserTaskContextImpl ctx;
+    private AnalyserTask task;
+
+    @Before
+    public void setUp() {
+        this.ctx = new 
AnalyserTaskContextImpl("myGroupId:myArtifactId:jar:myClassifier:1.0.0");
+        this.task = new CheckConfigurations();
+    }
+
+    @Test
+    public void testServiceRankingOk() throws Exception {
+        Configuration cfg = new Configuration("myPid");
+        cfg.getProperties().put(Constants.SERVICE_RANKING, 1);
+        this.ctx.getFeature().getConfigurations().add(cfg);
+        this.task.execute(this.ctx);
+        assertTrue(this.ctx.getErrors().isEmpty());
+    }
+
+    @Test
+    public void testServiceRankingLong() throws Exception {
+        Configuration cfg = new Configuration("myPid");
+        cfg.getProperties().put(Constants.SERVICE_RANKING, 1L);
+        this.ctx.getFeature().getConfigurations().add(cfg);
+        this.task.execute(this.ctx);
+        assertEquals(1, this.ctx.getErrors().size());
+    }
+
+    @Test
+    public void testServiceRankingString() throws Exception {
+        Configuration cfg = new Configuration("myPid");
+        cfg.getProperties().put(Constants.SERVICE_RANKING, "100");
+        this.ctx.getFeature().getConfigurations().add(cfg);
+        this.task.execute(this.ctx);
+        assertEquals(1, this.ctx.getErrors().size());
+    }
+}

Reply via email to