|
| 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