Fix FELIX-3249:
* Applied patch from Guillaume
* junit4osgi now uses the bnd ipojo plugin


git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@1208515 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/ipojo/bnd-ipojo-plugin/pom.xml b/ipojo/bnd-ipojo-plugin/pom.xml
index ed842f6..0473175 100644
--- a/ipojo/bnd-ipojo-plugin/pom.xml
+++ b/ipojo/bnd-ipojo-plugin/pom.xml
@@ -17,59 +17,56 @@
   specific language governing permissions and limitations
   under the License.
 -->
-<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
-  <modelVersion>4.0.0</modelVersion>
-  <parent>
-    <groupId>org.apache.felix</groupId>
-    <artifactId>felix-parent</artifactId>
-    <version>1.2.1</version>
-    <relativePath>../../pom/pom.xml</relativePath>
-  </parent>
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+    <modelVersion>4.0.0</modelVersion>
+    <parent>
+        <groupId>org.apache.felix</groupId>
+        <artifactId>felix-parent</artifactId>
+        <version>1.2.1</version>
+        <relativePath>../../pom/pom.xml</relativePath>
+    </parent>
 
-  <artifactId>bnd-ipojo-plugin</artifactId>
-  <version>1.0.0-SNAPSHOT</version>
+    <artifactId>bnd-ipojo-plugin</artifactId>
+    <version>1.0.0-SNAPSHOT</version>
     <name>Apache Felix iPOJO Bnd Plugin</name>
 
-  <developers>
-    <developer>
-      <name>Guillaume Sauthier</name>
-      <organization>OW2 Consortium</organization>
-      <roles>
-        <role>developer</role>
-      </roles>
-    </developer>
-  </developers>
+    <dependencies>
+        <dependency>
+            <groupId>biz.aQute</groupId>
+            <artifactId>bndlib</artifactId>
+            <version>1.43.0</version>
+            <scope>provided</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.felix</groupId>
+            <artifactId>org.apache.felix.ipojo.manipulator</artifactId>
+            <version>1.9.0-SNAPSHOT</version>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.felix</groupId>
+            <artifactId>org.apache.felix.ipojo.annotations</artifactId>
+            <version>1.8.0</version>
+        </dependency>
+        <dependency>
+            <groupId>org.mockito</groupId>
+            <artifactId>mockito-all</artifactId>
+            <version>1.8.5</version>
+            <scope>test</scope>
+        </dependency>
+    </dependencies>
 
-  <dependencies>
-    <dependency>
-      <groupId>biz.aQute</groupId>
-      <artifactId>bndlib</artifactId>
-      <version>1.43.0</version>
-      <scope>provided</scope>
-    </dependency>
-    <dependency>
-      <groupId>org.apache.felix</groupId>
-      <artifactId>org.apache.felix.ipojo.manipulator</artifactId>
-      <version>1.9.0-SNAPSHOT</version>
-    </dependency>
-    <dependency>
-      <groupId>org.apache.felix</groupId>
-      <artifactId>org.apache.felix.ipojo.annotations</artifactId>
-      <version>1.8.0</version>
-    </dependency>
-  </dependencies>
+    <build>
+        <plugins>
+            <plugin>
+                <groupId>org.apache.maven.plugins</groupId>
+                <artifactId>maven-compiler-plugin</artifactId>
+                <configuration>
+                    <source>1.5</source>
+                    <target>1.5</target>
+                </configuration>
+            </plugin>
+        </plugins>
+    </build>
 
-  <build>
-    <plugins>
-      <plugin>
-        <groupId>org.apache.maven.plugins</groupId>
-        <artifactId>maven-compiler-plugin</artifactId>
-        <configuration>
-          <source>1.5</source>
-          <target>1.5</target>
-        </configuration>
-      </plugin>
-    </plugins>
-  </build>
-    
 </project>
\ No newline at end of file
diff --git a/ipojo/bnd-ipojo-plugin/src/main/java/org/apache/felix/ipojo/bnd/BndJarResourceStore.java b/ipojo/bnd-ipojo-plugin/src/main/java/org/apache/felix/ipojo/bnd/BndJarResourceStore.java
index aab2239..24e1305 100644
--- a/ipojo/bnd-ipojo-plugin/src/main/java/org/apache/felix/ipojo/bnd/BndJarResourceStore.java
+++ b/ipojo/bnd-ipojo-plugin/src/main/java/org/apache/felix/ipojo/bnd/BndJarResourceStore.java
@@ -44,9 +44,10 @@
 
     private MetadataRenderer m_renderer = new MetadataRenderer();
 
