You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@felix.apache.org by cl...@apache.org on 2009/04/15 15:21:30 UTC

svn commit: r765176 [2/2] - in /felix/trunk/ipojo: api/ api/src/main/java/org/apache/felix/ipojo/api/composite/ api/src/test/java/org/apache/felix/ipojo/api/composite/ tests/ tests/api/ tests/api/src/ tests/api/src/main/ tests/api/src/main/java/ tests/...

Added: felix/trunk/ipojo/tests/api/src/test/java/org/apache/felix/ipojo/tests/api/OSGiHelper.java
URL: http://svn.apache.org/viewvc/felix/trunk/ipojo/tests/api/src/test/java/org/apache/felix/ipojo/tests/api/OSGiHelper.java?rev=765176&view=auto
==============================================================================
--- felix/trunk/ipojo/tests/api/src/test/java/org/apache/felix/ipojo/tests/api/OSGiHelper.java (added)
+++ felix/trunk/ipojo/tests/api/src/test/java/org/apache/felix/ipojo/tests/api/OSGiHelper.java Wed Apr 15 13:21:29 2009
@@ -0,0 +1,456 @@
+package org.apache.felix.ipojo.tests.api;
+
+import java.io.InputStream;
+import java.util.ArrayList;
+import java.util.List;
+
+import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.BundleException;
+import org.osgi.framework.InvalidSyntaxException;
+import org.osgi.framework.ServiceReference;
+import org.osgi.service.packageadmin.PackageAdmin;
+
+import static org.junit.Assert.fail;
+
+
+public class OSGiHelper {
+    
+    /**
+     * The bundle context.
+     */
+    private BundleContext context;
+    
+    /**
+     * List of get references.
+     */
+    private List<ServiceReference> m_references = new ArrayList<ServiceReference>();
+    
+    public OSGiHelper(BundleContext context) {
+        this.context = context;
+    }
+    
+    public void dispose() {
+        // Unget services
+        for (int i = 0; i < m_references.size(); i++) {
+            context.ungetService((ServiceReference) m_references.get(i));
+        }
+        m_references.clear();
+    }
+    
+    /**
+     * Gets the Bundle Context.
+     * @return the bundle context.
+     */
+    public BundleContext getContext() {
+        return context;
+    }
+    
+    /**
+     * Returns the service object of a service provided by the specified bundle,
+     * offering the specified interface and matching the given filter.
+     * 
+     * @param bundle the bundle from which the service is searched.
+     * @param itf the interface provided by the searched service.
+     * @param filter an additional filter (can be {@code null}).
+     * @return the service object provided by the specified bundle, offering the
+     *         specified interface and matching the given filter.
+     */
+    public static Object getServiceObject(Bundle bundle, String itf,
+            String filter) {
+        ServiceReference ref = getServiceReference(bundle, itf, filter);
+        if (ref != null) {
+            return bundle.getBundleContext().getService(ref);
+        } else {
+            return null;
+        }
+    }
+
+    /**
+     * Returns the service objects of the services provided by the specified
+     * bundle, offering the specified interface and matching the given filter.
+     * 
+     * @param bundle the bundle from which services are searched.
+     * @param itf the interface provided by the searched services.
+     * @param filter an additional filter (can be {@code null}).
+     * @return the service objects provided by the specified bundle, offering
+     *         the specified interface and matching the given filter.
+     */
+    public static Object[] getServiceObjects(Bundle bundle, String itf,
+            String filter) {
+        ServiceReference[] refs = getServiceReferences(bundle, itf, filter);
+        if (refs != null) {
+            Object[] list = new Object[refs.length];
+            for (int i = 0; i < refs.length; i++) {
+                list[i] = bundle.getBundleContext().getService(refs[i]);
+            }
+            return list;
+        } else {
+            return new Object[0];
+        }
+    }
+
+    /**
+     * Returns the service reference of a service provided by the specified
+     * bundle, offering the specified interface and matching the given filter.
+     * 
+     * @param bundle the bundle from which the service is searched.
+     * @param itf the interface provided by the searched service.
+     * @param filter an additional filter (can be {@code null}).
+     * @return a service reference provided by the specified bundle, offering
+     *         the specified interface and matching the given filter. If no
+     *         service is found, {@code null} is returned.
+     */
+    public static ServiceReference getServiceReference(Bundle bundle,
+            String itf, String filter) {
+        ServiceReference[] refs = getServiceReferences(bundle, itf, filter);
+        if (refs.length != 0) {
+            return refs[0];
+        } else {
+            // No service found
+            return null;
+        }
+    }
+
+    /**
+     * Checks if the service is available.
+     * @param itf the service interface
+     * @return <code>true</code> if the service is available, <code>false</code>
+     *         otherwise.
+     */
+    public boolean isServiceAvailable(String itf) {
+        ServiceReference ref = getServiceReference(itf, null);
+        return ref != null;
+    }
+
+    /**
+     * Checks if the service is available.
+     * @param itf the service interface
+     * @param pid the service pid
+     * @return <code>true</code> if the service is available, <code>false</code>
+     *         otherwise.
+     */
+    public boolean isServiceAvailableByPID(String itf, String pid) {
+        ServiceReference ref = getServiceReferenceByPID(itf, pid);
+        return ref != null;
+    }
+
+    /**
+     * Returns the service reference of the service provided by the specified
+     * bundle, offering the specified interface and having the given persistent
+     * ID.
+     * 
+     * @param bundle the bundle from which the service is searched.
+     * @param itf the interface provided by the searched service.
+     * @param pid the persistent ID of the searched service.
+     * @return a service provided by the specified bundle, offering the
+     *         specified interface and having the given persistent ID.
+     */
+    public static ServiceReference getServiceReferenceByPID(Bundle bundle,
+            String itf, String pid) {
+        String filter = "(" + "service.pid" + "=" + pid + ")";
+        ServiceReference[] refs = getServiceReferences(bundle, itf, filter);
+        if (refs == null) {
+            return null;
+        } else if (refs.length == 1) {
+            return refs[0];
+        } else {
+            throw new IllegalStateException(
+                    "A service lookup by PID returned several providers ("
+                            + refs.length + ")" + " for " + itf + " with pid="
+                            + pid);
+        }
+    }
+
+    /**
+     * Returns the service reference of all the services provided in the
+     * specified bundle, offering the specified interface and matching the given
+     * filter.
+     * 
+     * @param bundle the bundle from which services are searched.
+     * @param itf the interface provided by the searched services.
+     * @param filter an additional filter (can be {@code null}).
+     * @return all the service references provided in the specified bundle,
+     *         offering the specified interface and matching the given filter.
+     *         If no service matches, an empty array is returned.
+     */
+    public static ServiceReference[] getServiceReferences(Bundle bundle,
+            String itf, String filter) {
+        ServiceReference[] refs = null;
+        try {
+            // Get all the service references
+            refs = bundle.getBundleContext().getServiceReferences(itf, filter);
+        } catch (InvalidSyntaxException e) {
+            throw new IllegalArgumentException(
+                    "Cannot get service references: " + e.getMessage());
+        }
+        if (refs == null) {
+            return new ServiceReference[0];
+        } else {
+            return refs;
+        }
+    }
+
+    /**
+     * Returns the service object of a service provided by the local bundle,
+     * offering the specified interface and matching the given filter.
+     * 
+     * @param itf the interface provided by the searched service.
+     * @param filter an additional filter (can be {@code null}).
+     * @return the service object provided by the local bundle, offering the
+     *         specified interface and matching the given filter.
+     */
+    public Object getServiceObject(String itf, String filter) {
+        ServiceReference ref = getServiceReference(itf, filter);
+        if (ref != null) {
+            m_references.add(ref);
+            return context.getService(ref);
+        } else {
+            return null;
+        }
+    }
+
+    /**
+     * Returns the service object associated with this service reference.
+     * 
+     * @param ref service reference
+     * @return the service object.
+     */
+    public Object getServiceObject(ServiceReference ref) {
+        if (ref != null) {
+            m_references.add(ref);
+            return context.getService(ref);
+        } else {
+            return null;
+        }
+    }
+
+    /**
+     * Returns the service objects of the services provided by the local bundle,
+     * offering the specified interface and matching the given filter.
+     * 
+     * @param itf the interface provided by the searched services.
+     * @param filter an additional filter (can be {@code null}).
+     * @return the service objects provided by the local bundle, offering the
+     *         specified interface and matching the given filter.
+     */
+    public Object[] getServiceObjects(String itf, String filter) {
+        ServiceReference[] refs = getServiceReferences(itf, filter);
+        if (refs != null) {
+            Object[] list = new Object[refs.length];
+            for (int i = 0; i < refs.length; i++) {
+                m_references.add(refs[i]);
+                list[i] = context.getService(refs[i]);
+            }
+            return list;
+        } else {
+            return new Object[0];
+        }
+    }
+
+    /**
+     * Returns the service reference of a service provided by the local bundle,
+     * offering the specified interface and matching the given filter.
+     * 
+     * @param itf the interface provided by the searched service.
+     * @param filter an additional filter (can be {@code null}).
+     * @return a service reference provided by the local bundle, offering the
+     *         specified interface and matching the given filter. If no service
+     *         is found, {@code null} is returned.
+     */
+    public ServiceReference getServiceReference(String itf, String filter) {
+        return getServiceReference(context.getBundle(), itf, filter);
+    }
+
+    /**
+     * Returns the service reference of a service provided offering the
+     * specified interface.
+     * 
+     * @param itf the interface provided by the searched service.
+     * @return a service reference provided by the local bundle, offering the
+     *         specified interface and matching the given filter. If no service
+     *         is found, {@code null} is returned.
+     */
+    public ServiceReference getServiceReference(String itf) {
+        return getServiceReference(context.getBundle(), itf, null);
+    }
+
+    /**
+     * Returns the service reference of the service provided by the local
+     * bundle, offering the specified interface and having the given persistent
+     * ID.
+     * 
+     * @param itf the interface provided by the searched service.
+     * @param pid the persistent ID of the searched service.
+     * @return a service provided by the local bundle, offering the specified
+     *         interface and having the given persistent ID.
+     */
+    public ServiceReference getServiceReferenceByPID(String itf, String pid) {
+        return getServiceReferenceByPID(context.getBundle(), itf, pid);
+    }
+
+    /**
+     * Returns the service reference of all the services provided in the local
+     * bundle, offering the specified interface and matching the given filter.
+     * 
+     * @param itf the interface provided by the searched services.
+     * @param filter an additional filter (can be {@code null}).
+     * @return all the service references provided in the local bundle, offering
+     *         the specified interface and matching the given filter. If no
+     *         service matches, an empty array is returned.
+     */
+    public ServiceReference[] getServiceReferences(String itf, String filter) {
+        return getServiceReferences(context.getBundle(), itf, filter);
+    }
+    
+    /**
+     * Gets the package admin exposed by the framework.
+     * Fails if the package admin is not available. 
+     * @return the package admin service.
+     */
+    public PackageAdmin getPackageAdmin() {
+        PackageAdmin pa = (PackageAdmin) getServiceObject(PackageAdmin.class.getName(), null);
+        if (pa == null) {
+            fail("No package admin available");
+        }
+        return pa;
+    }
+    
+    /**
+     * Refresh the packages.
+     * Fails if the package admin service is not available.
+     */
+    public void refresh() {
+        getPackageAdmin().refreshPackages(null);
+    }
+    
+    /**
+     * Waits for a service. Fails on timeout.
+     * If timeout is set to 0, it sets the timeout to 10s.
+     * @param itf the service interface
+     * @param filter  the filter
+     * @param timeout the timeout
+     */
+    public void waitForService(String itf, String filter, long timeout) {
+        if (timeout == 0) {
+            timeout = 10000; // Default 10 secondes.
+        }
+        ServiceReference[] refs = getServiceReferences(itf, filter);
+        long begin = System.currentTimeMillis();
+        if (refs.length != 0) {
+            return;
+        } else {
+            while(refs.length == 0) {
+                try {
+                    Thread.sleep(5);
+                } catch (InterruptedException e) {
+                    // Interrupted
+                }
+                long now = System.currentTimeMillis();
+                
+                if ((now - begin) > timeout) {
+                    fail("Timeout ... no services matching with the request after " + timeout + "ms");
+                }
+                refs = getServiceReferences(itf, filter);
+            }
+        }
+    }
+    
+    
+    /**
+     * Installs a bundle.
+     * Fails if the bundle cannot be installed.
+     * Be aware that you have to uninstall the bundle yourself.
+     * @param url bundle url
+     * @return the installed bundle
+     */
+    public Bundle installBundle(String url) {
+        try {
+            return context.installBundle(url);
+        } catch (BundleException e) {
+            fail("Cannot install the bundle " + url + " : " + e.getMessage());
+        }
+        return null; // Can not happen
+    }
+    
+    /**
+     * Installs a bundle.
+     * Fails if the bundle cannot be installed.
+     * Be aware that you have to uninstall the bundle yourself.
+     * @param url bundle url
+     * @param stream input stream containing the bundle
+     * @return the installed bundle
+     */
+    public Bundle installBundle(String url, InputStream stream) {
+        try {
+            return context.installBundle(url, stream);
+        } catch (BundleException e) {
+            fail("Cannot install the bundle " + url + " : " + e.getMessage());
+        }
+        return null; // Can not happen
+    }
+    
+    /**
+     * Installs and starts a bundle.
+     * Fails if the bundle cannot be installed or an error occurs
+     * during startup. Be aware that you have to uninstall the bundle
+     * yourself.
+     * @param url the bundle url
+     * @return the Bundle object.
+     */
+    public Bundle installAndStart(String url) {
+        Bundle bundle = installBundle(url);
+        try {
+            bundle.start();
+        } catch (BundleException e) {
+           fail("Cannot start the bundle " + url + " : " + e.getMessage());
+        }
+        return bundle;
+    }
+    
+    /**
+     * Installs and starts a bundle.
+     * Fails if the bundle cannot be installed or an error occurs
+     * during startup. Be aware that you have to uninstall the bundle
+     * yourself.
+     * @param url the bundle url
+     * @param stream input stream containing the bundle
+     * @return the Bundle object.
+     */
+    public Bundle installAndStart(String url, InputStream stream) {
+        Bundle bundle = installBundle(url, stream);
+        try {
+            bundle.start();
+        } catch (BundleException e) {
+           fail("Cannot start the bundle " + url + " : " + e.getMessage());
+        }
+        return bundle;
+    }
+    
+    /**
+     * Get the bundle by its id.
+     * @param bundleId the bundle id.
+     * @return the bundle with the given id.
+     */
+    public Bundle getBundle(long bundleId) {
+        return context.getBundle(bundleId);
+    }
+    
+    /**
+     * Gets a bundle by its symbolic name.
+     * Fails if no bundle matches.
+     * @param name the symbolic name of the bundle
+     * @return the bundle object.
+     */
+    public Bundle getBundle(String name) {
+        Bundle[] bundles = context.getBundles();
+        for (int i = 0; i < bundles.length; i++) {
+            if (name.equals(bundles[i].getSymbolicName())) {
+                return bundles[i];
+            }
+        }
+        fail("No bundles with the given symbolic name " + name);
+        return null; // should not happen
+    }
+
+}

