Skip to content

Commit 7ea68c6

Browse files
committedJul 26, 2013
Created an initial OSGiTest base class + HelloWorld Test do check the roundtrip
1 parent 02a493b commit 7ea68c6

File tree

13 files changed

+176
-23
lines changed

13 files changed

+176
-23
lines changed
 

‎com.github.groovyosgi.testing.paymentservice/META-INF/MANIFEST.MF

+1
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ Bundle-SymbolicName: com.github.groovyosgi.testing.paymentservice
55
Bundle-Version: 1.0.0.qualifier
66
Bundle-Vendor: Money Bank
77
Bundle-RequiredExecutionEnvironment: JavaSE-1.7
8+
Export-Package: com.github.groovyosgi.testing.paymentservice

‎com.github.groovyosgi.testing.pizzaservice.impl/.project

+5
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@
2020
<arguments>
2121
</arguments>
2222
</buildCommand>
23+
<buildCommand>
24+
<name>org.eclipse.pde.ds.core.builder</name>
25+
<arguments>
26+
</arguments>
27+
</buildCommand>
2328
</buildSpec>
2429
<natures>
2530
<nature>org.eclipse.pde.PluginNature</nature>

‎com.github.groovyosgi.testing.pizzaservice.impl/META-INF/MANIFEST.MF

+7-1
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,10 @@ Bundle-Version: 1.0.0.qualifier
66
Bundle-Activator: com.github.groovyosgi.testing.pizzaservice.impl.Activator
77
Bundle-Vendor: Luigis Pizza
88
Bundle-RequiredExecutionEnvironment: JavaSE-1.7
9-
Import-Package: org.osgi.framework
9+
Import-Package: com.github.groovyosgi.testing.paymentservice,
10+
com.github.groovyosgi.testing.pizzaservice,
11+
com.github.groovyosgi.testing.pizzaservice.builder,
12+
com.github.groovyosgi.testing.pizzaservice.model,
13+
org.osgi.framework,
14+
org.osgi.service.component;version="1.2.0"
15+
Service-Component: OSGI-INF/PizzaSerive.xml
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0"
3+
name="com.github.groovyosgi.testing.pizzaservice.impl" immediate="true">
4+
<implementation
5+
class="com.github.groovyosgi.testing.pizzaservice.impl.PizzaSeriveImpl" />
6+
<reference bind="setCreditCardPaymentService" cardinality="1..1"
7+
interface="com.github.groovyosgi.testing.paymentservice.CreditCardPaymentService"
8+
name="CreditCardPaymentService" policy="static" unbind="unsetCreditCardPaymentService" />
9+
10+
<service>
11+
<provide
12+
interface="com.github.groovyosgi.testing.pizzaservice.PizzaService" />
13+
</service>
14+
15+
</scr:component>
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
source.. = src/main/java/
21
output.. = target/
32
bin.includes = META-INF/,\
4-
.
3+
.,\
4+
OSGI-INF/PizzaSerive.xml
5+
source.. = src/main/java/

‎com.github.groovyosgi.testing.pizzaservice.impl/src/main/java/com/github/groovyosgi/testing/pizzaservice/impl/Activator.java

+5-11
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,21 @@
33
import org.osgi.framework.BundleActivator;
44
import org.osgi.framework.BundleContext;
55