-    private Element m_metadata;
+    private List<Element> m_metadata;
 
     public BndJarResourceStore(Analyzer analyzer, Reporter reporter) {
+        m_metadata = new ArrayList<Element>();
         m_analyzer = analyzer;
         m_reporter = reporter;
     }
@@ -86,7 +87,7 @@
     }
 
     public void writeMetadata(Element metadata) {
-        m_metadata = metadata;
+        m_metadata.add(metadata);
 
         // Find referred packages and add them into Bnd
         for (String referred : Metadatas.findReferredPackages(metadata)) {
@@ -97,7 +98,6 @@
         }
 
         // IPOJO-Components will be written during the close method.
-
     }
 
     public void write(String resourcePath, byte[] resource) throws IOException {
@@ -107,13 +107,10 @@
 
     public void close() throws IOException {
         // Write the iPOJO header (including manipulation metadata)
-        String components = m_analyzer.getProperty("IPOJO-Components");
         StringBuilder builder = new StringBuilder();
-
-        if (components != null) {
-            builder.append(components);
+        for (Element metadata : m_metadata) {
+            builder.append(m_renderer.render(metadata));
         }
-        builder.append(m_renderer.render(m_metadata));
 
         if (builder.length() != 0) {
             m_analyzer.setProperty("IPOJO-Components", builder.toString());
diff --git a/ipojo/bnd-ipojo-plugin/src/test/java/org/apache/felix/ipojo/bnd/PojoizationPluginTestCase.java b/ipojo/bnd-ipojo-plugin/src/test/java/org/apache/felix/ipojo/bnd/PojoizationPluginTestCase.java
new file mode 100644
index 0000000..0ad3002
--- /dev/null
+++ b/ipojo/bnd-ipojo-plugin/src/test/java/org/apache/felix/ipojo/bnd/PojoizationPluginTestCase.java
@@ -0,0 +1,113 @@
+/*
+ * 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.felix.ipojo.bnd;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import aQute.lib.osgi.Analyzer;
+import aQute.lib.osgi.Jar;
+import aQute.lib.osgi.Resource;
+import aQute.lib.osgi.URLResource;
+import aQute.libg.reporter.Reporter;
+import junit.framework.TestCase;
+import org.easymock.EasyMock;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+import org.mockito.Spy;
+
+import static org.mockito.Matchers.eq;
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.spy;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.verifyZeroInteractions;
+import static org.mockito.Mockito.when;
+
+public class PojoizationPluginTestCase extends TestCase {
+
+    @Mock
+    private Reporter reporter;
+
+    @Spy
+    private Analyzer analyzer = new Analyzer();
+
+    @Spy
+    private Jar jar = new Jar("mock.jar");
+
+    @Override
+    public void setUp() throws Exception {
+        MockitoAnnotations.initMocks(this);
+    }
+
+    public void testAnalysisWithComponentOnlyMetadataXml() throws Exception {
+        PojoizationPlugin plugin = new PojoizationPlugin();
+
+        Map<String, String> props = new HashMap<String, String>();
+
+        Resource resource = new URLResource(getClass().getResource("/metadata-components-only.xml"));
+        doReturn(jar).when(analyzer).getJar();
+        doReturn(resource).when(jar).getResource(eq("META-INF/metadata.xml"));
+
+        plugin.setReporter(reporter);
+        plugin.setProperties(props);
+
+        plugin.analyzeJar(analyzer);
+
+        assertEquals("component { $class=\"com.acme.Thermometer\" }",
+                analyzer.getProperty("IPOJO-Components"));
+    }
+
+    public void testAnalysisWithInstanceOnlyMetadataXml() throws Exception {
+        PojoizationPlugin plugin = new PojoizationPlugin();
+
+        Map<String, String> props = new HashMap<String, String>();
+
+        Resource resource = new URLResource(getClass().getResource("/metadata-instances-only.xml"));
+        doReturn(jar).when(analyzer).getJar();
+        doReturn(resource).when(jar).getResource(eq("META-INF/metadata.xml"));
+
+        plugin.setReporter(reporter);
+        plugin.setProperties(props);
+
+        plugin.analyzeJar(analyzer);
+
+        assertEquals("instance { $component=\"com.acme.Thermometer\" }",
+                analyzer.getProperty("IPOJO-Components"));
+    }
+
+    public void testAnalysisWithComponentsAndInstancesMetadataXml() throws Exception {
+        PojoizationPlugin plugin = new PojoizationPlugin();
+
+        Map<String, String> props = new HashMap<String, String>();
+
+        Resource resource = new URLResource(getClass().getResource("/metadata-components-and-instances.xml"));
+        doReturn(jar).when(analyzer).getJar();
+        doReturn(resource).when(jar).getResource(eq("META-INF/metadata.xml"));
+
+        plugin.setReporter(reporter);
+        plugin.setProperties(props);
+
+        plugin.analyzeJar(analyzer);
+
+        assertEquals("component { $class=\"com.acme.Thermometer\" }" +
+                     "instance { $component=\"com.acme.Thermometer\" }" +
+                     "instance { $component=\"com.acme.Thermometer\" }",
+                     analyzer.getProperty("IPOJO-Components"));
+    }
+}
diff --git a/ipojo/bnd-ipojo-plugin/src/test/resources/metadata-components-and-instances.xml b/ipojo/bnd-ipojo-plugin/src/test/resources/metadata-components-and-instances.xml
new file mode 100644
index 0000000..19bc3d4
--- /dev/null
+++ b/ipojo/bnd-ipojo-plugin/src/test/resources/metadata-components-and-instances.xml
@@ -0,0 +1,5 @@
+<ipojo>
+  <component class="com.acme.Thermometer" />
+  <instance component="com.acme.Thermometer" />
+  <instance component="com.acme.Thermometer" />
+</ipojo>
diff --git a/ipojo/bnd-ipojo-plugin/src/test/resources/metadata-components-only.xml b/ipojo/bnd-ipojo-plugin/src/test/resources/metadata-components-only.xml
new file mode 100644
index 0000000..cc913fe
--- /dev/null
+++ b/ipojo/bnd-ipojo-plugin/src/test/resources/metadata-components-only.xml
@@ -0,0 +1,3 @@
+<ipojo>
+  <component class="com.acme.Thermometer"/>
+</ipojo>
diff --git a/ipojo/bnd-ipojo-plugin/src/test/resources/metadata-instances-only.xml b/ipojo/bnd-ipojo-plugin/src/test/resources/metadata-instances-only.xml
new file mode 100644
index 0000000..56aa63a
--- /dev/null
+++ b/ipojo/bnd-ipojo-plugin/src/test/resources/metadata-instances-only.xml
@@ -0,0 +1,3 @@
+<ipojo>
+    <instance component="com.acme.Thermometer"/>
+</ipojo>
\ No newline at end of file
diff --git a/ipojo/junit4osgi/junit4osgi/pom.xml b/ipojo/junit4osgi/junit4osgi/pom.xml
index ac21e03..110678f 100644
--- a/ipojo/junit4osgi/junit4osgi/pom.xml
+++ b/ipojo/junit4osgi/junit4osgi/pom.xml
@@ -16,124 +16,137 @@
   specific language governing permissions and limitations
   under the License.
 -->
-<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
-  <parent>
-    <groupId>org.apache.felix</groupId>
-    <artifactId>felix-parent</artifactId>
-    <version>1.2.1</version>
-  </parent>
-  <modelVersion>4.0.0</modelVersion>
-  <packaging>bundle</packaging>
-  <name>Junit4Osgi</name>
-  <artifactId>org.apache.felix.ipojo.junit4osgi</artifactId>
-  <version>1.1.0-SNAPSHOT</version>
-  <dependencies>
-    <dependency>
-      <groupId>org.apache.felix</groupId>
-      <artifactId>org.apache.felix.ipojo</artifactId>
-      <version>1.8.0</version>
-    </dependency>
-    <dependency>
-      <groupId>org.apache.felix</groupId>
-      <artifactId>org.apache.felix.ipojo.metadata</artifactId>
-      <version>1.4.0</version>
-    </dependency>
-    <dependency>
-      <groupId>junit</groupId>
-      <artifactId>junit</artifactId>
-      <version>3.8.1</version>
-    </dependency>
-    <dependency>
-      <groupId>org.osgi</groupId>
-      <artifactId>org.osgi.core</artifactId>
-      <version>4.2.0</version>
-    </dependency>
-  </dependencies>
-  <build>
-    <plugins>
-      <plugin>
-        <groupId>org.apache.maven.plugins</groupId>
-        <artifactId>maven-compiler-plugin</artifactId>
-        <configuration>
-          <source>1.4</source>
-          <target>1.4</target>
-        </configuration>
-      </plugin>
-      <plugin>
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+    <parent>
         <groupId>org.apache.felix</groupId>
-        <artifactId>maven-bundle-plugin</artifactId>
-        <version>2.3.5</version>
-        <extensions>true</extensions>
-        <configuration>
-          <instructions>
-            <Bundle-Name>
-              Apache Felix iPOJO OSGi Junit Runner
-            </Bundle-Name>
-            <Bundle-SymbolicName>
-              ${project.artifactId}
-            </Bundle-SymbolicName>
-            <Private-Package>
-              org.apache.felix.ipojo.junit4osgi.impl,
-              org.apache.felix.ipojo.junit4osgi.test,
-            </Private-Package>
-            <Export-Package>
-              org.apache.felix.ipojo.junit4osgi,
-              org.apache.felix.ipojo.junit4osgi.helpers,
-              junit.*
-            </Export-Package>
-            <Import-Package>!javax.swing*, *</Import-Package>
-            <Test-Suite>
-              org.apache.felix.ipojo.junit4osgi.test.TestTestCase,
-              org.apache.felix.ipojo.junit4osgi.test.TestOSGiTestCase,
-              org.apache.felix.ipojo.junit4osgi.test.TestTestSuite,
-              org.apache.felix.ipojo.junit4osgi.test.TestOSGiTestSuite,
-            </Test-Suite>
-            <Include-Resource>
-              META-INF/LICENSE=LICENSE,
-              META-INF/LICENSE.junit=LICENSE.junit,
-              META-INF/NOTICE=NOTICE
-            </Include-Resource>
-          </instructions>
-        </configuration>
-      </plugin>
-      <plugin>
-        <groupId>org.apache.felix</groupId>
-        <artifactId>maven-ipojo-plugin</artifactId>
-        <version>1.9.0-SNAPSHOT</version>
-        <executions>
-          <execution>
-            <goals>
-              <goal>ipojo-bundle</goal>
-            </goals>
-            <configuration>
-              <ignoreAnnotations>true</ignoreAnnotations>
-            </configuration>
-          </execution>
-        </executions>
-      </plugin>
-      <plugin>
-        <groupId>org.codehaus.mojo</groupId>
-        <artifactId>rat-maven-plugin</artifactId>
-        <configuration>
-          <excludeSubProjects>false</excludeSubProjects>
-          <useEclipseDefaultExcludes>true</useEclipseDefaultExcludes>
-          <useMavenDefaultExcludes>true</useMavenDefaultExcludes>
-          <excludes>
-            <param>doc/*</param>
-            <param>maven-eclipse.xml</param>
-            <param>.checkstyle</param>
-            <param>.externalToolBuilders/*</param>
-          </excludes>
-        </configuration>
-      </plugin>
-      <plugin>
-        <groupId>org.apache.maven.plugins</groupId>
-        <artifactId>maven-checkstyle-plugin</artifactId>
-        <configuration>
-          <configLocation>http://felix.apache.org/ipojo/dev/checkstyle_ipojo.xml</configLocation>
-          <violationSeverity>error</violationSeverity>
-        </configuration>
-      </plugin>
-    </plugins>
-  </build>
+        <artifactId>felix-parent</artifactId>
+        <version>1.2.1</version>
+    </parent>
+    <modelVersion>4.0.0</modelVersion>
+    <packaging>bundle</packaging>
+    <name>Junit4Osgi</name>
+    <artifactId>org.apache.felix.ipojo.junit4osgi</artifactId>
+    <version>1.1.0-SNAPSHOT</version>
+    <dependencies>
+        <dependency>
+            <groupId>org.apache.felix</groupId>
+            <artifactId>org.apache.felix.ipojo</artifactId>
+            <version>1.8.0</version>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.felix</groupId>
+            <artifactId>org.apache.felix.ipojo.metadata</artifactId>
+            <version>1.4.0</version>
+        </dependency>
+        <dependency>
+            <groupId>junit</groupId>
+            <artifactId>junit</artifactId>
+            <version>3.8.1</version>
+        </dependency>
+        <dependency>
+            <groupId>org.osgi</groupId>
+            <artifactId>org.osgi.core</artifactId>
+            <version>4.2.0</version>
+        </dependency>
+    </dependencies>
+    <build>
+        <plugins>
+            <plugin>
+                <groupId>org.apache.maven.plugins</groupId>
+                <artifactId>maven-compiler-plugin</artifactId>
+                <configuration>
+                    <source>1.4</source>
+                    <target>1.4</target>
+                </configuration>
+            </plugin>
+            <plugin>
+                <groupId>org.apache.felix</groupId>
+                <artifactId>maven-bundle-plugin</artifactId>
+                <version>2.3.5</version>
+                <extensions>true</extensions>
+                <configuration>
+                    <instructions>
+                        <Bundle-Name>
+                            Apache Felix iPOJO OSGi Junit Runner
+                        </Bundle-Name>
+                        <Bundle-SymbolicName>
+                            ${project.artifactId}
+                        </Bundle-SymbolicName>
+                        <Private-Package>
+                            org.apache.felix.ipojo.junit4osgi.impl,
+                            org.apache.felix.ipojo.junit4osgi.test,
+                        </Private-Package>
+                        <Export-Package>
+                            org.apache.felix.ipojo.junit4osgi,
+                            org.apache.felix.ipojo.junit4osgi.helpers,
+                            junit.*
+                        </Export-Package>
+                        <Import-Package>!javax.swing*, *</Import-Package>
+                        <Test-Suite>
+                            org.apache.felix.ipojo.junit4osgi.test.TestTestCase,
+                            org.apache.felix.ipojo.junit4osgi.test.TestOSGiTestCase,
+                            org.apache.felix.ipojo.junit4osgi.test.TestTestSuite,
+                            org.apache.felix.ipojo.junit4osgi.test.TestOSGiTestSuite,
+                        </Test-Suite>
+                        <Include-Resource>
+                            META-INF/LICENSE=LICENSE,
+                            META-INF/LICENSE.junit=LICENSE.junit,
+                            META-INF/NOTICE=NOTICE
+                        </Include-Resource>
+                        <_plugin>
+                            org.apache.felix.ipojo.bnd.PojoizationPlugin;metadata=${basedir}/metadata.xml;use-local-schemas=true
+                        </_plugin>
+                    </instructions>
+                </configuration>
+                <dependencies>
+                    <dependency>
+                        <groupId>org.apache.felix</groupId>
+                        <artifactId>bnd-ipojo-plugin</artifactId>
+                        <version>1.0.0-SNAPSHOT</version>
+                    </dependency>
+                </dependencies>
+            </plugin>
+            <!--
+            Use the BND-iPOJO-Plugin
+            <plugin>
+              <groupId>org.apache.felix</groupId>
+              <artifactId>maven-ipojo-plugin</artifactId>
+              <version>1.9.0-SNAPSHOT</version>
+              <executions>
+                <execution>
+                  <goals>
+                    <goal>ipojo-bundle</goal>
+                  </goals>
+                  <configuration>
+                    <ignoreAnnotations>true</ignoreAnnotations>
+                  </configuration>
+                </execution>
+              </executions>
+            </plugin>-->
+            <plugin>
+                <groupId>org.codehaus.mojo</groupId>
+                <artifactId>rat-maven-plugin</artifactId>
+                <configuration>
+                    <excludeSubProjects>false</excludeSubProjects>
+                    <useEclipseDefaultExcludes>true</useEclipseDefaultExcludes>
+                    <useMavenDefaultExcludes>true</useMavenDefaultExcludes>
+                    <excludes>
+                        <param>doc/*</param>
+                        <param>maven-eclipse.xml</param>
+                        <param>.checkstyle</param>
+                        <param>.externalToolBuilders/*</param>
+                    </excludes>
+                </configuration>
+            </plugin>
+            <plugin>
+                <groupId>org.apache.maven.plugins</groupId>
+                <artifactId>maven-checkstyle-plugin</artifactId>
+                <configuration>
+                    <configLocation>http://felix.apache.org/ipojo/dev/checkstyle_ipojo.xml</configLocation>
+                    <violationSeverity>error</violationSeverity>
+                </configuration>
+            </plugin>
+        </plugins>
+    </build>
 </project>