Propchange: felix/trunk/ipojo/tests/api/src/test/java/org/apache/felix/ipojo/tests/api/OSGiHelper.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: felix/trunk/ipojo/tests/api/src/test/java/org/apache/felix/ipojo/tests/api/PrimitiveComponentTest.java
URL: http://svn.apache.org/viewvc/felix/trunk/ipojo/tests/api/src/test/java/org/apache/felix/ipojo/tests/api/PrimitiveComponentTest.java?rev=765176&view=auto
==============================================================================
--- felix/trunk/ipojo/tests/api/src/test/java/org/apache/felix/ipojo/tests/api/PrimitiveComponentTest.java (added)
+++ felix/trunk/ipojo/tests/api/src/test/java/org/apache/felix/ipojo/tests/api/PrimitiveComponentTest.java Wed Apr 15 13:21:29 2009
@@ -0,0 +1,176 @@
+package org.apache.felix.ipojo.tests.api;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.CoreMatchers.notNullValue;
+import static org.hamcrest.CoreMatchers.nullValue;
+
+import static org.junit.Assert.assertThat;
+import static org.junit.Assert.fail;
+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.ipojo.ComponentInstance;
+import org.apache.felix.ipojo.api.Dependency;
+import org.apache.felix.ipojo.api.PrimitiveComponentType;
+import org.apache.felix.ipojo.api.Service;
+import org.apache.felix.ipojo.api.SingletonComponentType;
+import org.example.service.Foo;
+import org.example.service.impl.FooImpl;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.ops4j.pax.exam.Inject;
+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;
+import org.osgi.framework.ServiceReference;
+
+
+
+@RunWith( JUnit4TestRunner.class )
+public class PrimitiveComponentTest {
+    
+    @Inject
+    private BundleContext context;
+    
+    private OSGiHelper osgi;
+    
+    private IPOJOHelper ipojo;
+    
+    @Before
+    public void init() {
+        osgi = new OSGiHelper(context);
+        ipojo = new IPOJOHelper(context);
+    }
+    
+    @After
+    public void stop() {
+        ipojo.dispose();
+        osgi.dispose();
+    }
+    
+    @Configuration
+    public static Option[] configure() {    
+        Option[] opt =  options(
+                provision(
+                        mavenBundle().groupId("org.apache.felix").artifactId("org.apache.felix.ipojo").version("1.3.0-SNAPSHOT"),
+                        mavenBundle().groupId("org.apache.felix").artifactId("org.apache.felix.ipojo.api").version("1.3.0-SNAPSHOT")
+                    )
+                );
+        return opt;
+    }
+
+    @Test
+    public void createAServiceProvider()
+    {
+        assertThat( context, is( notNullValue() ) );
+        ComponentInstance ci = null;
+        
+        try {
+            PrimitiveComponentType type = createAProvider();
+            ci = type.createInstance();
+            assertThat("Ci is valid", ci.getState(), is(ComponentInstance.VALID));
+            ServiceReference ref = ipojo.getServiceReferenceByName(Foo.class.getName(), ci.getInstanceName());
+            assertThat( ref, is( notNullValue() ) );
+        } catch (Exception e) {
+            fail(e.getMessage());
+        }
+    }
+    
+    @Test
+    public void killTheFactory()
+    {
+        assertThat( context, is( notNullValue() ) );
+        ComponentInstance ci = null;
+        
+        try {
+            PrimitiveComponentType type = createAProvider();
+            ci = type.createInstance();
+            assertThat("Ci is valid", ci.getState(), is(ComponentInstance.VALID));
+            ServiceReference ref = ipojo.getServiceReferenceByName(Foo.class.getName(), ci.getInstanceName());
+            assertThat( ref, is( notNullValue() ) );
+            type.stop();
+            assertThat("Ci is disposed", ci.getState(), is(ComponentInstance.DISPOSED));
+            ref = ipojo.getServiceReferenceByName(Foo.class.getName(), ci.getInstanceName());
+            assertThat( ref, is( nullValue() ) );
+            
+        } catch (Exception e) {
+            fail(e.getMessage());
+        }
+    }
+    
+    @Test
+    public void createAServiceCons()
+    {
+        assertThat( context, is( notNullValue() ) );
+        ComponentInstance ci = null;
+        
+        try {
+            PrimitiveComponentType type = createAConsumer();
+            ci = type.createInstance();
+            assertThat("Ci is invalid", ci.getState(), is(ComponentInstance.INVALID));
+        } catch (Exception e) {
+            fail(e.getMessage());
+        }
+    }
+    
+    @Test
+    public void createBoth() throws Exception {
+        ComponentInstance cons = createAConsumer().createInstance();
+        // cons is invalid
+        assertThat("cons is invalid", cons.getState(), is(ComponentInstance.INVALID));
+                
+        ComponentInstance prov = createAProvider().createInstance();
+        assertThat("prov is valid", prov.getState(), is(ComponentInstance.VALID));
+        assertThat("cons is valid", cons.getState(), is(ComponentInstance.VALID));
+
+    }
+    
+    @Test
+    public void createTwoCons() throws Exception {
+        ComponentInstance cons1 = createAConsumer().createInstance();
+        // cons is invalid
+        assertThat("cons is invalid", cons1.getState(), is(ComponentInstance.INVALID));
+                
+        ComponentInstance prov = createAProvider().createInstance();
+        assertThat("prov is valid", prov.getState(), is(ComponentInstance.VALID));
+        assertThat("cons is valid", cons1.getState(), is(ComponentInstance.VALID));
+        
+        ComponentInstance cons2 = createAnOptionalConsumer().createInstance();
+     
+        assertThat("cons2 is valid", cons2.getState(), is(ComponentInstance.VALID));
+        
+        prov.stop();
+        assertThat("cons is invalid", cons1.getState(), is(ComponentInstance.INVALID));
+        assertThat("cons2 is valid", cons2.getState(), is(ComponentInstance.VALID));
+    }
+    
+    private PrimitiveComponentType createAProvider() {
+        return new PrimitiveComponentType()
+        .setBundleContext(context)
+        .setClassName(FooImpl.class.getName())
+        .addService(new Service()); // Provide the FooService
+    }
+    
+    private PrimitiveComponentType createAConsumer() {
+        return new SingletonComponentType()
+        .setBundleContext(context)
+        .setClassName(org.example.service.impl.MyComponentImpl.class.getName())
+        .addDependency(new Dependency().setField("myFoo"))
+        .setValidateMethod("start");
+    }
+    
+    private PrimitiveComponentType createAnOptionalConsumer() {
+        return new SingletonComponentType()
+        .setBundleContext(context)
+        .setClassName(org.example.service.impl.MyComponentImpl.class.getName())
+        .addDependency(new Dependency().setField("myFoo").setOptional(true))
+        .setValidateMethod("start");
+    }
+
+
+
+}

