SLING-9087 : Support creation of feature archives. Add unit tests
diff --git a/pom.xml b/pom.xml
index 73886cd..fcacec2 100644
--- a/pom.xml
+++ b/pom.xml
@@ -129,7 +129,7 @@
<dependency>
<groupId>org.apache.sling</groupId>
<artifactId>org.apache.sling.feature</artifactId>
- <version>1.1.2</version>
+ <version>1.1.3-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
diff --git a/src/main/java/org/apache/sling/feature/io/archive/ArchiveReader.java b/src/main/java/org/apache/sling/feature/io/archive/ArchiveReader.java
index 2bb3bed..00926ce 100644
--- a/src/main/java/org/apache/sling/feature/io/archive/ArchiveReader.java
+++ b/src/main/java/org/apache/sling/feature/io/archive/ArchiveReader.java
@@ -99,7 +99,7 @@
features.add(FeatureJSONReader.read(new InputStreamReader(jis, "UTF-8"), entry.getName()));
} else if ( !entry.isDirectory() && entry.getName().startsWith(ArchiveWriter.ARTIFACTS_PREFIX) ) { // artifact
final ArtifactId id = ArtifactId
- .fromMvnUrl("mvn:".concat(entry.getName().substring(ArchiveWriter.ARTIFACTS_PREFIX.length())));
+ .fromMvnPath(entry.getName().substring(ArchiveWriter.ARTIFACTS_PREFIX.length()));
if (consumer != null) {
consumer.consume(id, jis);
}
diff --git a/src/test/java/org/apache/sling/feature/io/archive/ArchiveWriterTest.java b/src/test/java/org/apache/sling/feature/io/archive/ArchiveWriterTest.java
new file mode 100644
index 0000000..cfd09f1
--- /dev/null
+++ b/src/test/java/org/apache/sling/feature/io/archive/ArchiveWriterTest.java
@@ -0,0 +1,99 @@
+/*
+ * 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.io.archive;
+
+import static org.junit.Assert.assertArrayEquals;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.ArrayList;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+
+import org.apache.sling.feature.Artifact;
+import org.apache.sling.feature.ArtifactId;
+import org.apache.sling.feature.Feature;
+import org.junit.Test;
+
+public class ArchiveWriterTest {
+
+ public static final String ARTIFACT = "/features/final.json";
+
+ @Test
+ public void testArchiveWrite() throws IOException {
+ final Feature f = new Feature(ArtifactId.parse("g:f:1"));
+ f.getBundles().add(new Artifact(ArtifactId.parse("g:a:2")));
+
+ byte[] archive = null;
+ final Set<ArtifactId> ids = new HashSet<>();
+ try (final ByteArrayOutputStream out = new ByteArrayOutputStream()) {
+ ArchiveWriter.write(out, null, id -> {
+ ids.add(id);
+ return ArchiveWriterTest.class.getResource(ARTIFACT);
+ }, f).finish();
+ out.flush();
+ archive = out.toByteArray();
+ }
+ assertEquals(1, ids.size());
+ assertTrue(ids.contains(ArtifactId.parse("g:a:2")));
+
+ // read "artifact"
+ final byte[] artifactBytes;
+ try (final InputStream is = ArchiveWriterTest.class.getResourceAsStream(ARTIFACT)) {
+ artifactBytes = readFromStream(is);
+ }
+
+ final Set<ArtifactId> readIds = new HashSet<>();
+ final List<Feature> features = new ArrayList<>();
+ try (final InputStream in = new ByteArrayInputStream(archive)) {
+ features.addAll(ArchiveReader.read(in, (id, is) -> {
+ readIds.add(id);
+ byte[] read = readFromStream(is);
+ assertEquals(artifactBytes.length, read.length);
+ assertArrayEquals(artifactBytes, read);
+ }));
+ }
+
+ assertEquals(1, readIds.size());
+ assertTrue(readIds.contains(ArtifactId.parse("g:a:2")));
+
+ assertEquals(1, features.size());
+ final Feature g = features.get(0);
+ assertEquals(f.getId(), g.getId());
+ }
+
+ private byte[] readFromStream(final InputStream is) throws IOException {
+ byte[] read;
+ try (final ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
+ byte[] buf = new byte[1024];
+ int l;
+ while ((l = is.read(buf)) > 0) {
+ baos.write(buf, 0, l);
+ }
+ baos.flush();
+ read = baos.toByteArray();
+ }
+ return read;
+ }
+}