You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by ro...@apache.org on 2024/06/21 20:21:19 UTC
(sling-slingfeature-maven-plugin) branch master updated (815703d -> 778e0cf)
This is an automated email from the ASF dual-hosted git repository.
rombert pushed a change to branch master
in repository https://gitbox.apache.org/repos/asf/sling-slingfeature-maven-plugin.git
from 815703d SLING-12354 - Set sling.java.version to 11 (#87)
new d8185aa SLING-12353 - The AnalyserMetadataHandler is executed too often
new 778e0cf SLING-12353 - The AnalyserMetadataHandler is executed too often
The 2 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails. The revisions
listed as "add" were already present in the repository and have only
been added to this reference.
Summary of changes:
.../feature/maven/mojos/AggregateFeaturesMojo.java | 36 +-
.../maven/mojos/AggregateFeaturesMojoTest.java | 533 +++++++--------------
2 files changed, 206 insertions(+), 363 deletions(-)
(sling-slingfeature-maven-plugin) 01/02: SLING-12353 - The AnalyserMetadataHandler is executed too often
Posted by ro...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
rombert pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/sling-slingfeature-maven-plugin.git
commit d8185aa46ca22ae241ce2c7f55f8ca91659613bc
Author: Robert Munteanu <ro...@apache.org>
AuthorDate: Fri Jun 21 20:40:16 2024 +0200
SLING-12353 - The AnalyserMetadataHandler is executed too often
Reduce code duplication in AggregateFeaturesMojoTest
---
.../maven/mojos/AggregateFeaturesMojoTest.java | 505 ++++++---------------
1 file changed, 146 insertions(+), 359 deletions(-)
diff --git a/src/test/java/org/apache/sling/feature/maven/mojos/AggregateFeaturesMojoTest.java b/src/test/java/org/apache/sling/feature/maven/mojos/AggregateFeaturesMojoTest.java
index 3f916f1..4448acb 100644
--- a/src/test/java/org/apache/sling/feature/maven/mojos/AggregateFeaturesMojoTest.java
+++ b/src/test/java/org/apache/sling/feature/maven/mojos/AggregateFeaturesMojoTest.java
@@ -24,6 +24,7 @@ import static org.junit.Assert.fail;
import java.io.File;
import java.io.FileReader;
+import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.nio.file.Path;
@@ -39,6 +40,7 @@ import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
+import java.util.function.BiConsumer;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.DefaultArtifact;
@@ -88,43 +90,12 @@ public class AggregateFeaturesMojoTest {
@Test
public void testAggregateFeaturesFromDirectory() throws Exception {
- File featuresDir = new File(
- getClass().getResource("/aggregate-features/dir2").getFile());
- // read features
- Map<String, Feature> featureMap = new HashMap<>();
- for (File f : featuresDir.listFiles((d,f) -> f.endsWith(".json"))) {
- Feature feat = FeatureJSONReader.read(new FileReader(f), null);
- featureMap.put(f.getAbsolutePath(), feat);
- }
-
- Aggregate fc = new Aggregate();
- fc.setFilesInclude("*.json");
-
- Build mockBuild = Mockito.mock(Build.class);
- Mockito.when(mockBuild.getDirectory()).thenReturn(tempDir.toString());
-
- Artifact parentArtifact = createMockArtifact();
- MavenProject mockProj = Mockito.mock(MavenProject.class);
- Mockito.when(mockProj.getBuild()).thenReturn(mockBuild);
- Mockito.when(mockProj.getGroupId()).thenReturn("org.foo");
- Mockito.when(mockProj.getArtifactId()).thenReturn("org.foo.bar");
- Mockito.when(mockProj.getVersion()).thenReturn("1.2.3-SNAPSHOT");
- Mockito.when(mockProj.getArtifact()).thenReturn(parentArtifact);
- Mockito.when(mockProj.getContextValue(Feature.class.getName() + "/rawmain.json-cache"))
- .thenReturn(featureMap);
- Mockito.when(mockProj.getContextValue(Feature.class.getName() + "/assembledmain.json-cache"))
- .thenReturn(featureMap);
- Mockito.when(mockProj.getContextValue(Preprocessor.class.getName())).thenReturn(Boolean.TRUE);
- AggregateFeaturesMojo af = new AggregateFeaturesMojo();
- fc.classifier = "aggregated";
- af.aggregates = Collections.singletonList(fc);
- af.project = mockProj;
- af.projectHelper = new DefaultMavenProjectHelper();
- af.features = featuresDir;
- af.execute();
+ TestContext ctx = prepareTestContext("/aggregate-features/dir2", new String[] { "*.json" } );
+
+ ctx.getMojo().execute();
- Feature genFeat = featureMap.get(":aggregate:aggregated:T");
+ Feature genFeat = ctx.getFeatureMap().get(":aggregate:aggregated:T");
assertNotNull(genFeat);
ArtifactId id = genFeat.getId();
@@ -158,50 +129,20 @@ public class AggregateFeaturesMojoTest {
}
assertConfigsEquivalent(expectedConfigs, actualConfigs);
}
-
+
@Test
public void testAggregateFeaturesFromDirectoryWithIncludesExcludes() throws Exception {
- File featuresDir = new File(
- getClass().getResource("/aggregate-features/dir").getFile());
- // read features
- Map<String, Feature> featureMap = new HashMap<>();
- for (File f : featuresDir.listFiles((d,f) -> f.endsWith(".json"))) {
- Feature feat = FeatureJSONReader.read(new FileReader(f), null);
- featureMap.put(f.getAbsolutePath(), feat);
- }
-
- Aggregate fc = new Aggregate();
- fc.setFilesInclude("*.json");
- fc.setFilesInclude("*.foobar");
- fc.setFilesExclude("*_v*");
- fc.setFilesExclude("test_w.json");
-
- Build mockBuild = Mockito.mock(Build.class);
- Mockito.when(mockBuild.getDirectory()).thenReturn(tempDir.toString());
-
- Artifact parentArtifact = createMockArtifact();
- MavenProject mockProj = Mockito.mock(MavenProject.class);
- Mockito.when(mockProj.getBuild()).thenReturn(mockBuild);
- Mockito.when(mockProj.getGroupId()).thenReturn("org.foo");
- Mockito.when(mockProj.getArtifactId()).thenReturn("org.foo.bar");
- Mockito.when(mockProj.getVersion()).thenReturn("1.2.3-SNAPSHOT");
- Mockito.when(mockProj.getArtifact()).thenReturn(parentArtifact);
- Mockito.when(mockProj.getContextValue(Feature.class.getName() + "/rawmain.json-cache"))
- .thenReturn(featureMap);
- Mockito.when(mockProj.getContextValue(Feature.class.getName() + "/assembledmain.json-cache"))
- .thenReturn(featureMap);
- Mockito.when(mockProj.getContextValue(Preprocessor.class.getName())).thenReturn(Boolean.TRUE);
-
- AggregateFeaturesMojo af = new AggregateFeaturesMojo();
- fc.classifier = "aggregated";
- af.aggregates = Collections.singletonList(fc);
- af.project = mockProj;
- af.projectHelper = new DefaultMavenProjectHelper();
- af.features = featuresDir;
-
- af.execute();
-
- Feature genFeat = featureMap.get(":aggregate:aggregated:T");
+
+ TestContext ctx = prepareTestContext("/aggregate-features/dir", new String[] { "*.json", "*.foobar" },
+ (agg, mojo) -> {
+ agg.setFilesExclude("*_v*");
+ agg.setFilesExclude("test_w.json");
+ }
+ );
+
+ ctx.getMojo().execute();
+
+ Feature genFeat = ctx.getFeatureMap().get(":aggregate:aggregated:T");
assertNotNull(genFeat);
ArtifactId id = genFeat.getId();
@@ -237,43 +178,11 @@ public class AggregateFeaturesMojoTest {
@Test
public void testNonMatchingDirectoryIncludes() throws Exception {
- File featuresDir = new File(
- getClass().getResource("/aggregate-features/dir").getFile());
- // read features
- Map<String, Feature> featureMap = new HashMap<>();
- for (File f : featuresDir.listFiles((d,f) -> f.endsWith(".json"))) {
- Feature feat = FeatureJSONReader.read(new FileReader(f), null);
- featureMap.put(f.getAbsolutePath(), feat);
- }
-
- Aggregate fc = new Aggregate();
- fc.setFilesInclude("doesnotexist.json");
-
- Build mockBuild = Mockito.mock(Build.class);
- Mockito.when(mockBuild.getDirectory()).thenReturn(tempDir.toString());
-
- Artifact parentArtifact = createMockArtifact();
- MavenProject mockProj = Mockito.mock(MavenProject.class);
- Mockito.when(mockProj.getBuild()).thenReturn(mockBuild);
- Mockito.when(mockProj.getGroupId()).thenReturn("org.foo");
- Mockito.when(mockProj.getArtifactId()).thenReturn("org.foo.bar");
- Mockito.when(mockProj.getVersion()).thenReturn("1.2.3-SNAPSHOT");
- Mockito.when(mockProj.getArtifact()).thenReturn(parentArtifact);
- Mockito.when(mockProj.getContextValue(Feature.class.getName() + "/rawmain.json-cache"))
- .thenReturn(featureMap);
- Mockito.when(mockProj.getContextValue(Feature.class.getName() + "/assembledmain.json-cache"))
- .thenReturn(featureMap);
- Mockito.when(mockProj.getContextValue(Preprocessor.class.getName())).thenReturn(Boolean.TRUE);
-
- AggregateFeaturesMojo af = new AggregateFeaturesMojo();
- fc.classifier = "aggregated";
- af.aggregates = Collections.singletonList(fc);
- af.project = mockProj;
- af.projectHelper = new DefaultMavenProjectHelper();
- af.features = featuresDir;
-
+
+ TestContext ctx = prepareTestContext("/aggregate-features/dir", new String[] { "doesnotexist.json" });
+
try {
- af.execute();
+ ctx.getMojo().execute();
fail("Should have thrown an exception because doesnotexist.json is not a file");
} catch (MojoExecutionException mee) {
assertTrue(mee.getMessage().contains("Include doesnotexist.json not found"));
@@ -282,43 +191,11 @@ public class AggregateFeaturesMojoTest {
@Test
public void testNonMatchingDirectoryExcludes() throws Exception {
- File featuresDir = new File(
- getClass().getResource("/aggregate-features/dir").getFile());
- // read features
- Map<String, Feature> featureMap = new HashMap<>();
- for (File f : featuresDir.listFiles((d,f) -> f.endsWith(".json"))) {
- Feature feat = FeatureJSONReader.read(new FileReader(f), null);
- featureMap.put(f.getAbsolutePath(), feat);
- }
-
- Aggregate fc = new Aggregate();
- fc.setFilesInclude("doesnotexist.json");
-
- Build mockBuild = Mockito.mock(Build.class);
- Mockito.when(mockBuild.getDirectory()).thenReturn(tempDir.toString());
-
- Artifact parentArtifact = createMockArtifact();
- MavenProject mockProj = Mockito.mock(MavenProject.class);
- Mockito.when(mockProj.getBuild()).thenReturn(mockBuild);
- Mockito.when(mockProj.getGroupId()).thenReturn("org.foo");
- Mockito.when(mockProj.getArtifactId()).thenReturn("org.foo.bar");
- Mockito.when(mockProj.getVersion()).thenReturn("1.2.3-SNAPSHOT");
- Mockito.when(mockProj.getArtifact()).thenReturn(parentArtifact);
- Mockito.when(mockProj.getContextValue(Feature.class.getName() + "/rawmain.json-cache"))
- .thenReturn(featureMap);
- Mockito.when(mockProj.getContextValue(Feature.class.getName() + "/assembledmain.json-cache"))
- .thenReturn(featureMap);
- Mockito.when(mockProj.getContextValue(Preprocessor.class.getName())).thenReturn(Boolean.TRUE);
-
- AggregateFeaturesMojo af = new AggregateFeaturesMojo();
- fc.classifier = "aggregated";
- af.aggregates = Collections.singletonList(fc);
- af.project = mockProj;
- af.projectHelper = new DefaultMavenProjectHelper();
- af.features = featuresDir;
-
+
+ TestContext ctx = prepareTestContext("/aggregate-features/dir", new String[] { "doesnotexist.json" });
+
try {
- af.execute();
+ ctx.getMojo().execute();
fail("Should have thrown an exception because doesnotexist.json is not a file");
} catch (MojoExecutionException mee) {
assertTrue(mee.getMessage().contains("FeatureInclude doesnotexist.json not found"));
@@ -327,58 +204,23 @@ public class AggregateFeaturesMojoTest {
@Test
public void testIncludeOrdering() throws Exception {
- File featuresDir = new File(
- getClass().getResource("/aggregate-features/dir4").getFile());
- // read features
- Map<String, Feature> featureMap = new HashMap<>();
- for (File f : featuresDir.listFiles((d,f) -> f.endsWith(".json"))) {
- Feature feat = FeatureJSONReader.read(new FileReader(f), null);
- featureMap.put(f.getAbsolutePath(), feat);
- }
-
- Aggregate fc1 = new Aggregate();
- fc1.setFilesInclude("test_x.json");
-
- fc1.setFilesInclude("test_u.json");
- fc1.setFilesInclude("test_y.json");
- fc1.setFilesInclude("test_v.json");
- fc1.setFilesInclude("test_z.json");
-
- fc1.setFilesInclude("test_t.json");
-
- Build mockBuild = Mockito.mock(Build.class);
- Mockito.when(mockBuild.getDirectory()).thenReturn(tempDir.toString());
-
- Artifact parentArtifact = createMockArtifact();
- MavenProject mockProj = Mockito.mock(MavenProject.class);
- Mockito.when(mockProj.getBuild()).thenReturn(mockBuild);
- Mockito.when(mockProj.getGroupId()).thenReturn("g");
- Mockito.when(mockProj.getArtifactId()).thenReturn("a");
- Mockito.when(mockProj.getVersion()).thenReturn("999");
- Mockito.when(mockProj.getArtifact()).thenReturn(parentArtifact);
- Mockito.when(mockProj.getContextValue(Feature.class.getName() + "/rawmain.json-cache"))
- .thenReturn(featureMap);
- Mockito.when(mockProj.getContextValue(Feature.class.getName() + "/assembledmain.json-cache"))
- .thenReturn(featureMap);
- Mockito.when(mockProj.getContextValue(Preprocessor.class.getName())).thenReturn(Boolean.TRUE);
-
-
- AggregateFeaturesMojo af = new AggregateFeaturesMojo();
- fc1.classifier = "agg";
- fc1.configurationOverrides = Arrays.asList("*=" + BuilderContext.CONFIG_MERGE_LATEST);
- af.aggregates = Arrays.asList(fc1);
- af.project = mockProj;
- af.projectHelper = new DefaultMavenProjectHelper();
- af.features = featuresDir;
-
- af.execute();
-
- Feature genFeat = featureMap.get(":aggregate:agg:T");
+
+ TestContext ctx = prepareTestContext("/aggregate-features/dir4",
+ new String[] { "test_x.json", "test_u.json", "test_y.json", "test_v.json", "test_z.json", "test_t.json" },
+ (agg, mojo) -> {
+ agg.configurationOverrides = Arrays.asList("*=" + BuilderContext.CONFIG_MERGE_LATEST);
+ agg.classifier = "agg";
+ });
+
+ ctx.getMojo().execute();
+
+ Feature genFeat = ctx.getFeatureMap().get(":aggregate:agg:T");
+ assertNotNull(genFeat);
ArtifactId id = genFeat.getId();
- assertEquals("g", id.getGroupId());
- assertEquals("a", id.getArtifactId());
- assertEquals("999", id.getVersion());
+ assertEquals("org.foo", id.getGroupId());
+ assertEquals("org.foo.bar", id.getArtifactId());
+ assertEquals("1.2.3-SNAPSHOT", id.getVersion());
assertEquals(FeatureConstants.PACKAGING_FEATURE, id.getType());
assertEquals("agg", id.getClassifier());
@@ -484,49 +326,19 @@ public class AggregateFeaturesMojoTest {
@Test
public void testPluginHandling() throws Exception {
- File featuresDir = new File(
- getClass().getResource("/aggregate-features/dir3").getFile());
- // read features
- Map<String, Feature> featureMap = new HashMap<>();
- for (File f : featuresDir.listFiles((d,f) -> f.endsWith(".json"))) {
- Feature feat = FeatureJSONReader.read(new FileReader(f), null);
- featureMap.put(f.getAbsolutePath(), feat);
- }
-
- Aggregate fc = new Aggregate();
- fc.setFilesInclude("*.json");
-
- Build mockBuild = Mockito.mock(Build.class);
- Mockito.when(mockBuild.getDirectory()).thenReturn(tempDir.toString());
-
- Artifact parentArtifact = createMockArtifact();
- MavenProject mockProj = Mockito.mock(MavenProject.class);
- Mockito.when(mockProj.getBuild()).thenReturn(mockBuild);
- Mockito.when(mockProj.getGroupId()).thenReturn("org.foo");
- Mockito.when(mockProj.getArtifactId()).thenReturn("org.foo.bar");
- Mockito.when(mockProj.getVersion()).thenReturn("1.2.3-SNAPSHOT");
- Mockito.when(mockProj.getArtifact()).thenReturn(parentArtifact);
- Mockito.when(mockProj.getContextValue(Feature.class.getName() + "/rawmain.json-cache"))
- .thenReturn(featureMap);
- Mockito.when(mockProj.getContextValue(Feature.class.getName() + "/assembledmain.json-cache"))
- .thenReturn(featureMap);
- Mockito.when(mockProj.getContextValue(Preprocessor.class.getName())).thenReturn(Boolean.TRUE);
-
- AggregateFeaturesMojo af = new AggregateFeaturesMojo();
- fc.classifier = "aggregated";
- af.aggregates = Collections.singletonList(fc);
- af.project = mockProj;
- af.projectHelper = new DefaultMavenProjectHelper();
- af.features = featuresDir;
- af.handlerConfiguration = new HashMap<>();
-
- Properties p3props = new Properties();
- p3props.put("test3cfg", "myval");
- p3props.put("test3cfg3", "somethingelse");
- af.handlerConfiguration.put("TestPlugin3", p3props);
-
+
+ TestContext ctx = prepareTestContext("/aggregate-features/dir3",
+ new String[] { "*.json" },
+ (agg, mojo) -> {
+ Properties p3props = new Properties();
+ p3props.put("test3cfg", "myval");
+ p3props.put("test3cfg3", "somethingelse");
+
+ mojo.handlerConfiguration = Map.of("TestPlugin3", p3props);
+ });
+
assertEquals("Precondition", 0, pluginCallbacks.size());
- af.execute();
+ ctx.getMojo().execute();
ArtifactId id = new ArtifactId("org.foo", "org.foo.bar", "1.2.3-SNAPSHOT", "aggregated", FeatureConstants.PACKAGING_FEATURE);
assertEquals(id, pluginCallbacks.get("TestPlugin1 - extension1"));
@@ -723,46 +535,18 @@ public class AggregateFeaturesMojoTest {
@Test
public void testOverrides() throws Exception {
- File featuresDir = new File(
- getClass().getResource("/aggregate-features/dir5").getFile());
-
- Map<String, Feature> featureMap = new HashMap<>();
- for (File f : featuresDir.listFiles((d,f) -> f.endsWith(".json"))) {
- Feature feat = FeatureJSONReader.read(new FileReader(f), null);
- featureMap.put(f.getAbsolutePath(), feat);
- }
-
- Aggregate ag = new Aggregate();
- ag.setFilesInclude("*.json");
- ag.classifier = "myagg";
- ag.artifactsOverrides = Arrays.asList("org.apache.sling:mybundle:HIGHEST",
- "org.apache.sling:somebundle:1.1.0", "org.apache.sling:somebundle:2.0.0");
-
- Build mockBuild = Mockito.mock(Build.class);
- Mockito.when(mockBuild.getDirectory()).thenReturn(tempDir.toString());
-
- Artifact parentArt = createMockArtifact();
- MavenProject mockProj = Mockito.mock(MavenProject.class);
- Mockito.when(mockProj.getBuild()).thenReturn(mockBuild);
- Mockito.when(mockProj.getGroupId()).thenReturn("org.apache.sling");
- Mockito.when(mockProj.getArtifactId()).thenReturn("org.apache.sling.test");
- Mockito.when(mockProj.getVersion()).thenReturn("1.0.1");
- Mockito.when(mockProj.getArtifact()).thenReturn(parentArt);
- Mockito.when(mockProj.getContextValue(Feature.class.getName() + "/rawmain.json-cache"))
- .thenReturn(featureMap);
- Mockito.when(mockProj.getContextValue(Feature.class.getName() + "/assembledmain.json-cache"))
- .thenReturn(featureMap);
- Mockito.when(mockProj.getContextValue(Preprocessor.class.getName())).thenReturn(Boolean.TRUE);
-
- AggregateFeaturesMojo af = new AggregateFeaturesMojo();
- af.aggregates = Collections.singletonList(ag);
- af.project = mockProj;
- af.projectHelper = new DefaultMavenProjectHelper();
- af.features = featuresDir;
- af.handlerConfiguration = new HashMap<>();
-
- af.execute();
- Feature genFeat = featureMap.get(":aggregate:myagg:T");
+
+ TestContext ctx = prepareTestContext("/aggregate-features/dir5",
+ new String[] { "*.json" },
+ (agg, mojo) -> {
+ agg.classifier = "myagg";
+ agg.artifactsOverrides = Arrays.asList("org.apache.sling:mybundle:HIGHEST",
+ "org.apache.sling:somebundle:1.1.0", "org.apache.sling:somebundle:2.0.0");
+ });
+
+ ctx.getMojo().execute();
+
+ Feature genFeat = ctx.getFeatureMap().get(":aggregate:myagg:T");
Bundles bundles = genFeat.getBundles();
assertEquals(3, bundles.size());
assertTrue(bundles.contains(new org.apache.sling.feature.Artifact(
@@ -775,44 +559,14 @@ public class AggregateFeaturesMojoTest {
@Test
public void testOverrideWithManualArtifactIDNoOverride() throws Exception {
- File featuresDir = new File(
- getClass().getResource("/aggregate-features/dir6").getFile());
-
- Map<String, Feature> featureMap = new HashMap<>();
- for (File f : featuresDir.listFiles((d,f) -> f.endsWith(".json"))) {
- Feature feat = FeatureJSONReader.read(new FileReader(f), null);
- featureMap.put(f.getAbsolutePath(), feat);
- }
-
- Aggregate ag = new Aggregate();
- ag.setFilesInclude("*.json");
- ag.classifier = "myagg";
-
- Build mockBuild = Mockito.mock(Build.class);
- Mockito.when(mockBuild.getDirectory()).thenReturn(tempDir.toString());
-
- Artifact parentArt = createMockArtifact();
- MavenProject mockProj = Mockito.mock(MavenProject.class);
- Mockito.when(mockProj.getBuild()).thenReturn(mockBuild);
- Mockito.when(mockProj.getGroupId()).thenReturn("org.apache.sling");
- Mockito.when(mockProj.getArtifactId()).thenReturn("org.apache.sling.test");
- Mockito.when(mockProj.getVersion()).thenReturn("1.0.1");
- Mockito.when(mockProj.getArtifact()).thenReturn(parentArt);
- Mockito.when(mockProj.getContextValue(Feature.class.getName() + "/rawmain.json-cache"))
- .thenReturn(featureMap);
- Mockito.when(mockProj.getContextValue(Feature.class.getName() + "/assembledmain.json-cache"))
- .thenReturn(featureMap);
- Mockito.when(mockProj.getContextValue(Preprocessor.class.getName())).thenReturn(Boolean.TRUE);
-
- AggregateFeaturesMojo af = new AggregateFeaturesMojo();
- af.aggregates = Collections.singletonList(ag);
- af.project = mockProj;
- af.projectHelper = new DefaultMavenProjectHelper();
- af.features = featuresDir;
- af.handlerConfiguration = new HashMap<>();
-
+ TestContext ctx = prepareTestContext("/aggregate-features/dir6",
+ new String[] { "*.json" },
+ (agg, mojo) -> {
+ agg.classifier = "myagg";
+ });
+
try {
- af.execute();
+ ctx.getMojo().execute();
fail("Should have thrown an exception as "
+ "org.apache.sling:somebundle has as alias org.apache.sling:myotherbundle");
} catch (Exception e) {
@@ -825,47 +579,16 @@ public class AggregateFeaturesMojoTest {
@Test
public void testOverrideWithManualArtifactID() throws Exception {
- File featuresDir = new File(
- getClass().getResource("/aggregate-features/dir6").getFile());
-
- Map<String, Feature> featureMap = new HashMap<>();
- for (File f : featuresDir.listFiles((d,f) -> f.endsWith(".json"))) {
- Feature feat = FeatureJSONReader.read(new FileReader(f), null);
- featureMap.put(f.getAbsolutePath(), feat);
- }
-
- Aggregate ag = new Aggregate();
- ag.setFilesInclude("test_c.json");
- ag.setFilesInclude("test_d.json");
- ag.classifier = "myagg";
- ag.artifactsOverrides = Arrays.asList("org.apache.sling:myotherbundle:LATEST");
-
- Build mockBuild = Mockito.mock(Build.class);
- Mockito.when(mockBuild.getDirectory()).thenReturn(tempDir.toString());
-
- Artifact parentArt = createMockArtifact();
- MavenProject mockProj = Mockito.mock(MavenProject.class);
- Mockito.when(mockProj.getBuild()).thenReturn(mockBuild);
- Mockito.when(mockProj.getGroupId()).thenReturn("org.apache.sling");
- Mockito.when(mockProj.getArtifactId()).thenReturn("org.apache.sling.test");
- Mockito.when(mockProj.getVersion()).thenReturn("1.0.1");
- Mockito.when(mockProj.getArtifact()).thenReturn(parentArt);
- Mockito.when(mockProj.getContextValue(Feature.class.getName() + "/rawmain.json-cache"))
- .thenReturn(featureMap);
- Mockito.when(mockProj.getContextValue(Feature.class.getName() + "/assembledmain.json-cache"))
- .thenReturn(featureMap);
- Mockito.when(mockProj.getContextValue(Preprocessor.class.getName())).thenReturn(Boolean.TRUE);
-
- AggregateFeaturesMojo af = new AggregateFeaturesMojo();
- af.aggregates = Collections.singletonList(ag);
- af.project = mockProj;
- af.projectHelper = new DefaultMavenProjectHelper();
- af.features = featuresDir;
- af.handlerConfiguration = new HashMap<>();
-
- af.execute();
-
- Feature genFeat = featureMap.get(":aggregate:myagg:T");
+ TestContext ctx = prepareTestContext("/aggregate-features/dir6",
+ new String[] { "test_c.json", "test_d.json" },
+ (agg, mojo) -> {
+ agg.classifier = "myagg";
+ agg.artifactsOverrides = Arrays.asList("org.apache.sling:myotherbundle:LATEST");
+ });
+
+ ctx.getMojo().execute();
+
+ Feature genFeat = ctx.getFeatureMap().get(":aggregate:myagg:T");
Bundles bundles = genFeat.getBundles();
assertEquals(1, bundles.size());
assertTrue(bundles.contains(new org.apache.sling.feature.Artifact(
@@ -1087,4 +810,68 @@ public class AggregateFeaturesMojoTest {
Mockito.when(parentArtifact.getType()).thenReturn("foo");
return parentArtifact;
}
+
+ private TestContext prepareTestContext(String featuresDirS, String[] filesInclude) throws IOException {
+ return prepareTestContext(featuresDirS, filesInclude, (a,m) -> {});
+ }
+
+ private TestContext prepareTestContext(String featuresDirS, String[] filesInclude, BiConsumer<Aggregate, AggregateFeaturesMojo> customiser) throws IOException {
+ File featuresDir = new File(
+ getClass().getResource(featuresDirS).getFile());
+ // read features
+ Map<String, Feature> featureMap = new HashMap<>();
+ for (File f : featuresDir.listFiles((d,f) -> f.endsWith(".json"))) {
+ Feature feat = FeatureJSONReader.read(new FileReader(f), null);
+ featureMap.put(f.getAbsolutePath(), feat);
+ }
+
+ Aggregate fc = new Aggregate();
+ fc.classifier = "aggregated";
+ for (String include : filesInclude)
+ fc.setFilesInclude(include);
+
+ Build mockBuild = Mockito.mock(Build.class);
+ Mockito.when(mockBuild.getDirectory()).thenReturn(tempDir.toString());
+
+ Artifact parentArtifact = createMockArtifact();
+ MavenProject mockProj = Mockito.mock(MavenProject.class);
+ Mockito.when(mockProj.getBuild()).thenReturn(mockBuild);
+ Mockito.when(mockProj.getGroupId()).thenReturn("org.foo");
+ Mockito.when(mockProj.getArtifactId()).thenReturn("org.foo.bar");
+ Mockito.when(mockProj.getVersion()).thenReturn("1.2.3-SNAPSHOT");
+ Mockito.when(mockProj.getArtifact()).thenReturn(parentArtifact);
+ Mockito.when(mockProj.getContextValue(Feature.class.getName() + "/rawmain.json-cache"))
+ .thenReturn(featureMap);
+ Mockito.when(mockProj.getContextValue(Feature.class.getName() + "/assembledmain.json-cache"))
+ .thenReturn(featureMap);
+ Mockito.when(mockProj.getContextValue(Preprocessor.class.getName())).thenReturn(Boolean.TRUE);
+
+ AggregateFeaturesMojo af = new AggregateFeaturesMojo();
+ af.aggregates = Collections.singletonList(fc);
+ af.project = mockProj;
+ af.projectHelper = new DefaultMavenProjectHelper();
+ af.features = featuresDir;
+
+ customiser.accept(fc, af);
+
+ return new TestContext(af, featureMap);
+ }
+
+ static class TestContext {
+ private final AggregateFeaturesMojo mojo;
+ private final Map<String, Feature> featureMap;
+
+ public TestContext(AggregateFeaturesMojo mojo, Map<String, Feature> featureMap) {
+ this.mojo = mojo;
+ this.featureMap = featureMap;
+ }
+
+ public AggregateFeaturesMojo getMojo() {
+ return mojo;
+ }
+
+ public Map<String, Feature> getFeatureMap() {
+ return featureMap;
+ }
+ }
}
(sling-slingfeature-maven-plugin) 02/02: SLING-12353 - The AnalyserMetadataHandler is executed too often
Posted by ro...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
rombert pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/sling-slingfeature-maven-plugin.git
commit 778e0cff357439ea4ebdae0ffef9ac0d296e08b9
Author: Robert Munteanu <ro...@apache.org>
AuthorDate: Fri Jun 21 21:02:51 2024 +0200
SLING-12353 - The AnalyserMetadataHandler is executed too often
Add an 'additionalPostProcessHandlers' parameters to AggregateFeaturesMojo
---
.../feature/maven/mojos/AggregateFeaturesMojo.java | 36 +++++++++++++++++++---
.../maven/mojos/AggregateFeaturesMojoTest.java | 28 +++++++++++++++++
2 files changed, 60 insertions(+), 4 deletions(-)
diff --git a/src/main/java/org/apache/sling/feature/maven/mojos/AggregateFeaturesMojo.java b/src/main/java/org/apache/sling/feature/maven/mojos/AggregateFeaturesMojo.java
index 53be836..727051d 100644
--- a/src/main/java/org/apache/sling/feature/maven/mojos/AggregateFeaturesMojo.java
+++ b/src/main/java/org/apache/sling/feature/maven/mojos/AggregateFeaturesMojo.java
@@ -16,6 +16,7 @@
*/
package org.apache.sling.feature.maven.mojos;
+import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@@ -23,6 +24,7 @@ import java.util.Properties;
import java.util.ServiceLoader;
import java.util.Spliterator;
import java.util.Spliterators;
+import java.util.stream.Stream;
import java.util.stream.StreamSupport;
import org.apache.maven.plugin.MojoExecutionException;
@@ -60,7 +62,18 @@ public class AggregateFeaturesMojo extends AbstractIncludingFeatureMojo {
@Parameter
Map<String, Properties> handlerConfiguration = new HashMap<>();
-
+
+ /**
+ * Additional post process handlers to use when aggregating
+ *
+ * <p>Normally handlers are discovered using the {@link ServiceLoader} mechanism but some
+ * special-purpose handlers are not registered by default.</p>
+ *
+ * <p>These handlers will be installed after the ones discovered by the {@link ServiceLoader}.</p>
+ */
+ @Parameter
+ List<String> additionalPostProcessHandlers = new ArrayList<>();
+
@Override
public void execute() throws MojoExecutionException {
checkPreconditions();
@@ -121,9 +134,7 @@ public class AggregateFeaturesMojo extends AbstractIncludingFeatureMojo {
.addMergeExtensions(StreamSupport.stream(Spliterators.spliteratorUnknownSize(
ServiceLoader.load(MergeHandler.class).iterator(), Spliterator.ORDERED),
false).toArray(MergeHandler[]::new))
- .addPostProcessExtensions(StreamSupport.stream(Spliterators.spliteratorUnknownSize(
- ServiceLoader.load(PostProcessHandler.class).iterator(), Spliterator.ORDERED),
- false).toArray(PostProcessHandler[]::new));
+ .addPostProcessExtensions(postProcessHandlers().toArray(PostProcessHandler[]::new));
for (final ArtifactId rule : aggregate.getArtifactOverrideRules()) {
builderContext.addArtifactsOverride(rule);
}
@@ -169,6 +180,23 @@ public class AggregateFeaturesMojo extends AbstractIncludingFeatureMojo {
}
}
+ private Stream<PostProcessHandler> postProcessHandlers() {
+
+ Stream<PostProcessHandler> serviceLoaderHandlers = StreamSupport.stream(Spliterators.spliteratorUnknownSize(
+ ServiceLoader.load(PostProcessHandler.class).iterator(), Spliterator.ORDERED), false);
+
+ Stream<PostProcessHandler> additionalHandlers = this.additionalPostProcessHandlers.stream()
+ .map( extension -> {
+ try {
+ return (PostProcessHandler) Class.forName(extension).getDeclaredConstructor().newInstance();
+ } catch (Exception e) {
+ throw new RuntimeException("Unable to instantiate post-process extension " + extension, e);
+ }
+ });
+
+ return Stream.concat(serviceLoaderHandlers, additionalHandlers);
+ }
+
Feature assembleFeature(final ArtifactId newFeatureID, final BuilderContext builderContext,
final Map<String, Feature> selection) throws MojoExecutionException {
try {
diff --git a/src/test/java/org/apache/sling/feature/maven/mojos/AggregateFeaturesMojoTest.java b/src/test/java/org/apache/sling/feature/maven/mojos/AggregateFeaturesMojoTest.java
index 4448acb..1866fb6 100644
--- a/src/test/java/org/apache/sling/feature/maven/mojos/AggregateFeaturesMojoTest.java
+++ b/src/test/java/org/apache/sling/feature/maven/mojos/AggregateFeaturesMojoTest.java
@@ -40,6 +40,7 @@ import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
+import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.BiConsumer;
import org.apache.maven.artifact.Artifact;
@@ -54,8 +55,11 @@ import org.apache.maven.project.DefaultMavenProjectHelper;
import org.apache.maven.project.MavenProject;
import org.apache.sling.feature.ArtifactId;
import org.apache.sling.feature.Bundles;
+import org.apache.sling.feature.Extension;
import org.apache.sling.feature.Feature;
import org.apache.sling.feature.builder.BuilderContext;
+import org.apache.sling.feature.builder.HandlerContext;
+import org.apache.sling.feature.builder.PostProcessHandler;
import org.apache.sling.feature.io.json.FeatureJSONReader;
import org.apache.sling.feature.maven.FeatureConstants;
import org.apache.sling.feature.maven.Preprocessor;
@@ -801,6 +805,20 @@ public class AggregateFeaturesMojoTest {
assertEquals("More than one feature file for classifier myagg in project test.aggregate.project1 : [aggregate myagg, aggregate myagg]", e.getMessage());
}
}
+
+ @Test
+ public void customAggregate() throws Exception {
+
+ TestContext ctx = prepareTestContext("/aggregate-features/dir2",
+ new String[] { "test_w.json", "test_y.json" }, // control the number of aggregations
+ (agg, mojo) -> mojo.additionalPostProcessHandlers.add(SpyAggregateHandler.class.getTypeName()) );
+
+ ctx.getMojo().execute();
+
+ // we expect 2 invocations because we have 2 feature files as input
+ assertEquals("Additional postProcessHandler invocation count", 2, SpyAggregateHandler.invocationCount.get());
+
+ }
private Artifact createMockArtifact() {
Artifact parentArtifact = Mockito.mock(Artifact.class);
@@ -874,4 +892,14 @@ public class AggregateFeaturesMojoTest {
return featureMap;
}
}
+
+ public static class SpyAggregateHandler implements PostProcessHandler {
+
+ static final AtomicInteger invocationCount = new AtomicInteger(0);
+
+ @Override
+ public void postProcess(HandlerContext context, Feature feature, Extension extension) {
+ invocationCount.incrementAndGet();
+ }
+ }
}