Permalink
Browse files

touge.testrunner: initial import. bundle evolved from bug labs osgi t…

…ester bundle.
  • Loading branch information...
1 parent 14b2ba0 commit 5ccbe40219d7d9e40e8d1fba93a6cefc5d1bf4a1 @kgilmer kgilmer committed Nov 18, 2011
@@ -0,0 +1,8 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<classpath>
+ <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/>
+ <classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/>
+ <classpathentry kind="src" path="src"/>
+ <classpathentry kind="lib" path="lib/junit-4.9.jar"/>
+ <classpathentry kind="output" path="bin"/>
+</classpath>
@@ -0,0 +1,28 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<projectDescription>
+ <name>org.touge.osgi.testrunner</name>
+ <comment></comment>
+ <projects>
+ </projects>
+ <buildSpec>
+ <buildCommand>
+ <name>org.eclipse.jdt.core.javabuilder</name>
+ <arguments>
+ </arguments>
+ </buildCommand>
+ <buildCommand>
+ <name>org.eclipse.pde.ManifestBuilder</name>
+ <arguments>
+ </arguments>
+ </buildCommand>
+ <buildCommand>
+ <name>org.eclipse.pde.SchemaBuilder</name>
+ <arguments>
+ </arguments>
+ </buildCommand>
+ </buildSpec>
+ <natures>
+ <nature>org.eclipse.pde.PluginNature</nature>
+ <nature>org.eclipse.jdt.core.javanature</nature>
+ </natures>
+</projectDescription>
@@ -0,0 +1,22 @@
+Manifest-Version: 1.0
+Bundle-ManifestVersion: 2
+Bundle-Name: OSGi Test-buddy
+Bundle-SymbolicName: org.touge.osgi.testrunner
+Bundle-Version: 1.0.0.qualifier
+Bundle-Activator: org.touge.osgi.testrunner.Activator
+Import-Package:
+ org.osgi.framework;version="1.3.0",
+ org.osgi.util.tracker;resolution:=optional
+Bundle-RequiredExecutionEnvironment: JavaSE-1.6
+Bundle-ClassPath: lib/junit-4.9.jar,
+ .
+Export-Package: junit.extensions;version="4.9.0",
+ junit.framework;version="4.9.0",
+ junit.runner;version="4.9.0",
+ junit.textui;version="4.9.0",
+ org.junit;version="4.9.0",
+ org.junit.rules;version="4.9.0",
+ org.junit.runner;version="4.9.0",
+ org.junit.runner.manipulation;version="4.9.0",
+ org.junit.runner.notification;version="4.9.0",
+ org.junit.runners;version="4.9.0"
@@ -0,0 +1,20 @@
+This is an OSGi bundle intended to be used to run JUnit test suites
+in an automated fashion. It also exports the JUnit and Hamcrest libraries to
+simplify test environment setup.
+
+This bundle is not intended to be used outside of a build/test environment. It
+bundles dependent code rather than imports to minimize runtime dependencies.
+
+Upon bundle activation, the bundle will:
+1. Determine directory where test results are stored, create directory if necessary.
+2. Wait for a configurable amount of time for other bundles/services to start.
+3. Scan the OSGi service registry for junit.framework.TestSuite
+4. For each test suit, execute the tests and store the results.
+5. Wait for a configurable amount of time, then shutdown the entire framework.
+
+Configuration Options
+testrunner.report.dir : Path to directory where test results are to be stored.
+testrunner.startup.delay : Amount of time to wait before executing tests.
+testrunner.shutdown.delay : Amount of time to wait before framework shutdown after test execution.
+testrunner.output.verbose : Verbose output
+testrunner.keeprunning : if set to true bundle will not shutdown framework after test execution completes.
@@ -0,0 +1,5 @@
+source.. = src/
+output.. = bin/
+bin.includes = META-INF/,\
+ .,\
+ lib/junit-4.9.jar
@@ -0,0 +1,54 @@
+<?xml version="1.0"?>
+<project name="org.test_buddy">
+ <property name="wget.bin" location="/usr/bin/wget" />
+ <property name="product.dir" location="${basedir}" />
+ <property name="dependencies.dir" location="${product.dir}/deps" />
+ <property name="build-common.url" value="http://shinyama.info/build-common.xml" />
+
+ <target name="fetch.deps" description="download dependencies">
+ <exec executable="${wget.bin}">
+ <arg line="-P ${dependencies.dir}" />
+ <arg line="--no-check-certificate" />
+ <arg line="-nc" />
+ <arg line="http://www.osgi.org/download/r4v42/osgi.core.jar" />
+ </exec>
+
+ <exec executable="${wget.bin}">
+ <arg line="-P ${dependencies.dir}" />
+ <arg line="--no-check-certificate" />
+ <arg line="-nc" />
+ <arg line="http://www.osgi.org/download/r4v42/osgi.cmpn.jar" />
+ </exec>
+
+ <exec executable="${wget.bin}">
+ <arg line="-P ${dependencies.dir}" />
+ <arg line="--no-check-certificate" />
+ <arg line="-nc" />
+ <arg line="https://github.com/downloads/KentBeck/junit/junit-4.9.jar" />
+ </exec>
+ </target>
+
+ <exec executable="${wget.bin}">
+ <arg line="-P ${dependencies.dir}" />
+ <arg line="--no-check-certificate" />
+ <arg line="-nc" />
+ <arg line="${build-common.url}" />
+ </exec>
+
+ <target name="jar" depends="compile" description="create a Jar file for the application">
+ <mkdir dir="${dist.dir}" />
+ <copy file="META-INF/MANIFEST.MF" todir="${class.dir}"/>
+ <mkdir dir="lib" />
+ <copy file="${dependencies.dir}/junit-4.9.jar" todir="lib" />
+ <manifest file="${class.dir}/MANIFEST.MF" mode="update">
+ <attribute name="Built-By" value="${user.name}" />
+ <attribute name="Built-Date" value="${TS_NOW}" />
+ </manifest>
+ <jar destfile="${dist.dir}/${ant.project.name}.jar" manifest="${class.dir}/MANIFEST.MF" >
+ <fileset dir="${class.dir}" includes="**/*.class" />
+ <fileset dir="${basedir}" includes="lib/junit-4.9.jar" />
+ </jar>
+ </target>
+
+ <import file="${dependencies.dir}/build-common.xml"/>
+ </project>
@@ -0,0 +1,155 @@
+/*
+ * Copyright (c) OSGi Alliance (2000, 2008). All Rights Reserved.
+ *
+ * Licensed 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.osgi.service.log;
+
+import org.osgi.framework.ServiceReference;
+
+/**
+ * Provides methods for bundles to write messages to the log.
+ *
+ * <p>
+ * <code>LogService</code> methods are provided to log messages; optionally with a
+ * <code>ServiceReference</code> object or an exception.
+ *
+ * <p>
+ * Bundles must log messages in the OSGi environment with a severity level
+ * according to the following hierarchy:
+ * <ol>
+ * <li>{@link #LOG_ERROR}
+ * <li>{@link #LOG_WARNING}
+ * <li>{@link #LOG_INFO}
+ * <li>{@link #LOG_DEBUG}
+ * </ol>
+ *
+ * @ThreadSafe
+ * @version $Revision: 5654 $
+ */
+public interface LogService {
+ /**
+ * An error message (Value 1).
+ *
+ * <p>
+ * This log entry indicates the bundle or service may not be functional.
+ */
+ public static final int LOG_ERROR = 1;
+ /**
+ * A warning message (Value 2).
+ *
+ * <p>
+ * This log entry indicates a bundle or service is still functioning but may
+ * experience problems in the future because of the warning condition.
+ */
+ public static final int LOG_WARNING = 2;
+ /**
+ * An informational message (Value 3).
+ *
+ * <p>
+ * This log entry may be the result of any change in the bundle or service
+ * and does not indicate a problem.
+ */
+ public static final int LOG_INFO = 3;
+ /**
+ * A debugging message (Value 4).
+ *
+ * <p>
+ * This log entry is used for problem determination and may be irrelevant to
+ * anyone but the bundle developer.
+ */
+ public static final int LOG_DEBUG = 4;
+
+ /**
+ * Logs a message.
+ *
+ * <p>
+ * The <code>ServiceReference</code> field and the <code>Throwable</code> field
+ * of the <code>LogEntry</code> object will be set to <code>null</code>.
+ *
+ * @param level The severity of the message. This should be one of the
+ * defined log levels but may be any integer that is interpreted in a
+ * user defined way.
+ * @param message Human readable string describing the condition or
+ * <code>null</code>.
+ * @see #LOG_ERROR
+ * @see #LOG_WARNING
+ * @see #LOG_INFO
+ * @see #LOG_DEBUG
+ */
+ public void log(int level, String message);
+
+ /**
+ * Logs a message with an exception.
+ *
+ * <p>
+ * The <code>ServiceReference</code> field of the <code>LogEntry</code> object
+ * will be set to <code>null</code>.
+ *
+ * @param level The severity of the message. This should be one of the
+ * defined log levels but may be any integer that is interpreted in a
+ * user defined way.
+ * @param message The human readable string describing the condition or
+ * <code>null</code>.
+ * @param exception The exception that reflects the condition or
+ * <code>null</code>.
+ * @see #LOG_ERROR
+ * @see #LOG_WARNING
+ * @see #LOG_INFO
+ * @see #LOG_DEBUG
+ */
+ public void log(int level, String message, Throwable exception);
+
+ /**
+ * Logs a message associated with a specific <code>ServiceReference</code>
+ * object.
+ *
+ * <p>
+ * The <code>Throwable</code> field of the <code>LogEntry</code> will be set to
+ * <code>null</code>.
+ *
+ * @param sr The <code>ServiceReference</code> object of the service that this
+ * message is associated with or <code>null</code>.
+ * @param level The severity of the message. This should be one of the
+ * defined log levels but may be any integer that is interpreted in a
+ * user defined way.
+ * @param message Human readable string describing the condition or
+ * <code>null</code>.
+ * @see #LOG_ERROR
+ * @see #LOG_WARNING
+ * @see #LOG_INFO
+ * @see #LOG_DEBUG
+ */
+ public void log(ServiceReference sr, int level, String message);
+
+ /**
+ * Logs a message with an exception associated and a
+ * <code>ServiceReference</code> object.
+ *
+ * @param sr The <code>ServiceReference</code> object of the service that this
+ * message is associated with.
+ * @param level The severity of the message. This should be one of the
+ * defined log levels but may be any integer that is interpreted in a
+ * user defined way.
+ * @param message Human readable string describing the condition or
+ * <code>null</code>.
+ * @param exception The exception that reflects the condition or
+ * <code>null</code>.
+ * @see #LOG_ERROR
+ * @see #LOG_WARNING
+ * @see #LOG_INFO
+ * @see #LOG_DEBUG
+ */
+ public void log(ServiceReference sr, int level, String message,
+ Throwable exception);
+}
@@ -0,0 +1,64 @@
+package org.touge.osgi.testrunner;
+
+import java.io.File;
+
+import org.osgi.framework.BundleActivator;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.BundleException;
+import org.osgi.service.log.LogService;
+
+/**
+ * A test runner for OSGi-contexted JUnit tests.
+ *
+ * To expose tests to this runner, register the tests as such:
+ *
+ * <code>BundleContext.registerService(TestSuite.class.getName(), new TestSuite(<Test Case Name>.class), null); </code>
+ *
+ * In runtime, two console commands become available: tlist and trun
+ *
+ * <code>tlist</code> is used to list available test cases that the tester can see.
+ * <code>trun</code> is used to execute all or a specific test case.
+ *
+ * @author kgilmer
+ *
+ */
+public class Activator implements BundleActivator {
+ private static final String JUNIT_REPORT_DIR = "testrunner.report.dir";
+ private static final String SHUTDOWN_DELAY_MILLIS = "testrunner.shutdown.delay";
+ private static final String STARTUP_DELAY_MILLIS = "testrunner.startup.delay";
+ private static final String VERBOSE_OUTPUT = "testrunner.output.verbose";
+ private static final String KEEP_RUNNING = "testrunner.keeprunning";
+ private static final int DEFAULT_SHUTDOWN_TIMEOUT = 10000;
+ private static final long SETTLE_MILLIS = 5000;
+
+ @Override
+ public void start(final BundleContext context) throws Exception {
+ LogService log = LogServiceUtil.getLogService(context);
+ File outputDir = context.getDataFile("temp").getParentFile();
+
+ if (context.getProperty(JUNIT_REPORT_DIR) != null) {
+ outputDir = new File(context.getProperty(JUNIT_REPORT_DIR));
+
+ if (outputDir.isFile())
+ throw new BundleException("Unable to start tester, " + JUNIT_REPORT_DIR + " is set to an existing file, needs to be a directory.");
+
+ if (!outputDir.exists())
+ if (!outputDir.mkdirs())
+ throw new BundleException("Unable to start tester, unable to create directory " + JUNIT_REPORT_DIR);
+ }
+ log.log(LogService.LOG_INFO, Activator.class.getName() + " Test report output directory: " + outputDir);
+
+ long startupTimeout = OSGiUtil.getProperty(context, STARTUP_DELAY_MILLIS, SETTLE_MILLIS);
+ long shutdownTimeout = OSGiUtil.getProperty(context, SHUTDOWN_DELAY_MILLIS, DEFAULT_SHUTDOWN_TIMEOUT);
+ boolean verbose = OSGiUtil.getProperty(context, VERBOSE_OUTPUT, true);
+ boolean keepRunning = OSGiUtil.getProperty(context, KEEP_RUNNING, false);
+
+ BundleTestRunnerThread thread = new BundleTestRunnerThread(context, outputDir, startupTimeout, shutdownTimeout, log, verbose, keepRunning);
+ thread.start();
+ }
+
+ @Override
+ public void stop(BundleContext context) throws Exception {
+
+ }
+}
Oops, something went wrong.

0 comments on commit 5ccbe40

Please sign in to comment.