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 2008/12/01 18:06:13 UTC
svn commit: r722135 [2/4] - in /felix/trunk/ipojo/examples/junit4osgi: ./
felix-command/
felix-command/src/main/java/org/apache/felix/ipojo/junit4osgi/command/
immediate-launcher/
immediate-launcher/src/main/java/org/apache/felix/ipojo/junit4osgi/comma...
Modified: felix/trunk/ipojo/examples/junit4osgi/junit4osgi/src/main/java/org/apache/felix/ipojo/junit4osgi/OSGiTestCase.java
URL: http://svn.apache.org/viewvc/felix/trunk/ipojo/examples/junit4osgi/junit4osgi/src/main/java/org/apache/felix/ipojo/junit4osgi/OSGiTestCase.java?rev=722135&r1=722134&r2=722135&view=diff
==============================================================================
--- felix/trunk/ipojo/examples/junit4osgi/junit4osgi/src/main/java/org/apache/felix/ipojo/junit4osgi/OSGiTestCase.java (original)
+++ felix/trunk/ipojo/examples/junit4osgi/junit4osgi/src/main/java/org/apache/felix/ipojo/junit4osgi/OSGiTestCase.java Mon Dec 1 09:06:11 2008
@@ -29,441 +29,538 @@
import org.osgi.framework.ServiceReference;
/**
- * OSGi Test Case. Allow the injection of the bundle context.
+ * OSGi Test Case.
+ * Allows the injection of the bundle context.
*
* @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a>
*/
public class OSGiTestCase extends TestCase {
- protected BundleContext context;
-
-
- private List references = new ArrayList();
-
+ /**
+ * The bundle context.
+ */
+ protected BundleContext context;
+
+ /**
+ * List of get references.
+ */
+ private List m_references = new ArrayList();
+
+ /**
+ * List of helpers.
+ */
+ private List m_helpers = new ArrayList();
+
+ /**
+ * Gets the Bundle Context.
+ * @return the bundle context.
+ */
+ public BundleContext getContext() {
+ return context;
+ }
+
+ /**
+ * Add an helper.
+ * This method is called by the {@link Helper#Helper(OSGiTestCase)}
+ * method.
+ * @param helper the helper object.
+ */
+ public void addHelper(Helper helper) {
+ m_helpers.add(helper);
+ }
+
/**
* Extends runBare to release (unget) services after the teardown.
* @throws Throwable when an error occurs.
* @see junit.framework.TestCase#runBare()
*/
public void runBare() throws Throwable {
- super.runBare();
- // Unget services
- for (int i = 0; i < references.size(); i++) {
- context.ungetService((ServiceReference) references.get(i));
- }
- references.clear();
- }
-
- public void setBundleContext(BundleContext bc) {
- context = bc;
- }
-
- public BundleContext getBundleContext() {
- return context;
- }
-
- public static void assertContains(String message, String[] array, String txt) {
- for (int i = 0; i < array.length; i++) {
- if (array[i].equals(txt)) {
- return;
- }
- }
- fail(formatContainsMessage(message, array, txt));
- }
-
- public static void assertContains(String message, byte[] array, int txt) {
- for (int i = 0; i < array.length; i++) {
- if (array[i] == txt) {
- return;
- }
- }
- Byte[] bytes = new Byte[array.length];
- for (int i = 0; i < array.length; i++) {
- bytes[i] = new Byte(array[i]);
- }
- fail(formatContainsMessage(message, bytes, new Integer(txt)));
- }
-
- public static void assertContains(String message, short[] array, int txt) {
- for (int i = 0; i < array.length; i++) {
- if (array[i] == txt) {
- return;
- }
- }
- Short[] bytes = new Short[array.length];
- for (int i = 0; i < array.length; i++) {
- bytes[i] = new Short(array[i]);
- }
- fail(formatContainsMessage(message, bytes, new Integer(txt)));
- }
-
- public static void assertContains(String message, int[] array, int txt) {
- for (int i = 0; i < array.length; i++) {
- if (array[i] == txt) {
- return;
- }
- }
- Integer[] bytes = new Integer[array.length];
- for (int i = 0; i < array.length; i++) {
- bytes[i] = new Integer(array[i]);
- }
- fail(formatContainsMessage(message, bytes, new Integer(txt)));
- }
-
- public static void assertContains(String message, long[] array, long txt) {
- for (int i = 0; i < array.length; i++) {
- if (array[i] == txt) {
- return;
- }
- }
- Long[] bytes = new Long[array.length];
- for (int i = 0; i < array.length; i++) {
- bytes[i] = new Long(array[i]);
- }
- fail(formatContainsMessage(message, bytes, new Long(txt)));
- }
-
- public static void assertContains(String message, float[] array, float txt) {
- for (int i = 0; i < array.length; i++) {
- if (array[i] == txt) {
- return;
- }
- }
- Float[] bytes = new Float[array.length];
- for (int i = 0; i < array.length; i++) {
- bytes[i] = new Float(array[i]);
- }
- fail(formatContainsMessage(message, bytes, new Float(txt)));
- }
-
- public static void assertContains(String message, double[] array, double txt) {
- for (int i = 0; i < array.length; i++) {
- if (array[i] == txt) {
- return;
- }
- }
- Double[] bytes = new Double[array.length];
- for (int i = 0; i < array.length; i++) {
- bytes[i] = new Double(array[i]);
- }
- fail(formatContainsMessage(message, bytes, new Double(txt)));
- }
-
- public static void assertContains(String message, char[] array, char txt) {
- for (int i = 0; i < array.length; i++) {
- if (array[i] == txt) {
- return;
- }
- }
- Character[] bytes = new Character[array.length];
- for (int i = 0; i < array.length; i++) {
- bytes[i] = new Character(array[i]);
- }
- fail(formatContainsMessage(message, bytes, new Character(txt)));
- }
-
- /**
- * Asserts that two doubles are equal. If they are not an
- * AssertionFailedError is thrown with the given message.
- */
- public static void assertEquals(String message, double expected,
- double actual) {
- if (expected != actual) {
- fail(formatEqualsMessage(message, new Double(expected), new Double(
- actual)));
- }
- }
-
- public static void assertNotEquals(String message, Object o1, Object o2) {
- if (o1.equals(o2)) {
- fail(formatNotEqualsMessage(message, o1, o2));
- }
- }
-
- public static boolean contains(String string, String[] array) {
- for (int i = 0; array != null && i < array.length; i++) {
- if (array[i] != null && array[i].equals(string)) {
- return true;
- }
- }
- return false;
- }
-
- public static boolean contains(int value, int[] array) {
- for (int i = 0; array != null && i < array.length; i++) {
- if (array[i] == value) {
- return true;
- }
- }
- return false;
- }
-
- private static String formatEqualsMessage(String message, Object expected,
- Object actual) {
- String formatted = "";
- if (message != null) {
- formatted = message + " ";
- }
- return formatted + "expected:<" + expected + "> but was:<" + actual
- + ">";
- }
-
- private static String formatNotEqualsMessage(String message, Object o1,
- Object o2) {
- String formatted = "";
- if (message != null) {
- formatted = message + " ";
- }
- return formatted + "o1:<" + o1 + "> is equals to o2:<" + o2 + ">";
- }
-
- private static String formatContainsMessage(String message, Object[] array,
- Object txt) {
- String formatted = "";
- if (message != null) {
- formatted = message + " ";
- }
-
- String arr = null;
- for (int i = 0; i < array.length; i++) {
- if (arr == null) {
- arr = "[" + array[i];
- } else {
- arr += "," + array[i];
- }
- }
- arr += "]";
-
- return formatted + "array:" + arr + " does not contains:<" + txt + ">";
- }
-
-
-
- /**
- * 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 in 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 in 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 in 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) {
+ setUp();
+ try {
+ runTest();
+ } finally {
+ tearDown();
+ // Stop Helpers
+ for (int i = 0; i < m_helpers.size(); i++) {
+ ((Helper) m_helpers.get(i)).dispose();
+ }
+ // Unget services
+ for (int i = 0; i < m_references.size(); i++) {
+ context.ungetService((ServiceReference) m_references.get(i));
+ }
+ m_references.clear();
+ }
+
+ }
+
+ public void setBundleContext(BundleContext bc) {
+ context = bc;
+ }
+
+ public BundleContext getBundleContext() {
+ return context;
+ }
+
+ /**
+ * Checks that the given string is contained in the given array.
+ * @param message the assert point message
+ * @param array the String array
+ * @param txt the String to search
+ */
+ public static void assertContains(String message, String[] array, String txt) {
+ for (int i = 0; i < array.length; i++) {
+ if (array[i].equals(txt)) {
+ return;
+ }
+ }
+ fail(formatContainsMessage(message, array, txt));
+ }
+
+ /**
+ * Checks that the given integer is contained in the given array.
+ * @param message the assert point message
+ * @param array the byte array
+ * @param num the number to search
+ */
+ public static void assertContains(String message, byte[] array, int num) {
+ for (int i = 0; i < array.length; i++) {
+ if (array[i] == num) {
+ return;
+ }
+ }
+ Byte[] bytes = new Byte[array.length];
+ for (int i = 0; i < array.length; i++) {
+ bytes[i] = new Byte(array[i]);
+ }
+ fail(formatContainsMessage(message, bytes, new Integer(num)));
+ }
+
+ /**
+ * Checks that the given integer is contained in the given array.
+ * @param message the assert point message
+ * @param array the short array
+ * @param num the number to search
+ */
+ public static void assertContains(String message, short[] array, int num) {
+ for (int i = 0; i < array.length; i++) {
+ if (array[i] == num) {
+ return;
+ }
+ }
+ Short[] bytes = new Short[array.length];
+ for (int i = 0; i < array.length; i++) {
+ bytes[i] = new Short(array[i]);
+ }
+ fail(formatContainsMessage(message, bytes, new Integer(num)));
+ }
+
+ /**
+ * Checks that the given integer is contained in the given array.
+ * @param message the assert point message
+ * @param array the integer array
+ * @param num the number to search
+ */
+ public static void assertContains(String message, int[] array, int num) {
+ for (int i = 0; i < array.length; i++) {
+ if (array[i] == num) {
+ return;
+ }
+ }
+ Integer[] bytes = new Integer[array.length];
+ for (int i = 0; i < array.length; i++) {
+ bytes[i] = new Integer(array[i]);
+ }
+ fail(formatContainsMessage(message, bytes, new Integer(num)));
+ }
+
+ /**
+ * Checks that the given long is contained in the given array.
+ * @param message the assert point message
+ * @param array the long array
+ * @param num the number to search
+ */
+ public static void assertContains(String message, long[] array, long num) {
+ for (int i = 0; i < array.length; i++) {
+ if (array[i] == num) {
+ return;
+ }
+ }
+ Long[] bytes = new Long[array.length];
+ for (int i = 0; i < array.length; i++) {
+ bytes[i] = new Long(array[i]);
+ }
+ fail(formatContainsMessage(message, bytes, new Long(num)));
+ }
+
+ /**
+ * Checks that the given float is contained in the given array.
+ * @param message the assert point message
+ * @param array the float array
+ * @param num the number to search
+ */
+ public static void assertContains(String message, float[] array, float num) {
+ for (int i = 0; i < array.length; i++) {
+ if (array[i] == num) {
+ return;
+ }
+ }
+ Float[] bytes = new Float[array.length];
+ for (int i = 0; i < array.length; i++) {
+ bytes[i] = new Float(array[i]);
+ }
+ fail(formatContainsMessage(message, bytes, new Float(num)));
+ }
+
+ /**
+ * Checks that the given double is contained in the given array.
+ * @param message the assert point message
+ * @param array the double array
+ * @param num the number to search
+ */
+ public static void assertContains(String message, double[] array, double num) {
+ for (int i = 0; i < array.length; i++) {
+ if (array[i] == num) {
+ return;
+ }
+ }
+ Double[] bytes = new Double[array.length];
+ for (int i = 0; i < array.length; i++) {
+ bytes[i] = new Double(array[i]);
+ }
+ fail(formatContainsMessage(message, bytes, new Double(num)));
+ }
+
+ /**
+ * Checks that the given character is contained in the given array.
+ * @param message the assert point message
+ * @param array the character array
+ * @param character the character to search
+ */
+ public static void assertContains(String message, char[] array, char character) {
+ for (int i = 0; i < array.length; i++) {
+ if (array[i] == character) {
+ return;
+ }
+ }
+ Character[] bytes = new Character[array.length];
+ for (int i = 0; i < array.length; i++) {
+ bytes[i] = new Character(array[i]);
+ }
+ fail(formatContainsMessage(message, bytes, new Character(character)));
+ }
+
+ /**
+ * Asserts that two doubles are equal. If they are not an
+ * AssertionFailedError is thrown with the given message.
+ * @param message the assert point message
+ * @param expected the expected double
+ * @param actual the received double
+ */
+ public static void assertEquals(String message, double expected,
+ double actual) {
+ if (expected != actual) {
+ fail(formatEqualsMessage(message, new Double(expected), new Double(
+ actual)));
+ }
+ }
+
+ /**
+ * Asserts that two objects are not equal. If they are an
+ * AssertionFailedError is thrown with the given message.
+ * @param message the assert point message
+ * @param o1 the unexpected object
+ * @param o2 the received object
+ */
+ public static void assertNotEquals(String message, Object o1, Object o2) {
+ if (o1.equals(o2)) {
+ fail(formatNotEqualsMessage(message, o1, o2));
+ }
+ }
+
+ /**
+ * Checks that the given string is contained in the given array.
+ * @param string the String to search
+ * @param array the String array
+ * @return <code>true</code> if the array contains the string
+ */
+ public static boolean contains(String string, String[] array) {
+ for (int i = 0; array != null && i < array.length; i++) {
+ if (array[i] != null && array[i].equals(string)) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ /**
+ * Checks that the given integer is contained in the given array.
+ * @param value the number to search
+ * @param array the integer array
+ * @return <code>true</code> if the array contains the value
+ */
+ public static boolean contains(int value, int[] array) {
+ for (int i = 0; array != null && i < array.length; i++) {
+ if (array[i] == value) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ /**
+ * Formats a failure message for 'equality' tests.
+ * @param message the assertion point message
+ * @param expected the expected value
+ * @param actual the received value
+ * @return the computed message
+ */
+ private static String formatEqualsMessage(String message, Object expected,
+ Object actual) {
+ String formatted = "";
+ if (message != null) {
+ formatted = message + " ";
+ }
+ return formatted + "expected:<" + expected + "> but was:<" + actual
+ + ">";
+ }
+
+ /**
+ * Formats a failure message for 'un-equality' tests.
+ * @param message the assertion point message
+ * @param o1 the unexpected value
+ * @param o2 the received value
+ * @return the computed message
+ */
+ private static String formatNotEqualsMessage(String message, Object o1,
+ Object o2) {
+ String formatted = "";
+ if (message != null) {
+ formatted = message + " ";
+ }
+ return formatted + "o1:<" + o1 + "> is equals to o2:<" + o2 + ">";
+ }
+
+ /**
+ * Formats a failure message for 'contains' tests.
+ * @param message the assertion point message
+ * @param array the array
+ * @param txt the looked value
+ * @return the computed message
+ */
+ private static String formatContainsMessage(String message, Object[] array,
+ Object txt) {
+ String formatted = "";
+ if (message != null) {
+ formatted = message + " ";
+ }
+
+ String arr = null;
+ for (int i = 0; i < array.length; i++) {
+ if (arr == null) {
+ arr = "[" + array[i];
+ } else {
+ arr += "," + array[i];
+ }
+ }
+ arr += "]";
+
+ return formatted + "array:" + arr + " does not contains:<" + txt + ">";
+ }
+
+ /**
+ * 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.
+ * @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 reference of the service provided by the specified
- * bundle, offering the specified interface and having the given persistent
- * ID.
- *
- * @param bundle
- * the bundle in 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 in 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);
+ /**
+ * 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) {
- references.add(ref);
+ m_references.add(ref);
return context.getService(ref);
} else {
return null;
}
- }
-
-
- /**
- * Returns the service object associated with this service
- * reference.
+ }
+
+ /**
+ * Returns the service object associated with this service reference.
*
- * @param ref
- * service reference
+ * @param ref service reference
* @return the service object.
*/
public Object getServiceObject(ServiceReference ref) {
if (ref != null) {
- references.add(ref);
+ 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);
+ /**
+ * 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++) {
- references.add(refs[i]);
+ m_references.add(refs[i]);
list[i] = context.getService(refs[i]);
}
return list;
@@ -472,69 +569,59 @@
}
}
- /**
- * 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.
+ /**
+ * 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);
- }
-
-
+ /**
+ * 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);
+ }
}
Modified: felix/trunk/ipojo/examples/junit4osgi/junit4osgi/src/main/java/org/apache/felix/ipojo/junit4osgi/OSGiTestSuite.java
URL: http://svn.apache.org/viewvc/felix/trunk/ipojo/examples/junit4osgi/junit4osgi/src/main/java/org/apache/felix/ipojo/junit4osgi/OSGiTestSuite.java?rev=722135&r1=722134&r2=722135&view=diff
==============================================================================
--- felix/trunk/ipojo/examples/junit4osgi/junit4osgi/src/main/java/org/apache/felix/ipojo/junit4osgi/OSGiTestSuite.java (original)
+++ felix/trunk/ipojo/examples/junit4osgi/junit4osgi/src/main/java/org/apache/felix/ipojo/junit4osgi/OSGiTestSuite.java Mon Dec 1 09:06:11 2008
@@ -32,38 +32,71 @@
*/
public class OSGiTestSuite extends TestSuite {
- protected BundleContext context;
+ /**
+ * The bundle context of the bundle containing
+ * the test suite.
+ */
+ protected BundleContext m_context;
+ /**
+ * Creates a OSGiTestSuite.
+ * @param clazz the class
+ * @param bc the bundle context
+ * @see TestSuite#TestSuite(Class)
+ */
public OSGiTestSuite(Class clazz, BundleContext bc) {
super(clazz);
- context = bc;
+ m_context = bc;
}
+ /**
+ * Creates a OSGiTestSuite.
+ * @param bc the bundle context
+ * @see TestSuite#TestSuite()
+ */
public OSGiTestSuite(BundleContext bc) {
super();
- context = bc;
+ m_context = bc;
}
+ /**
+ * Creates a OSGiTestSuite.
+ * @param name the name
+ * @param bc the bundle context
+ * @see TestSuite#TestSuite(String)
+ */
public OSGiTestSuite(String name, BundleContext bc) {
super(name);
- context = bc;
+ m_context = bc;
}
+ /**
+ * Creates a OSGiTestSuite.
+ * @param clazz the class
+ * @param name the name
+ * @param bc the bundle context
+ * @see TestSuite#TestSuite(Class, String)
+ */
public OSGiTestSuite(Class clazz, String name, BundleContext bc) {
super(clazz, name);
- context = bc;
+ m_context = bc;
}
+ /**
+ * Set the bundle context.
+ * @param bc the bundle context to use.
+ */
public void setBundleContext(BundleContext bc) {
- context = bc;
+ m_context = bc;
}
/**
- * Adds the tests from the given class to the suite
+ * Adds the tests from the given class to the suite.
+ * @param testClass the class to add
*/
public void addTestSuite(Class testClass) {
if (OSGiTestCase.class.isAssignableFrom(testClass)) {
- addTest(new OSGiTestSuite(testClass, context));
+ addTest(new OSGiTestSuite(testClass, m_context));
} else if (TestCase.class.isAssignableFrom(testClass)) {
addTest(new TestSuite(testClass));
} else {
@@ -71,12 +104,19 @@
}
}
+ /**
+ * Executes the given {@link Test} with the
+ * given {@link TestResult}.
+ * @param test the test
+ * @param result the test result.
+ * @see junit.framework.TestSuite#runTest(junit.framework.Test, junit.framework.TestResult)
+ */
public void runTest(Test test, TestResult result) {
if (test instanceof OSGiTestSuite) {
- ((OSGiTestSuite) test).context = context;
+ ((OSGiTestSuite) test).m_context = m_context;
test.run(result);
} else if (test instanceof OSGiTestCase) {
- ((OSGiTestCase) test).context = context;
+ ((OSGiTestCase) test).setBundleContext(m_context);
test.run(result);
} else {
test.run(result);
Modified: felix/trunk/ipojo/examples/junit4osgi/junit4osgi/src/main/java/org/apache/felix/ipojo/junit4osgi/helpers/IPOJOHelper.java
URL: http://svn.apache.org/viewvc/felix/trunk/ipojo/examples/junit4osgi/junit4osgi/src/main/java/org/apache/felix/ipojo/junit4osgi/helpers/IPOJOHelper.java?rev=722135&r1=722134&r2=722135&view=diff
==============================================================================
--- felix/trunk/ipojo/examples/junit4osgi/junit4osgi/src/main/java/org/apache/felix/ipojo/junit4osgi/helpers/IPOJOHelper.java (original)
+++ felix/trunk/ipojo/examples/junit4osgi/junit4osgi/src/main/java/org/apache/felix/ipojo/junit4osgi/helpers/IPOJOHelper.java Mon Dec 1 09:06:11 2008
@@ -29,6 +29,7 @@
import org.apache.felix.ipojo.HandlerFactory;
import org.apache.felix.ipojo.ServiceContext;
import org.apache.felix.ipojo.architecture.Architecture;
+import org.apache.felix.ipojo.junit4osgi.Helper;
import org.apache.felix.ipojo.junit4osgi.OSGiTestCase;
import org.apache.felix.ipojo.metadata.Element;
import org.apache.felix.ipojo.parser.ManifestMetadataParser;
@@ -39,47 +40,74 @@
import org.osgi.framework.ServiceReference;
import org.osgi.service.cm.ManagedServiceFactory;
-public class IPOJOHelper {
-
- private BundleContext context;
- private OSGiTestCase testcase;
-
-
- private List instances;
-
+/**
+ * iPOJO Helper.
+ * This helper helps getting {@link Factory}, and managing
+ * {@link ComponentInstance}.
+ * @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a>
+ */
+public class IPOJOHelper extends Helper {
+
+ /**
+ * The bundle context.
+ */
+ private BundleContext m_context;
+ /**
+ * The test case.
+ */
+ private OSGiTestCase m_testcase;
+
+ /**
+ * List of instances.
+ */
+ private List m_instances;
+
+ /**
+ * Creates a IPOJOHelper.
+ * @param tc the OSGi Test Case
+ */
public IPOJOHelper(OSGiTestCase tc) {
- testcase = tc;
- context = testcase.getBundleContext();
- instances = new ArrayList();
+ super(tc);
+ m_testcase = tc;
+ m_context = m_testcase.getBundleContext();
+ m_instances = new ArrayList();
}
-
+
+ /**
+ * Disposes created instances.
+ * @see org.apache.felix.ipojo.junit4osgi.Helper#dispose()
+ */
public void dispose() {
- for (int i = 0; i < instances.size(); i++) {
- ((ComponentInstance) instances.get(i)).dispose();
+ for (int i = 0; i < m_instances.size(); i++) {
+ ((ComponentInstance) m_instances.get(i)).dispose();
}
- instances.clear();
+ m_instances.clear();
}
-
+
+ /**
+ * Gets a created instance from the instance name.
+ * @param name the instance name.
+ * @return the created {@link ComponentInstance} or <code>null</code>
+ * if the instance was not created during the session.
+ */
public ComponentInstance getInstanceByName(String name) {
- for (int i = 0; i < instances.size(); i++) {
- if (((ComponentInstance) instances.get(i)).getInstanceName().equals(name)) {
- return (ComponentInstance) instances.get(i);
+ for (int i = 0; i < m_instances.size(); i++) {
+ if (((ComponentInstance) m_instances.get(i)).getInstanceName()
+ .equals(name)) {
+ return (ComponentInstance) m_instances.get(i);
}
}
return null;
}
-
+
/**
* Creates a new component instance with the given name (and empty
* configuration), from the factory specified in the given bundle.
*
- * @param bundle
- * the bundle in which the component factory is defined.
- * @param factoryName
- * the name of the component factory, defined in the specified
- * bundle.
- * @param instanceName
- * the name of the component instance to create.
+ * @param bundle the bundle from which the component factory is defined.
+ * @param factoryName the name of the component factory, defined in the
+ * specified bundle.
+ * @param instanceName the name of the component instance to create.
* @return the newly created component instance.
*/
public static ComponentInstance createComponentInstance(Bundle bundle,
@@ -96,13 +124,11 @@
* Creates a new component instance with the given configuration, from the
* factory specified in the given bundle.
*
- * @param bundle
- * the bundle in which the component factory is defined.
- * @param factoryName
- * the name of the component factory, defined in the specified
- * bundle.
- * @param configuration
- * the configuration of the component instance to create.
+ * @param bundle the bundle from which the component factory is defined.
+ * @param factoryName the name of the component factory, defined in the
+ * specified bundle.
+ * @param configuration the configuration of the component instance to
+ * create.
* @return the newly created component instance.
*/
public static ComponentInstance createComponentInstance(Bundle bundle,
@@ -123,8 +149,8 @@
return fact.createComponentInstance(configuration);
} catch (Exception e) {
throw new IllegalArgumentException(
- "Cannot create the component instance with the given configuration:" +
- e.getMessage());
+ "Cannot create the component instance with the given configuration:"
+ + e.getMessage());
}
}
@@ -132,15 +158,11 @@
* Creates a new component instance with the given name and configuration,
* from the factory specified in the given bundle.
*
- * @param bundle
- * the bundle in which the component factory is defined.
- * @param factoryName
- * the name of the component factory, defined in the specified
- * bundle.
- * @param instanceName
- * the name of the component instance to create.
- * @param configuration
- * the configuration of the instance to create.
+ * @param bundle the bundle from which the component factory is defined.
+ * @param factoryName the name of the component factory, defined in the
+ * specified bundle.
+ * @param instanceName the name of the component instance to create.
+ * @param configuration the configuration of the instance to create.
* @return the newly created component instance.
*/
public static ComponentInstance createComponentInstance(Bundle bundle,
@@ -156,14 +178,11 @@
* Creates a new component instance with the given name (and an empty
* configuration), from the factory specified in the given service context.
*
- * @param serviceContext
- * the service context in which the component factory service is
- * registered.
- * @param factoryName
- * the name of the component factory, defined in the specified
- * service context.
- * @param instanceName
- * the name of the component instance to create.
+ * @param serviceContext the service context in which the component factory
+ * service is registered.
+ * @param factoryName the name of the component factory, defined in the
+ * specified service context.
+ * @param instanceName the name of the component instance to create.
* @return the newly created component instance.
*/
public static ComponentInstance createComponentInstance(
@@ -182,14 +201,11 @@
* Creates a new component instance with the given name and configuration,
* from the factory specified in the given service context.
*
- * @param serviceContext
- * the service context in which the component factory service is
- * registered.
- * @param factoryName
- * the name of the component factory, defined in the specified
- * service context.
- * @param configuration
- * the configuration of the instance to create.
+ * @param serviceContext the service context in which the component factory
+ * service is registered.
+ * @param factoryName the name of the component factory, defined in the
+ * specified service context.
+ * @param configuration the configuration of the instance to create.
* @return the newly created component instance.
*/
public static ComponentInstance createComponentInstance(
@@ -210,8 +226,8 @@
return fact.createComponentInstance(configuration);
} catch (Exception e) {
throw new IllegalArgumentException(
- "Cannot create the component instance with the given configuration: " +
- e.getMessage());
+ "Cannot create the component instance with the given configuration: "
+ + e.getMessage());
}
}
@@ -219,16 +235,12 @@
* Creates a new component instance with the given name and configuration,
* from the factory specified in the given service context.
*
- * @param serviceContext
- * the service context in which the component factory service is
- * registered.
- * @param factoryName
- * the name of the component factory, defined in the specified
- * service context.
- * @param instanceName
- * the name of the component instance to create.
- * @param configuration
- * the configuration of the instance to create.
+ * @param serviceContext the service context in which the component factory
+ * service is registered.
+ * @param factoryName the name of the component factory, defined in the
+ * specified service context.
+ * @param instanceName the name of the component instance to create.
+ * @param configuration the configuration of the instance to create.
* @return the newly created component instance.
*/
public static ComponentInstance createComponentInstance(
@@ -241,23 +253,21 @@
return createComponentInstance(serviceContext, factoryName,
configuration);
}
-
+
/**
* Creates a new component instance with the given name (and empty
* configuration), from the factory specified in the local bundle.
*
- * @param factoryName
- * the name of the component factory, defined in the local
- * bundle.
- * @param instanceName
- * the name of the component instance to create.
+ * @param factoryName the name of the component factory, defined in the
+ * local bundle.
+ * @param instanceName the name of the component instance to create.
* @return the newly created component instance.
*/
public ComponentInstance createComponentInstance(String factoryName,
String instanceName) {
- ComponentInstance ci = createComponentInstance(context.getBundle(), factoryName,
- instanceName);
- instances.add(ci);
+ ComponentInstance ci = createComponentInstance(m_context.getBundle(),
+ factoryName, instanceName);
+ m_instances.add(ci);
return ci;
}
@@ -265,32 +275,32 @@
* Creates a new component instance with the given configuration, from the
* factory specified in the local bundle.
*
- * @param factoryName
- * the name of the component factory, in the local bundle.
- * @param configuration
- * the configuration of the component instance to create.
+ * @param factoryName the name of the component factory, in the local
+ * bundle.
+ * @param configuration the configuration of the component instance to
+ * create.
* @return the newly created component instance.
*/
public ComponentInstance createComponentInstance(String factoryName,
Dictionary configuration) {
- ComponentInstance ci = createComponentInstance(context.getBundle(), factoryName,
- configuration);
- instances.add(ci);
+ ComponentInstance ci = createComponentInstance(m_context.getBundle(),
+ factoryName, configuration);
+ m_instances.add(ci);
return ci;
}
-
+
/**
- * Creates a new component instance with no configuration, from the
- * factory specified in the local bundle.
+ * Creates a new component instance with no configuration, from the factory
+ * specified in the local bundle.
*
- * @param factoryName
- * the name of the component factory, in the local bundle.
+ * @param factoryName the name of the component factory, in the local
+ * bundle.
* @return the newly created component instance.
*/
public ComponentInstance createComponentInstance(String factoryName) {
- ComponentInstance ci = createComponentInstance(context.getBundle(), factoryName,
- (Dictionary) null);
- instances.add(ci);
+ ComponentInstance ci = createComponentInstance(m_context.getBundle(),
+ factoryName, (Dictionary) null);
+ m_instances.add(ci);
return ci;
}
@@ -298,68 +308,58 @@
* Creates a new component instance with the given name and configuration,
* from the factory specified in the given bundle.
*
- * @param bundle
- * the bundle in which the component factory is defined.
- * @param factoryName
- * the name of the component factory, defined in the specified
- * bundle.
- * @param instanceName
- * the name of the component instance to create.
- * @param configuration
- * the configuration of the instance to create.
+ * @param factoryName the name of the component factory, defined in the
+ * specified bundle.
+ * @param instanceName the name of the component instance to create.
+ * @param configuration the configuration of the instance to create.
* @return the newly created component instance.
*/
public ComponentInstance createComponentInstance(String factoryName,
String instanceName, Dictionary configuration) {
- ComponentInstance ci = createComponentInstance(context.getBundle(), factoryName,
- instanceName, configuration);
- instances.add(ci);
+ ComponentInstance ci = createComponentInstance(m_context.getBundle(),
+ factoryName, instanceName, configuration);
+ m_instances.add(ci);
return ci;
}
/**
* Returns the component factory with the given name in the local bundle.
*
- * @param factoryName
- * the name of the factory to retrieve.
+ * @param factoryName the name of the factory to retrieve.
* @return the component factory with the given name in the local bundle, or
* {@code null} if not found.
*/
public Factory getFactory(String factoryName) {
- return getFactory(context.getBundle(), factoryName);
+ return getFactory(m_context.getBundle(), factoryName);
}
/**
* Returns the handler factory with the given name in the local bundle.
*
- * @param factoryName
- * the name of the handler factory to retrieve.
+ * @param factoryName the name of the handler factory to retrieve.
* @return the handler factory with the given name in the local bundle, or
* {@code null} if not found.
*/
public HandlerFactory getHandlerFactory(String factoryName) {
- return getHandlerFactory(context.getBundle(), factoryName);
+ return getHandlerFactory(m_context.getBundle(), factoryName);
}
/**
* Returns the metadata description of the component defined in this bundle.
*
- * @param component
- * the name of the locally defined component.
+ * @param component the name of the locally defined component.
* @return the metadata description of the component with the given name,
* defined in this given bundle, or {@code null} if not found.
*/
public Element getMetadata(String component) {
- return getMetadata(context.getBundle(), component);
+ return getMetadata(m_context.getBundle(), component);
}
-
+
/**
* Returns the component factory with the given name in the given bundle.
*
- * @param bundle
- * the bundle in which the component factory is defined.
- * @param factoryName
- * the name of the defined factory.
+ * @param bundle the bundle from which the component factory is defined.
+ * @param factoryName the name of the defined factory.
* @return the component factory with the given name in the given bundle, or
* {@code null} if not found.
*/
@@ -371,7 +371,7 @@
Factory.class.getName(),
"(factory.name=" + factoryName + ")");
if (refs != null) {
- return ((Factory) bundle.getBundleContext().getService(refs[0]));
+ return (Factory) bundle.getBundleContext().getService(refs[0]);
}
// Factory not found...
@@ -379,7 +379,8 @@
} catch (InvalidSyntaxException e) {
throw new IllegalArgumentException(
- "Cannot get the component factory services: " + e.getMessage());
+ "Cannot get the component factory services: "
+ + e.getMessage());
}
}
@@ -387,10 +388,9 @@
* Returns the component factory with the given name, registered in the
* given service context.
*
- * @param serviceContext
- * the service context in which the factory service is defined.
- * @param factoryName
- * the name of the factory.
+ * @param serviceContext the service context in which the factory service is
+ * defined.
+ * @param factoryName the name of the factory.
* @return the component factory with the given name, registered in the
* given service context.
*/
@@ -403,7 +403,7 @@
refs = serviceContext.getServiceReferences(Factory.class.getName(),
"(factory.name=" + factoryName + ")");
if (refs != null) {
- return ((Factory) serviceContext.getService(refs[0]));
+ return (Factory) serviceContext.getService(refs[0]);
}
return null;
@@ -417,10 +417,8 @@
/**
* Returns the handler factory with the given name in the given bundle.
*
- * @param bundle
- * the bundle in which the handler factory is defined.
- * @param factoryName
- * the name of the handler factory to retrieve.
+ * @param bundle the bundle from which the handler factory is defined.
+ * @param factoryName the name of the handler factory to retrieve.
* @return the handler factory with the given name in the given bundle, or
* {@code null} if not found.
*/
@@ -442,7 +440,8 @@
return null;
} catch (InvalidSyntaxException e) {
throw new IllegalArgumentException(
- "Cannot get the handler factory services: " + e.getMessage());
+ "Cannot get the handler factory services: "
+ + e.getMessage());
}
}
@@ -450,10 +449,8 @@
* Returns the metadata description of the component with the given name,
* defined in the given bundle.
*
- * @param bundle
- * the bundle in which the component is defined.
- * @param component
- * the name of the defined component.
+ * @param bundle the bundle from which the component is defined.
+ * @param component the name of the defined component.
* @return the metadata description of the component with the given name,
* defined in the given bundle, or {@code null} if not found.
*/
@@ -493,18 +490,16 @@
+ bundle.getSymbolicName() + "): " + e.getMessage());
}
}
-
+
/**
* Returns the service object of a service registered in the specified
* service context, offering the specified interface and matching the given
* filter.
*
- * @param serviceContext
- * the service context in which the service is searched.
- * @param itf
- * the interface provided by the searched service.
- * @param filter
- * an additional filter (can be {@code null}).
+ * @param serviceContext the service context in 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.
*/
@@ -517,18 +512,15 @@
return null;
}
}
-
+
/**
* Returns the service objects of the services registered in the specified
* service context, offering the specified interface and matching the given
* filter.
*
- * @param serviceContext
- * the service context in which services are searched.
- * @param itf
- * the interface provided by the searched services.
- * @param filter
- * an additional filter (can be {@code null}).
+ * @param serviceContext the service context in 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.
*/
@@ -546,18 +538,15 @@
return new Object[0];
}
}
-
+
/**
* Returns the service reference of a service registered in the specified
* service context, offering the specified interface and matching the given
* filter.
*
- * @param serviceContext
- * the service context in which services are searched.
- * @param itf
- * the interface provided by the searched service.
- * @param filter
- * an additional filter (can be {@code null}).
+ * @param serviceContext the service context in which services are searched.
+ * @param itf the interface provided by the searched service.
+ * @param filter an additional filter (can be {@code null}).
* @return a service reference registered in the specified service context,
* offering the specified interface and matching the given filter.
* If no service is found, {@code null} is returned.
@@ -573,18 +562,15 @@
return null;
}
}
-
+
/**
* Returns the service reference of the service registered in the specified
* service context, offering the specified interface and having the given
* persistent ID.
*
- * @param serviceContext
- * the service context in which services are searched.
- * @param itf
- * the interface provided by the searched service.
- * @param pid
- * the persistent ID of the searched service.
+ * @param serviceContext the service context in which services are searched.
+ * @param itf the interface provided by the searched service.
+ * @param pid the persistent ID of the searched service.
* @return a service registered in the specified service context, offering
* the specified interface and having the given persistent ID.
*/
@@ -604,18 +590,15 @@
+ pid);
}
}
-
+
/**
* Returns the service reference of all the services registered in the
* specified service context, offering the specified interface and matching
* the given filter.
*
- * @param serviceContext
- * the service context in which services are searched.
- * @param itf
- * the interface provided by the searched services.
- * @param filter
- * an additional filter (can be {@code null}).
+ * @param serviceContext the service context in 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 registered in the specified service
* context, offering the specified interface and matching the given
* filter. If no service matches, an empty array is returned.
@@ -637,18 +620,14 @@
}
}
-
/**
* Returns the service reference of a service registered in the specified
* service context, offering the specified interface and having the given
* name.
*
- * @param serviceContext
- * the service context in which services are searched.
- * @param itf
- * the interface provided by the searched service.
- * @param name
- * the name of the searched service.
+ * @param serviceContext the service context in which services are searched.
+ * @param itf the interface provided by the searched service.
+ * @param name the name of the searched service.
* @return a service registered in the specified service context, offering
* the specified interface and having the given name.
*/
@@ -665,56 +644,54 @@
}
return getServiceReference(serviceContext, itf, filter);
}
-
+
/**
* Checks the availability of a service inside the given service context.
* @param sc the service context
* @param itf the service interface to found
* @return <code>true</code> if the service is available in the service
- * context, <code>false</code> otherwise.
+ * context, <code>false</code> otherwise.
*/
public static boolean isServiceAvailable(ServiceContext sc, String itf) {
ServiceReference ref = getServiceReference(sc, itf, null);
return ref != null;
}
-
/**
* Checks the availability of a service inside the given service context.
* @param sc the service context
* @param itf the service interface to found
* @param name the service provider name
* @return <code>true</code> if the service is available in the service
- * context, <code>false</code> otherwise.
+ * context, <code>false</code> otherwise.
*/
- public static boolean isServiceAvailableByName(ServiceContext sc, String itf, String name) {
+ public static boolean isServiceAvailableByName(ServiceContext sc,
+ String itf, String name) {
ServiceReference ref = getServiceReferenceByName(sc, itf, name);
return ref != null;
}
-
+
/**
* Checks the availability of a service inside the given service context.
* @param sc the service context
* @param itf the service interface to found
* @param pid the pid of the service
* @return <code>true</code> if the service is available in the service
- * context, <code>false</code> otherwise.
+ * context, <code>false</code> otherwise.
*/
- public static boolean isServiceAvailableByPID(ServiceContext sc, String itf, String pid) {
+ public static boolean isServiceAvailableByPID(ServiceContext sc,
+ String itf, String pid) {
ServiceReference ref = getServiceReferenceByPID(sc, itf, pid);
return ref != null;
}
-
+
/**
* Returns the service reference of a service provided by the specified
* bundle, offering the specified interface and having the given name.
*
- * @param bundle
- * the bundle in which the service is searched.
- * @param itf
- * the interface provided by the searched service.
- * @param name
- * the name of the searched service.
+ * @param bundle the bundle from which the service is searched.
+ * @param itf the interface provided by the searched service.
+ * @param name the name of the searched service.
* @return a service provided by the specified bundle, offering the
* specified interface and having the given name.
*/
@@ -731,33 +708,30 @@
}
return OSGiTestCase.getServiceReference(bundle, itf, filter);
}
-
+
/**
- * Returns the service reference of a service provided by the local
- * bundle, offering the specified interface and having the given name.
+ * Returns the service reference of a service provided by the local bundle,
+ * offering the specified interface and having the given name.
*
- * @param itf
- * the interface provided by the searched service.
- * @param name
- * the name of the searched service.
+ * @param itf the interface provided by the searched service.
+ * @param name the name of the searched service.
* @return a service provided by the specified bundle, offering the
* specified interface and having the given name.
*/
public ServiceReference getServiceReferenceByName(String itf, String name) {
- return getServiceReferenceByName(context.getBundle(), itf, name);
+ return getServiceReferenceByName(m_context.getBundle(), itf, name);
}
-
+
/**
* Checks if the service is available.
* @param itf the service interface
- * @param the service provider name
- * @return <code>true</code> if the service is available,
- * <code>false</code> otherwise.
+ * @param name the service provider name
+ * @return <code>true</code> if the service is available, <code>false</code>
+ * otherwise.
*/
public boolean isServiceAvailableByName(String itf, String name) {
ServiceReference ref = getServiceReferenceByName(itf, name);
return ref != null;
}
-
}
Modified: felix/trunk/ipojo/examples/junit4osgi/junit4osgi/src/main/java/org/apache/felix/ipojo/junit4osgi/impl/JunitExtender.java
URL: http://svn.apache.org/viewvc/felix/trunk/ipojo/examples/junit4osgi/junit4osgi/src/main/java/org/apache/felix/ipojo/junit4osgi/impl/JunitExtender.java?rev=722135&r1=722134&r2=722135&view=diff
==============================================================================
--- felix/trunk/ipojo/examples/junit4osgi/junit4osgi/src/main/java/org/apache/felix/ipojo/junit4osgi/impl/JunitExtender.java (original)
+++ felix/trunk/ipojo/examples/junit4osgi/junit4osgi/src/main/java/org/apache/felix/ipojo/junit4osgi/impl/JunitExtender.java Mon Dec 1 09:06:11 2008
@@ -48,14 +48,34 @@
*/
public class JunitExtender implements OSGiJunitRunner {
+ /**
+ * Suite method name.
+ */
public static final String SUITE_METHODNAME = "suite";
+ /**
+ * List of found suites (Bundle-> List of Class).
+ */
private Map/*<Bundle, List<Class>>*/ m_suites = new HashMap/*<Bundle, List<Class>>*/();
+ /**
+ * The result printer.
+ * By default, prints result on {@link System#out}
+ */
private ResultPrinter m_printer = new ResultPrinter(System.out);
+ /**
+ * The log service used to log messages.
+ * If not provided, a default implementation
+ * printing messages on the console is used.
+ */
private LogService m_log;
+ /**
+ * A new matching bundle arrives.
+ * @param bundle the matching bundle
+ * @param header the looked header value
+ */
void onBundleArrival(Bundle bundle, String header) {
String[] tss = ParseUtils.split(header, ",");
for (int i = 0; i < tss.length; i++) {
@@ -71,6 +91,11 @@
}
}
+ /**
+ * Adds a test suite.
+ * @param bundle the bundle declaring the test suite.
+ * @param test the test class.
+ */
private synchronized void addTestSuite(Bundle bundle, Class/*<? extends Test>*/ test) {
List/*<Class>*/ list = (List) m_suites.get(bundle);
if (list == null) {
@@ -82,19 +107,37 @@
}
}
+ /**
+ * Removes the test suites provided by the given bundles.
+ * @param bundle the leaving bundles.
+ */
private synchronized void removeTestSuites(Bundle bundle) {
List list = (List) m_suites.remove(bundle);
m_log.log(LogService.LOG_INFO, "Unload test suites " + list);
}
+ /**
+ * A matching bundle is leaving.
+ * @param bundle the leaving bundle.
+ */
void onBundleDeparture(Bundle bundle) {
removeTestSuites(bundle);
}
+ /**
+ * Set the result printer.
+ * @param pw the stream to use.
+ * @see org.apache.felix.ipojo.junit4osgi.OSGiJunitRunner#setResultPrinter(java.io.PrintStream)
+ */
public void setResultPrinter(PrintStream pw) {
m_printer = new ResultPrinter(pw);
}
+ /**
+ * Runs tests.
+ * @return the list of {@link TestResult}
+ * @see org.apache.felix.ipojo.junit4osgi.OSGiJunitRunner#run()
+ */
public synchronized List/*<TestResult>*/ run() {
List/*<TestResult>*/ results = new ArrayList/*<TestResult>*/(m_suites.size());
Iterator/*<Entry<Bundle, List<Class>>>*/ it = m_suites.entrySet().iterator();
@@ -103,7 +146,7 @@
Bundle bundle = (Bundle) entry.getKey();
List/*<Class>*/ list = (List) m_suites.get(bundle);
for (int i = 0; i < list.size(); i++) {
- Test test = createTestFromClass((Class)list.get(i), bundle);
+ Test test = createTestFromClass((Class) list.get(i), bundle);
TestResult tr = doRun(test);
results.add(tr);
}
@@ -111,6 +154,11 @@
return results;
}
+ /**
+ * Internal methods executing tests.
+ * @param test the test to execute
+ * @return the result
+ */
private TestResult doRun(Test test) {
TestResult result = new TestResult();
result.addListener(m_printer);
@@ -125,6 +173,15 @@
return result;
}
+ /**
+ * Creates a {@link Test} object from the
+ * given class from the given bundle.
+ * This method creates {@link OSGiTestCase} and
+ * {@link OSGiTestSuite} when required.
+ * @param clazz the class
+ * @param bundle the bundle
+ * @return the resulting Test object.
+ */
private Test createTestFromClass(Class/*<?>*/ clazz, Bundle bundle) {
Method suiteMethod = null;
boolean bc = false;
@@ -171,6 +228,11 @@
return test;
}
+ /**
+ * Gets the list of {@link Test}.
+ * @return the list of {@link Test}
+ * @see org.apache.felix.ipojo.junit4osgi.OSGiJunitRunner#getTests()
+ */
public synchronized List/*<Test>*/ getTests() {
List/*<Test>*/ results = new ArrayList/*<Test>*/();
Iterator/*<Entry<Bundle, List<Class>>>*/ it = m_suites.entrySet().iterator();
@@ -186,10 +248,23 @@
return results;
}
+ /**
+ * Runs the given tests.
+ * @param test the test to execute
+ * @return the result
+ * @see org.apache.felix.ipojo.junit4osgi.OSGiJunitRunner#run(junit.framework.Test)
+ */
public TestResult run(Test test) {
return doRun(test);
}
+ /**
+ * Gets the list of {@link Test} from the bundle (specified
+ * by using the bundle id).
+ * @param bundleId the bundle id
+ * @return the list of {@link Test} declared in this bundle.
+ * @see org.apache.felix.ipojo.junit4osgi.OSGiJunitRunner#getTests(long)
+ */
public synchronized List/*<Test>*/ getTests(long bundleId) {
Iterator/*<Entry<Bundle, List<Class>>>*/ it = m_suites.entrySet().iterator();
while (it.hasNext()) {
@@ -208,6 +283,13 @@
return null;
}
+ /**
+ * Runs the tests declared in the bundle
+ * (specified by the bundle id).
+ * @param bundleId the bundle id
+ * @return the List of {@link TestResult}
+ * @see org.apache.felix.ipojo.junit4osgi.OSGiJunitRunner#run(long)
+ */
public synchronized List/*<TestResult>*/ run(long bundleId) {
Iterator/*<Entry<Bundle, List<Class>>>*/ it = m_suites.entrySet().iterator();
while (it.hasNext()) {
@@ -227,15 +309,29 @@
return null;
}
+ /**
+ * Stop method.
+ * Clears test suites.
+ */
public synchronized void stopping() {
m_log.log(LogService.LOG_INFO, "Cleaning test suites ...");
m_suites.clear();
}
+ /**
+ * Start method.
+ */
public void starting() {
m_log.log(LogService.LOG_INFO, "Junit Extender starting ...");
}
+ /**
+ * Helper method analyzing the {@link Bundle} object
+ * to get the {@link BundleContext} object.
+ * @param bundle the Bundle
+ * @return the BundleContext of <code>null</code> if
+ * the BundleContext cannot be collected.
+ */
private BundleContext getBundleContext(Bundle bundle) {
if (bundle == null) { return null; }
@@ -288,10 +384,10 @@
try {
return (BundleContext) fields[i].get(bundle);
} catch (IllegalArgumentException e) {
- m_log.log(LogService.LOG_ERROR, "Cannot get the BundleContext by invoking " + meth.getName(), e);
+ m_log.log(LogService.LOG_ERROR, "Cannot get the BundleContext by reflecting on " + fields[i].getName(), e);
return null;
} catch (IllegalAccessException e) {
- m_log.log(LogService.LOG_ERROR, "Cannot get the BundleContext by invoking " + meth.getName(), e);
+ m_log.log(LogService.LOG_ERROR, "Cannot get the BundleContext by reflecting on " + fields[i].getName(), e);
return null;
}
}
Modified: felix/trunk/ipojo/examples/junit4osgi/junit4osgi/src/main/java/org/apache/felix/ipojo/junit4osgi/impl/LogServiceImpl.java
URL: http://svn.apache.org/viewvc/felix/trunk/ipojo/examples/junit4osgi/junit4osgi/src/main/java/org/apache/felix/ipojo/junit4osgi/impl/LogServiceImpl.java?rev=722135&r1=722134&r2=722135&view=diff
==============================================================================
--- felix/trunk/ipojo/examples/junit4osgi/junit4osgi/src/main/java/org/apache/felix/ipojo/junit4osgi/impl/LogServiceImpl.java (original)
+++ felix/trunk/ipojo/examples/junit4osgi/junit4osgi/src/main/java/org/apache/felix/ipojo/junit4osgi/impl/LogServiceImpl.java Mon Dec 1 09:06:11 2008
@@ -1,10 +1,39 @@
+/*
+ * 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.junit4osgi.impl;
import org.osgi.framework.ServiceReference;
import org.osgi.service.log.LogService;
+/**
+ * Log Service default implementation.
+ * @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a>
+ */
public class LogServiceImpl implements LogService {
+ /**
+ * Creates a message.
+ * @param level the log level
+ * @param msg the message
+ * @param exception the thrown exception
+ * @return the computed message
+ */
private String computeLogMessage(int level, String msg, Throwable exception) {
String message = null;
switch (level) {
@@ -20,6 +49,8 @@
case LogService.LOG_WARNING:
message = "[WARNING] " + msg;
break;
+ default:
+ break;
}
if (exception != null) {
@@ -29,18 +60,46 @@
return message;
}
+ /**
+ * Logs a message.
+ * @param arg0 the log level
+ * @param arg1 the message
+ * @see org.osgi.service.log.LogService#log(int, java.lang.String)
+ */
public void log(int arg0, String arg1) {
System.err.println(computeLogMessage(arg0, arg1, null));
}
+ /**
+ * Logs a message.
+ * @param arg0 the log level
+ * @param arg1 the message
+ * @param arg2 the thrown exception
+ * @see org.osgi.service.log.LogService#log(int, java.lang.String)
+ */
public void log(int arg0, String arg1, Throwable arg2) {
System.err.println(computeLogMessage(arg0, arg1, arg2));
}
+ /**
+ * Logs a message.
+ * @param arg0 the service reference
+ * @param arg1 the log level
+ * @param arg2 the message
+ * @see org.osgi.service.log.LogService#log(ServiceReference, int, String)
+ */
public void log(ServiceReference arg0, int arg1, String arg2) {
System.err.println(computeLogMessage(arg1, arg2, null));
}
+ /**
+ * Logs a message.
+ * @param arg0 the service reference
+ * @param arg1 the log level
+ * @param arg2 the message
+ * @param arg3 the thrown exception
+ * @see org.osgi.service.log.LogService#log(ServiceReference, int, String, Throwable)
+ */
public void log(ServiceReference arg0, int arg1, String arg2, Throwable arg3) {
System.err.println(computeLogMessage(arg1, arg2, arg3));
}
Modified: felix/trunk/ipojo/examples/junit4osgi/junit4osgi/src/main/java/org/apache/felix/ipojo/junit4osgi/impl/ResultPrinter.java
URL: http://svn.apache.org/viewvc/felix/trunk/ipojo/examples/junit4osgi/junit4osgi/src/main/java/org/apache/felix/ipojo/junit4osgi/impl/ResultPrinter.java?rev=722135&r1=722134&r2=722135&view=diff
==============================================================================
--- felix/trunk/ipojo/examples/junit4osgi/junit4osgi/src/main/java/org/apache/felix/ipojo/junit4osgi/impl/ResultPrinter.java (original)
+++ felix/trunk/ipojo/examples/junit4osgi/junit4osgi/src/main/java/org/apache/felix/ipojo/junit4osgi/impl/ResultPrinter.java Mon Dec 1 09:06:11 2008
@@ -34,18 +34,29 @@
* @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a>
*/
public class ResultPrinter implements TestListener {
- PrintStream fWriter;
+ /**
+ * the writer.
+ */
+ PrintStream m_fWriter;
- int fColumn = 0;
+ /**
+ * The column .
+ */
+ int m_fColumn = 0;
+ /**
+ * Creates a ResultPrinter.
+ * @param writer the printer
+ */
public ResultPrinter(PrintStream writer) {
- fWriter = writer;
+ m_fWriter = writer;
}
- /*
- * API for use by textui.TestRunner
+ /**
+ * Prints the result.
+ * @param result the test result
+ * @param runTime the test duration
*/
-
synchronized void print(TestResult result, long runTime) {
printHeader(runTime);
printErrors(result);
@@ -53,6 +64,9 @@
printFooter(result);
}
+ /**
+ * Prints message wiating for prompt.
+ */
void printWaitPrompt() {
getWriter().println();
getWriter().println("<RETURN> to continue");
@@ -62,46 +76,86 @@
* Internal methods
*/
+ /**
+ * Prints the result header.
+ * @param runTime the test execution duration
+ */
protected void printHeader(long runTime) {
getWriter().println();
getWriter().println("Time: " + elapsedTimeAsString(runTime));
}
+ /**
+ * Prints the errors.
+ * @param result the test result
+ */
protected void printErrors(TestResult result) {
printDefects(result.errors(), result.errorCount(), "error");
}
+ /**
+ * Prints failures.
+ * @param result the test result
+ */
protected void printFailures(TestResult result) {
printDefects(result.failures(), result.failureCount(), "failure");
}
+ /**
+ * Prints failures.
+ * @param booBoos the failures
+ * @param count the number of failures
+ * @param type the type
+ */
protected void printDefects(Enumeration/*<TestFailure>*/ booBoos, int count, String type) {
- if (count == 0)
+ if (count == 0) {
return;
- if (count == 1)
+ }
+
+ if (count == 1) {
getWriter().println("There was " + count + " " + type + ":");
- else
+ } else {
getWriter().println("There were " + count + " " + type + "s:");
+ }
+
for (int i = 1; booBoos.hasMoreElements(); i++) {
printDefect((TestFailure) booBoos.nextElement(), i);
}
}
+ /**
+ * Prints a failure.
+ * @param booBoo the failure
+ * @param count the count
+ */
public void printDefect(TestFailure booBoo, int count) { // only public for testing purposes
printDefectHeader(booBoo, count);
printDefectTrace(booBoo);
}
+ /**
+ * Prints defect header.
+ * @param booBoo the failure
+ * @param count the count
+ */
protected void printDefectHeader(TestFailure booBoo, int count) {
// I feel like making this a println, then adding a line giving the throwable a chance to print something
// before we get to the stack trace.
getWriter().print(count + ") " + booBoo.failedTest());
}
+ /**
+ * Prints the stack trace.
+ * @param booBoo the failure
+ */
protected void printDefectTrace(TestFailure booBoo) {
getWriter().print(BaseTestRunner.getFilteredTrace(booBoo.trace()));
}
+ /**
+ * Prints the footer.
+ * @param result the test result.
+ */
protected void printFooter(TestResult result) {
if (result.wasSuccessful()) {
getWriter().println();
@@ -117,17 +171,22 @@
}
/**
- * Returns the formatted string of the elapsed time. Duplicated from BaseTestRunner. Fix it.
+ * Returns the formatted string of the elapsed time.
+ * @param runTime the elapsed time
+ * @return the elapsed time.
*/
protected String elapsedTimeAsString(long runTime) {
return NumberFormat.getInstance().format((double) runTime / 1000);
}
public PrintStream getWriter() {
- return fWriter;
+ return m_fWriter;
}
/**
+ * Adds an error.
+ * @param test the test in error.
+ * @param t the thrown error
* @see junit.framework.TestListener#addError(Test, Throwable)
*/
public void addError(Test test, Throwable t) {
@@ -135,6 +194,9 @@
}
/**
+ * Adds a failure.
+ * @param test the failing test.
+ * @param t the thrown failure
* @see junit.framework.TestListener#addFailure(Test, AssertionFailedError)
*/
public void addFailure(Test test, AssertionFailedError t) {
@@ -142,19 +204,23 @@
}
/**
+ * A test ends.
+ * (do nothing)
+ * @param test the ending test
* @see junit.framework.TestListener#endTest(Test)
*/
- public void endTest(Test test) {
- }
+ public void endTest(Test test) { }
/**
+ * A test starts.
+ * @param test the starting test
* @see junit.framework.TestListener#startTest(Test)
*/
public void startTest(Test test) {
getWriter().print(".");
- if (fColumn++ >= 40) {
+ if (m_fColumn++ >= 40) {
getWriter().println();
- fColumn = 0;
+ m_fColumn = 0;
}
}