Added the first set of integration tests based on Pax Exam.

git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@882937 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/dependencymanager/pom.xml b/dependencymanager/pom.xml
index ceb11bd..9012544 100644
--- a/dependencymanager/pom.xml
+++ b/dependencymanager/pom.xml
@@ -1,23 +1,21 @@
 <?xml version="1.0" encoding="UTF-8"?>
-<!--
- 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
+  <!--
+    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.
--->
-<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">
+    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.
+  -->
+<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</artifactId>
@@ -42,6 +40,27 @@
       <version>1.2.0</version>
       <scope>provided</scope>
     </dependency>
+    <dependency>
+      <groupId>org.ops4j.pax.exam</groupId>
+      <artifactId>pax-exam</artifactId>
+      <version>1.2.0</version>
+    </dependency>
+    <dependency>
+      <groupId>org.ops4j.pax.exam</groupId>
+      <artifactId>pax-exam-container-default</artifactId>
+      <version>1.2.0</version>
+    </dependency>
+    <dependency>
+      <groupId>org.ops4j.pax.exam</groupId>
+      <artifactId>pax-exam-junit</artifactId>
+      <version>1.2.0</version>
+    </dependency>
+    <dependency>
+      <groupId>junit</groupId>
+      <artifactId>junit</artifactId>
+      <version>4.7</version>
+      <scope>test</scope>
+    </dependency>
   </dependencies>
   <build>
     <plugins>
@@ -54,16 +73,41 @@
           <instructions>
             <Bundle-SymbolicName>org.apache.felix.dependencymanager</Bundle-SymbolicName>
             <Bundle-Name>Apache Felix Dependency Manager</Bundle-Name>
-            <Bundle-Description>A bundle that provides a run-time service dependency manager.</Bundle-Description>
+            <Bundle-Description>A bundle that provides a run-time
+              service dependency manager.</Bundle-Description>
             <Bundle-Vendor>The Apache Software Foundation</Bundle-Vendor>
             <Export-Package>org.apache.felix.dependencymanager</Export-Package>
             <Import-Package>!org.apache.felix.dependencymanager,*</Import-Package>
           </instructions>
         </configuration>
       </plugin>
+      <plugin>
+        <artifactId>maven-compiler-plugin</artifactId>
+        <executions>
+          <execution>
+            <id>default-testCompile</id>
+            <configuration>
+              <source>1.5</source>
+              <target>1.5</target>
+            </configuration>
+          </execution>
+        </executions>
+      </plugin>
+      <plugin>
+        <groupId>org.ops4j.pax.exam</groupId>
+        <artifactId>maven-paxexam-plugin</artifactId>
+        <version>1.2.0</version>
+        <executions>
+          <execution>
+            <id>generate-config</id>
+            <goals>
+              <goal>generate-depends-file</goal>
+            </goals>
+          </execution>
+        </executions>
+      </plugin>
     </plugins>
   </build>
-
   <scm>
     <connection>scm:svn:https://svn.apache.org/repos/asf/felix/releases/org.apache.felix.dependencymanager-3.0.0</connection>
     <developerConnection>scm:svn:https://svn.apache.org/repos/asf/felix/releases/org.apache.felix.dependencymanager-3.0.0</developerConnection>