6-
public class Activator implements BundleActivator {
6+
public final class Activator implements BundleActivator {
77

88
private static BundleContext context;
99

1010
static BundleContext getContext() {
1111
return context;
1212
}
1313

14-
/*
15-
* (non-Javadoc)
16-
* @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext)
17-
*/
18-
public void start(BundleContext bundleContext) throws Exception {
14+
@Override
15+
public void start(final BundleContext bundleContext) {
1916
Activator.context = bundleContext;
2017
}
2118

22-
/*
23-
* (non-Javadoc)
24-
* @see org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext)
25-
*/
26-
public void stop(BundleContext bundleContext) throws Exception {
19+
@Override
20+
public void stop(final BundleContext bundleContext) {
2721
Activator.context = null;
2822
}
2923

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.github.groovyosgi.testing.pizzaservice.impl;
2+
3+
import org.osgi.service.component.ComponentContext;
4+
5+
import com.github.groovyosgi.testing.paymentservice.CreditCardPaymentService;
6+
import com.github.groovyosgi.testing.pizzaservice.PizzaService;
7+
import com.github.groovyosgi.testing.pizzaservice.model.Order;
8+
9+
public class PizzaSeriveImpl implements PizzaService {
10+
11+
private static final String TAG = PizzaSeriveImpl.class.getName();
12+
private CreditCardPaymentService creditCardPaymentService;
13+
14+
public void activate(final ComponentContext componentContext) {
15+
System.out.println(TAG + " started!");
16+
}
17+
18+
public void deactivate(final ComponentContext componentContext) {
19+
System.out.println(TAG + " stoped!");
20+
}
21+
22+
@Override
23+
public void placeOrder(final Order order) {
24+
creditCardPaymentService.handleTransaction();
25+
}
26+
27+
public void setCreditCardPaymentService(
28+
final CreditCardPaymentService creditCardPaymentService) {
29+
this.creditCardPaymentService = creditCardPaymentService;
30+
}
31+
32+
public void unsetCreditCardPaymentService(
33+
final CreditCardPaymentService creditCardPaymentService) {
34+
this.creditCardPaymentService = null;
35+
}
36+
}

‎com.github.groovyosgi.testing.pizzaservice.test/HelloWorldTest.launch

+3-2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
<booleanAttribute key="clearwslog" value="false"/>
1313
<stringAttribute key="configLocation" value="${workspace_loc}/.metadata/.plugins/org.eclipse.pde.core/pde-junit"/>
1414
<booleanAttribute key="default" value="false"/>
15+
<booleanAttribute key="default_auto_start" value="true"/>
1516
<booleanAttribute key="includeOptional" value="true"/>
1617
<stringAttribute key="location" value="${workspace_loc}/../junit-workspace"/>
1718
<listAttribute key="org.eclipse.debug.core.MAPPED_RESOURCE_PATHS">
@@ -34,8 +35,8 @@
3435
<stringAttribute key="pde.version" value="3.3"/>
3536
<stringAttribute key="product" value="org.eclipse.epp.package.standard.product"/>
3637
<booleanAttribute key="run_in_ui_thread" value="true"/>
37-
<stringAttribute key="selected_target_plugins" value="javax.servlet@default:default,javax.xml@default:default,org.apache.ant@default:default,org.apache.commons.logging@default:default,org.codehaus.groovy*2.1.5.xx-20130703-1600-e43-RELEASE@default:default,org.eclipse.core.contenttype@default:default,org.eclipse.core.jobs@default:default,org.eclipse.core.runtime.compatibility.registry@default:false,org.eclipse.core.runtime@default:true,org.eclipse.equinox.app@default:default,org.eclipse.equinox.common@2:true,org.eclipse.equinox.preferences@default:default,org.eclipse.equinox.registry@default:default,org.eclipse.osgi.services@default:default,org.eclipse.osgi@-1:true,org.hamcrest.core@default:default,org.junit@default:default"/>
38-
<stringAttribute key="selected_workspace_plugins" value="com.github.groovyosgi.testing.pizzaservice.impl@default:default,com.github.groovyosgi.testing.pizzaservice.test@default:false"/>
38+
<stringAttribute key="selected_target_plugins" value="javax.servlet@default:default,javax.xml@default:default,org.apache.ant@default:default,org.apache.commons.logging@default:default,org.codehaus.groovy*2.1.5.xx-20130703-1600-e43-RELEASE@default:default,org.eclipse.core.contenttype@default:default,org.eclipse.core.jobs@default:default,org.eclipse.core.runtime.compatibility.registry@default:false,org.eclipse.core.runtime@default:true,org.eclipse.equinox.app@default:default,org.eclipse.equinox.common@2:true,org.eclipse.equinox.ds@1:true,org.eclipse.equinox.preferences@default:default,org.eclipse.equinox.registry@default:default,org.eclipse.equinox.util@default:default,org.eclipse.osgi.services@default:default,org.eclipse.osgi@-1:true,org.hamcrest.core@default:default,org.junit@default:default"/>
39+
<stringAttribute key="selected_workspace_plugins" value="com.github.groovyosgi.testing.paymentservice@default:default,com.github.groovyosgi.testing.pizzaservice.impl@default:default,com.github.groovyosgi.testing.pizzaservice.test@default:false,com.github.groovyosgi.testing.pizzaservice@default:default"/>
3940
<booleanAttribute key="show_selected_only" value="false"/>
4041
<booleanAttribute key="tracing" value="false"/>
4142
<booleanAttribute key="useCustomFeatures" value="false"/>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.github.groovyosgi.testing
2+
3+
import static org.hamcrest.CoreMatchers.*
4+
import static org.junit.Assert.*
5+
import static org.junit.matchers.JUnitMatchers.*
6+
7+
import org.junit.After
8+
import org.junit.Before
9+
import org.osgi.framework.BundleContext
10+
import org.osgi.framework.ServiceReference
11+
import org.osgi.framework.ServiceRegistration
12+
13+
abstract class OSGiTest {
14+
15+
private static BundleContext bundleContext
16+
17+
Map<String, ServiceRegistration> registeredServices = [:]
18+
19+
protected abstract BundleContext getBundleContext()
20+
21+
@Before
22+
void bindBundleContext() {
23+
bundleContext = getBundleContext()
24+
assertNotNull("Bundle context must not be null for OSGi tests.", bundleContext)
25+
}
26+
27+
def getService(Class clazz){
28+
ServiceReference<?> serviceReference = bundleContext.getServiceReference(clazz.name)
29+
30+
assertThat serviceReference, is(notNullValue())
31+
32+
return bundleContext.getService(serviceReference)
33+
}
34+
35+
def registerMock(def mock) {
36+
registeredServices.put(mock.interfaceName, bundleContext.registerService(mock.interfaceName, mock, null))
37+
}
38+
39+
def unregisterMock(def mock) {
40+
registeredServices.get(mock.interfaceName).unregister()
41+
registeredServices.remove(mock.interfaceName)
42+
}
43+
44+
@After
45+
void unregisterMocks(){
46+
registeredServices.each() { interfaceName, service ->
47+
service.unregister()
48+
}
49+
registeredServices.clear()
50+
}
51+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.github.groovyosgi.testing.pizzaservice.impl
2+
3+
/**
4+
* This class provides the {@link BundleContext} via delegating the call to the
5+
* {@link Activator}. This is needed in order to keep the {@link Activator}
6+
* package protected.
7+
*
8+
*/
9+
class BundleContextProvider {
10+
11+
static def getBundleContext(){
12+
return Activator.context
13+
}
14+
}
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,44 @@
1-
package com.github.groovyosgi.testing.pizzaservice.test;
1+
package com.github.groovyosgi.testing.pizzaservice.test
22

33
import static org.hamcrest.CoreMatchers.*
44
import static org.junit.Assert.*
55
import static org.junit.matchers.JUnitMatchers.*
66

7-
import org.junit.Test;
7+
import org.junit.Test
8+
import org.osgi.framework.BundleContext
89

9-
class HelloWorldTest {
10+
import com.github.groovyosgi.testing.OSGiTest
11+
import com.github.groovyosgi.testing.paymentservice.CreditCardPaymentService
12+
import com.github.groovyosgi.testing.pizzaservice.PizzaService
13+
import com.github.groovyosgi.testing.pizzaservice.impl.BundleContextProvider
14+
15+
class HelloWorldTest extends OSGiTest{
1016

1117
@Test
1218
public void test() {
13-
println "FFFFOOOOBBBAAARRR"
14-
assertThat true, is(true)
19+
20+
def transactionCalled = false
21+
22+
def paymentSerive = [
23+
handleTransaction: {
24+
println "handleTransaction called"
25+
transactionCalled = true
26+
}
27+
] as CreditCardPaymentService
28+
29+
30+
paymentSerive.metaClass.getInterfaceName << { -> CreditCardPaymentService.class.name }
31+
32+
registerMock(paymentSerive)
33+
34+
PizzaService pizzaService = getService(PizzaService)
35+
pizzaService.placeOrder(null)
36+
37+
assertThat transactionCalled, is(true)
1538
}
1639

40+
@Override
41+
protected BundleContext getBundleContext() {
42+
return BundleContextProvider.bundleContext
43+
}
1744
}

‎com.github.groovyosgi.testing.pizzaservice/META-INF/MANIFEST.MF

+3
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,6 @@ Bundle-SymbolicName: com.github.groovyosgi.testing.pizzaservice
55
Bundle-Version: 1.0.0.qualifier
66
Bundle-Vendor: Luigis Pizza
77
Bundle-RequiredExecutionEnvironment: JavaSE-1.7
8+
Export-Package: com.github.groovyosgi.testing.pizzaservice,
9+
com.github.groovyosgi.testing.pizzaservice.builder,
10+
com.github.groovyosgi.testing.pizzaservice.model

‎com.github.groovyosgi.testing.pizzaservice/src/main/java/com/github/groovyosgi/testing/pizzaservice/PizzaService.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@
22

33
import com.github.groovyosgi.testing.pizzaservice.model.Order;
44

5-
65
public interface PizzaService {
76

8-
void placeOrder(Order order);
7+
void placeOrder(Order order);
98

109
}

0 commit comments

Comments
 (0)
Please sign in to comment.