You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by da...@apache.org on 2020/08/13 11:41:29 UTC
[sling-org-apache-sling-installer-factory-feature] branch
SLING-9658 updated: Add unit tests
This is an automated email from the ASF dual-hosted git repository.
davidb pushed a commit to branch SLING-9658
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-installer-factory-feature.git
The following commit(s) were added to refs/heads/SLING-9658 by this push:
new 30e46cb Add unit tests
30e46cb is described below
commit 30e46cb2825d500e45b891e387765f5b290eaaa3
Author: David Bosschaert <bo...@adobe.com>
AuthorDate: Thu Aug 13 12:41:16 2020 +0100
Add unit tests
---
bnd.bnd | 2 +-
pom.xml | 12 ++
.../model/impl/BinaryArtifactExtensionHandler.java | 2 +
.../model/impl/BinaryPackageInstallerPlugin.java | 55 +++++++++
.../model/impl/FeatureModelInstallerPlugin.java | 35 +++---
.../model/impl/InstallFeatureModelTask.java | 64 +++++-----
.../model/impl/InstallFeatureModelTaskTest.java | 131 +++++++++++++++++++++
.../model/impl/RepoinitExtensionHandlerTest.java | 68 +++++++++++
8 files changed, 317 insertions(+), 52 deletions(-)
diff --git a/bnd.bnd b/bnd.bnd
index d2c832f..3e6776b 100644
--- a/bnd.bnd
+++ b/bnd.bnd
@@ -1,2 +1,2 @@
-includeresource: @org.apache.sling.feature.extension.apiregions-[0-9.]*.jar!/org/apache/sling/feature/extension/apiregions/launcher/LauncherProperties.*
-Conditional-Package: org.apache.sling.feature.extension.apiregions.api
+Conditional-Package: org.apache.sling.feature.extension.apiregions.api,org.apache.felix.utils.manifest
diff --git a/pom.xml b/pom.xml
index 3e246df..87e14df 100644
--- a/pom.xml
+++ b/pom.xml
@@ -120,6 +120,18 @@
<version>3.3.0</version>
<scope>test</scope>
</dependency>
+ <dependency>
+ <groupId>org.apache.johnzon</groupId>
+ <artifactId>johnzon-core</artifactId>
+ <version>1.2.8</version>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.felix</groupId>
+ <artifactId>org.apache.felix.cm.json</artifactId>
+ <version>1.0.2</version>
+ <scope>test</scope>
+ </dependency>
</dependencies>
</project>
diff --git a/src/main/java/org/apache/sling/installer/factory/model/impl/BinaryArtifactExtensionHandler.java b/src/main/java/org/apache/sling/installer/factory/model/impl/BinaryArtifactExtensionHandler.java
index dc8162b..7ce99ae 100644
--- a/src/main/java/org/apache/sling/installer/factory/model/impl/BinaryArtifactExtensionHandler.java
+++ b/src/main/java/org/apache/sling/installer/factory/model/impl/BinaryArtifactExtensionHandler.java
@@ -16,6 +16,7 @@
* specific language governing permissions and limitations
* under the License.
*/
+
package org.apache.sling.installer.factory.model.impl;
import org.apache.felix.utils.manifest.Clause;
@@ -71,3 +72,4 @@ public class BinaryArtifactExtensionHandler implements ExtensionHandler {
}
}
}
+
diff --git a/src/main/java/org/apache/sling/installer/factory/model/impl/BinaryPackageInstallerPlugin.java b/src/main/java/org/apache/sling/installer/factory/model/impl/BinaryPackageInstallerPlugin.java
new file mode 100644
index 0000000..67033d4
--- /dev/null
+++ b/src/main/java/org/apache/sling/installer/factory/model/impl/BinaryPackageInstallerPlugin.java
@@ -0,0 +1,55 @@
+/*
+ * 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.installer.factory.model.impl;
+
+import org.apache.sling.installer.api.tasks.InstallTask;
+import org.apache.sling.installer.api.tasks.InstallTaskFactory;
+import org.apache.sling.installer.api.tasks.RegisteredResource;
+import org.apache.sling.installer.api.tasks.ResourceTransformer;
+import org.apache.sling.installer.api.tasks.TaskResourceGroup;
+import org.apache.sling.installer.api.tasks.TransformationResult;
+import org.osgi.service.component.annotations.Component;
+import org.osgi.service.metatype.annotations.Designate;
+import org.osgi.service.metatype.annotations.ObjectClassDefinition;
+
+@Component(service = { InstallTaskFactory.class, ResourceTransformer.class })
+@Designate(ocd = BinaryPackageInstallerPlugin.Config.class)
+public class BinaryPackageInstallerPlugin implements InstallTaskFactory, ResourceTransformer {
+
+ @ObjectClassDefinition(name = "Binary Package Installer",
+ description = "This component supports installing binary packages into the OSGi installer")
+ public @interface Config {
+ String directory();
+
+ String overwrite() default "true";
+ }
+
+ @Override
+ public TransformationResult[] transform(RegisteredResource resource) {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ @Override
+ public InstallTask createTask(TaskResourceGroup group) {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+}
diff --git a/src/main/java/org/apache/sling/installer/factory/model/impl/FeatureModelInstallerPlugin.java b/src/main/java/org/apache/sling/installer/factory/model/impl/FeatureModelInstallerPlugin.java
index 048b309..a3bc8f6 100644
--- a/src/main/java/org/apache/sling/installer/factory/model/impl/FeatureModelInstallerPlugin.java
+++ b/src/main/java/org/apache/sling/installer/factory/model/impl/FeatureModelInstallerPlugin.java
@@ -18,21 +18,6 @@
*/
package org.apache.sling.installer.factory.model.impl;
-import java.io.File;
-import java.io.FileWriter;
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.InputStreamReader;
-import java.io.Reader;
-import java.io.StringWriter;
-import java.io.Writer;
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-import java.util.regex.Pattern;
-
import org.apache.sling.feature.Feature;
import org.apache.sling.feature.builder.BuilderContext;
import org.apache.sling.feature.builder.FeatureBuilder;
@@ -41,6 +26,7 @@ import org.apache.sling.feature.io.artifacts.ArtifactManager;
import org.apache.sling.feature.io.artifacts.ArtifactManagerConfig;
import org.apache.sling.feature.io.json.FeatureJSONReader;
import org.apache.sling.feature.io.json.FeatureJSONWriter;
+import org.apache.sling.feature.spi.context.ExtensionHandler;
import org.apache.sling.installer.api.InstallableResource;
import org.apache.sling.installer.api.tasks.InstallTask;
import org.apache.sling.installer.api.tasks.InstallTaskFactory;
@@ -59,6 +45,21 @@ import org.osgi.service.metatype.annotations.ObjectClassDefinition;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
+import java.io.File;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.Reader;
+import java.io.StringWriter;
+import java.io.Writer;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.regex.Pattern;
+
/**
* This task factory processes model resources detected by
* the {@link FeatureModelTransformer}.
@@ -102,6 +103,8 @@ public class FeatureModelInstallerPlugin implements InstallTaskFactory, Resource
private final File storageDirectory;
+ private volatile List<ExtensionHandler> extensionHandlers;
+
@Activate
public FeatureModelInstallerPlugin(final BundleContext ctx, final Config config) throws IOException {
this.bundleContext = ctx;
@@ -232,7 +235,7 @@ public class FeatureModelInstallerPlugin implements InstallTaskFactory, Resource
}
final InstallContext ctx = new InstallContext(this.artifactManager, this.storageDirectory);
return new InstallFeatureModelTask(group,
- ctx, this.bundleContext);
+ ctx, this.bundleContext, this.extensionHandlers);
}
boolean classifierMatches(String classifier) {
diff --git a/src/main/java/org/apache/sling/installer/factory/model/impl/InstallFeatureModelTask.java b/src/main/java/org/apache/sling/installer/factory/model/impl/InstallFeatureModelTask.java
index 9b37be6..e2b42a6 100644
--- a/src/main/java/org/apache/sling/installer/factory/model/impl/InstallFeatureModelTask.java
+++ b/src/main/java/org/apache/sling/installer/factory/model/impl/InstallFeatureModelTask.java
@@ -18,21 +18,6 @@
*/
package org.apache.sling.installer.factory.model.impl;
-import java.io.File;
-import java.io.FileOutputStream;
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.OutputStream;
-import java.io.Reader;
-import java.io.StringReader;
-import java.net.MalformedURLException;
-import java.net.URL;
-import java.net.URLConnection;
-import java.util.ArrayList;
-import java.util.Dictionary;
-import java.util.Hashtable;
-import java.util.List;
-
import org.apache.sling.feature.Artifact;
import org.apache.sling.feature.ArtifactId;
import org.apache.sling.feature.Configuration;
@@ -52,25 +37,35 @@ import org.apache.sling.installer.api.tasks.ResourceState;
import org.apache.sling.installer.api.tasks.TaskResource;
import org.apache.sling.installer.api.tasks.TaskResourceGroup;
import org.osgi.framework.BundleContext;
-import org.osgi.util.tracker.ServiceTracker;
+
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.io.Reader;
+import java.io.StringReader;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.net.URLConnection;
+import java.util.ArrayList;
+import java.util.Dictionary;
+import java.util.Hashtable;
+import java.util.List;
/**
* This task installs a feature model resources.
*/
public class InstallFeatureModelTask extends AbstractFeatureModelTask {
private final InstallContext installContext;
-
-
-// It's not a DS component so we can't do @Reference
-// @Reference
-// private List<ExtensionHandler> extensionHandlers;
- final ServiceTracker<ExtensionHandler, ExtensionHandler> extensionHandlerTracker;
+ private final List<ExtensionHandler> extensionHandlers;
public InstallFeatureModelTask(final TaskResourceGroup group,
- final InstallContext installContext, final BundleContext bundleContext) {
+ final InstallContext installContext, final BundleContext bundleContext,
+ final List<ExtensionHandler> extensionHandlers) {
super(group, bundleContext);
this.installContext = installContext;
- this.extensionHandlerTracker = new ServiceTracker<>(bundleContext, ExtensionHandler.class, null);
+ this.extensionHandlers = extensionHandlers;
}
@Override
@@ -141,23 +136,22 @@ public class InstallFeatureModelTask extends AbstractFeatureModelTask {
for (Extension ext : feature.getExtensions()) {
boolean handlerFound = false;
- for (ExtensionHandler eh : extensionHandlerTracker.getServices(new ExtensionHandler[] {})) {
+ for (ExtensionHandler eh : extensionHandlers) {
try {
handlerFound |= eh.handle(context, ext, feature);
} catch (Exception e) {
logger.error("Exception while processing extension {} with handler {}", ext, eh, e);
}
-
- if (!handlerFound) {
- if (ExtensionType.ARTIFACTS == ext.getType()) {
- // Unhandled ARTIFACTS extensions get stored
- for (final Artifact artifact : ext.getArtifacts()) {
- addArtifact(artifact, result);
- }
- } else {
- // should this be an error?
- logger.warn("No extension handler found for mandartory extension " + ext);
+ }
+ if (!handlerFound) {
+ if (ExtensionType.ARTIFACTS == ext.getType()) {
+ // Unhandled ARTIFACTS extensions get stored
+ for (final Artifact artifact : ext.getArtifacts()) {
+ addArtifact(artifact, result);
}
+ } else {
+ // should this be an error?
+ logger.warn("No extension handler found for mandartory extension " + ext);
}
}
}
diff --git a/src/test/java/org/apache/sling/installer/factory/model/impl/InstallFeatureModelTaskTest.java b/src/test/java/org/apache/sling/installer/factory/model/impl/InstallFeatureModelTaskTest.java
new file mode 100644
index 0000000..05e1b2f
--- /dev/null
+++ b/src/test/java/org/apache/sling/installer/factory/model/impl/InstallFeatureModelTaskTest.java
@@ -0,0 +1,131 @@
+/*
+ * 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.installer.factory.model.impl;
+
+import org.apache.sling.feature.Extension;
+import org.apache.sling.feature.ExtensionType;
+import org.apache.sling.feature.Feature;
+import org.apache.sling.feature.spi.context.ExtensionHandler;
+import org.apache.sling.feature.spi.context.ExtensionHandlerContext;
+import org.apache.sling.installer.api.tasks.InstallationContext;
+import org.apache.sling.installer.api.tasks.ResourceState;
+import org.apache.sling.installer.api.tasks.TaskResource;
+import org.apache.sling.installer.api.tasks.TaskResourceGroup;
+import org.junit.Test;
+import org.mockito.Mockito;
+import org.osgi.framework.BundleContext;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
+import static org.junit.Assert.assertEquals;
+
+public class InstallFeatureModelTaskTest {
+ private static String FEATURE_JSON_1 = "{" +
+ " \"id\":\"com.adobe.someproj:feature:7.0.126\"," +
+// " \"bundles\": [" +
+// " \"com.adobe.osgi.wrapper:com.adobe.osgi.wrapper.javassist:3.15.0-GA-wrapper.1\"" +
+// " ]," +
+ " \"system-fonts:ARTIFACTS|true\": [" +
+ " \"com.adobe.aemfd.core:adobe-aemfd-core-fonts:jar:0.7.1\"" +
+ " ]," +
+ " \"blahblahblah:ARTIFACTS|true\": [" +
+ " ]," +
+ " \"framework-properties\": {" +
+ " \"foo\": \"bar\"" +
+ " }" +
+ "}";
+
+// private Path tempDir;
+//
+//
+// @Before
+// public void setUp() throws IOException {
+// tempDir = Files.createTempDirectory(getClass().getSimpleName());
+// }
+//
+// @After
+// public void tearDown() throws IOException {
+// // Delete the temp dir again
+// Files.walk(tempDir)
+// .sorted(Comparator.reverseOrder())
+// .map(Path::toFile)
+// .forEach(File::delete);
+// }
+
+ @Test
+ public void testTransform() {
+ TaskResource resource = Mockito.mock(TaskResource.class);
+ Mockito.when(resource.getAttribute(FeatureModelInstallerPlugin.ATTR_MODEL))
+ .thenReturn(FEATURE_JSON_1);
+
+ TaskResourceGroup group = Mockito.mock(TaskResourceGroup.class);
+ Mockito.when(group.getActiveResource()).thenReturn(resource);
+
+ InstallContext installContext = new InstallContext(null, null);
+ BundleContext bundleContext = Mockito.mock(BundleContext.class);
+
+ TestExtensionHandler testEH = new TestExtensionHandler("system-fonts", ExtensionType.ARTIFACTS);
+ List<ExtensionHandler> extensionHandlers = Arrays.asList(testEH);
+
+ InstallFeatureModelTask ifmt = new InstallFeatureModelTask(group, installContext,
+ bundleContext, extensionHandlers );
+
+ InstallationContext ctx = Mockito.mock(InstallationContext.class);
+
+ assertEquals("Precondition", 0, testEH.handled.size());
+ assertEquals("Precondition", 0, testEH.notHandled.size());
+
+ ifmt.execute(ctx);
+
+ assertEquals(1, testEH.handled.size());
+ assertEquals("system-fonts", testEH.handled.get(0).getName());
+
+ assertEquals(1, testEH.notHandled.size());
+ assertEquals("blahblahblah", testEH.notHandled.get(0).getName());
+
+ Mockito.verify(group).setFinishState(ResourceState.INSTALLED);
+ }
+
+ private static class TestExtensionHandler implements ExtensionHandler {
+ private final String extensionName;
+ private final ExtensionType extensionType;
+ private final List<Extension> handled = new ArrayList<>();
+ private final List<Extension> notHandled = new ArrayList<>();
+
+ private TestExtensionHandler(String name, ExtensionType type) {
+ extensionName = name;
+ extensionType = type;
+ }
+
+ @Override
+ public boolean handle(ExtensionHandlerContext context, Extension extension, Feature feature) throws Exception {
+ if (extensionName.equals(extension.getName()) &&
+ extensionType == extension.getType()) {
+
+ handled.add(extension);
+ return true;
+ }
+
+ notHandled.add(extension);
+ return false;
+ }
+ }
+}
diff --git a/src/test/java/org/apache/sling/installer/factory/model/impl/RepoinitExtensionHandlerTest.java b/src/test/java/org/apache/sling/installer/factory/model/impl/RepoinitExtensionHandlerTest.java
new file mode 100644
index 0000000..b557e9b
--- /dev/null
+++ b/src/test/java/org/apache/sling/installer/factory/model/impl/RepoinitExtensionHandlerTest.java
@@ -0,0 +1,68 @@
+/*
+ * 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.installer.factory.model.impl;
+
+import org.apache.sling.feature.ArtifactId;
+import org.apache.sling.feature.Extension;
+import org.apache.sling.feature.ExtensionState;
+import org.apache.sling.feature.ExtensionType;
+import org.apache.sling.feature.Feature;
+import org.apache.sling.feature.spi.context.ExtensionHandlerContext;
+import org.junit.Test;
+import org.mockito.Mockito;
+import org.osgi.framework.Constants;
+
+import java.util.Dictionary;
+import java.util.Hashtable;
+
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+public class RepoinitExtensionHandlerTest {
+ @Test
+ public void testExtensionHandler() throws Exception {
+ RepoinitExtensionHandler rieh = new RepoinitExtensionHandler();
+
+ ExtensionHandlerContext ctx = Mockito.mock(ExtensionHandlerContext.class);
+ Extension ex = new Extension(ExtensionType.TEXT,
+ Extension.EXTENSION_NAME_REPOINIT, ExtensionState.REQUIRED);
+ ex.setText("hello 123");
+
+ Feature feat = new Feature(ArtifactId.fromMvnId("a:b:1"));
+
+ assertTrue(rieh.handle(ctx, ex, feat));
+
+ Dictionary<String,Object> props = new Hashtable<>();
+ props.put("scripts", "hello 123");
+ props.put(Constants.SERVICE_RANKING, 200);
+
+ Mockito.verify(ctx).addConfiguration(null, "org.apache.sling.jcr.repoinit.RepositoryInitializer~b_1.jar", props);
+ }
+
+ @Test
+ public void testSkipUnrelatedExtensions() throws Exception {
+ RepoinitExtensionHandler rieh = new RepoinitExtensionHandler();
+
+ ExtensionHandlerContext ctx = Mockito.mock(ExtensionHandlerContext.class);
+ assertFalse(rieh.handle(ctx, new Extension(ExtensionType.TEXT,
+ "someother-extension", ExtensionState.REQUIRED), null));
+ assertFalse(rieh.handle(ctx, new Extension(ExtensionType.JSON,
+ Extension.EXTENSION_NAME_REPOINIT, ExtensionState.REQUIRED), null));
+ }
+}