Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHristo Iliev2011-10-18 08:14:41 -0400
committerHristo Iliev2011-10-18 08:14:41 -0400
commit76d0d345b3cc28324920c0649da1d616d1dff307 (patch)
treec418795c2c1d216e540245f07b77a07e6b48d7d9
parentd7945dc186bbf7b010f1c3865d59f7b7574297f5 (diff)
downloadorg.eclipse.virgo.medic-76d0d345b3cc28324920c0649da1d616d1dff307.tar.gz
org.eclipse.virgo.medic-76d0d345b3cc28324920c0649da1d616d1dff307.tar.xz
org.eclipse.virgo.medic-76d0d345b3cc28324920c0649da1d616d1dff307.zip
Bug 358129: Tests for LogController and CfgChangeListenerBug-358129-Medic-Cfg
-rwxr-xr-xorg.eclipse.virgo.medic.core/src/test/java/org/eclipse/virgo/medic/impl/LogControllerTests.java232
-rw-r--r--org.eclipse.virgo.medic.core/src/test/java/org/eclipse/virgo/medic/impl/config/ConfigurationAdminConfigurationProviderTests.java131
2 files changed, 342 insertions, 21 deletions
diff --git a/org.eclipse.virgo.medic.core/src/test/java/org/eclipse/virgo/medic/impl/LogControllerTests.java b/org.eclipse.virgo.medic.core/src/test/java/org/eclipse/virgo/medic/impl/LogControllerTests.java
new file mode 100755
index 0000000..8dd0825
--- /dev/null
+++ b/org.eclipse.virgo.medic.core/src/test/java/org/eclipse/virgo/medic/impl/LogControllerTests.java
@@ -0,0 +1,232 @@
+/*******************************************************************************
+ * Copyright (c) 2011 SAP AG.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Hristo Iliev, SAP AG. - initial contribution
+ *******************************************************************************/
+
+package org.eclipse.virgo.medic.impl;
+
+import org.eclipse.virgo.medic.impl.config.ConfigurationAdminConfigurationProvider;
+import org.eclipse.virgo.medic.impl.config.ConfigurationProvider;
+import org.eclipse.virgo.medic.log.ConfigurationPublicationFailedException;
+import org.eclipse.virgo.medic.log.DelegatingPrintStream;
+import org.eclipse.virgo.medic.log.impl.LoggingPrintStreamWrapper;
+import org.eclipse.virgo.medic.log.impl.StandardDelegatingPrintStream;
+import org.eclipse.virgo.teststubs.osgi.framework.StubBundleContext;
+import org.eclipse.virgo.util.osgi.ServiceRegistrationTracker;
+import org.junit.Test;
+import org.osgi.framework.InvalidSyntaxException;
+import org.osgi.framework.ServiceReference;
+import org.osgi.framework.ServiceRegistration;
+import org.osgi.service.cm.Configuration;
+import org.osgi.service.cm.ConfigurationAdmin;
+
+import java.io.IOException;
+import java.io.PrintStream;
+import java.util.Arrays;
+import java.util.Dictionary;
+import java.util.Hashtable;
+import java.util.logging.ConsoleHandler;
+import java.util.logging.Handler;
+import java.util.logging.Logger;
+
+import static org.easymock.EasyMock.*;
+import static org.junit.Assert.*;
+
+public class LogControllerTests {
+
+ private static final String LOGGER_NAME_SYSERR = "System.err";
+ private static final String LOGGER_NAME_SYSOUT = "System.out";
+
+ private static final String LOGGER_NAME_SYSERR_DELEGATE = "delegating.System.err";
+ private static final String LOGGER_NAME_SYSOUT_DELEGATE = "delegating.System.out";
+
+ private final StubBundleContext bundleContext = new StubBundleContext();
+
+ @Test
+ @SuppressWarnings("unchecked")
+ public void loggingWithWrappedStreams() throws IOException, ConfigurationPublicationFailedException, InvalidSyntaxException {
+ ConfigurationAdmin configurationAdmin = createMock(ConfigurationAdmin.class);
+ Configuration configuration = createMock(Configuration.class);
+
+ Dictionary properties = new Hashtable();
+ properties.put(ConfigurationProvider.KEY_LOG_WRAP_SYSERR, "true");
+ properties.put(ConfigurationProvider.KEY_LOG_WRAP_SYSOUT, "true");
+ createConfigurationMocks(configurationAdmin, configuration, properties, 1);
+
+ ConfigurationAdminConfigurationProvider configurationProvider = new ConfigurationAdminConfigurationProvider(this.bundleContext);
+ LogController controller = new LogController(this.bundleContext, configurationProvider, new ServiceRegistrationTracker());
+
+ controller.logStart();
+
+ checkPublishedStreamServices(DelegatingPrintStream.class, StandardDelegatingPrintStream.class, LOGGER_NAME_SYSOUT_DELEGATE, LOGGER_NAME_SYSERR_DELEGATE);
+ checkPublishedStreamServices(PrintStream.class, PrintStream.class, LOGGER_NAME_SYSOUT, LOGGER_NAME_SYSERR);
+
+ assertTrue(System.out instanceof LoggingPrintStreamWrapper);
+ assertTrue(System.err instanceof LoggingPrintStreamWrapper);
+
+ controller.logStop();
+
+ assertFalse(System.out instanceof LoggingPrintStreamWrapper);
+ assertFalse(System.err instanceof LoggingPrintStreamWrapper);
+
+ verify(configurationAdmin, configuration);
+ }
+
+ @Test
+ @SuppressWarnings("unchecked")
+ public void loggingWithNonWrappedStreams() throws IOException, ConfigurationPublicationFailedException, InvalidSyntaxException {
+ ConfigurationAdmin configurationAdmin = createMock(ConfigurationAdmin.class);
+ Configuration configuration = createMock(Configuration.class);
+
+ Dictionary properties = new Hashtable();
+ properties.put(ConfigurationProvider.KEY_LOG_WRAP_SYSERR, "false");
+ properties.put(ConfigurationProvider.KEY_LOG_WRAP_SYSOUT, "false");
+ createConfigurationMocks(configurationAdmin, configuration, properties, 1);
+
+ ConfigurationAdminConfigurationProvider configurationProvider = new ConfigurationAdminConfigurationProvider(this.bundleContext);
+ LogController controller = new LogController(this.bundleContext, configurationProvider, new ServiceRegistrationTracker());
+
+ controller.logStart();
+
+ assertTrue(System.out instanceof StandardDelegatingPrintStream);
+ assertTrue(System.err instanceof StandardDelegatingPrintStream);
+
+ controller.logStop();
+
+ assertFalse(System.out instanceof LoggingPrintStreamWrapper);
+ assertFalse(System.err instanceof LoggingPrintStreamWrapper);
+
+ verify(configurationAdmin, configuration);
+ }
+
+ @Test
+ @SuppressWarnings("unchecked")
+ public void changeFromWrappedToNonWrappedStreams() throws IOException, ConfigurationPublicationFailedException, InvalidSyntaxException {
+ ConfigurationAdmin configurationAdmin = createMock(ConfigurationAdmin.class);
+ Configuration configuration = createMock(Configuration.class);
+
+ Dictionary properties = new Hashtable();
+ properties.put(ConfigurationProvider.KEY_LOG_WRAP_SYSERR, "true");
+ properties.put(ConfigurationProvider.KEY_LOG_WRAP_SYSOUT, "true");
+ createConfigurationMocks(configurationAdmin, configuration, properties, 1);
+
+ ConfigurationAdminConfigurationProvider configurationProvider = new ConfigurationAdminConfigurationProvider(this.bundleContext);
+ LogController controller = new LogController(this.bundleContext, configurationProvider, new ServiceRegistrationTracker());
+
+ controller.logStart();
+
+ properties.put(ConfigurationProvider.KEY_LOG_WRAP_SYSERR, "false");
+ properties.put(ConfigurationProvider.KEY_LOG_WRAP_SYSOUT, "false");
+ controller.configurationChanged(configurationProvider);
+
+ assertNull(this.bundleContext.getServiceReferences(DelegatingPrintStream.class.getName(), null));
+
+ assertTrue(System.out instanceof StandardDelegatingPrintStream);
+ assertTrue(System.err instanceof StandardDelegatingPrintStream);
+
+ controller.logStop();
+
+ assertFalse(System.out instanceof LoggingPrintStreamWrapper);
+ assertFalse(System.err instanceof LoggingPrintStreamWrapper);
+ }
+
+ @Test
+ @SuppressWarnings("unchecked")
+ public void loggingWithEnabledJULConsoleHandler() throws IOException, ConfigurationPublicationFailedException {
+ ConfigurationAdmin configurationAdmin = createMock(ConfigurationAdmin.class);
+ Configuration configuration = createMock(Configuration.class);
+
+ Dictionary properties = new Hashtable();
+ properties.put(ConfigurationProvider.KEY_ENABLE_JUL_CONSOLE_HANDLER, "true");
+ createConfigurationMocks(configurationAdmin, configuration, properties, 1);
+
+ ConfigurationAdminConfigurationProvider configurationProvider = new ConfigurationAdminConfigurationProvider(this.bundleContext);
+ LogController controller = new LogController(this.bundleContext, configurationProvider, new ServiceRegistrationTracker());
+
+ controller.logStart();
+ assertTrue(checkForJULConsoleHandler());
+
+ controller.logStop();
+ assertTrue(checkForJULConsoleHandler());
+
+ verify(configurationAdmin, configuration);
+ }
+
+ @Test
+ @SuppressWarnings("unchecked")
+ public void loggingWithDisabledJULConsoleHandler() throws IOException, ConfigurationPublicationFailedException {
+ ConfigurationAdmin configurationAdmin = createMock(ConfigurationAdmin.class);
+ Configuration configuration = createMock(Configuration.class);
+
+ Dictionary properties = new Hashtable();
+ properties.put(ConfigurationProvider.KEY_ENABLE_JUL_CONSOLE_HANDLER, "false");
+ createConfigurationMocks(configurationAdmin, configuration, properties, 1);
+
+ ConfigurationAdminConfigurationProvider configurationProvider = new ConfigurationAdminConfigurationProvider(this.bundleContext);
+ LogController controller = new LogController(this.bundleContext, configurationProvider, new ServiceRegistrationTracker());
+
+ controller.logStart();
+ assertFalse(checkForJULConsoleHandler());
+
+ controller.logStop();
+ assertTrue(checkForJULConsoleHandler());
+
+ verify(configurationAdmin, configuration);
+ }
+
+ private void checkPublishedStreamServices(Class registeredClass, Class serviceClass, String... streamNames) throws InvalidSyntaxException {
+ ServiceReference<?> serviceReferences[] = this.bundleContext.getServiceReferences(registeredClass.getName(), null);
+
+ for (ServiceReference reference : serviceReferences) {
+ String streamName = (String) reference.getProperty("org.eclipse.virgo.medic.log.printStream");
+
+ boolean foundMatch = checkForMatchingNames(streamName, streamNames);
+ if (!foundMatch) {
+ fail("Stream name [" + streamName + "] not one of the expected " + Arrays.toString(streamNames));
+ }
+
+ assertEquals(serviceClass, this.bundleContext.getService(reference).getClass());
+ }
+ }
+
+ private boolean checkForMatchingNames(String streamName, String[] streamNames) {
+ boolean foundMatch = false;
+ for (String name : streamNames) {
+ if (name.equals(streamName)) {
+ foundMatch = true;
+ break;
+ }
+ }
+ return foundMatch;
+ }
+
+ private ServiceRegistration createConfigurationMocks(ConfigurationAdmin configurationAdmin, Configuration configuration, Dictionary properties, int times) throws IOException {
+ ServiceRegistration serviceRegistration = this.bundleContext.registerService(ConfigurationAdmin.class.getName(), configurationAdmin, null);
+
+ expect(configurationAdmin.getConfiguration("org.eclipse.virgo.medic", null)).andReturn(configuration).times(times);
+ expect(configuration.getProperties()).andReturn(properties).times(times);
+
+ replay(configurationAdmin, configuration);
+
+ return serviceRegistration;
+ }
+
+ private boolean checkForJULConsoleHandler() {
+ Logger rootLogger = Logger.getLogger("");
+ Handler[] handlers = rootLogger.getHandlers();
+ for (Handler handler : handlers) {
+ if (handler instanceof ConsoleHandler) {
+ return true;
+ }
+ }
+
+ return false;
+ }
+
+}
diff --git a/org.eclipse.virgo.medic.core/src/test/java/org/eclipse/virgo/medic/impl/config/ConfigurationAdminConfigurationProviderTests.java b/org.eclipse.virgo.medic.core/src/test/java/org/eclipse/virgo/medic/impl/config/ConfigurationAdminConfigurationProviderTests.java
index 5ecc0a8..1d96963 100644
--- a/org.eclipse.virgo.medic.core/src/test/java/org/eclipse/virgo/medic/impl/config/ConfigurationAdminConfigurationProviderTests.java
+++ b/org.eclipse.virgo.medic.core/src/test/java/org/eclipse/virgo/medic/impl/config/ConfigurationAdminConfigurationProviderTests.java
@@ -11,33 +11,32 @@
package org.eclipse.virgo.medic.impl.config;
-import static org.easymock.EasyMock.createMock;
-import static org.easymock.EasyMock.expect;
-import static org.easymock.EasyMock.replay;
-import static org.easymock.EasyMock.verify;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNotNull;
+import org.eclipse.virgo.teststubs.osgi.framework.StubBundleContext;
+import org.junit.Test;
+import org.osgi.framework.ServiceRegistration;
+import org.osgi.service.cm.Configuration;
+import org.osgi.service.cm.ConfigurationAdmin;
+import org.osgi.service.cm.ConfigurationEvent;
import java.io.IOException;
import java.util.Dictionary;
import java.util.Hashtable;
-import org.junit.Test;
-import org.osgi.service.cm.Configuration;
-import org.osgi.service.cm.ConfigurationAdmin;
+import static org.easymock.EasyMock.*;
+import static org.junit.Assert.*;
-import org.eclipse.virgo.medic.impl.config.ConfigurationAdminConfigurationProvider;
-import org.eclipse.virgo.medic.impl.config.ConfigurationProvider;
-import org.eclipse.virgo.teststubs.osgi.framework.StubBundleContext;
+public class ConfigurationAdminConfigurationProviderTests implements ConfigurationChangeListener {
+
+ private static final String CONFIG_ADMIN_PID = "org.eclipse.virgo.medic";
-public class ConfigurationAdminConfigurationProviderTests {
+ private final StubBundleContext bundleContext = new StubBundleContext();
- private final StubBundleContext bundleContext = new StubBundleContext();
+ private int notificationCount;
@Test
@SuppressWarnings("unchecked")
public void getConfigurationWithoutConfigurationAdmin() {
- ConfigurationProvider configurationProvider = new ConfigurationAdminConfigurationProvider(bundleContext);
+ ConfigurationProvider configurationProvider = new ConfigurationAdminConfigurationProvider(bundleContext);
Dictionary configuration = configurationProvider.getConfiguration();
assertNotNull(configuration);
assertEquals(".", configuration.get(ConfigurationProvider.KEY_DUMP_ROOT_DIRECTORY));
@@ -54,7 +53,7 @@ public class ConfigurationAdminConfigurationProviderTests {
expect(configuration.getProperties()).andReturn(null);
replay(configurationAdmin, configuration);
-
+
ConfigurationProvider configurationProvider = new ConfigurationAdminConfigurationProvider(bundleContext);
Dictionary configDictionary = configurationProvider.getConfiguration();
@@ -70,14 +69,10 @@ public class ConfigurationAdminConfigurationProviderTests {
ConfigurationAdmin configurationAdmin = createMock(ConfigurationAdmin.class);
Configuration configuration = createMock(Configuration.class);
- this.bundleContext.registerService(ConfigurationAdmin.class.getName(), configurationAdmin, null);
Dictionary properties = new Hashtable();
properties.put(ConfigurationProvider.KEY_DUMP_ROOT_DIRECTORY, "target");
properties.put("a.b.c", "d.e.f");
- expect(configurationAdmin.getConfiguration("org.eclipse.virgo.medic", null)).andReturn(configuration);
- expect(configuration.getProperties()).andReturn(properties);
-
- replay(configurationAdmin, configuration);
+ createConfigurationMocks(configurationAdmin, configuration, properties, 1);
ConfigurationProvider configurationProvider = new ConfigurationAdminConfigurationProvider(bundleContext);
Dictionary configDictionary = configurationProvider.getConfiguration();
@@ -87,4 +82,98 @@ public class ConfigurationAdminConfigurationProviderTests {
verify(configurationAdmin, configuration);
}
+
+ @Test
+ @SuppressWarnings("unchecked")
+ public void configurationListenerNotification() throws IOException {
+ ConfigurationAdmin configurationAdmin = createMock(ConfigurationAdmin.class);
+ Configuration configuration = createMock(Configuration.class);
+
+ Dictionary properties = new Hashtable();
+ properties.put(ConfigurationProvider.KEY_DUMP_ROOT_DIRECTORY, "target");
+ properties.put("a.b.c", "d.e.f");
+ ServiceRegistration serviceRegistration = createConfigurationMocks(configurationAdmin, configuration, properties, 5);
+
+ ConfigurationAdminConfigurationProvider configurationProvider = new ConfigurationAdminConfigurationProvider(bundleContext);
+ ConfigurationEvent event = new ConfigurationEvent(serviceRegistration.getReference(), ConfigurationEvent.CM_UPDATED, null, CONFIG_ADMIN_PID);
+
+ notificationCount = 0;
+
+ updateConfigurationAndCheckForNotification(configurationProvider, event, 0);
+
+ configurationProvider.addChangeListener(this);
+ updateConfigurationAndCheckForNotification(configurationProvider, event, 1);
+
+ updateConfigurationAndCheckForNotification(configurationProvider, event, 2);
+
+ configurationProvider.removeChangeListener(this);
+ updateConfigurationAndCheckForNotification(configurationProvider, event, 2);
+
+ verify(configurationAdmin, configuration);
+ }
+
+ @Test
+ @SuppressWarnings("unchecked")
+ public void configurationChange() throws IOException {
+ ConfigurationAdmin configurationAdmin = createMock(ConfigurationAdmin.class);
+ Configuration configuration = createMock(Configuration.class);
+
+ Dictionary properties = new Hashtable();
+ properties.put(ConfigurationProvider.KEY_DUMP_ROOT_DIRECTORY, "target");
+ properties.put("a.b.c", "d.e.f");
+ ServiceRegistration serviceRegistration = createConfigurationMocks(configurationAdmin, configuration, properties, 2);
+
+ // Initial configuration publishing
+ ConfigurationAdminConfigurationProvider configurationProvider = new ConfigurationAdminConfigurationProvider(bundleContext);
+ ConfigurationEvent event = new ConfigurationEvent(serviceRegistration.getReference(), ConfigurationEvent.CM_UPDATED, null, CONFIG_ADMIN_PID);
+
+ notificationCount = 0;
+
+ configurationProvider.addChangeListener(this);
+ updateConfigurationAndCheckForNotification(configurationProvider, event, 1);
+
+ verify(configurationAdmin, configuration);
+
+ // Update the configuration
+ Dictionary newProperties = new Hashtable((Hashtable) properties);
+ newProperties.put("1.2.3", "4.5.6");
+
+ reset(configurationAdmin, configuration);
+
+ serviceRegistration = createConfigurationMocks(configurationAdmin, configuration, newProperties, 1);
+ event = new ConfigurationEvent(serviceRegistration.getReference(), ConfigurationEvent.CM_UPDATED, null, CONFIG_ADMIN_PID);
+
+ // Trigger change event
+ configurationProvider.addChangeListener(this);
+ updateConfigurationAndCheckForNotification(configurationProvider, event, 2);
+
+ Dictionary configDictionary = configurationProvider.getConfiguration();
+ assertNotNull(configDictionary);
+ assertEquals("target", configDictionary.get(ConfigurationProvider.KEY_DUMP_ROOT_DIRECTORY));
+ assertEquals("d.e.f", configDictionary.get("a.b.c"));
+ assertEquals("4.5.6", configDictionary.get("1.2.3"));
+
+ verify(configurationAdmin, configuration);
+ }
+
+ private ServiceRegistration createConfigurationMocks(ConfigurationAdmin configurationAdmin, Configuration configuration, Dictionary properties, int times) throws IOException {
+ ServiceRegistration serviceRegistration = this.bundleContext.registerService(ConfigurationAdmin.class.getName(), configurationAdmin, null);
+
+ expect(configurationAdmin.getConfiguration("org.eclipse.virgo.medic", null)).andReturn(configuration).times(times);
+ expect(configuration.getProperties()).andReturn(properties).times(times);
+
+ replay(configurationAdmin, configuration);
+
+ return serviceRegistration;
+ }
+
+ private void updateConfigurationAndCheckForNotification(ConfigurationAdminConfigurationProvider configurationProvider, ConfigurationEvent event, int count) {
+ configurationProvider.configurationEvent(event);
+ assertEquals(count, notificationCount);
+ }
+
+ @Override
+ public void configurationChanged(ConfigurationProvider configurationProvider) {
+ this.notificationCount++;
+ }
}

Back to the top