Skip to content

Commit

Permalink
- added OSGi related tests, similar to the one's done in
Browse files Browse the repository at this point in the history
slf4j/integrator module
  • Loading branch information
ceki committed Dec 3, 2009
1 parent 6bf6cb2 commit 4ad3aac
Show file tree
Hide file tree
Showing 13 changed files with 521 additions and 1 deletion.
3 changes: 3 additions & 0 deletions logback-classic/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
bundle/
lib/
felix-cache/
84 changes: 84 additions & 0 deletions logback-classic/osgi-build.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<project name="osgi-test" default="testAll">

<!--
This build file is usually run indirectly via Maven.
When running this build file through Ant directly, you must
define the currentVersion property on the command line, e.g.:
ant -DcurrentVersion=1.5.4-SNAPSHOT
-->

<echo message="compile classpath: ${currentVersion}" />
<echo message="test classpath: ${test_classpath}" />
<echo message="basedir: ${basedir}" />

<property name="iBundleJar" value="target/iBundle.jar"/>
<property name="bundlesDir" value="bundle"/>


<!-- this is really very ugly, but it's the only way to circumvent
http://jira.codehaus.org/browse/MANTRUN-95
-->
<taskdef name="junit" classpath="${test_classpath}"
classname="org.apache.tools.ant.taskdefs.optional.junit.JUnitTask" />

<path id="minimal">
<pathelement location="target/test-classes/" />
</path >

<target name="copySLF4J_JAR">
<echo>Copying ${slf4jJAR} to lib/</echo>
<delete>
<fileset dir="../logback-examples/lib/" includes="slf4j-*SNAPSHOT.jar" />
</delete>
<copy file="${slf4jJAR}" todir="lib/" />
</target>

<target name="init" depends="copySLF4J_JAR">
<mkdir dir="target/unit-reports" />
</target>

<target name="createIBundle">
<mkdir dir="${bundlesDir}"/>
<jar destFile="${iBundleJar}"
manifest="src/IBUNDLE-META-INF/MANIFEST.MF"
basedir="target/test-classes/"
includes="integrator/**.class"/>

</target>

<target name="testAll" depends="init, createIBundle, logback">
</target>


<macrodef name="prepareOSGiHarness">
<attribute name="binding"/>
<sequential>
<delete>
<fileset dir="${bundlesDir}" includes="*.jar"/>
</delete>
<copy file="${iBundleJar}" todir="${bundlesDir}"/>
<copy file="../logback-examples/lib/slf4j-api-${slf4j.version}.jar" todir="${bundlesDir}"/>
<copy file="../logback-core/target/logback-core-${currentVersion}.jar" todir="${bundlesDir}"/>
<copy file="target/logback-classic-${currentVersion}.jar" todir="${bundlesDir}"/>
<echo>value of t = @{t}</echo>
</sequential>
</macrodef>


<!-- for some reason if mvn is invoked from the parent directory, junit gets
invoked from the parent dir, which messes up theses tests. Hence, the
fork="yes" dir="${basedir}" -->


<target name="logback">
<prepareOSGiHarness binding="logback"/>
<junit printsummary="yes" fork="yes" dir="${basedir}" haltonfailure="yes">
<classpath path="${test_classpath}"/>
<formatter type="plain" />
<test fork="yes" todir="target/unit-reports" name="org.slf4j.test_osgi.BundleTest" />
</junit>
</target>

