This is an automated email from the ASF dual-hosted git repository.

davidb pushed a commit to branch master
in repository 
https://gitbox.apache.org/repos/asf/sling-slingfeature-maven-plugin.git


The following commit(s) were added to refs/heads/master by this push:
     new 6b04d27  SLING-7860 Enhance slingfeature-maven-plugin to aggregate 
multiple features into a single feature
6b04d27 is described below

commit 6b04d274ccebe1dffe28a175696239a9fb993970
Author: David Bosschaert <[email protected]>
AuthorDate: Tue Aug 28 16:48:49 2018 +0100

    SLING-7860 Enhance slingfeature-maven-plugin to aggregate multiple features 
into a single feature
    
    Some unit tests
---
 .../feature/maven/mojos/AggregateFeatures.java     |  22 +--
 .../feature/maven/mojos/AggregateFeaturesTest.java | 203 +++++++++++++++++++++
 src/test/resources/aggregate-features/test_x.json  |   9 +
 3 files changed, 223 insertions(+), 11 deletions(-)

diff --git 
a/src/main/java/org/apache/sling/feature/maven/mojos/AggregateFeatures.java 
b/src/main/java/org/apache/sling/feature/maven/mojos/AggregateFeatures.java
index 07ed674..59e86a8 100644
--- a/src/main/java/org/apache/sling/feature/maven/mojos/AggregateFeatures.java
+++ b/src/main/java/org/apache/sling/feature/maven/mojos/AggregateFeatures.java
@@ -60,23 +60,23 @@ import java.util.stream.Collectors;
     threadSafe = true
 )
 public class AggregateFeatures extends AbstractFeatureMojo {
-    @Parameter(required=false)
-    private String classifier;
+    @Parameter(required = true)
+    String classifier;
 
-    @Parameter
-    private List<FeatureConfig> features;
+    @Parameter(required = true)
+    List<FeatureConfig> features;
 
     @Parameter(property = "project.remoteArtifactRepositories", readonly = 
true, required = true)
-    protected List<ArtifactRepository> remoteRepositories;
+    List<ArtifactRepository> remoteRepositories;
 
     @Parameter(property = "localRepository", readonly = true, required = true)
-    protected ArtifactRepository localRepository;
+    ArtifactRepository localRepository;
 
     @Component
-    private RepositorySystem repoSystem;
+    RepositorySystem repoSystem;
 
     @Component
-    private ArtifactResolver artifactResolver;
+    ArtifactResolver artifactResolver;
 
     @Override
     public void execute() throws MojoExecutionException, MojoFailureException {
@@ -130,9 +130,9 @@ public class AggregateFeatures extends AbstractFeatureMojo {
                 fc.groupId, fc.artifactId, fc.version, fc.type, fc.classifier);
 
         ArtifactResolutionRequest resReq = new ArtifactResolutionRequest()
-                .setArtifact(art)
-                .setLocalRepository(localRepository)
-                .setRemoteRepositories(remoteRepositories);
+            .setArtifact(art)
+            .setLocalRepository(localRepository)
+            .setRemoteRepositories(remoteRepositories);
         artifactResolver.resolve(resReq);
 
         File artFile = art.getFile();
diff --git 
a/src/test/java/org/apache/sling/feature/maven/mojos/AggregateFeaturesTest.java 
b/src/test/java/org/apache/sling/feature/maven/mojos/AggregateFeaturesTest.java
new file mode 100644
index 0000000..c468b24
--- /dev/null
+++ 
b/src/test/java/org/apache/sling/feature/maven/mojos/AggregateFeaturesTest.java
@@ -0,0 +1,203 @@
+/*
+ * 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.maven.mojos;
+
+import org.apache.maven.artifact.Artifact;
+import org.apache.maven.artifact.repository.ArtifactRepository;
+import org.apache.maven.artifact.resolver.ArtifactResolutionRequest;
+import org.apache.maven.artifact.resolver.ArtifactResolutionResult;
+import org.apache.maven.artifact.resolver.ArtifactResolver;
+import org.apache.maven.model.Build;
+import org.apache.maven.project.MavenProject;
+import org.apache.maven.repository.RepositorySystem;
+import org.apache.sling.feature.ArtifactId;
+import org.apache.sling.feature.Feature;
+import org.apache.sling.feature.io.json.FeatureJSONReader;
+import org.apache.sling.feature.maven.FeatureConstants;
+import org.apache.sling.feature.maven.mojos.AggregateFeatures.FeatureConfig;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.mockito.Mockito;
+import org.mockito.invocation.InvocationOnMock;
+import org.mockito.stubbing.Answer;
+
+import java.io.File;
+import java.io.FileReader;
+import java.io.Reader;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.HashSet;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertSame;
+
+public class AggregateFeaturesTest {
+    private Path tempDir;
+
+    @Before
+    public void setup() throws Exception {
+        tempDir = Files.createTempDirectory(getClass().getSimpleName());
+    }
+
+    @After
+    public void tearDown() throws Exception {
+        // Delete the temp dir again
+        Files.walk(tempDir)
+            .sorted(Comparator.reverseOrder())
+            .map(Path::toFile)
+            .forEach(File::delete);
+    }
+
+    @Test
+    public void testFeatureConfig() {
+        FeatureConfig fc = new FeatureConfig();
+
+        assertEquals(0, fc.includes.size());
+        assertEquals(0, fc.excludes.size());
+        assertNull(fc.location);
+        assertNull(fc.groupId);
+        assertNull(fc.artifactId);
+        assertNull(fc.version);
+        assertNull(fc.type);
+        assertNull(fc.classifier);
+
+        fc.setLocation("loc1");
+        fc.setIncludes("i1");
+        fc.setIncludes("i2");
+        fc.setExcludes("e1");
+        fc.setGroupId("gid1");
+        fc.setArtifactId("aid1");
+        fc.setVersion("1.2.3");
+        fc.setType("slingfeature");
+        fc.setClassifier("clf1");
+
+        assertEquals(new HashSet<>(Arrays.asList("i1", "i2")), fc.includes);
+        assertEquals(Collections.singleton("e1"), fc.excludes);
+
+        assertEquals("loc1", fc.location);
+        assertEquals("gid1", fc.groupId);
+        assertEquals("aid1", fc.artifactId);
+        assertEquals("1.2.3", fc.version);
+        assertEquals("slingfeature", fc.type);
+        assertEquals("clf1", fc.classifier);
+    }
+
+    @Test
+    public void testReadFeatureFromArtifact() throws Exception {
+        File featureFile = new File(
+                
getClass().getResource("/aggregate-features/test_x.json").getFile());
+
+        FeatureConfig fc = new FeatureConfig();
+        fc.setGroupId("g1");
+        fc.setArtifactId("a1");
+        fc.setVersion("9.9.9");
+        fc.setType("slingfeature");
+        fc.setClassifier("c1");
+
+        RepositorySystem mockRepo = createMockRepo();
+
+        Build mockBuild = Mockito.mock(Build.class);
+        Mockito.when(mockBuild.getDirectory()).thenReturn(tempDir.toString());
+
+        MavenProject mockProj = Mockito.mock(MavenProject.class);
+        Mockito.when(mockProj.getBuild()).thenReturn(mockBuild);
+        Mockito.when(mockProj.getGroupId()).thenReturn("mygroup");
+        Mockito.when(mockProj.getArtifactId()).thenReturn("myart");
+        Mockito.when(mockProj.getVersion()).thenReturn("42");
+
+        AggregateFeatures af = new AggregateFeatures();
+        af.classifier = "mynewfeature";
+        af.features = Collections.singletonList(fc);
+        af.repoSystem = mockRepo;
+        af.localRepository = Mockito.mock(ArtifactRepository.class);
+        af.remoteRepositories = Collections.emptyList();
+        af.project = mockProj;
+
+        af.artifactResolver = Mockito.mock(ArtifactResolver.class);
+        
Mockito.when(af.artifactResolver.resolve(Mockito.isA(ArtifactResolutionRequest.class)))
+            .then(new Answer<ArtifactResolutionResult>() {
+                @Override
+                public ArtifactResolutionResult answer(InvocationOnMock 
invocation) throws Throwable {
+                    ArtifactResolutionRequest arr = 
(ArtifactResolutionRequest) invocation.getArguments()[0];
+                    Artifact a = arr.getArtifact();
+                    assertEquals("g1", a.getGroupId());
+                    assertEquals("a1", a.getArtifactId());
+                    assertEquals("9.9.9", a.getVersion());
+                    assertEquals("slingfeature", a.getType());
+                    assertEquals("c1", a.getClassifier());
+
+                    assertSame(af.localRepository, arr.getLocalRepository());
+                    assertSame(af.remoteRepositories, 
arr.getRemoteRepositories());
+
+                    // Configure Artifact.getFile()
+                    Mockito.when(a.getFile()).thenReturn(featureFile);
+
+                    return null;
+                }
+            });
+
+        af.execute();
+
+        File expectedFile = new File(tempDir.toFile(), 
FeatureConstants.FEATURE_PROCESSED_LOCATION + "/mynewfeature.json");
+        try (Reader fr = new FileReader(expectedFile)) {
+            Feature genFeat = FeatureJSONReader.read(fr, null, 
FeatureJSONReader.SubstituteVariables.NONE);
+            ArtifactId id = genFeat.getId();
+            assertEquals("mygroup", id.getGroupId());
+            assertEquals("myart", id.getArtifactId());
+            assertEquals("42", id.getVersion());
+            assertEquals("slingfeature", id.getType());
+            assertEquals("mynewfeature", id.getClassifier());
+
+            int numFound = 0;
+            for (org.apache.sling.feature.Artifact art :
+                (Iterable<org.apache.sling.feature.Artifact>) () -> 
genFeat.getBundles().iterator()) {
+                numFound++;
+
+                ArtifactId expectedBundleCoords =
+                        new ArtifactId("mygroup", "org.apache.aries.util", 
"1.1.3", null, null);
+                assertEquals(expectedBundleCoords, art.getId());
+            }
+            assertEquals("Expectec only one bundle", 1, numFound);
+        }
+    }
+
+    private RepositorySystem createMockRepo() {
+        RepositorySystem repo = Mockito.mock(RepositorySystem.class);
+        Mockito.when(repo.createArtifactWithClassifier(
+                Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), 
Mockito.anyString(), Mockito.anyString()))
+            .then(new Answer<Artifact>() {
+                @Override
+                public Artifact answer(InvocationOnMock inv) throws Throwable {
+                    Artifact art = Mockito.mock(Artifact.class);
+
+                    Mockito.when(art.getGroupId()).thenReturn((String) 
inv.getArguments()[0]);
+                    Mockito.when(art.getArtifactId()).thenReturn((String) 
inv.getArguments()[1]);
+                    Mockito.when(art.getVersion()).thenReturn((String) 
inv.getArguments()[2]);
+                    Mockito.when(art.getType()).thenReturn((String) 
inv.getArguments()[3]);
+                    Mockito.when(art.getClassifier()).thenReturn((String) 
inv.getArguments()[4]);
+
+                    return art;
+                }
+            });
+        return repo;
+    }
+}
diff --git a/src/test/resources/aggregate-features/test_x.json 
b/src/test/resources/aggregate-features/test_x.json
new file mode 100644
index 0000000..62ca4ce
--- /dev/null
+++ b/src/test/resources/aggregate-features/test_x.json
@@ -0,0 +1,9 @@
+{
+  "id":"g1:a1:slingfeature:c1:9.9.9",
+  "bundles":[
+    {
+      "id":"${project.groupId}:org.apache.aries.util:1.1.3",
+      "start-level":"20"
+    }
+  ]
+}

Reply via email to