Propchange: felix/trunk/ipojo/tests/api/src/test/java/org/apache/felix/ipojo/tests/api/PrimitiveComponentTest.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: felix/trunk/ipojo/tests/api/src/test/java/org/apache/felix/ipojo/tests/api/SingletonComponentTest.java
URL: http://svn.apache.org/viewvc/felix/trunk/ipojo/tests/api/src/test/java/org/apache/felix/ipojo/tests/api/SingletonComponentTest.java?rev=765176&view=auto
==============================================================================
--- felix/trunk/ipojo/tests/api/src/test/java/org/apache/felix/ipojo/tests/api/SingletonComponentTest.java (added)
+++ felix/trunk/ipojo/tests/api/src/test/java/org/apache/felix/ipojo/tests/api/SingletonComponentTest.java Wed Apr 15 13:21:29 2009
@@ -0,0 +1,197 @@
+package org.apache.felix.ipojo.tests.api;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.CoreMatchers.notNullValue;
+import static org.hamcrest.CoreMatchers.nullValue;
+import static org.junit.Assert.assertThat;
+import static org.junit.Assert.fail;
+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.ipojo.ComponentInstance;
+import org.apache.felix.ipojo.api.Dependency;
+import org.apache.felix.ipojo.api.PrimitiveComponentType;
+import org.apache.felix.ipojo.api.Service;
+import org.apache.felix.ipojo.api.SingletonComponentType;
+import org.example.service.Foo;
+import org.example.service.impl.FooImpl;
+import org.example.service.impl.MyComponentImpl;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Ignore;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.ops4j.pax.exam.Inject;
+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;
+import org.osgi.framework.ServiceReference;
+
+
+
+@RunWith( JUnit4TestRunner.class )
+public class SingletonComponentTest {
+    
+    @Inject
+    private BundleContext context;
+    
+    private OSGiHelper osgi;
+    
+    private IPOJOHelper ipojo;
+    
+    @Before
+    public void init() {
+        osgi = new OSGiHelper(context);
+        ipojo = new IPOJOHelper(context);
+    }
+    
+    @After
+    public void stop() {
+        ipojo.dispose();
+        osgi.dispose();
+    }
+    
+    @Configuration
+    public static Option[] configure() {    
+        Option[] opt =  options(
+                provision(
+                        mavenBundle().groupId("org.apache.felix").artifactId("org.apache.felix.ipojo").version("1.3.0-SNAPSHOT"),
+                        mavenBundle().groupId("org.apache.felix").artifactId("org.apache.felix.ipojo.api").version("1.3.0-SNAPSHOT")
+                    )
+                );
+        return opt;
+    }
+
+    @Test
+    public void createAServiceProvider()
+    {
+        assertThat( context, is( notNullValue() ) );
+        ComponentInstance ci = null;
+        
+        try {
+            SingletonComponentType type = createAProvider();
+            ci = type.create();
+            assertThat("Ci is valid", ci.getState(), is(ComponentInstance.VALID));
+            ServiceReference ref = ipojo.getServiceReferenceByName(Foo.class.getName(), ci.getInstanceName());
+            assertThat( ref, is( notNullValue() ) );
+        } catch (Exception e) {
+            fail(e.getMessage());
+        }
+    }
+    
+    @Test
+    public void killTheFactory()
+    {
+        assertThat( context, is( notNullValue() ) );
+        ComponentInstance ci = null;
+        
+        try {
+            SingletonComponentType type = createAProvider();
+            ci = type.create();
+            assertThat("Ci is valid", ci.getState(), is(ComponentInstance.VALID));
+            ServiceReference ref = ipojo.getServiceReferenceByName(Foo.class.getName(), ci.getInstanceName());
+            assertThat( ref, is( notNullValue() ) );
+            type.stop();
+            assertThat("Ci is disposed", ci.getState(), is(ComponentInstance.DISPOSED));
+            ref = ipojo.getServiceReferenceByName(Foo.class.getName(), ci.getInstanceName());
+            assertThat( ref, is( nullValue() ) );
+            
+        } catch (Exception e) {
+            fail(e.getMessage());
+        }
+    }
+    
+    @Test
+    public void createAServiceCons()
+    {
+        assertThat( context, is( notNullValue() ) );
+        ComponentInstance ci = null;
+        
+        try {
+            SingletonComponentType type = createAConsumer();
+            ci = type.create();
+            assertThat("Ci is invalid", ci.getState(), is(ComponentInstance.INVALID));
+        } catch (Exception e) {
+            fail(e.getMessage());
+        }
+    }
+    
+    @Test
+    public void createBoth() throws Exception {
+        ComponentInstance cons = createAConsumer().create();
+        // cons is invalid
+        assertThat("cons is invalid", cons.getState(), is(ComponentInstance.INVALID));
+                
+        ComponentInstance prov = createAProvider().create();
+        assertThat("prov is valid", prov.getState(), is(ComponentInstance.VALID));
+        assertThat("cons is valid", cons.getState(), is(ComponentInstance.VALID));
+
+    }
+    
+    @Test
+    public void createTwoCons() throws Exception {
+        ComponentInstance cons1 = createAConsumer().create();
+        // cons is invalid
+        assertThat("cons is invalid", cons1.getState(), is(ComponentInstance.INVALID));
+                
+        ComponentInstance prov = createAProvider().create();
+        assertThat("prov is valid", prov.getState(), is(ComponentInstance.VALID));
+        assertThat("cons is valid", cons1.getState(), is(ComponentInstance.VALID));
+        
+        ComponentInstance cons2 = createAnOptionalConsumer().create();
+     
+        assertThat("cons2 is valid", cons2.getState(), is(ComponentInstance.VALID));
+        
+        prov.stop();
+        assertThat("cons is invalid", cons1.getState(), is(ComponentInstance.INVALID));
+        assertThat("cons2 is valid", cons2.getState(), is(ComponentInstance.VALID));
+    }
+    
+    @Test
+    @Ignore
+    public void setObject() throws Exception {
+        ComponentInstance cons = createAConsumer().setObject(new MyComponentImpl(5)).create();
+        // cons is invalid
+        assertThat("cons is invalid", cons.getState(), is(ComponentInstance.INVALID));
+                
+        ComponentInstance prov = createAProvider().create();
+        assertThat("prov is valid", prov.getState(), is(ComponentInstance.VALID));
+        assertThat("cons is valid", cons.getState(), is(ComponentInstance.VALID));
+        
+    }
+    
+    private SingletonComponentType createAProvider() {
+        PrimitiveComponentType type =  new SingletonComponentType()
+        .setBundleContext(context)
+        .setClassName(FooImpl.class.getName())
+        .addService(new Service()); // Provide the FooService
+        
+        return (SingletonComponentType) type;
+    }
+    
+    private SingletonComponentType createAConsumer() {
+        PrimitiveComponentType type =  new SingletonComponentType()
+        .setBundleContext(context)
+        .setClassName(org.example.service.impl.MyComponentImpl.class.getName())
+        .addDependency(new Dependency().setField("myFoo"))
+        .setValidateMethod("start");
+        
+        return (SingletonComponentType) type;
+    }
+    
+    private SingletonComponentType createAnOptionalConsumer() {
+        PrimitiveComponentType type =  new SingletonComponentType()
+        .setBundleContext(context)
+        .setClassName(org.example.service.impl.MyComponentImpl.class.getName())
+        .addDependency(new Dependency().setField("myFoo").setOptional(true))
+        .setValidateMethod("start");
+        
+        return (SingletonComponentType) type;
+
+    }
+
+
+
+}

