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-whiteboard.git
The following commit(s) were added to refs/heads/master by this push:
new eea57b2 SLING-9670: add a new unpack extension for the feature model
eea57b2 is described below
commit eea57b21d2190e7a1a108b9a3dc393d3e8d694da
Author: David Bosschaert <[email protected]>
AuthorDate: Tue Aug 18 16:56:42 2020 +0100
SLING-9670: add a new unpack extension for the feature model
Unit tests
---
.../unpack/impl/UnpackArchiveExtensionHandler.java | 8 +-
.../unpack/impl/UnpackArchiveInstallerPlugin.java | 8 +-
.../impl/UnpackArchiveExtensionHandlerTest.java | 65 ++++++++++
.../impl/UnpackArchiveInstallerPluginTest.java | 136 +++++++++++++++++++++
4 files changed, 213 insertions(+), 4 deletions(-)
diff --git
a/featuremodel-unpack-extension/src/main/java/org/apache/sling/feature/extension/unpack/impl/UnpackArchiveExtensionHandler.java
b/featuremodel-unpack-extension/src/main/java/org/apache/sling/feature/extension/unpack/impl/UnpackArchiveExtensionHandler.java
index 438e3e5..1d1b846 100644
---
a/featuremodel-unpack-extension/src/main/java/org/apache/sling/feature/extension/unpack/impl/UnpackArchiveExtensionHandler.java
+++
b/featuremodel-unpack-extension/src/main/java/org/apache/sling/feature/extension/unpack/impl/UnpackArchiveExtensionHandler.java
@@ -32,11 +32,15 @@ import org.osgi.service.component.annotations.Component;
public class UnpackArchiveExtensionHandler implements ExtensionHandler {
static final String UNPACK_EXTENSIONS_PROP =
"org.apache.sling.feature.unpack.extensions";
- private final Unpack unpack;
+ final Unpack unpack;
@Activate
public UnpackArchiveExtensionHandler(BundleContext bc) {
- unpack = Unpack.fromMapping(bc.getProperty(UNPACK_EXTENSIONS_PROP));
+ this(Unpack.fromMapping(bc.getProperty(UNPACK_EXTENSIONS_PROP)));
+ }
+
+ UnpackArchiveExtensionHandler(Unpack unpack) {
+ this.unpack = unpack;
}
@Override
diff --git
a/featuremodel-unpack-extension/src/main/java/org/apache/sling/feature/extension/unpack/impl/UnpackArchiveInstallerPlugin.java
b/featuremodel-unpack-extension/src/main/java/org/apache/sling/feature/extension/unpack/impl/UnpackArchiveInstallerPlugin.java
index fbb0843..e5185b7 100644
---
a/featuremodel-unpack-extension/src/main/java/org/apache/sling/feature/extension/unpack/impl/UnpackArchiveInstallerPlugin.java
+++
b/featuremodel-unpack-extension/src/main/java/org/apache/sling/feature/extension/unpack/impl/UnpackArchiveInstallerPlugin.java
@@ -49,11 +49,15 @@ public class UnpackArchiveInstallerPlugin implements
InstallTaskFactory, Resourc
private final Logger logger = LoggerFactory.getLogger(this.getClass());
- private final Unpack unpack;
+ final Unpack unpack;
@Activate
public UnpackArchiveInstallerPlugin(BundleContext bc) {
- unpack =
Unpack.fromMapping(bc.getProperty(UnpackArchiveExtensionHandler.UNPACK_EXTENSIONS_PROP));
+
this(Unpack.fromMapping(bc.getProperty(UnpackArchiveExtensionHandler.UNPACK_EXTENSIONS_PROP)));
+ }
+
+ UnpackArchiveInstallerPlugin(Unpack unpack) {
+ this.unpack = unpack;
}
@Override
diff --git
a/featuremodel-unpack-extension/src/test/java/org/apache/sling/feature/extension/unpack/impl/UnpackArchiveExtensionHandlerTest.java
b/featuremodel-unpack-extension/src/test/java/org/apache/sling/feature/extension/unpack/impl/UnpackArchiveExtensionHandlerTest.java
new file mode 100644
index 0000000..7c28a17
--- /dev/null
+++
b/featuremodel-unpack-extension/src/test/java/org/apache/sling/feature/extension/unpack/impl/UnpackArchiveExtensionHandlerTest.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.extension.unpack.impl;
+
+import org.apache.sling.feature.Extension;
+import org.apache.sling.feature.builder.ArtifactProvider;
+import org.apache.sling.feature.extension.unpack.Unpack;
+import org.apache.sling.feature.spi.context.ExtensionHandlerContext;
+import org.junit.Test;
+import org.mockito.Mockito;
+import org.osgi.framework.BundleContext;
+
+import java.lang.reflect.Field;
+
+import static org.junit.Assert.assertEquals;
+
+public class UnpackArchiveExtensionHandlerTest {
+ @Test
+ public void testCreateUnpacker() throws Exception {
+ BundleContext bc = Mockito.mock(BundleContext.class);
+
Mockito.when(bc.getProperty(UnpackArchiveExtensionHandler.UNPACK_EXTENSIONS_PROP))
+ .thenReturn("foobar;dir:=/abc;default:=true");
+
+ UnpackArchiveExtensionHandler uaeh = new
UnpackArchiveExtensionHandler(bc);
+
+ Unpack unpack = uaeh.unpack;
+ Field dmf = Unpack.class.getDeclaredField("defaultMapping");
+ dmf.setAccessible(true);
+ assertEquals("foobar", dmf.get(unpack));
+ }
+
+ @Test
+ public void testCallHandler() throws Exception {
+ Unpack unpack = Mockito.mock(Unpack.class);
+
+ UnpackArchiveExtensionHandler uaeh = new
UnpackArchiveExtensionHandler(unpack);
+
+ ArtifactProvider ap = Mockito.mock(ArtifactProvider.class);
+
+ ExtensionHandlerContext ctx =
Mockito.mock(ExtensionHandlerContext.class);
+ Mockito.when(ctx.getArtifactProvider()).thenReturn(ap);
+
+ Extension ext = Mockito.mock(Extension.class);
+
+ uaeh.handle(ctx, ext, null);
+
+ Mockito.verify(unpack).handle(Mockito.eq(ext), Mockito.eq(ap),
Mockito.notNull());
+ }
+}
diff --git
a/featuremodel-unpack-extension/src/test/java/org/apache/sling/feature/extension/unpack/impl/UnpackArchiveInstallerPluginTest.java
b/featuremodel-unpack-extension/src/test/java/org/apache/sling/feature/extension/unpack/impl/UnpackArchiveInstallerPluginTest.java
new file mode 100644
index 0000000..32034dd
--- /dev/null
+++
b/featuremodel-unpack-extension/src/test/java/org/apache/sling/feature/extension/unpack/impl/UnpackArchiveInstallerPluginTest.java
@@ -0,0 +1,136 @@
+/*
+ * 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.extension.unpack.impl;
+
+import org.apache.sling.feature.ArtifactId;
+import org.apache.sling.feature.extension.unpack.Unpack;
+import org.apache.sling.installer.api.InstallableResource;
+import org.apache.sling.installer.api.tasks.RegisteredResource;
+import org.apache.sling.installer.api.tasks.TransformationResult;
+import org.junit.Test;
+import org.mockito.Mockito;
+import org.osgi.framework.BundleContext;
+
+import java.io.ByteArrayInputStream;
+import java.io.InputStream;
+import java.lang.reflect.Field;
+import java.util.Hashtable;
+import java.util.Map;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertSame;
+
+public class UnpackArchiveInstallerPluginTest {
+ @Test
+ public void testCreatePlugin() throws Exception {
+ BundleContext bc = Mockito.mock(BundleContext.class);
+
Mockito.when(bc.getProperty(UnpackArchiveExtensionHandler.UNPACK_EXTENSIONS_PROP))
+ .thenReturn("foobar;dir:=/abc;default:=true");
+
+ UnpackArchiveInstallerPlugin aeip = new
UnpackArchiveInstallerPlugin(bc);
+
+ Unpack unpack = aeip.unpack;
+ Field dmf = Unpack.class.getDeclaredField("defaultMapping");
+ dmf.setAccessible(true);
+ assertEquals("foobar", dmf.get(unpack));
+ }
+
+ @Test
+ public void testNoTransform() {
+ Unpack unpack = Mockito.mock(Unpack.class);
+ UnpackArchiveInstallerPlugin aeip = new
UnpackArchiveInstallerPlugin(unpack);
+
+ RegisteredResource res0 = Mockito.mock(RegisteredResource.class);
+ assertNull(aeip.transform(res0));
+
+ RegisteredResource res1 = Mockito.mock(RegisteredResource.class);
+ Mockito.when(res1.getType()).thenReturn("Toast");
+ assertNull(aeip.transform(res1));
+
+ RegisteredResource res2 = Mockito.mock(RegisteredResource.class);
+ Mockito.when(res2.getType()).thenReturn(InstallableResource.TYPE_FILE);
+ assertNull(aeip.transform(res2));
+ }
+
+ @Test
+ public void testTransform() throws Exception {
+ ArtifactId aid = ArtifactId.fromMvnId("g:a:9");
+ Hashtable <String,Object> props = new Hashtable<>();
+ props.put("dir", "/some/where");
+ props.put("artifact.id", aid);
+
+ InputStream bais = new ByteArrayInputStream("".getBytes());
+
+ Unpack unpack = Mockito.mock(Unpack.class);
+ Mockito.when(unpack.handles(bais, props)).thenReturn(true);
+
+ UnpackArchiveInstallerPlugin aeip = new
UnpackArchiveInstallerPlugin(unpack);
+
+ RegisteredResource res = Mockito.mock(RegisteredResource.class);
+ Mockito.when(res.getDictionary()).thenReturn(props);
+ Mockito.when(res.getInputStream()).thenReturn(bais);
+ Mockito.when(res.getType()).thenReturn(InstallableResource.TYPE_FILE);
+ TransformationResult[] tra = aeip.transform(res);
+
+ assertEquals(1, tra.length);
+ TransformationResult tr = tra[0];
+
+ assertEquals(UnpackArchiveInstallerPlugin.TYPE_UNPACK_ARCHIVE,
tr.getResourceType());
+ assertEquals("g:a", tr.getId());
+ assertSame(bais, tr.getInputStream());
+
+ Map<String,Object> ctx = tr.getAttributes();
+ assertSame(ctx, ctx.get("context"));
+
+ for (Map.Entry<String,Object> entry : props.entrySet()) {
+ assertEquals(entry.getValue(), ctx.get(entry.getKey()));
+ }
+ }
+
+ @Test
+ public void testTransformConstructArtifactID() throws Exception {
+ Hashtable <String,Object> props = new Hashtable<>();
+
+ InputStream bais = new ByteArrayInputStream("".getBytes());
+
+ Unpack unpack = Mockito.mock(Unpack.class);
+ Mockito.when(unpack.handles(bais, props)).thenReturn(true);
+
+ UnpackArchiveInstallerPlugin aeip = new
UnpackArchiveInstallerPlugin(unpack);
+
+ RegisteredResource res = Mockito.mock(RegisteredResource.class);
+ Mockito.when(res.getDictionary()).thenReturn(props);
+ Mockito.when(res.getInputStream()).thenReturn(bais);
+ Mockito.when(res.getType()).thenReturn(InstallableResource.TYPE_FILE);
+ Mockito.when(res.getURL()).thenReturn("myscheme:/somefilename.bin");
+ Mockito.when(res.getDigest()).thenReturn("digestive!");
+ TransformationResult[] tra = aeip.transform(res);
+
+ assertEquals(1, tra.length);
+ TransformationResult tr = tra[0];
+
+ assertEquals(UnpackArchiveInstallerPlugin.TYPE_UNPACK_ARCHIVE,
tr.getResourceType());
+ assertEquals("unpack.packages:somefilename", tr.getId());
+ assertSame(bais, tr.getInputStream());
+
+ Map<String,Object> ctx = tr.getAttributes();
+ assertSame(ctx, ctx.get("context"));
+ }
+}