Bug 510679 - Fix implementation of ScrService and add very basic test

Change-Id: I4f79c6c96accc17fb531e7a7a5289130f77d33d5
Signed-off-by: Thomas Watson <tjwatson@us.ibm.com>
diff --git a/bundles/org.eclipse.equinox.ds.tests/.classpath b/bundles/org.eclipse.equinox.ds.tests/.classpath
index d81e9c0..90ccffe 100644
--- a/bundles/org.eclipse.equinox.ds.tests/.classpath
+++ b/bundles/org.eclipse.equinox.ds.tests/.classpath
@@ -31,5 +31,6 @@
 	<classpathentry kind="src" output="scr_test/tb23" path="bundles_src/tb23"/>
 	<classpathentry kind="src" output="scr_test/tb25" path="bundles_src/tb25"/>
 	<classpathentry kind="src" output="scr_test/tb26" path="bundles_src/tb26"/>
+	<classpathentry kind="src" output="scr_test/tb27" path="bundles_src/tb27"/>
 	<classpathentry kind="output" path="bin"/>
 </classpath>
diff --git a/bundles/org.eclipse.equinox.ds.tests/META-INF/MANIFEST.MF b/bundles/org.eclipse.equinox.ds.tests/META-INF/MANIFEST.MF
index a9d43fb..8bf07ea 100644
--- a/bundles/org.eclipse.equinox.ds.tests/META-INF/MANIFEST.MF
+++ b/bundles/org.eclipse.equinox.ds.tests/META-INF/MANIFEST.MF
@@ -8,6 +8,7 @@
 Bundle-ActivationPolicy: lazy
 Require-Bundle: org.junit
 Import-Package: 
+ org.apache.felix.scr;version="1.6.0",
  org.eclipse.osgi.service.urlconversion;version="1.0.0",
  org.osgi.framework;version="1.3.0",
  org.osgi.service.cm;version="1.2.0",
diff --git a/bundles/org.eclipse.equinox.ds.tests/build.properties b/bundles/org.eclipse.equinox.ds.tests/build.properties
index 55429d9..c7239fb 100644
--- a/bundles/org.eclipse.equinox.ds.tests/build.properties
+++ b/bundles/org.eclipse.equinox.ds.tests/build.properties
@@ -40,6 +40,7 @@
                scr_test/tb24.jar,\
                scr_test/tb25.jar,\
                scr_test/tb26.jar,\
+               scr_test/tb27.jar,\
                test.xml
 jars.compile.order = .,\
                      scr_test/tb1.jar,\
@@ -69,7 +70,8 @@
                      scr_test/tb23.jar,\
                      scr_test/tb24.jar,\
                      scr_test/tb25.jar,\
-                     scr_test/tb26.jar
+                     scr_test/tb26.jar,\
+                     scr_test/tb27.jar
 source.scr_test/tb1.jar = bundles_src/tb1/
 manifest.scr_test/tb1.jar = META-INF/MANIFEST.MF
 source.scr_test/tb1a.jar = bundles_src/tb1a/
@@ -126,3 +128,5 @@
 manifest.scr_test/tb25.jar = META-INF/MANIFEST.MF
 source.scr_test/tb26.jar = bundles_src/tb26/
 manifest.scr_test/tb26.jar = META-INF/MANIFEST.MF