Propchange: felix/trunk/ipojo/tests/api/src/test/java/org/apache/felix/ipojo/tests/api/SingletonComponentTest.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: felix/trunk/ipojo/tests/api/src/test/java/org/example/service/BarService.java
URL: http://svn.apache.org/viewvc/felix/trunk/ipojo/tests/api/src/test/java/org/example/service/BarService.java?rev=765176&view=auto
==============================================================================
--- felix/trunk/ipojo/tests/api/src/test/java/org/example/service/BarService.java (added)
+++ felix/trunk/ipojo/tests/api/src/test/java/org/example/service/BarService.java Wed Apr 15 13:21:29 2009
@@ -0,0 +1,8 @@
+package org.example.service;
+
+public interface BarService {
+    
+    public void doSomethingWithBar();
+    
+
+}

Propchange: felix/trunk/ipojo/tests/api/src/test/java/org/example/service/BarService.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: felix/trunk/ipojo/tests/api/src/test/java/org/example/service/Foo.java
URL: http://svn.apache.org/viewvc/felix/trunk/ipojo/tests/api/src/test/java/org/example/service/Foo.java?rev=765176&view=auto
==============================================================================
--- felix/trunk/ipojo/tests/api/src/test/java/org/example/service/Foo.java (added)
+++ felix/trunk/ipojo/tests/api/src/test/java/org/example/service/Foo.java Wed Apr 15 13:21:29 2009
@@ -0,0 +1,8 @@
+package org.example.service;
+
+public interface Foo {
+    
+    
+    public void doSomething();
+
+}

