Skip to content

Commit 374fce5

Browse files
committed
felix.log: Add integration test for log entry to event adaptation.
1 parent e940e77 commit 374fce5

File tree

5 files changed

+426
-0
lines changed

5 files changed

+426
-0
lines changed

log/itests/base.bndrun

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
-resolve.effective: resolve, active
2+
3+
-runstartlevel: \
4+
order=leastdependenciesfirst,\
5+
begin=-1
6+
7+
-noimportjava: true
8+
-reproducible: true

log/itests/log-events/itest.bndrun

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
-include: ../base.bndrun
2+
3+
-runfw: org.apache.felix.framework
4+
5+
-runrequires:\
6+
osgi.identity;filter:='(osgi.identity=org.apache.felix.log)',\
7+
osgi.identity;filter:='(osgi.identity=org.apache.felix.log.itests.logevents)'
8+
9+
-runbundles: \
10+
org.apache.felix.configadmin;version='[1.9.22,1.9.23)',\
11+
org.apache.felix.log;version='[1.3.1,1.3.2)',\
12+
org.apache.felix.eventadmin;version='[1.6.4,1.6.5)',\
13+
org.apache.servicemix.bundles.junit;version='[4.13.2,4.13.3)',\
14+
org.osgi.service.event;version='[1.4.1,1.4.2)',\
15+
org.apache.felix.log.itests.logevents;version='[1.0.0,1.0.1)'
16+
17+
#-runjdb: 8000

log/itests/log-events/pom.xml

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<!--
2+
Licensed to the Apache Software Foundation (ASF) under one
3+
or more contributor license agreements. See the NOTICE file
4+
distributed with this work for additional information
5+
regarding copyright ownership. The ASF licenses this file
6+
to you under the Apache License, Version 2.0 (the
7+
"License"); you may not use this file except in compliance
8+
with the License. You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing,
13+
software distributed under the License is distributed on an
14+
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
KIND, either express or implied. See the License for the
16+
specific language governing permissions and limitations
17+
under the License.
18+
-->
19+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
20+
<parent>
21+
<groupId>org.apache.felix</groupId>
22+
<artifactId>org.apache.felix.log.itests.reactor</artifactId>
23+
<version>1.0.0-SNAPSHOT</version>
24+
</parent>
25+
<modelVersion>4.0.0</modelVersion>
26+
27+
<artifactId>org.apache.felix.log.itests.logevents</artifactId>
28+
<packaging>jar</packaging>
29+
30+
<dependencies>
31+
<dependency>
32+
<groupId>org.apache.felix</groupId>
33+
<artifactId>org.apache.felix.eventadmin</artifactId>
34+
</dependency>
35+
</dependencies>
36+
37+
<build>
38+
<plugins>
39+
<plugin>
40+
<groupId>biz.aQute.bnd</groupId>
41+
<artifactId>bnd-resolver-maven-plugin</artifactId>
42+
</plugin>
43+
<plugin>
44+
<groupId>biz.aQute.bnd</groupId>
45+
<artifactId>bnd-testing-maven-plugin</artifactId>
46+
</plugin>
47+
</plugins>
48+
</build>
49+
50+
</project>
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.apache.felix.log.itests.log.events;
20+
21+
import java.util.ArrayList;
22+
import java.util.Dictionary;
23+
import java.util.Hashtable;
24+
import java.util.List;
25+
import java.util.concurrent.CountDownLatch;
26+
import java.util.concurrent.TimeUnit;
27+
28+
import org.junit.Assert;
29+
import org.junit.Test;
30+
import org.osgi.annotation.bundle.Requirement;
31+
import org.osgi.framework.Bundle;
32+
import org.osgi.framework.BundleContext;
33+
import org.osgi.framework.FrameworkUtil;
34+
import org.osgi.framework.ServiceReference;
35+
import org.osgi.framework.ServiceRegistration;
36+
import org.osgi.namespace.service.ServiceNamespace;
37+
import org.osgi.resource.Namespace;
38+
import org.osgi.service.event.Event;
39+
import org.osgi.service.event.EventAdmin;
40+
import org.osgi.service.event.EventConstants;
41+
import org.osgi.service.event.EventHandler;
42+
import org.osgi.service.log.Logger;
43+
44+
@Requirement(namespace = ServiceNamespace.SERVICE_NAMESPACE,
45+
filter = "(objectClass=org.osgi.service.event.EventAdmin)",
46+
effective = Namespace.EFFECTIVE_ACTIVE)
47+
public class LogEventsTest {
48+
public static org.osgi.service.log.Logger getLogger(Class<?> clazz) {
49+
BundleContext bundleContext = FrameworkUtil.getBundle(clazz).getBundleContext();
50+
ServiceReference<org.osgi.service.log.LoggerFactory> serviceReference =
51+
bundleContext.getServiceReference(org.osgi.service.log.LoggerFactory.class);
52+
org.osgi.service.log.LoggerFactory loggerFactory = bundleContext.getService(serviceReference);
53+
final Logger logger = loggerFactory.getLogger(clazz);
54+
Assert.assertNotNull(logger);
55+
return logger;
56+
}
57+
58+
public static EventAdmin getEventAdmin() {
59+
final Bundle bundle = FrameworkUtil.getBundle(LogEventsTest.class);
60+
final BundleContext bundleContext = bundle.getBundleContext();
61+
final ServiceReference<EventAdmin> eventAdminRef = bundleContext.getServiceReference(EventAdmin.class);
62+
final EventAdmin eventAdmin = bundleContext.getService(eventAdminRef);
63+
Assert.assertNotNull(eventAdmin);
64+
return eventAdmin;
65+
}
66+
67+
static class LogEventHandler implements EventHandler {
68+
private final List<Event> events = new ArrayList<>(1);
69+
private final CountDownLatch latch = new CountDownLatch(1);
70+
71+
public synchronized List<Event> getEvents() {
72+
return new ArrayList<>(events);
73+
}
74+
75+
public CountDownLatch getLatch() {
76+
return latch;
77+
}
78+
79+
@Override
80+
public synchronized void handleEvent(Event event) {
81+
events.add(event);
82+
latch.countDown();
83+
}
84+
}
85+
86+
@Test
87+
public void test() throws InterruptedException {
88+
final Bundle bundle = FrameworkUtil.getBundle(LogEventsTest.class);
89+
final BundleContext bundleContext = bundle.getBundleContext();
90+
final LogEventHandler handler = new LogEventHandler();
91+
Dictionary<String, Object> handlerProps = new Hashtable<>(2);
92+
handlerProps.put(EventConstants.EVENT_TOPIC, "org/osgi/service/log/LogEntry/LOG_ERROR");
93+
handlerProps.put(EventConstants.EVENT_FILTER, "(bundle.symbolicName=org.apache.felix.log.itests.logevents)" );
94+
ServiceRegistration<?> eventHandlerRegistration
95+
= bundleContext.registerService(EventHandler.class.getName(), handler, handlerProps);
96+
try {
97+
final org.osgi.service.log.Logger logger = getLogger(LogEventsTest.class);
98+
logger.error("test");
99+
handler.getLatch().await(1, TimeUnit.SECONDS);
100+
eventHandlerRegistration.unregister();
101+
eventHandlerRegistration = null;
102+
final List<Event> events = handler.getEvents();
103+
Assert.assertEquals("Expecting one event", 1, events.size());
104+
} finally {
105+
if (eventHandlerRegistration != null) {
106+
eventHandlerRegistration.unregister();
107+
}
108+
}
109+
}
110+
}

0 commit comments

Comments
 (0)