</project>
55 changes: 55 additions & 0 deletions logback-classic/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,13 @@
<type>test-jar</type>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>integration</artifactId>
<version>${slf4j.version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.apache.geronimo.specs</groupId>
Expand Down Expand Up @@ -142,6 +149,13 @@
<version>1.6</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.apache.felix</groupId>
<artifactId>org.apache.felix.main</artifactId>
<version>2.0.2</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down Expand Up @@ -178,6 +192,45 @@
</executions>
</plugin>


<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.2</version>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
</dependency>
<dependency>
<groupId>ant</groupId>
<artifactId>ant-junit</artifactId>
<version>1.6.5</version>
</dependency>
</dependencies>
<executions>
<execution>
<id>ant-osgi-test</id>
<phase>package</phase>
<configuration>
<tasks>
<property name="currentVersion" value="${version}" />
<property name="slf4j.version" value="${slf4j.version}"/>
<property name="test_classpath" refid="maven.test.classpath"/>
<property name="slf4jJAR"
value="${settings.localRepository}/org/slf4j/slf4j-api/${slf4j.version}/slf4j-api-${slf4j.version}.jar" />
<property name="basedir" value="${basedir}"/>
<ant antfile="${basedir}/osgi-build.xml"/>
</tasks>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>

</executions>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
Expand All @@ -189,6 +242,8 @@
<exclude>**/AllClassicTest.java</exclude>
<exclude>**/PackageTest.java</exclude>
<exclude>**/TestConstants.java</exclude>
<exclude>**/test_osgi/BundleTest.java</exclude>
org.slf4j.test_osgi
<exclude>**/SerializationPerfsTest.java</exclude>
</excludes>
</configuration>
Expand Down
15 changes: 15 additions & 0 deletions logback-classic/src/IBUNDLE-META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Created-By: Apache Maven
Built-By: ceki
Build-Jdk: 1.6.0_16
Bundle-Description: iBundle
Bundle-Version: 0.1
Bundle-Activator: integrator.Activator
Implementation-Title: iBundle
Bundle-ManifestVersion: 2
Bundle-SymbolicName: iBundle
Bundle-Name: abundle
Bundle-RequiredExecutionEnvironment: J2SE-1.3
Export-Package: apack
Import-Package: org.osgi.framework, org.slf4j;version=1.5, ch.qos.logback.core, ch.qos.logback.core.joran.spi, ch.qos.logback.core.util, ch.qos.logback.classic, ch.qos.logback.classic.joran
19 changes: 19 additions & 0 deletions logback-classic/src/test/input/osgi/simple.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration>

<configuration debug="false">

<appender name="LIST" class="ch.qos.logback.core.FileAppender">
<file>target/test-output/osgi-test.log</file>
<layout>
<pattern>%d %level - %m%n</pattern>
</layout>
</appender>

<logger name="ch.qos.logback.classic.joran" level="INFO" />

<root level="DEBUG">
<appender-ref ref="LIST" />
</root>

</configuration>
77 changes: 77 additions & 0 deletions logback-classic/src/test/java/integrator/Activator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* Copyright (c) 2004-2009 QOS.ch All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package integrator;

import org.osgi.framework.Bundle;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.joran.JoranConfigurator;
import ch.qos.logback.core.joran.spi.JoranException;
import ch.qos.logback.core.util.StatusPrinter;

/**
* A BundleActivator which invokes slf4j loggers
* @author Ceki G&uuml;lc&uuml;
*
*/
public class Activator implements BundleActivator {

private BundleContext m_context = null;

public void start(BundleContext context) {
LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();

try {
JoranConfigurator configurator = new JoranConfigurator();
configurator.setContext(lc);
// the context was probably already configured by default configuration
// rules
lc.reset();
configurator.doConfigure("src/test/input/osgi/simple.xml");
} catch (JoranException je) {
je.printStackTrace();
}
StatusPrinter.printInCaseOfErrorsOrWarnings(lc);


Logger logger = LoggerFactory.getLogger(this.getClass());
logger.info("Activator.start()");
m_context = context;
}

public void stop(BundleContext context) {
m_context = null;
Logger logger = LoggerFactory.getLogger(this.getClass());
logger.info("Activator.stop");
}

public Bundle[] getBundles() {
if (m_context != null) {
return m_context.getBundles();
}
return null;
}
}
35 changes: 35 additions & 0 deletions logback-classic/src/test/java/org/slf4j/test_osgi/BundleTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package org.slf4j.test_osgi;

import java.io.File;

import junit.framework.TestCase;

public class BundleTest extends TestCase {

FrameworkErrorListener fel = new FrameworkErrorListener();
CheckingBundleListener mbl = new CheckingBundleListener();

FelixHost felixHost = new FelixHost(fel, mbl);

protected void setUp() throws Exception {
super.setUp();
felixHost.doLaunch();
}

protected void tearDown() throws Exception {
super.tearDown();
felixHost.stop();
}

public void testSmoke() {
System.out.println("==========="+new File(".").getAbsolutePath());
mbl.dumpAll();
// check that the bundle was installed
assertTrue(mbl.exists("iBundle"));
if(fel.errorList.size() != 0) {
fel.dumpAll();
}
// check that no errors occured
assertEquals(0, fel.errorList.size());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package org.slf4j.test_osgi;

import java.util.ArrayList;
import java.util.List;

import org.osgi.framework.Bundle;
import org.osgi.framework.BundleEvent;
import org.osgi.framework.BundleListener;

public class CheckingBundleListener implements BundleListener {

List eventList = new ArrayList();

public void bundleChanged(BundleEvent be) {
eventList.add(be);
}

private void dump(BundleEvent be) {
System.out.println("BE:" + ", source " + be.getSource() + ", bundle="
+ be.getBundle() + ", type=" + be.getType());

}

public void dumpAll() {
for (int i = 0; i < eventList.size(); i++) {
BundleEvent fe = (BundleEvent) eventList.get(i);
dump(fe);
}
}

boolean exists(String bundleName) {
for (int i = 0; i < eventList.size(); i++) {
BundleEvent fe = (BundleEvent) eventList.get(i);
Bundle b = fe.getBundle();
System.out.println("===["+b+"]");
if (bundleName.equals(b.getSymbolicName())) {
return true;
}
}
return false;
}

}
Loading

0 comments on commit 4ad3aac

Please sign in to comment.