Propchange: felix/trunk/ipojo/tests/api/src/test/java/org/example/service/Foo.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: felix/trunk/ipojo/tests/api/src/test/java/org/example/service/MyService.java
URL: http://svn.apache.org/viewvc/felix/trunk/ipojo/tests/api/src/test/java/org/example/service/MyService.java?rev=765176&view=auto
==============================================================================
--- felix/trunk/ipojo/tests/api/src/test/java/org/example/service/MyService.java (added)
+++ felix/trunk/ipojo/tests/api/src/test/java/org/example/service/MyService.java Wed Apr 15 13:21:29 2009
@@ -0,0 +1,5 @@
+package org.example.service;
+
+public interface MyService {
+    double compute(double value);
+}

Propchange: felix/trunk/ipojo/tests/api/src/test/java/org/example/service/MyService.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: felix/trunk/ipojo/tests/api/src/test/java/org/example/service/impl/FooImpl.java
URL: http://svn.apache.org/viewvc/felix/trunk/ipojo/tests/api/src/test/java/org/example/service/impl/FooImpl.java?rev=765176&view=auto
==============================================================================
--- felix/trunk/ipojo/tests/api/src/test/java/org/example/service/impl/FooImpl.java (added)
+++ felix/trunk/ipojo/tests/api/src/test/java/org/example/service/impl/FooImpl.java Wed Apr 15 13:21:29 2009
@@ -0,0 +1,25 @@
+package org.example.service.impl;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.example.service.Foo;
+
+public class FooImpl implements Foo {
+    
+   // private List<String> m_list = new ArrayList<String>();
+
+    public void doSomething() {
+       // Do something...
+        System.out.println("Hello World !");
+    }
+    
+    public FooImpl(String s) {
+        _setIM(s);
+    }
+    
+    public void _setIM(String s) {
+        
+    }
+
+}

