Author: clement
Date: Wed Jan 23 14:25:24 2008
New Revision: 614712
URL: http://svn.apache.org/viewvc?rev=614712&view=rev
Log:
Commit the junit4osgi framework example
Added:
felix/sandbox/clement/ipojo/examples/junit4osgi/ (with props)
felix/sandbox/clement/ipojo/examples/junit4osgi/metadata.xml
felix/sandbox/clement/ipojo/examples/junit4osgi/pom.xml
felix/sandbox/clement/ipojo/examples/junit4osgi/src/
felix/sandbox/clement/ipojo/examples/junit4osgi/src/main/
felix/sandbox/clement/ipojo/examples/junit4osgi/src/main/java/
felix/sandbox/clement/ipojo/examples/junit4osgi/src/main/java/org/
felix/sandbox/clement/ipojo/examples/junit4osgi/src/main/java/org/apache/
felix/sandbox/clement/ipojo/examples/junit4osgi/src/main/java/org/apache/felix/
felix/sandbox/clement/ipojo/examples/junit4osgi/src/main/java/org/apache/felix/ipojo/
felix/sandbox/clement/ipojo/examples/junit4osgi/src/main/java/org/apache/felix/ipojo/junit4osgi/
felix/sandbox/clement/ipojo/examples/junit4osgi/src/main/java/org/apache/felix/ipojo/junit4osgi/OSGiJunitRunner.java
felix/sandbox/clement/ipojo/examples/junit4osgi/src/main/java/org/apache/felix/ipojo/junit4osgi/OSGiTestCase.java
felix/sandbox/clement/ipojo/examples/junit4osgi/src/main/java/org/apache/felix/ipojo/junit4osgi/OSGiTestSuite.java
felix/sandbox/clement/ipojo/examples/junit4osgi/src/main/java/org/apache/felix/ipojo/junit4osgi/command/
felix/sandbox/clement/ipojo/examples/junit4osgi/src/main/java/org/apache/felix/ipojo/junit4osgi/command/JunitCommand.java
felix/sandbox/clement/ipojo/examples/junit4osgi/src/main/java/org/apache/felix/ipojo/junit4osgi/impl/
felix/sandbox/clement/ipojo/examples/junit4osgi/src/main/java/org/apache/felix/ipojo/junit4osgi/impl/JunitExtender.java
felix/sandbox/clement/ipojo/examples/junit4osgi/src/main/java/org/apache/felix/ipojo/junit4osgi/impl/ResultPrinter.java
felix/sandbox/clement/ipojo/examples/junit4osgi/src/main/java/org/apache/felix/ipojo/junit4osgi/test/
felix/sandbox/clement/ipojo/examples/junit4osgi/src/main/java/org/apache/felix/ipojo/junit4osgi/test/TestOSGiTestCase.java
felix/sandbox/clement/ipojo/examples/junit4osgi/src/main/java/org/apache/felix/ipojo/junit4osgi/test/TestOSGiTestSuite.java
felix/sandbox/clement/ipojo/examples/junit4osgi/src/main/java/org/apache/felix/ipojo/junit4osgi/test/TestTestCase.java
felix/sandbox/clement/ipojo/examples/junit4osgi/src/main/java/org/apache/felix/ipojo/junit4osgi/test/TestTestSuite.java
felix/sandbox/clement/ipojo/examples/junit4osgi/src/main/resources/
Propchange: felix/sandbox/clement/ipojo/examples/junit4osgi/
------------------------------------------------------------------------------
--- svn:ignore (added)
+++ svn:ignore Wed Jan 23 14:25:24 2008
@@ -0,0 +1,6 @@
+target*
+bin*
+.settings
+.project
+.classpath
+.checkstyle
Added: felix/sandbox/clement/ipojo/examples/junit4osgi/metadata.xml
URL:
http://svn.apache.org/viewvc/felix/sandbox/clement/ipojo/examples/junit4osgi/metadata.xml?rev=614712&view=auto
==============================================================================
--- felix/sandbox/clement/ipojo/examples/junit4osgi/metadata.xml (added)
+++ felix/sandbox/clement/ipojo/examples/junit4osgi/metadata.xml Wed Jan 23
14:25:24 2008
@@ -0,0 +1,15 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<iPOJO xmlns:extender="org.apache.felix.ipojo.extender">
+ <Component
className="org.apache.felix.ipojo.junit4osgi.impl.JunitExtender">
+ <extender:extender extension="Test-Suite"
onArrival="onBundleArrival" onDeparture="onBundleDeparture"/>
+ <callback transition="invalidate" method="stopping"/>
+ <provides/>
+ </Component>
+ <instance
component="org.apache.felix.ipojo.junit4osgi.impl.JunitExtender"/>
+
+ <component
className="org.apache.felix.ipojo.junit4osgi.command.JunitCommand"
factory="false">
+ <requires field="runner"/>
+ <provides/>
+ </component>
+ <instance
component="org.apache.felix.ipojo.junit4osgi.command.JunitCommand"/>
+</iPOJO>
\ No newline at end of file
Added: felix/sandbox/clement/ipojo/examples/junit4osgi/pom.xml
URL:
http://svn.apache.org/viewvc/felix/sandbox/clement/ipojo/examples/junit4osgi/pom.xml?rev=614712&view=auto
==============================================================================
--- felix/sandbox/clement/ipojo/examples/junit4osgi/pom.xml (added)
+++ felix/sandbox/clement/ipojo/examples/junit4osgi/pom.xml Wed Jan 23 14:25:24
2008
@@ -0,0 +1,76 @@
+<project>
+ <parent>
+ <groupId>ipojo.examples</groupId>
+ <artifactId>ipojo.examples</artifactId>
+ <version>0.7.6-SNAPSHOT</version>
+ <relativePath>../pom.xml</relativePath>
+ </parent>
+ <modelVersion>4.0.0</modelVersion>
+ <packaging>bundle</packaging>
+ <name>Junit4Osgi</name>
+ <artifactId>org.apache.felix.ipojo.junit4osgi</artifactId>
+ <dependencies>
+ <dependency>
+ <groupId>org.apache.felix</groupId>
+ <artifactId>org.apache.felix.ipojo</artifactId>
+ <version>${pom.version}</version>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.felix</groupId>
+ <artifactId>org.apache.felix.ipojo.metadata</artifactId>
+ <version>${pom.version}</version>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.felix</groupId>
+ <artifactId>org.apache.felix.shell</artifactId>
+ <version>1.0.0</version>
+ </dependency>
+ <dependency>
+ <groupId>junit</groupId>
+ <artifactId>junit</artifactId>
+ <version>3.8.1</version>
+ </dependency>
+ </dependencies>
+ <build>
+ <plugins>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-compiler-plugin</artifactId>
+ <configuration>
+ <source>1.5</source>
+ <target>1.5</target>
+ </configuration>
+ </plugin>
+ <plugin>
+ <groupId>org.apache.felix</groupId>
+ <artifactId>maven-bundle-plugin</artifactId>
+ <version>1.2.0</version>
+ <extensions>true</extensions>
+ <configuration>
+ <instructions>
+ <Bundle-Name>iPOJO OSGi Junit Runner</Bundle-Name>
+ <Bundle-SymbolicName>${pom.artifactId}</Bundle-SymbolicName>
+ <Private-Package>org.apache.felix.ipojo.junit4osgi.impl,
org.apache.felix.ipojo.junit4osgi.command,
org.apache.felix.ipojo.junit4osgi.test</Private-Package>
+ <Export-Package>org.apache.felix.ipojo.junit4osgi,
junit.*</Export-Package>
+ <Test-Suite>org.apache.felix.ipojo.junit4osgi.test.TestTestCase,
org.apache.felix.ipojo.junit4osgi.test.TestOSGiTestCase,
org.apache.felix.ipojo.junit4osgi.test.TestTestSuite,
org.apache.felix.ipojo.junit4osgi.test.TestOSGiTestSuite</Test-Suite>
+ </instructions>
+ </configuration>
+ </plugin>
+ <plugin>
+ <groupId>org.apache.felix</groupId>
+ <artifactId>maven-ipojo-plugin</artifactId>
+ <version>${pom.version}</version>
+ <executions>
+ <execution>
+ <goals>
+ <goal>ipojo-bundle</goal>
+ </goals>
+ <configuration>
+ <ignoreAnnotations>true</ignoreAnnotations>
+ </configuration>
+ </execution>
+ </executions>
+ </plugin>
+ </plugins>
+ </build>
+</project>
Added:
felix/sandbox/clement/ipojo/examples/junit4osgi/src/main/java/org/apache/felix/ipojo/junit4osgi/OSGiJunitRunner.java
URL:
http://svn.apache.org/viewvc/felix/sandbox/clement/ipojo/examples/junit4osgi/src/main/java/org/apache/felix/ipojo/junit4osgi/OSGiJunitRunner.java?rev=614712&view=auto
==============================================================================
---
felix/sandbox/clement/ipojo/examples/junit4osgi/src/main/java/org/apache/felix/ipojo/junit4osgi/OSGiJunitRunner.java
(added)
+++
felix/sandbox/clement/ipojo/examples/junit4osgi/src/main/java/org/apache/felix/ipojo/junit4osgi/OSGiJunitRunner.java
Wed Jan 23 14:25:24 2008
@@ -0,0 +1,22 @@
+package org.apache.felix.ipojo.junit4osgi;
+
+import java.io.PrintStream;
+import java.util.List;
+
+import junit.framework.Test;
+import junit.framework.TestResult;
+
+
+public interface OSGiJunitRunner {
+
+ public void setResultPrinter(PrintStream ps);
+
+ public List<TestResult> run();
+
+ public List<TestResult> run(long bundleId);
+
+ public List<Test> getTests();
+
+ public List<Test> getTests(long bundleId);
+
+}
Added:
felix/sandbox/clement/ipojo/examples/junit4osgi/src/main/java/org/apache/felix/ipojo/junit4osgi/OSGiTestCase.java
URL:
http://svn.apache.org/viewvc/felix/sandbox/clement/ipojo/examples/junit4osgi/src/main/java/org/apache/felix/ipojo/junit4osgi/OSGiTestCase.java?rev=614712&view=auto
==============================================================================
---
felix/sandbox/clement/ipojo/examples/junit4osgi/src/main/java/org/apache/felix/ipojo/junit4osgi/OSGiTestCase.java
(added)
+++
felix/sandbox/clement/ipojo/examples/junit4osgi/src/main/java/org/apache/felix/ipojo/junit4osgi/OSGiTestCase.java
Wed Jan 23 14:25:24 2008
@@ -0,0 +1,148 @@
+package org.apache.felix.ipojo.junit4osgi;
+
+import junit.framework.TestCase;
+
+import org.osgi.framework.BundleContext;
+
+public class OSGiTestCase extends TestCase {
+
+ protected BundleContext context;
+
+ public void setBundleContext(BundleContext bc) {
+ context = bc;
+ }
+
+ /**
+ * Asserts that two doubles are equal. If they are not
+ * an AssertionFailedError is thrown with the given message.
+ */
+ static public void assertEquals(String message, double expected, double
actual) {
+ if (Double.compare(expected, actual) != 0) {
+ fail(formatEqualsMessage(message, expected, actual));
+ }
+ }
+
+ static String formatEqualsMessage(String message, Object expected, Object
actual) {
+ String formatted = "";
+ if (message != null)
+ formatted = message + " ";
+ return formatted + "expected:<" + expected + "> but was:<" + actual +
">";
+ }
+
+ 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 + ">";
+ }
+
+ 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 + ">";
+ }
+
+ static public void assertNotEquals(String message, Object o1, Object o2) {
+ if (o1.equals(o2)) {
+ fail(formatNotEqualsMessage(message, o1, o2));
+ }
+ }
+
+ static public 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));
+ }
+
+ static public 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, txt));
+ }
+
+ static public 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, txt));
+ }
+
+ static public 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, txt));
+ }
+
+ static public 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, txt));
+ }
+
+ static public 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, txt));
+ }
+
+ static public 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, txt));
+ }
+
+ static public 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, txt));
+ }
+
+
+}
Added:
felix/sandbox/clement/ipojo/examples/junit4osgi/src/main/java/org/apache/felix/ipojo/junit4osgi/OSGiTestSuite.java
URL:
http://svn.apache.org/viewvc/felix/sandbox/clement/ipojo/examples/junit4osgi/src/main/java/org/apache/felix/ipojo/junit4osgi/OSGiTestSuite.java?rev=614712&view=auto
==============================================================================
---
felix/sandbox/clement/ipojo/examples/junit4osgi/src/main/java/org/apache/felix/ipojo/junit4osgi/OSGiTestSuite.java
(added)
+++
felix/sandbox/clement/ipojo/examples/junit4osgi/src/main/java/org/apache/felix/ipojo/junit4osgi/OSGiTestSuite.java
Wed Jan 23 14:25:24 2008
@@ -0,0 +1,66 @@
+package org.apache.felix.ipojo.junit4osgi;
+
+import junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestResult;
+import junit.framework.TestSuite;
+
+import org.osgi.framework.BundleContext;
+
+public class OSGiTestSuite extends TestSuite {
+
+ protected BundleContext context;
+
+ public OSGiTestSuite(Class clazz, BundleContext bc) {
+ super(clazz);
+ context = bc;
+ }
+
+ public OSGiTestSuite(BundleContext bc) {
+ super();
+ context = bc;
+ }
+
+ public OSGiTestSuite(String name, BundleContext bc) {
+ super(name);
+ context = bc;
+ }
+
+ public OSGiTestSuite(Class clazz, String name, BundleContext bc) {
+ super(clazz, name);
+ context = bc;
+ }
+
+ public void setBundleContext(BundleContext bc) {
+ context = bc;
+ }
+
+ /**
+ * Adds the tests from the given class to the suite
+ */
+ public void addTestSuite(Class testClass) {
+ if (OSGiTestCase.class.isAssignableFrom(testClass)) {
+ addTest(new OSGiTestSuite(testClass, context));
+ } else if (TestCase.class.isAssignableFrom(testClass)) {
+ addTest(new TestSuite(testClass));
+ } else {
+ System.out.println("Error : the " + testClass + " is not a valid
test class");
+ }
+ }
+
+
+ public void runTest(Test test, TestResult result) {
+ if (test instanceof OSGiTestSuite) {
+ ((OSGiTestSuite) test).context = context;
+ test.run(result);
+ } else if (test instanceof OSGiTestCase) {
+ ((OSGiTestCase) test).context = context;
+ test.run(result);
+ } else {
+ test.run(result);
+ }
+
+
+ }
+
+}
Added:
felix/sandbox/clement/ipojo/examples/junit4osgi/src/main/java/org/apache/felix/ipojo/junit4osgi/command/JunitCommand.java
URL:
http://svn.apache.org/viewvc/felix/sandbox/clement/ipojo/examples/junit4osgi/src/main/java/org/apache/felix/ipojo/junit4osgi/command/JunitCommand.java?rev=614712&view=auto
==============================================================================
---
felix/sandbox/clement/ipojo/examples/junit4osgi/src/main/java/org/apache/felix/ipojo/junit4osgi/command/JunitCommand.java
(added)
+++
felix/sandbox/clement/ipojo/examples/junit4osgi/src/main/java/org/apache/felix/ipojo/junit4osgi/command/JunitCommand.java
Wed Jan 23 14:25:24 2008
@@ -0,0 +1,89 @@
+package org.apache.felix.ipojo.junit4osgi.command;
+
+import java.io.PrintStream;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.ListIterator;
+
+import junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestFailure;
+import junit.framework.TestResult;
+import junit.framework.TestSuite;
+
+import org.apache.felix.ipojo.junit4osgi.OSGiJunitRunner;
+import org.apache.felix.shell.Command;
+
+public class JunitCommand implements Command {
+
+ private OSGiJunitRunner runner;
+
+ private List<String> getNamesFromTests(List<Test> list) {
+ List<String> names = new ArrayList<String>(list.size());
+ for (int i = 0; i < list.size(); i++) {
+ if (list.get(i) instanceof TestCase) {
+ names.add(((TestCase) list.get(i)).getName());
+ }
+ if (list.get(i) instanceof TestSuite) {
+ String name = ((TestSuite) list.get(i)).getName();
+ if (name == null) {
+ name = ((TestSuite) list.get(i)).toString();
+ }
+ names.add(name);
+ }
+ }
+ return names;
+ }
+
+ public void execute(String line, PrintStream out, PrintStream err) {
+ line = line.substring(getName().length()).trim();
+ List<TestResult> tr = null;
+ if (line.equals("all")) {
+ if (runner.getTests() == null) {
+ err.println("No tests to execute");
+ return;
+ } else {
+ out.println("Executing " +
getNamesFromTests(runner.getTests()));
+ tr = runner.run();
+ }
+ } else {
+ try {
+ Long bundleId = new Long(line);
+ if (runner.getTests(bundleId) == null) {
+ err.println("No tests to execute");
+ } else {
+ out.println("Executing " +
getNamesFromTests(runner.getTests(bundleId)));
+ tr = runner.run(bundleId);
+ }
+ } catch (NumberFormatException e ) {
+ System.err.println("Unable to parse id " + line);
+ return;
+ }
+ }
+
+ ListIterator<TestResult> it = tr.listIterator();
+ while(it.hasNext()) {
+ TestResult result = it.next();
+ if (result.failureCount() != 0) {
+ TestFailure fail = (TestFailure)
result.failures().nextElement();
+ System.out.println(fail.trace());
+ return;
+ }
+ }
+
+ }
+
+
+ public String getName() {
+ return "junit";
+ }
+
+ public String getShortDescription() {
+ return "launch junit tests";
+ }
+
+ public String getUsage() {
+ return "junit";
+ }
+
+}
Added:
felix/sandbox/clement/ipojo/examples/junit4osgi/src/main/java/org/apache/felix/ipojo/junit4osgi/impl/JunitExtender.java
URL:
http://svn.apache.org/viewvc/felix/sandbox/clement/ipojo/examples/junit4osgi/src/main/java/org/apache/felix/ipojo/junit4osgi/impl/JunitExtender.java?rev=614712&view=auto
==============================================================================
---
felix/sandbox/clement/ipojo/examples/junit4osgi/src/main/java/org/apache/felix/ipojo/junit4osgi/impl/JunitExtender.java
(added)
+++
felix/sandbox/clement/ipojo/examples/junit4osgi/src/main/java/org/apache/felix/ipojo/junit4osgi/impl/JunitExtender.java
Wed Jan 23 14:25:24 2008
@@ -0,0 +1,228 @@
+/*
+ * 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 java.io.PrintStream;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.lang.reflect.Modifier;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
+
+import junit.framework.Test;
+import junit.framework.TestResult;
+import junit.framework.TestSuite;
+
+import org.apache.felix.ipojo.junit4osgi.OSGiJunitRunner;
+import org.apache.felix.ipojo.junit4osgi.OSGiTestCase;
+import org.apache.felix.ipojo.junit4osgi.OSGiTestSuite;
+import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleContext;
+
+/**
+ * @author <a href="mailto:[EMAIL PROTECTED]">Felix Project Team</a>
+ */
+public class JunitExtender implements OSGiJunitRunner {
+
+ public static final String SUITE_METHODNAME = "suite";
+
+ private Map<Bundle, List<Class>> m_suites = new HashMap<Bundle,
List<Class>>();
+
+ private ResultPrinter m_printer = new ResultPrinter(System.out);
+
+ void onBundleArrival(Bundle bundle, String header) {
+ String[] tss = header.split(",");
+ for (int i = 0; i < tss.length; i++) {
+ try {
+ if (tss[i].length() != 0) {
+ System.out.println("Loading " + tss[i]);
+ Class<? extends Test> clazz =
bundle.loadClass(tss[i].trim());
+ addTestSuite(bundle, clazz);
+ }
+ } catch (ClassNotFoundException e) {
+ System.err.println("The test suite " + tss[i] + " is not in
the bundle " + bundle.getBundleId() + " : " + e.getMessage());
+ }
+ }
+ }
+
+ private void addTestSuite(Bundle bundle, Class<? extends Test> test) {
+ List<Class> list = m_suites.get(bundle);
+ if (list == null) {
+ list = new ArrayList<Class>();
+ list.add(test);
+ m_suites.put(bundle, list);
+ } else {
+ list.add(test);
+ }
+ }
+
+ private void removeTestSuites(Bundle bundle) {
+ m_suites.remove(bundle);
+ }
+
+ void onBundleDeparture(Bundle bundle) {
+ removeTestSuites(bundle);
+ }
+
+ public void setResultPrinter(PrintStream pw) {
+ m_printer = new ResultPrinter(pw);
+ }
+
+ public List<TestResult> run() {
+ List<TestResult> results = new ArrayList<TestResult>(m_suites.size());
+ Iterator<Entry<Bundle, List<Class>>> it =
m_suites.entrySet().iterator();
+ while(it.hasNext()) {
+ Entry<Bundle, List<Class>> entry = it.next();
+ Bundle bundle = entry.getKey();
+ List<Class> list = m_suites.get(bundle);
+ for (int i = 0; i < list.size(); i++) {
+ Test test = createTestFromClass(list.get(i), bundle);
+ TestResult tr = doRun(test);
+ results.add(tr);
+ }
+ }
+ return results;
+ }
+
+ private TestResult doRun(Test test) {
+ TestResult result= new TestResult();
+ result.addListener(m_printer);
+ long startTime= System.currentTimeMillis();
+
+ test.run(result);
+
+ long endTime= System.currentTimeMillis();
+ long runTime= endTime-startTime;
+ m_printer.print(result, runTime);
+
+ return result;
+ }
+
+ private Test createTestFromClass(Class<?> clazz, Bundle bundle) {
+ Method suiteMethod= null;
+ boolean bc = false;
+ try {
+ suiteMethod= clazz.getMethod(SUITE_METHODNAME, new Class[0]);
+ } catch(Exception e) {
+ // try to use a suite method receiving a bundle context
+ try {
+ suiteMethod= clazz.getMethod(SUITE_METHODNAME, new Class[]
{BundleContext.class});
+ bc = true;
+ } catch(Exception e2) {
+ // try to extract a test suite automatically
+ if (OSGiTestSuite.class.isAssignableFrom(clazz)) {
+ OSGiTestSuite ts = new OSGiTestSuite(clazz,
bundle.getBundleContext());
+ return ts;
+ } else if (OSGiTestCase.class.isAssignableFrom(clazz)) {
+ OSGiTestSuite ts = new OSGiTestSuite(clazz,
bundle.getBundleContext());
+ return ts;
+ } else {
+ return new TestSuite(clazz);
+ }
+ }
+ }
+
+ if (! Modifier.isStatic(suiteMethod.getModifiers())) {
+ System.err.println("Suite() method must be static");
+ return null;
+ }
+ Test test= null;
+ try {
+ if (bc) {
+ test = (Test)suiteMethod.invoke(null, new Object[]
{bundle.getBundleContext()}); // static method injection the bundle context
+ } else {
+ test = (Test)suiteMethod.invoke(null, (Object[])new
Class[0]); // static method
+ }
+ }
+ catch (InvocationTargetException e) {
+ System.err.println("Failed to invoke suite():" +
e.getTargetException().toString());
+ return null;
+ }
+ catch (IllegalAccessException e) {
+ System.err.println("Failed to invoke suite():" + e.toString());
+ return null;
+ }
+
+ return test;
+ }
+
+ public List<Test> getTests() {
+ List<Test> results = new ArrayList<Test>();
+ Iterator<Entry<Bundle, List<Class>>> it =
m_suites.entrySet().iterator();
+ while(it.hasNext()) {
+ Entry<Bundle, List<Class>> entry = it.next();
+ Bundle bundle = entry.getKey();
+ List<Class> list = m_suites.get(bundle);
+ for (int i = 0; i < list.size(); i++) {
+ Test test = createTestFromClass(list.get(i), bundle);
+ results.add(test);
+ }
+ }
+ return results;
+ }
+
+ public List<Test> getTests(long bundleId) {
+ Iterator<Entry<Bundle, List<Class>>> it =
m_suites.entrySet().iterator();
+ while(it.hasNext()) {
+ Entry<Bundle, List<Class>> entry = it.next();
+ Bundle bundle = entry.getKey();
+ if (bundle.getBundleId() == bundleId) {
+ List<Test> results = new ArrayList<Test>();
+ List<Class> list = m_suites.get(bundle);
+ for (int i = 0; i < list.size(); i++) {
+ Test test = createTestFromClass(list.get(i), bundle);
+ results.add(test);
+ }
+ return results;
+ }
+ }
+ return null;
+ }
+
+ public List<TestResult> run(long bundleId) {
+ Iterator<Entry<Bundle, List<Class>>> it =
m_suites.entrySet().iterator();
+ while(it.hasNext()) {
+ Entry<Bundle, List<Class>> entry = it.next();
+ Bundle bundle = entry.getKey();
+ if (bundle.getBundleId() == bundleId) {
+ List<TestResult> results = new ArrayList<TestResult>();
+ List<Class> list = m_suites.get(bundle);
+ for (int i = 0; i < list.size(); i++) {
+ Test test = createTestFromClass(list.get(i), bundle);
+ TestResult tr = doRun(test);
+ results.add(tr);
+ }
+ return results;
+ }
+ }
+ return null;
+ }
+
+ public void stopping() {
+ System.out.println("Cleaning test suites ...");
+ m_suites = null;
+ }
+
+
+
+}
Added:
felix/sandbox/clement/ipojo/examples/junit4osgi/src/main/java/org/apache/felix/ipojo/junit4osgi/impl/ResultPrinter.java
URL:
http://svn.apache.org/viewvc/felix/sandbox/clement/ipojo/examples/junit4osgi/src/main/java/org/apache/felix/ipojo/junit4osgi/impl/ResultPrinter.java?rev=614712&view=auto
==============================================================================
---
felix/sandbox/clement/ipojo/examples/junit4osgi/src/main/java/org/apache/felix/ipojo/junit4osgi/impl/ResultPrinter.java
(added)
+++
felix/sandbox/clement/ipojo/examples/junit4osgi/src/main/java/org/apache/felix/ipojo/junit4osgi/impl/ResultPrinter.java
Wed Jan 23 14:25:24 2008
@@ -0,0 +1,139 @@
+
+package org.apache.felix.ipojo.junit4osgi.impl;
+
+import java.io.PrintStream;
+import java.text.NumberFormat;
+import java.util.Enumeration;
+
+import junit.framework.AssertionFailedError;
+import junit.framework.Test;
+import junit.framework.TestFailure;
+import junit.framework.TestListener;
+import junit.framework.TestResult;
+import junit.runner.BaseTestRunner;
+
+public class ResultPrinter implements TestListener {
+ PrintStream fWriter;
+ int fColumn= 0;
+
+ public ResultPrinter(PrintStream writer) {
+ fWriter= writer;
+ }
+
+ /* API for use by textui.TestRunner
+ */
+
+ synchronized void print(TestResult result, long runTime) {
+ printHeader(runTime);
+ printErrors(result);
+ printFailures(result);
+ printFooter(result);
+ }
+
+ void printWaitPrompt() {
+ getWriter().println();
+ getWriter().println("<RETURN> to continue");
+ }
+
+ /* Internal methods
+ */
+
+ protected void printHeader(long runTime) {
+ getWriter().println();
+ getWriter().println("Time: "+elapsedTimeAsString(runTime));
+ }
+
+ protected void printErrors(TestResult result) {
+ printDefects(result.errors(), result.errorCount(), "error");
+ }
+
+ protected void printFailures(TestResult result) {
+ printDefects(result.failures(), result.failureCount(),
"failure");
+ }
+
+ protected void printDefects(Enumeration<TestFailure> booBoos, int
count, String type) {
+ if (count == 0) return;
+ if (count == 1)
+ getWriter().println("There was " + count + " " + type +
":");
+ else
+ getWriter().println("There were " + count + " " + type
+ "s:");
+ for (int i= 1; booBoos.hasMoreElements(); i++) {
+ printDefect(booBoos.nextElement(), i);
+ }
+ }
+
+ public void printDefect(TestFailure booBoo, int count) { // only public
for testing purposes
+ printDefectHeader(booBoo, count);
+ printDefectTrace(booBoo);
+ }
+
+ 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());
+ }
+
+ protected void printDefectTrace(TestFailure booBoo) {
+
getWriter().print(BaseTestRunner.getFilteredTrace(booBoo.trace()));
+ }
+
+ protected void printFooter(TestResult result) {
+ if (result.wasSuccessful()) {
+ getWriter().println();
+ getWriter().print("OK");
+ getWriter().println (" (" + result.runCount() + " test"
+ (result.runCount() == 1 ? "": "s") + ")");
+
+ } else {
+ getWriter().println();
+ getWriter().println("FAILURES!!!");
+ getWriter().println("Tests run: "+result.runCount()+
+ ", Failures: "+result.failureCount()+
+ ", Errors: "+result.errorCount());
+ }
+ getWriter().println();
+ }
+
+
+ /**
+ * Returns the formatted string of the elapsed time.
+ * Duplicated from BaseTestRunner. Fix it.
+ */
+ protected String elapsedTimeAsString(long runTime) {
+ return NumberFormat.getInstance().format((double)runTime/1000);
+ }
+
+ public PrintStream getWriter() {
+ return fWriter;
+ }
+ /**
+ * @see junit.framework.TestListener#addError(Test, Throwable)
+ */
+ public void addError(Test test, Throwable t) {
+ getWriter().print("E");
+ }
+
+ /**
+ * @see junit.framework.TestListener#addFailure(Test,
AssertionFailedError)
+ */
+ public void addFailure(Test test, AssertionFailedError t) {
+ getWriter().print("F");
+ }
+
+ /**
+ * @see junit.framework.TestListener#endTest(Test)
+ */
+ public void endTest(Test test) {
+ }
+
+ /**
+ * @see junit.framework.TestListener#startTest(Test)
+ */
+ public void startTest(Test test) {
+ getWriter().print(".");
+ if (fColumn++ >= 40) {
+ getWriter().println();
+ fColumn= 0;
+ }
+ }
+
+}
Added:
felix/sandbox/clement/ipojo/examples/junit4osgi/src/main/java/org/apache/felix/ipojo/junit4osgi/test/TestOSGiTestCase.java
URL:
http://svn.apache.org/viewvc/felix/sandbox/clement/ipojo/examples/junit4osgi/src/main/java/org/apache/felix/ipojo/junit4osgi/test/TestOSGiTestCase.java?rev=614712&view=auto
==============================================================================
---
felix/sandbox/clement/ipojo/examples/junit4osgi/src/main/java/org/apache/felix/ipojo/junit4osgi/test/TestOSGiTestCase.java
(added)
+++
felix/sandbox/clement/ipojo/examples/junit4osgi/src/main/java/org/apache/felix/ipojo/junit4osgi/test/TestOSGiTestCase.java
Wed Jan 23 14:25:24 2008
@@ -0,0 +1,12 @@
+package org.apache.felix.ipojo.junit4osgi.test;
+
+import org.apache.felix.ipojo.junit4osgi.OSGiTestCase;
+
+public class TestOSGiTestCase extends OSGiTestCase {
+
+ public void test1() {
+ System.out.println("Test BC");
+ assertNotNull("Test bundle context", context);
+ }
+
+}
Added:
felix/sandbox/clement/ipojo/examples/junit4osgi/src/main/java/org/apache/felix/ipojo/junit4osgi/test/TestOSGiTestSuite.java
URL:
http://svn.apache.org/viewvc/felix/sandbox/clement/ipojo/examples/junit4osgi/src/main/java/org/apache/felix/ipojo/junit4osgi/test/TestOSGiTestSuite.java?rev=614712&view=auto
==============================================================================
---
felix/sandbox/clement/ipojo/examples/junit4osgi/src/main/java/org/apache/felix/ipojo/junit4osgi/test/TestOSGiTestSuite.java
(added)
+++
felix/sandbox/clement/ipojo/examples/junit4osgi/src/main/java/org/apache/felix/ipojo/junit4osgi/test/TestOSGiTestSuite.java
Wed Jan 23 14:25:24 2008
@@ -0,0 +1,21 @@
+package org.apache.felix.ipojo.junit4osgi.test;
+
+import junit.framework.Test;
+import junit.framework.TestSuite;
+
+import org.apache.felix.ipojo.junit4osgi.OSGiTestSuite;
+import org.osgi.framework.BundleContext;
+
+public class TestOSGiTestSuite extends TestSuite {
+
+ public static Test suite(BundleContext bc) {
+ TestSuite ts = new TestSuite();
+ ts.setName("Test OSGi suite() method");
+ ts.addTestSuite(TestTestCase.class);
+ OSGiTestSuite ots = new OSGiTestSuite(TestOSGiTestCase.class, bc);
+ ots.setBundleContext(bc);
+ ts.addTest(ots);
+ return ts;
+ }
+
+}
Added:
felix/sandbox/clement/ipojo/examples/junit4osgi/src/main/java/org/apache/felix/ipojo/junit4osgi/test/TestTestCase.java
URL:
http://svn.apache.org/viewvc/felix/sandbox/clement/ipojo/examples/junit4osgi/src/main/java/org/apache/felix/ipojo/junit4osgi/test/TestTestCase.java?rev=614712&view=auto
==============================================================================
---
felix/sandbox/clement/ipojo/examples/junit4osgi/src/main/java/org/apache/felix/ipojo/junit4osgi/test/TestTestCase.java
(added)
+++
felix/sandbox/clement/ipojo/examples/junit4osgi/src/main/java/org/apache/felix/ipojo/junit4osgi/test/TestTestCase.java
Wed Jan 23 14:25:24 2008
@@ -0,0 +1,15 @@
+package org.apache.felix.ipojo.junit4osgi.test;
+
+import junit.framework.TestCase;
+
+public class TestTestCase extends TestCase {
+
+ public void test1() { }
+
+ public void test2() { }
+
+ public void test3() { }
+
+ public void test4() { }
+
+}
Added:
felix/sandbox/clement/ipojo/examples/junit4osgi/src/main/java/org/apache/felix/ipojo/junit4osgi/test/TestTestSuite.java
URL:
http://svn.apache.org/viewvc/felix/sandbox/clement/ipojo/examples/junit4osgi/src/main/java/org/apache/felix/ipojo/junit4osgi/test/TestTestSuite.java?rev=614712&view=auto
==============================================================================
---
felix/sandbox/clement/ipojo/examples/junit4osgi/src/main/java/org/apache/felix/ipojo/junit4osgi/test/TestTestSuite.java
(added)
+++
felix/sandbox/clement/ipojo/examples/junit4osgi/src/main/java/org/apache/felix/ipojo/junit4osgi/test/TestTestSuite.java
Wed Jan 23 14:25:24 2008
@@ -0,0 +1,15 @@
+package org.apache.felix.ipojo.junit4osgi.test;
+
+import junit.framework.Test;
+import junit.framework.TestSuite;
+
+public class TestTestSuite extends TestSuite {
+
+ public static Test suite() {
+ TestSuite ts = new TestSuite();
+ ts.setName("Test suite() method");
+ ts.addTestSuite(TestTestCase.class);
+ return ts;
+ }
+
+}