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

simonetripodi 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 45de42b  [sp2fm] added BundleEntryHandler unit test
45de42b is described below

commit 45de42b5fedf22cfab6ad6762dd94c43f20ab12b
Author: Simo Tripodi <[email protected]>
AuthorDate: Thu Mar 7 00:39:33 2019 +0100

    [sp2fm] added BundleEntryHandler unit test
---
 content-package-2-feature-model/pom.xml            |  11 +++
 .../ContentPackage2FeatureModelConverter.java      |   4 +-
 .../cp2fm/handlers/BundleEntryHandlerTest.java     | 101 +++++++++++++++++++++
 .../apache/sling/cp2fm/handlers/test-framework.jar | Bin 0 -> 13288 bytes
 4 files changed, 114 insertions(+), 2 deletions(-)

diff --git a/content-package-2-feature-model/pom.xml 
b/content-package-2-feature-model/pom.xml
index 2a0ca31..2eff7ce 100644
--- a/content-package-2-feature-model/pom.xml
+++ b/content-package-2-feature-model/pom.xml
@@ -214,6 +214,7 @@
       <groupId>org.mockito</groupId>
       <artifactId>mockito-core</artifactId>
       <version>2.25.0</version>
+      <scope>test</scope>
     </dependency>
   </dependencies>
 
@@ -230,6 +231,16 @@
       </plugin>
 
       <plugin>
+        <groupId>org.apache.maven.plugins</groupId>
+        <artifactId>maven-surefire-plugin</artifactId>
+        <configuration>
+          <systemPropertyVariables>
+            
<testDirectory>${project.build.directory}/unit-tests</testDirectory>
+          </systemPropertyVariables>
+        </configuration>
+      </plugin>
+
+      <plugin>
         <groupId>org.codehaus.mojo</groupId>
         <artifactId>license-maven-plugin</artifactId>
         <version>1.16</version>
diff --git 
a/content-package-2-feature-model/src/main/java/org/apache/sling/cp2fm/ContentPackage2FeatureModelConverter.java
 
b/content-package-2-feature-model/src/main/java/org/apache/sling/cp2fm/ContentPackage2FeatureModelConverter.java
index edb36b9..1746389 100644
--- 
a/content-package-2-feature-model/src/main/java/org/apache/sling/cp2fm/ContentPackage2FeatureModelConverter.java
+++ 
b/content-package-2-feature-model/src/main/java/org/apache/sling/cp2fm/ContentPackage2FeatureModelConverter.java
@@ -283,7 +283,7 @@ public class ContentPackage2FeatureModelConverter {
 
         Artifact artifact = new Artifact(new ArtifactId(groupId, artifactId, 
version, classifier, type));
         artifact.setStartOrder(bundlesStartOrder);
-        targetFeature.getBundles().add(artifact);
+        getTargetFeature().getBundles().add(artifact);
     }
 
     public void deployLocally(InputStream input,
@@ -292,7 +292,7 @@ public class ContentPackage2FeatureModelConverter {
                               String version,
                               String classifier,
                               String type) throws IOException {
-        File targetDir = new File(outputDirectory, "bundles");
+        File targetDir = new File(getOutputDirectory(), "bundles");
 
         StringTokenizer tokenizer = new StringTokenizer(groupId, ".");
         while (tokenizer.hasMoreTokens()) {
diff --git 
a/content-package-2-feature-model/src/test/java/org/apache/sling/cp2fm/handlers/BundleEntryHandlerTest.java
 
b/content-package-2-feature-model/src/test/java/org/apache/sling/cp2fm/handlers/BundleEntryHandlerTest.java
new file mode 100644
index 0000000..fa8e85b
--- /dev/null
+++ 
b/content-package-2-feature-model/src/test/java/org/apache/sling/cp2fm/handlers/BundleEntryHandlerTest.java
@@ -0,0 +1,101 @@
+/*
+ * 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.cp2fm.handlers;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.anyString;
+import static org.mockito.Mockito.doCallRealMethod;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.spy;
+import static org.mockito.Mockito.when;
+
+import java.io.File;
+import java.io.InputStream;
+
+import org.apache.jackrabbit.vault.fs.io.Archive;
+import org.apache.jackrabbit.vault.fs.io.Archive.Entry;
+import org.apache.sling.cp2fm.ContentPackage2FeatureModelConverter;
+import org.apache.sling.cp2fm.spi.EntryHandler;
+import org.apache.sling.feature.Bundles;
+import org.apache.sling.feature.Feature;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.mockito.invocation.InvocationOnMock;
+import org.mockito.stubbing.Answer;
+
+public final class BundleEntryHandlerTest {
+
+    private EntryHandler bundleEntryHandler;
+
+    @Before
+    public void setUp() {
+        bundleEntryHandler = new BundleEntryHandler();
+    }
+
+    @After
+    public void tearDown() {
+        bundleEntryHandler = null;
+    }
+
+    @Test
+    public void matches() {
+        
assertFalse(bundleEntryHandler.matches("jcr_root/not/a/valid/recognised/bundle.jar"));
+        
assertTrue(bundleEntryHandler.matches("jcr_root/apps/asd/install/asd.jar"));
+    }
+
+    @Test
+    public void deployBundle() throws Exception {
+        Archive archive = mock(Archive.class);
+        Entry entry = mock(Entry.class);
+
+        when(entry.getName()).thenReturn("test-framework.jar");
+        when(archive.openInputStream(entry)).then(new Answer<InputStream>() {
+
+            @Override
+            public InputStream answer(InvocationOnMock invocation) throws 
Throwable {
+                return getClass().getResourceAsStream("test-framework.jar");
+            }
+
+        });
+
+        ContentPackage2FeatureModelConverter converter = 
spy(ContentPackage2FeatureModelConverter.class);
+
+        File testDirectory = new File(System.getProperty("testDirectory"), 
getClass().getName());
+        when(converter.getOutputDirectory()).thenReturn(testDirectory);
+
+        
doCallRealMethod().when(converter).deployLocallyAndAttach(any(InputStream.class),
 anyString(), anyString(), anyString(), anyString(), anyString());
+        
doCallRealMethod().when(converter).deployLocally(any(InputStream.class), 
anyString(), anyString(), anyString(), anyString(), anyString());
+
+        Feature feature = mock(Feature.class);
+        when(feature.getBundles()).thenReturn(new Bundles());
+        when(converter.getTargetFeature()).thenReturn(feature);
+
+        
bundleEntryHandler.handle("jcr_root/apps/asd/install/test-framework.jar", 
archive, entry, converter);
+
+        assertTrue(new File(testDirectory, 
"bundles/org/apache/felix/org.apache.felix.framework/6.0.1/org.apache.felix.framework-6.0.1.pom").exists());
+        assertTrue(new File(testDirectory, 
"bundles/org/apache/felix/org.apache.felix.framework/6.0.1/org.apache.felix.framework-6.0.1.jar").exists());
+
+        assertFalse(feature.getBundles().isEmpty());
+        assertEquals(1, feature.getBundles().size());
+        assertEquals("org.apache.felix:org.apache.felix.framework:6.0.1", 
feature.getBundles().get(0).getId().toMvnId());
+    }
+
+}
diff --git 
a/content-package-2-feature-model/src/test/resources/org/apache/sling/cp2fm/handlers/test-framework.jar
 
b/content-package-2-feature-model/src/test/resources/org/apache/sling/cp2fm/handlers/test-framework.jar
new file mode 100644
index 0000000..9ce022d
Binary files /dev/null and 
b/content-package-2-feature-model/src/test/resources/org/apache/sling/cp2fm/handlers/test-framework.jar
 differ

Reply via email to