Propchange: felix/trunk/ipojo/tests/api/src/test/java/org/example/service/impl/FooImpl.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: felix/trunk/ipojo/tests/api/src/test/java/org/example/service/impl/MyComponentImpl.java
URL: http://svn.apache.org/viewvc/felix/trunk/ipojo/tests/api/src/test/java/org/example/service/impl/MyComponentImpl.java?rev=765176&view=auto
==============================================================================
--- felix/trunk/ipojo/tests/api/src/test/java/org/example/service/impl/MyComponentImpl.java (added)
+++ felix/trunk/ipojo/tests/api/src/test/java/org/example/service/impl/MyComponentImpl.java Wed Apr 15 13:21:29 2009
@@ -0,0 +1,26 @@
+package org.example.service.impl;
+
+import org.example.service.Foo;
+
+public class MyComponentImpl {
+    
+    private Foo myFoo;
+    
+    private int anInt;
+    
+    public MyComponentImpl() {
+        anInt = 2;
+    }
+    
+    public MyComponentImpl(int i) {
+        anInt = i;
+    }
+
+    public void start() {
+       myFoo.doSomething();
+       if (anInt > 0) {
+           System.out.println("Set int to " + anInt);
+       }
+    }
+
+}

Propchange: felix/trunk/ipojo/tests/api/src/test/java/org/example/service/impl/MyComponentImpl.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: felix/trunk/ipojo/tests/api/src/test/java/org/example/service/impl/MyServiceImpl.java
URL: http://svn.apache.org/viewvc/felix/trunk/ipojo/tests/api/src/test/java/org/example/service/impl/MyServiceImpl.java?rev=765176&view=auto
==============================================================================
--- felix/trunk/ipojo/tests/api/src/test/java/org/example/service/impl/MyServiceImpl.java (added)
+++ felix/trunk/ipojo/tests/api/src/test/java/org/example/service/impl/MyServiceImpl.java Wed Apr 15 13:21:29 2009
@@ -0,0 +1,9 @@
+package org.example.service.impl;
+
+import org.example.service.MyService;
+
+public class MyServiceImpl implements MyService {
+    public double compute(double value) {
+	return Math.exp(value * Math.cosh(value));
+    }
+}