+source.scr_test/tb27.jar = bundles_src/tb27/
+manifest.scr_test/tb27.jar = META-INF/MANIFEST.MF
diff --git a/bundles/org.eclipse.equinox.ds.tests/bundles_src/tb27/META-INF/MANIFEST.MF b/bundles/org.eclipse.equinox.ds.tests/bundles_src/tb27/META-INF/MANIFEST.MF
new file mode 100755
index 0000000..6b98fa5
--- /dev/null
+++ b/bundles/org.eclipse.equinox.ds.tests/bundles_src/tb27/META-INF/MANIFEST.MF
@@ -0,0 +1,5 @@
+Manifest-Version: 1.0
+Bundle-SymbolicName: org.eclipse.equinox.ds.tests.tb27
+Require-Capability: osgi.extender;
+ filter:="(&(osgi.extender=osgi.component)(version>=1.3)(!(version>=2.0)))"
+Service-Component: OSGI-INF/*.xml
diff --git a/bundles/org.eclipse.equinox.ds.tests/bundles_src/tb27/OSGI-INF/test1.xml b/bundles/org.eclipse.equinox.ds.tests/bundles_src/tb27/OSGI-INF/test1.xml
new file mode 100755
index 0000000..58e4424
--- /dev/null
+++ b/bundles/org.eclipse.equinox.ds.tests/bundles_src/tb27/OSGI-INF/test1.xml
@@ -0,0 +1,14 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<root>
+	<scr:component 
+			name="test1"
+			enabled="true"
+			immediate="true"
+			xmlns:scr="http://www.osgi.org/xmlns/scr/v1.3.0">
+		<implementation class="org.eclipse.equinox.ds.tests.tb27.impl.TestService"/>
+		<service>
+			<provide interface="java.util.List"/>
+		</service>
+		<reference name="log" interface="org.osgi.service.log.LogService" />
+	</scr:component>
+</root>
diff --git a/bundles/org.eclipse.equinox.ds.tests/bundles_src/tb27/org/eclipse/equinox/ds/tests/tb27/impl/TestService.java b/bundles/org.eclipse.equinox.ds.tests/bundles_src/tb27/org/eclipse/equinox/ds/tests/tb27/impl/TestService.java
new file mode 100644
index 0000000..ce77a21
--- /dev/null
+++ b/bundles/org.eclipse.equinox.ds.tests/bundles_src/tb27/org/eclipse/equinox/ds/tests/tb27/impl/TestService.java
@@ -0,0 +1,19 @@
+package org.eclipse.equinox.ds.tests.tb27.impl;
+
+import java.util.AbstractCollection;
+import java.util.Collections;
+import java.util.Iterator;
+
+public class TestService<T> extends AbstractCollection<T> {
+
+	@Override
+	public Iterator<T> iterator() {
+		return (Iterator<T>) Collections.emptyList().iterator();
+	}
+
+	@Override
+	public int size() {
+		return 0;
+	}
+
+}
diff --git a/bundles/org.eclipse.equinox.ds.tests/src/org/eclipse/equinox/ds/tests/tbc/DSTest.java b/bundles/org.eclipse.equinox.ds.tests/src/org/eclipse/equinox/ds/tests/tbc/DSTest.java
index b08b45a..7b1f036 100644
--- a/bundles/org.eclipse.equinox.ds.tests/src/org/eclipse/equinox/ds/tests/tbc/DSTest.java
+++ b/bundles/org.eclipse.equinox.ds.tests/src/org/eclipse/equinox/ds/tests/tbc/DSTest.java
@@ -32,6 +32,9 @@
 import java.util.zip.ZipEntry;
 import java.util.zip.ZipInputStream;
 
+import org.apache.felix.scr.Component;
+import org.apache.felix.scr.Reference;
+import org.apache.felix.scr.ScrService;
 import org.eclipse.equinox.ds.tests.BundleInstaller;
 import org.eclipse.equinox.ds.tests.DSTestsActivator;
 import org.junit.Before;
@@ -2897,6 +2900,33 @@
     }
   }
 
+  @SuppressWarnings("deprecation")
+@Test
+  public void testScrService() throws BundleException {
+    Bundle tb27 = installBundle("tb27");
+    ServiceReference<ScrService> ref = getContext().getServiceReference(ScrService.class);
+    ScrService scrService = ref == null ? null : getContext().getService(ref);
+    assertNotNull("No ScrService.", scrService);
+    try {
+      tb27.start();
+      waitBundleStart();
+      Component[] tb27Comps = scrService.getComponents(tb27);
+      assertNotNull("No tb26 components.", tb27Comps);
+      assertEquals("Wrong number of components.", 1, tb27Comps.length);
+      Component test1 = tb27Comps[0];
+      assertEquals("Wrong name.", "test1", test1.getName());
+      Reference[] refs = test1.getReferences();
+      assertNotNull("No references.", refs);
+      assertEquals("Wrong number of references.", 1, refs.length);
+      assertEquals("Wrong ref name.", "log", refs[0].getName());
+      ServiceReference<?>[] services = refs[0].getServiceReferences();
+      assertNotNull("No service references.", services);
+      assertEquals("Wrong number of service refs.", 1, services.length);
+    } finally {
+      uninstallBundle(tb27);
+    }
+  }
+
   /**
    * Asserts that two lists contain equal elements (the order doesn't matter).
    */
diff --git a/bundles/org.eclipse.equinox.ds/src/org/eclipse/equinox/internal/ds/ScrServiceImpl.java b/bundles/org.eclipse.equinox.ds/src/org/eclipse/equinox/internal/ds/ScrServiceImpl.java
index 7f5a97f..22d702d 100644
--- a/bundles/org.eclipse.equinox.ds/src/org/eclipse/equinox/internal/ds/ScrServiceImpl.java
+++ b/bundles/org.eclipse.equinox.ds/src/org/eclipse/equinox/internal/ds/ScrServiceImpl.java
@@ -36,8 +36,14 @@
 	}
 
 	private Component[] toComponents(Collection<ComponentDescriptionDTO> componentDescriptionDTOs) {
-		// TODO Auto-generated method stub
-		return null;
+		Collection<Component> components = new ArrayList<Component>();
+		for (ComponentDescriptionDTO dto : componentDescriptionDTOs) {
+			Component component = toComponent(dto);
+			if (component != null) {
+				components.add(component);
+			}
+		}
+		return components.isEmpty() ? null : components.toArray(new Component[0]);
 	}
 
 	@Override
@@ -52,8 +58,12 @@
 	}
 
 	private Component toComponent(ComponentDescriptionDTO dto) {
-		// TODO Auto-generated method stub
-		return null;
+		Collection<ComponentConfigurationDTO> configs = scr.getComponentConfigurationDTOs(dto);
+		if (configs.isEmpty()) {
+			return null;
+		}
+
+		return new ComponentImpl(configs.iterator().next());
 	}
 
 	@Override
@@ -324,8 +334,8 @@
 			this.unsatisfied = unsatisifed;
 		}
 
-		@SuppressWarnings("rawtypes")
 		@Override
+		@SuppressWarnings("rawtypes")
 		public ServiceReference[] getServiceReferences() {
 			Collection<ServiceReference<?>> serviceReferences = new ArrayList<ServiceReference<?>>();
 			for (ServiceReferenceDTO serviceRefDTO : unsatisfied.targetServices) {