1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
|
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.jdisc.core;
import com.yahoo.jdisc.application.OsgiFramework;
import com.yahoo.jdisc.test.TestDriver;
import org.junit.Test;
import org.osgi.framework.BundleContext;
import org.osgi.framework.BundleException;
import org.osgi.service.log.LogReaderService;
import org.osgi.service.log.LogService;
import org.osgi.util.tracker.ServiceTracker;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
/**
* @author <a href="mailto:simon@yahoo-inc.com">Simon Thoresen Hult</a>
*/
public class OsgiLogServiceTestCase {
@Test
public void requireThatLogServiceIsRegistered() throws BundleException, InterruptedException {
OsgiFramework osgi = TestDriver.newOsgiFramework();
osgi.start();
ServiceTracker<?,?> logs = newTracker(osgi, LogService.class);
ServiceTracker<?,?> logReaders = newTracker(osgi, LogReaderService.class);
assertEquals(1, logs.getTrackingCount());
assertEquals(1, logReaders.getTrackingCount());
OsgiLogService service = new OsgiLogService();
service.start(osgi.bundleContext());
assertEquals(2, logs.getTrackingCount());
assertEquals(2, logReaders.getTrackingCount());
osgi.stop();
}
@Test
public void requireThatLogServiceCanNotBeStartedTwice() throws BundleException {
OsgiFramework osgi = TestDriver.newOsgiFramework();
osgi.start();
BundleContext ctx = osgi.bundleContext();
OsgiLogService service = new OsgiLogService();
service.start(ctx);
try {
service.start(ctx);
fail();
} catch (IllegalStateException e) {
}
osgi.stop();
}
@Test
public void requireThatLogServiceCanNotBeStoppedTwice() throws BundleException {
OsgiFramework osgi = TestDriver.newOsgiFramework();
osgi.start();
BundleContext ctx = osgi.bundleContext();
OsgiLogService service = new OsgiLogService();
service.start(ctx);
service.stop();
try {
service.stop();
fail();
} catch (NullPointerException e) {
}
osgi.stop();
}
@Test
public void requireThatUnstartedLogServiceCanNotBeStopped() throws BundleException {
try {
new OsgiLogService().stop();
fail();
} catch (NullPointerException e) {
}
}
@Test
public void requireThatLogServiceCanNotStartWithoutBundleContext() throws BundleException {
try {
new OsgiLogService().start(null);
fail();
} catch (NullPointerException e) {
}
}
@SuppressWarnings("unchecked")
private static ServiceTracker<?,?> newTracker(OsgiFramework osgi, Class<?> trackedClass) {
ServiceTracker<?,?> tracker = new ServiceTracker<>(osgi.bundleContext(), trackedClass, null);
tracker.open();
return tracker;
}
}
|