Propchange: felix/trunk/ipojo/tests/api/src/test/java/org/example/service/impl/MyServiceImpl.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: felix/trunk/ipojo/tests/composite/import-export/src/main/resources/metadata.xml
URL: http://svn.apache.org/viewvc/felix/trunk/ipojo/tests/composite/import-export/src/main/resources/metadata.xml?rev=765176&r1=765175&r2=765176&view=diff
==============================================================================
--- felix/trunk/ipojo/tests/composite/import-export/src/main/resources/metadata.xml (original)
+++ felix/trunk/ipojo/tests/composite/import-export/src/main/resources/metadata.xml Wed Apr 15 13:21:29 2009
@@ -8,38 +8,38 @@
 	<comp:composite name="composite.requires.1" architecture="true">
 		<subservice action="import"
 			specification="org.apache.felix.ipojo.test.composite.service.FooService"
-			scope="comp:composite" />
+			scope="composite" />
 	</comp:composite>
 
 	<comp:composite name="composite.requires.2" architecture="true">
 		<subservice action="import"
 			specification="org.apache.felix.ipojo.test.composite.service.FooService"
-			aggregate="true" scope="comp:composite" />
+			aggregate="true" scope="composite" />
 	</comp:composite>
 
 	<comp:composite name="composite.requires.3" architecture="true">
 		<subservice action="import"
 			specification="org.apache.felix.ipojo.test.composite.service.FooService"