diff --git a/dependencymanager/src/test/java/org/apache/felix/dependencymanager/test/ComponentLifeCycleTest.java b/dependencymanager/src/test/java/org/apache/felix/dependencymanager/test/ComponentLifeCycleTest.java
new file mode 100644
index 0000000..0a57d73
--- /dev/null
+++ b/dependencymanager/src/test/java/org/apache/felix/dependencymanager/test/ComponentLifeCycleTest.java
@@ -0,0 +1,95 @@
+package org.apache.felix.dependencymanager.test;
+
+import static org.ops4j.pax.exam.CoreOptions.mavenBundle;
+import static org.ops4j.pax.exam.CoreOptions.options;
+import static org.ops4j.pax.exam.CoreOptions.provision;
+
+import org.apache.felix.dependencymanager.DependencyManager;
+import org.apache.felix.dependencymanager.Logger;
+import org.apache.felix.dependencymanager.Service;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.ops4j.pax.exam.Option;
+import org.ops4j.pax.exam.junit.Configuration;
+import org.ops4j.pax.exam.junit.JUnit4TestRunner;
+import org.osgi.framework.BundleContext;
+
+@RunWith( JUnit4TestRunner.class )
+public class ComponentLifeCycleTest {
+    @Configuration
+    public static Option[] configuration() {
+        return options(
+            provision(
+                mavenBundle().groupId("org.apache.felix").artifactId("org.osgi.compendium").versionAsInProject(),
+                mavenBundle().groupId("org.apache.felix").artifactId("org.apache.felix.dependencymanager").versionAsInProject()
+            )
+        );
+    }    
+    
+    @Test
+    public void testComponentLifeCycleCallbacks(BundleContext context) {
+        DependencyManager m = new DependencyManager(context, new Logger(context));
+        // helper class that ensures certain steps get executed in sequence
+        Ensure e = new Ensure();
+        // create a simple service component
+        Service s = m.createService().setImplementation(new ComponentInstance(e));
+        // add it, and since it has no dependencies, it should be activated immediately
+        m.add(s);
+        // remove it so it gets destroyed
+        m.remove(s);
+        // ensure we executed all steps inside the component instance
+        e.step(6);
+        
+        // helper class that ensures certain steps get executed in sequence
+        e = new Ensure();
+        // create a simple service component
+        s = m.createService().setImplementation(new CustomComponentInstance(e)).setCallbacks("a", "b", "c", "d");
+        // add it, and since it has no dependencies, it should be activated immediately
+        m.add(s);
+        // remove it so it gets destroyed
+        m.remove(s);
+        // ensure we executed all steps inside the component instance
+        e.step(6);
+    }
+    
+}
+
+class ComponentInstance {
+    private final Ensure m_ensure;
+    public ComponentInstance(Ensure e) {
+        m_ensure = e;
+        m_ensure.step(1);
+    }
+    public void init() {
+        m_ensure.step(2);
+    }
+    public void start() {
+        m_ensure.step(3);
+    }
+    public void stop() {
+        m_ensure.step(4);
+    }
+    public void destroy() {
+        m_ensure.step(5);
+    }
+}
+
+class CustomComponentInstance {
+    private final Ensure m_ensure;
+    public CustomComponentInstance(Ensure e) {
+        m_ensure = e;
+        m_ensure.step(1);
+    }
+    public void a() {
+        m_ensure.step(2);
+    }
+    public void b() {
+        m_ensure.step(3);
+    }
+    public void c() {
+        m_ensure.step(4);
+    }
+    public void d() {
+        m_ensure.step(5);
+    }
+}
diff --git a/dependencymanager/src/test/java/org/apache/felix/dependencymanager/test/Ensure.java b/dependencymanager/src/test/java/org/apache/felix/dependencymanager/test/Ensure.java
new file mode 100644
index 0000000..c27e246
--- /dev/null
+++ b/dependencymanager/src/test/java/org/apache/felix/dependencymanager/test/Ensure.java
@@ -0,0 +1,11 @@
+package org.apache.felix.dependencymanager.test;
+
+import junit.framework.Assert;
+
+public class Ensure {
+    int step = 1;
+    public synchronized void step(int nr) {
+        Assert.assertEquals(nr, step);
+        step++;
+    }
+}
diff --git a/dependencymanager/src/test/java/org/apache/felix/dependencymanager/test/ServiceDependencyTest.java b/dependencymanager/src/test/java/org/apache/felix/dependencymanager/test/ServiceDependencyTest.java
new file mode 100644
index 0000000..ddd5af6
--- /dev/null
+++ b/dependencymanager/src/test/java/org/apache/felix/dependencymanager/test/ServiceDependencyTest.java
@@ -0,0 +1,76 @@
+package org.apache.felix.dependencymanager.test;
+
+import static org.ops4j.pax.exam.CoreOptions.mavenBundle;
+import static org.ops4j.pax.exam.CoreOptions.options;
+import static org.ops4j.pax.exam.CoreOptions.provision;
+
+import org.apache.felix.dependencymanager.DependencyManager;
+import org.apache.felix.dependencymanager.Logger;
+import org.apache.felix.dependencymanager.Service;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.ops4j.pax.exam.Option;
+import org.ops4j.pax.exam.junit.Configuration;
+import org.ops4j.pax.exam.junit.JUnit4TestRunner;
+import org.osgi.framework.BundleContext;
+
+@RunWith( JUnit4TestRunner.class )
+public class ServiceDependencyTest {
+    @Configuration
+    public static Option[] configuration() {
+        return options(
+            provision(
+                mavenBundle().groupId("org.apache.felix").artifactId("org.osgi.compendium").versionAsInProject(),
+                mavenBundle().groupId("org.apache.felix").artifactId("org.apache.felix.dependencymanager").versionAsInProject()
+            )
+        );
+    }    
+
+    @Test
+    public void testServiceRegistrationAndConsumption(BundleContext context) {
+        DependencyManager m = new DependencyManager(context, new Logger(context));
+        // helper class that ensures certain steps get executed in sequence
+        Ensure e = new Ensure();
+        // create a service provider and consumer
+        Service sp = m.createService().setImplementation(new ServiceProvider(e)).setInterface(ServiceInterface.class.getName(), null);
+        Service sc = m.createService().setImplementation(new ServiceConsumer(e)).add(m.createServiceDependency().setService(ServiceInterface.class).setRequired(true));
+        m.add(sp);
+        m.add(sc);
+        m.remove(sp);
+        m.remove(sc);
+        // ensure we executed all steps inside the component instance
+        e.step(4);
+    }
+}
+
+interface ServiceInterface {
+    public void invoke();
+}
+
+class ServiceProvider implements ServiceInterface {
+    private final Ensure m_ensure;
+    public ServiceProvider(Ensure e) {
+        m_ensure = e;
+    }
+    public void invoke() {
+        m_ensure.step(2);
+    }
+}
+
+class ServiceConsumer {
+    private volatile ServiceInterface m_service;
+    private final Ensure m_ensure;
+
+    public ServiceConsumer(Ensure e) {
+        m_ensure = e;
+    }
+    
+    public void start() {
+        m_ensure.step(1);
+        m_service.invoke();
+    }
+    
+    public void stop() {
+        m_ensure.step(3);
+    }
+}