Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id C9826200C78 for ; Thu, 18 May 2017 11:25:26 +0200 (CEST) Received: by cust-asf.ponee.io (Postfix) id C6E97160BB0; Thu, 18 May 2017 09:25:26 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id C0A1C160BC4 for ; Thu, 18 May 2017 11:25:25 +0200 (CEST) Received: (qmail 45576 invoked by uid 500); 18 May 2017 09:25:25 -0000 Mailing-List: contact commits-help@karaf.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@karaf.apache.org Delivered-To: mailing list commits@karaf.apache.org Received: (qmail 45566 invoked by uid 99); 18 May 2017 09:25:23 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 18 May 2017 09:25:23 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id 49A51DFF93; Thu, 18 May 2017 09:25:23 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: cschneider@apache.org To: commits@karaf.apache.org Date: Thu, 18 May 2017 09:25:24 -0000 Message-Id: <477f7c7c40be4c40856c1a8e4045a4db@git.apache.org> In-Reply-To: References: X-Mailer: ASF-Git Admin Mailer Subject: [2/2] karaf git commit: Simplify MavenResolverTests archived-at: Thu, 18 May 2017 09:25:27 -0000 Simplify MavenResolverTests Project: http://git-wip-us.apache.org/repos/asf/karaf/repo Commit: http://git-wip-us.apache.org/repos/asf/karaf/commit/646c91b7 Tree: http://git-wip-us.apache.org/repos/asf/karaf/tree/646c91b7 Diff: http://git-wip-us.apache.org/repos/asf/karaf/diff/646c91b7 Branch: refs/heads/master Commit: 646c91b7555e59f983d0c3cde1d3234da1d425bf Parents: e3b275b Author: Christian Schneider Authored: Thu May 18 11:25:16 2017 +0200 Committer: Christian Schneider Committed: Thu May 18 11:25:16 2017 +0200 ---------------------------------------------------------------------- .../KarafMinimalMonitoredTestSupport.java | 58 ++++++++++---------- .../karaf/itests/monitoring/Activator.java | 15 ++--- .../itests/monitoring/RegisteredService.java | 34 ------------ .../karaf/itests/monitoring/ServiceMonitor.java | 15 ++--- 4 files changed, 43 insertions(+), 79 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/karaf/blob/646c91b7/itests/src/test/java/org/apache/karaf/itests/mavenresolver/KarafMinimalMonitoredTestSupport.java ---------------------------------------------------------------------- diff --git a/itests/src/test/java/org/apache/karaf/itests/mavenresolver/KarafMinimalMonitoredTestSupport.java b/itests/src/test/java/org/apache/karaf/itests/mavenresolver/KarafMinimalMonitoredTestSupport.java index d5c7d66..eb6d45f 100644 --- a/itests/src/test/java/org/apache/karaf/itests/mavenresolver/KarafMinimalMonitoredTestSupport.java +++ b/itests/src/test/java/org/apache/karaf/itests/mavenresolver/KarafMinimalMonitoredTestSupport.java @@ -16,43 +16,57 @@ */ package org.apache.karaf.itests.mavenresolver; +import static org.apache.karaf.itests.KarafTestSupport.MAX_RMI_REG_PORT; +import static org.apache.karaf.itests.KarafTestSupport.MAX_RMI_SERVER_PORT; +import static org.apache.karaf.itests.KarafTestSupport.MIN_RMI_REG_PORT; +import static org.apache.karaf.itests.KarafTestSupport.MIN_RMI_SERVER_PORT; +import static org.ops4j.pax.exam.CoreOptions.composite; +import static org.ops4j.pax.exam.CoreOptions.maven; +import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.configureSecurity; +import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.editConfigurationFilePut; +import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.karafDistributionConfiguration; +import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.keepRuntimeFolder; +import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.logLevel; +import static org.ops4j.pax.tinybundles.core.TinyBundles.bundle; +import static org.ops4j.pax.tinybundles.core.TinyBundles.withBnd; +import static org.osgi.framework.Constants.OBJECTCLASS; + import java.io.File; import java.io.InputStream; import java.net.URL; -import java.util.ArrayList; -import java.util.List; +import java.util.function.Function; import javax.inject.Inject; import org.apache.karaf.itests.KarafTestSupport; import org.apache.karaf.itests.monitoring.Activator; -import org.apache.karaf.itests.monitoring.RegisteredService; import org.apache.karaf.itests.monitoring.ServiceMonitor; import org.ops4j.pax.exam.Option; +import org.ops4j.pax.exam.ProbeBuilder; +import org.ops4j.pax.exam.TestProbeBuilder; import org.ops4j.pax.exam.karaf.options.LogLevelOption; import org.ops4j.pax.exam.options.MavenArtifactUrlReference; import org.ops4j.store.Handle; import org.ops4j.store.Store; import org.ops4j.store.intern.TemporaryStore; -import org.osgi.framework.BundleContext; import org.osgi.framework.Constants; -import org.osgi.framework.ServiceReference; +import org.osgi.framework.ServiceEvent; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import static org.apache.karaf.itests.KarafTestSupport.*; -import static org.ops4j.pax.exam.CoreOptions.composite; -import static org.ops4j.pax.exam.CoreOptions.maven; -import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.*; -import static org.ops4j.pax.tinybundles.core.TinyBundles.bundle; - // don't extend, because we don't want @Rule Retry public abstract class KarafMinimalMonitoredTestSupport { public static Logger LOG = LoggerFactory.getLogger(KarafMinimalMonitoredTestSupport.class); @Inject - protected BundleContext bundleContext; + protected ServiceMonitor serviceMonitor; + + @ProbeBuilder + public TestProbeBuilder probeConfiguration(TestProbeBuilder probe) { + probe.setHeader(Constants.IMPORT_PACKAGE, ServiceMonitor.class.getPackage().getName()); + return probe; + } public Option[] baseConfig() throws Exception { MavenArtifactUrlReference karafUrl = maven() @@ -70,38 +84,26 @@ public abstract class KarafMinimalMonitoredTestSupport { karafDistributionConfiguration().frameworkUrl(karafUrl).name("Apache Karaf").unpackDirectory(new File("target/exam")), // enable JMX RBAC security, thanks to the KarafMBeanServerBuilder configureSecurity().disableKarafMBeanServerBuilder(), - keepRuntimeFolder(), logLevel(LogLevelOption.LogLevel.INFO), editConfigurationFilePut("etc/org.apache.karaf.management.cfg", "rmiRegistryPort", rmiRegistryPort), editConfigurationFilePut("etc/org.apache.karaf.management.cfg", "rmiServerPort", rmiServerPort), editConfigurationFilePut("etc/startup.properties", "file:../../" + new File(url.toURI()).getName(), "1"), -// new TimeoutOption(3600000), -// KarafDistributionOption.debugConfiguration("8889", true), composite(editConfigurationFilePut("etc/org.apache.karaf.features.cfg", new File("target/test-classes/etc/org.apache.karaf.features.cfg"))) }; } private InputStream createMonitorBundle() { return bundle() - .set(Constants.BUNDLE_SYMBOLICNAME, "monitor") .set(Constants.BUNDLE_ACTIVATOR, Activator.class.getName()) - .set(Constants.IMPORT_PACKAGE, "org.osgi.framework") - .set(Constants.EXPORT_PACKAGE, Activator.class.getPackage().getName()) - .set(Constants.BUNDLE_VERSION, "1.0.0") - .set(Constants.BUNDLE_MANIFESTVERSION, "2") + .set(Constants.EXPORT_PACKAGE, ServiceMonitor.class.getPackage().getName()) .add(Activator.class) - .add(RegisteredService.class) .add(ServiceMonitor.class) - .build(); + .build(withBnd()); } - @SuppressWarnings({ - "unchecked", "rawtypes" - }) protected long numberOfServiceEventsFor(String serviceName) { - ServiceReference sr = bundleContext.getBundle(0L).getBundleContext().getServiceReference(List.class); - List services = new ArrayList<>(bundleContext.getService(sr)); - return services.stream().filter(v -> v.equals(serviceName)).count(); + Function getObjectClass = event -> ((String[])event.getServiceReference().getProperty(OBJECTCLASS))[0]; + return serviceMonitor.getEvents().stream().map(getObjectClass).filter(v -> v.equals(serviceName)).count(); } } http://git-wip-us.apache.org/repos/asf/karaf/blob/646c91b7/itests/src/test/java/org/apache/karaf/itests/monitoring/Activator.java ---------------------------------------------------------------------- diff --git a/itests/src/test/java/org/apache/karaf/itests/monitoring/Activator.java b/itests/src/test/java/org/apache/karaf/itests/monitoring/Activator.java index 8edfc8c..8ffe9ff 100644 --- a/itests/src/test/java/org/apache/karaf/itests/monitoring/Activator.java +++ b/itests/src/test/java/org/apache/karaf/itests/monitoring/Activator.java @@ -16,28 +16,23 @@ */ package org.apache.karaf.itests.monitoring; -import java.util.List; - import org.osgi.framework.BundleActivator; import org.osgi.framework.BundleContext; -import org.osgi.framework.ServiceRegistration; public class Activator implements BundleActivator { - private ServiceRegistration registration; - private ServiceMonitor service; + private ServiceMonitor serviceMonitor; @Override public void start(BundleContext context) throws Exception { - service = new ServiceMonitor(); - registration = context.registerService(List.class, service.getServices(), null); - context.addServiceListener(service::addServiceEvent); + serviceMonitor = new ServiceMonitor(); + context.registerService(ServiceMonitor.class, serviceMonitor, null); + context.addServiceListener(serviceMonitor); } @Override public void stop(BundleContext context) throws Exception { - context.removeServiceListener(service::addServiceEvent); - registration.unregister(); + context.removeServiceListener(serviceMonitor); } } http://git-wip-us.apache.org/repos/asf/karaf/blob/646c91b7/itests/src/test/java/org/apache/karaf/itests/monitoring/RegisteredService.java ---------------------------------------------------------------------- diff --git a/itests/src/test/java/org/apache/karaf/itests/monitoring/RegisteredService.java b/itests/src/test/java/org/apache/karaf/itests/monitoring/RegisteredService.java deleted file mode 100644 index 51f51b5..0000000 --- a/itests/src/test/java/org/apache/karaf/itests/monitoring/RegisteredService.java +++ /dev/null @@ -1,34 +0,0 @@ -/* - * 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.karaf.itests.monitoring; - -import org.osgi.framework.BundleContext; -import org.osgi.framework.ServiceEvent; - -public class RegisteredService { - - private String[] objectClass; - private int type; - private Object service; - - public RegisteredService(BundleContext context, ServiceEvent event) { - objectClass = (String[])event.getServiceReference().getProperty("objectClass"); - type = event.getType(); - service = context.getService(event.getServiceReference()); - } - -} http://git-wip-us.apache.org/repos/asf/karaf/blob/646c91b7/itests/src/test/java/org/apache/karaf/itests/monitoring/ServiceMonitor.java ---------------------------------------------------------------------- diff --git a/itests/src/test/java/org/apache/karaf/itests/monitoring/ServiceMonitor.java b/itests/src/test/java/org/apache/karaf/itests/monitoring/ServiceMonitor.java index 1999e83..c47414d 100644 --- a/itests/src/test/java/org/apache/karaf/itests/monitoring/ServiceMonitor.java +++ b/itests/src/test/java/org/apache/karaf/itests/monitoring/ServiceMonitor.java @@ -20,17 +20,18 @@ import java.util.LinkedList; import java.util.List; import org.osgi.framework.ServiceEvent; +import org.osgi.framework.ServiceListener; -public class ServiceMonitor { +public class ServiceMonitor implements ServiceListener { - private List services = new LinkedList<>(); + private List events = new LinkedList<>(); - public void addServiceEvent(ServiceEvent event) { - services.add(((String[])event.getServiceReference().getProperty("objectClass"))[0]); + @Override + public void serviceChanged(ServiceEvent event) { + events.add(event); } - public List getServices() { - return services; + public List getEvents() { + return events; } - }