-			optional="true" scope="comp:composite" />
+			optional="true" scope="composite" />
 	</comp:composite>
 
 	<comp:composite name="composite.requires.4" architecture="true">
 		<subservice action="import"
 			specification="org.apache.felix.ipojo.test.composite.service.FooService"
-			optional="true" aggregate="true" scope="comp:composite" />
+			optional="true" aggregate="true" scope="composite" />
 	</comp:composite>
 
 	<comp:composite name="composite.requires.5" architecture="true">
 		<subservice action="import"
 			specification="org.apache.felix.ipojo.test.composite.service.FooService"
-			filter="(&amp;(int=2)(long=40))" scope="comp:composite" />
+			filter="(&amp;(int=2)(long=40))" scope="composite" />
 	</comp:composite>
 
 	<comp:composite name="composite.export.1" architecture="true">
 		<subservice action="import"
 			specification="org.apache.felix.ipojo.test.composite.service.BazService"
 			aggregate="true" optional="true" filter="(!(instance.name=export))"
-			scope="comp:composite" />
+			scope="composite" />
 		<comp:provides action="export"
 			specification="org.apache.felix.ipojo.test.composite.service.BazService" />
 	</comp:composite>
@@ -47,7 +47,7 @@
 	<comp:composite name="composite.export.2" architecture="true">
 		<subservice action="import"
 			specification="org.apache.felix.ipojo.test.composite.service.BazService"
-			scope="comp:composite" aggregate="true" optional="true"
+			scope="composite" aggregate="true" optional="true"
 			filter="(!(instance.name=export))" />
 		<comp:provides action="export"
 			specification="org.apache.felix.ipojo.test.composite.service.BazService"
@@ -57,7 +57,7 @@
 	<comp:composite name="composite.export.3" architecture="true">
 		<subservice action="import"
 			specification="org.apache.felix.ipojo.test.composite.service.BazService"
-			scope="comp:composite" aggregate="true" optional="true"
+			scope="composite" aggregate="true" optional="true"
 			filter="(!(instance.name=export))" />
 		<comp:provides action="export"
 			specification="org.apache.felix.ipojo.test.composite.service.BazService"

Modified: felix/trunk/ipojo/tests/core/service-providing-strategies/src/main/java/org/apache/felix/ipojo/test/scenarios/component/strategies/DummyCreationStrategy.java
URL: http://svn.apache.org/viewvc/felix/trunk/ipojo/tests/core/service-providing-strategies/src/main/java/org/apache/felix/ipojo/test/scenarios/component/strategies/DummyCreationStrategy.java?rev=765176&r1=765175&r2=765176&view=diff
==============================================================================
--- felix/trunk/ipojo/tests/core/service-providing-strategies/src/main/java/org/apache/felix/ipojo/test/scenarios/component/strategies/DummyCreationStrategy.java (original)
+++ felix/trunk/ipojo/tests/core/service-providing-strategies/src/main/java/org/apache/felix/ipojo/test/scenarios/component/strategies/DummyCreationStrategy.java Wed Apr 15 13:21:29 2009
@@ -57,7 +57,7 @@
         
         throw new UnsupportedOperationException("This service requires an advanced creation policy. "
                 + "Before calling the service, call the getService(ComponentInstance) method to get "
-                + "the service object. " + arg1.getName()); // TODO DEBUG
+                + "the service object. " + arg1.getName()); 
     }
 
     /**

Modified: felix/trunk/ipojo/tests/pom.xml
URL: http://svn.apache.org/viewvc/felix/trunk/ipojo/tests/pom.xml?rev=765176&r1=765175&r2=765176&view=diff
==============================================================================
--- felix/trunk/ipojo/tests/pom.xml (original)
+++ felix/trunk/ipojo/tests/pom.xml Wed Apr 15 13:21:29 2009
@@ -49,6 +49,7 @@
 	<module>handler/whiteboard</module>
 	<module>handler/eventadmin</module>
 	<module>core/configadmin</module>
+	<module>api</module>
    </modules>
    
    <profiles>