Skip to content

Commit

Permalink
felix.log: Add integration test for log entry to event adaptation.
Browse files Browse the repository at this point in the history
  • Loading branch information
wilx committed Dec 18, 2024
1 parent e940e77 commit 374fce5
Show file tree
Hide file tree
Showing 5 changed files with 426 additions and 0 deletions.
8 changes: 8 additions & 0 deletions log/itests/base.bndrun
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
-resolve.effective: resolve, active

-runstartlevel: \
order=leastdependenciesfirst,\
begin=-1

-noimportjava: true
-reproducible: true
17 changes: 17 additions & 0 deletions log/itests/log-events/itest.bndrun
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
-include: ../base.bndrun

-runfw: org.apache.felix.framework

-runrequires:\
osgi.identity;filter:='(osgi.identity=org.apache.felix.log)',\
osgi.identity;filter:='(osgi.identity=org.apache.felix.log.itests.logevents)'

-runbundles: \
org.apache.felix.configadmin;version='[1.9.22,1.9.23)',\
org.apache.felix.log;version='[1.3.1,1.3.2)',\
org.apache.felix.eventadmin;version='[1.6.4,1.6.5)',\
org.apache.servicemix.bundles.junit;version='[4.13.2,4.13.3)',\
org.osgi.service.event;version='[1.4.1,1.4.2)',\
org.apache.felix.log.itests.logevents;version='[1.0.0,1.0.1)'

#-runjdb: 8000
50 changes: 50 additions & 0 deletions log/itests/log-events/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you 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.
-->
<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">
<parent>
<groupId>org.apache.felix</groupId>
<artifactId>org.apache.felix.log.itests.reactor</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>org.apache.felix.log.itests.logevents</artifactId>
<packaging>jar</packaging>

<dependencies>
<dependency>
<groupId>org.apache.felix</groupId>
<artifactId>org.apache.felix.eventadmin</artifactId>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>biz.aQute.bnd</groupId>
<artifactId>bnd-resolver-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>biz.aQute.bnd</groupId>
<artifactId>bnd-testing-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.apache.felix.log.itests.log.events;

import java.util.ArrayList;
import java.util.Dictionary;
import java.util.Hashtable;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

import org.junit.Assert;
import org.junit.Test;
import org.osgi.annotation.bundle.Requirement;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.FrameworkUtil;
import org.osgi.framework.ServiceReference;
import org.osgi.framework.ServiceRegistration;
import org.osgi.namespace.service.ServiceNamespace;
import org.osgi.resource.Namespace;
import org.osgi.service.event.Event;
import org.osgi.service.event.EventAdmin;
import org.osgi.service.event.EventConstants;
import org.osgi.service.event.EventHandler;
import org.osgi.service.log.Logger;

@Requirement(namespace = ServiceNamespace.SERVICE_NAMESPACE,
filter = "(objectClass=org.osgi.service.event.EventAdmin)",
effective = Namespace.EFFECTIVE_ACTIVE)
public class LogEventsTest {
public static org.osgi.service.log.Logger getLogger(Class<?> clazz) {
BundleContext bundleContext = FrameworkUtil.getBundle(clazz).getBundleContext();
ServiceReference<org.osgi.service.log.LoggerFactory> serviceReference =
bundleContext.getServiceReference(org.osgi.service.log.LoggerFactory.class);
org.osgi.service.log.LoggerFactory loggerFactory = bundleContext.getService(serviceReference);
final Logger logger = loggerFactory.getLogger(clazz);
Assert.assertNotNull(logger);
return logger;
}

public static EventAdmin getEventAdmin() {
final Bundle bundle = FrameworkUtil.getBundle(LogEventsTest.class);
final BundleContext bundleContext = bundle.getBundleContext();
final ServiceReference<EventAdmin> eventAdminRef = bundleContext.getServiceReference(EventAdmin.class);
final EventAdmin eventAdmin = bundleContext.getService(eventAdminRef);
Assert.assertNotNull(eventAdmin);
return eventAdmin;
}

static class LogEventHandler implements EventHandler {
private final List<Event> events = new ArrayList<>(1);
private final CountDownLatch latch = new CountDownLatch(1);

public synchronized List<Event> getEvents() {
return new ArrayList<>(events);
}

public CountDownLatch getLatch() {
return latch;
}

@Override
public synchronized void handleEvent(Event event) {
events.add(event);
latch.countDown();
}
}

@Test
public void test() throws InterruptedException {
final Bundle bundle = FrameworkUtil.getBundle(LogEventsTest.class);
final BundleContext bundleContext = bundle.getBundleContext();
final LogEventHandler handler = new LogEventHandler();
Dictionary<String, Object> handlerProps = new Hashtable<>(2);
handlerProps.put(EventConstants.EVENT_TOPIC, "org/osgi/service/log/LogEntry/LOG_ERROR");
handlerProps.put(EventConstants.EVENT_FILTER, "(bundle.symbolicName=org.apache.felix.log.itests.logevents)" );
ServiceRegistration<?> eventHandlerRegistration
= bundleContext.registerService(EventHandler.class.getName(), handler, handlerProps);
try {
final org.osgi.service.log.Logger logger = getLogger(LogEventsTest.class);
logger.error("test");
handler.getLatch().await(1, TimeUnit.SECONDS);
eventHandlerRegistration.unregister();
eventHandlerRegistration = null;
final List<Event> events = handler.getEvents();
Assert.assertEquals("Expecting one event", 1, events.size());
} finally {
if (eventHandlerRegistration != null) {
eventHandlerRegistration.unregister();
}
}
}
}
Loading

0 comments on commit 374fce5

Please sign in to comment.