aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorOlivier Girardot2010-08-09 10:56:45 (EDT)
committerSteve Powell2010-08-09 11:34:09 (EDT)
commitb3371b81c9e8558cbd6c06a189a9bedc98e7681b (patch)
tree83742a66c6a41be8dc3bc7af0cd12215e317ebe7
parent71dd3c0dfd459ecc55b54e551371eb7cf8ae7281 (diff)
downloadorg.eclipse.virgo.kernel-b3371b81c9e8558cbd6c06a189a9bedc98e7681b.zip
org.eclipse.virgo.kernel-b3371b81c9e8558cbd6c06a189a9bedc98e7681b.tar.gz
org.eclipse.virgo.kernel-b3371b81c9e8558cbd6c06a189a9bedc98e7681b.tar.bz2
Added ConfigurationAdminDumpContributorTests from Olivier Girardot (Bugzilla 322088).
-rw-r--r--org.eclipse.virgo.kernel.core/.classpath2
-rw-r--r--org.eclipse.virgo.kernel.core/src/main/java/org/eclipse/virgo/kernel/config/internal/ConfigurationAdminDumpContributor.java10
-rw-r--r--org.eclipse.virgo.kernel.core/src/test/java/org/eclipse/virgo/kernel/config/internal/ConfigurationAdminDumpContributorTests.java128
3 files changed, 132 insertions, 8 deletions
diff --git a/org.eclipse.virgo.kernel.core/.classpath b/org.eclipse.virgo.kernel.core/.classpath
index 5332fa2..07ecfa8 100644
--- a/org.eclipse.virgo.kernel.core/.classpath
+++ b/org.eclipse.virgo.kernel.core/.classpath
@@ -29,7 +29,7 @@
<classpathentry kind="var" path="KERNEL_IVY_CACHE/org.apache.felix/org.apache.felix.configadmin/1.2.4/org.apache.felix.configadmin-1.2.4.jar" sourcepath="/KERNEL_IVY_CACHE/org.apache.felix/org.apache.felix.configadmin/1.2.4/org.apache.felix.configadmin-sources-1.2.4.jar"/>
<classpathentry kind="var" path="KERNEL_IVY_CACHE/org.springframework/org.springframework.core/3.0.0.RELEASE/org.springframework.core-3.0.0.RELEASE.jar" sourcepath="/KERNEL_IVY_CACHE/org.springframework/org.springframework.core/3.0.0.RELEASE/org.springframework.core-sources-3.0.0.RELEASE.jar"/>
<classpathentry kind="con" path="org.eclipse.ajdt.core.ASPECTJRT_CONTAINER"/>
- <classpathentry kind="var" path="KERNEL_IVY_CACHE/org.eclipse.virgo.medic/org.eclipse.virgo.medic/2.1.0.M03-incubation/org.eclipse.virgo.medic-2.1.0.M03-incubation.jar" sourcepath="/KERNEL_IVY_CACHE/org.eclipse.virgo.medic/org.eclipse.virgo.medic/1.0.0.CI-B20/org.eclipse.virgo.medic-sources-1.0.0.CI-B20.jar">
+ <classpathentry kind="var" path="KERNEL_IVY_CACHE/org.eclipse.virgo.medic/org.eclipse.virgo.medic/2.1.0.M03-incubation/org.eclipse.virgo.medic-2.1.0.M03-incubation.jar" sourcepath="/KERNEL_IVY_CACHE/org.eclipse.virgo.medic/org.eclipse.virgo.medic/2.1.0.M03-incubation/org.eclipse.virgo.medic-sources-2.1.0.M03-incubation.jar">
<attributes>
<attribute name="org.eclipse.ajdt.aspectpath" value="org.eclipse.ajdt.aspectpath"/>
</attributes>
diff --git a/org.eclipse.virgo.kernel.core/src/main/java/org/eclipse/virgo/kernel/config/internal/ConfigurationAdminDumpContributor.java b/org.eclipse.virgo.kernel.core/src/main/java/org/eclipse/virgo/kernel/config/internal/ConfigurationAdminDumpContributor.java
index 8b85a3f..f315f00 100644
--- a/org.eclipse.virgo.kernel.core/src/main/java/org/eclipse/virgo/kernel/config/internal/ConfigurationAdminDumpContributor.java
+++ b/org.eclipse.virgo.kernel.core/src/main/java/org/eclipse/virgo/kernel/config/internal/ConfigurationAdminDumpContributor.java
@@ -11,6 +11,8 @@
package org.eclipse.virgo.kernel.config.internal;
+import static org.eclipse.virgo.util.io.IOUtils.closeQuietly;
+
import java.io.IOException;
import java.io.Writer;
import java.util.Dictionary;
@@ -57,13 +59,7 @@ public class ConfigurationAdminDumpContributor implements DumpContributor {
} catch (IOException e) {
logger.warn("Could not write configurationAdmin dump");
} finally {
- if (out != null) {
- try {
- out.close();
- } catch (IOException e) {
- // Nothing to do
- }
- }
+ closeQuietly(out);
}
}
diff --git a/org.eclipse.virgo.kernel.core/src/test/java/org/eclipse/virgo/kernel/config/internal/ConfigurationAdminDumpContributorTests.java b/org.eclipse.virgo.kernel.core/src/test/java/org/eclipse/virgo/kernel/config/internal/ConfigurationAdminDumpContributorTests.java
new file mode 100644
index 0000000..c6bb548
--- /dev/null
+++ b/org.eclipse.virgo.kernel.core/src/test/java/org/eclipse/virgo/kernel/config/internal/ConfigurationAdminDumpContributorTests.java
@@ -0,0 +1,128 @@
+/* Copyright (c) 2010 Olivier Girardot
+ * 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:
+ * Olivier Girardot - initial contribution
+ */
+
+package org.eclipse.virgo.kernel.config.internal;
+
+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.eclipse.virgo.util.io.FileSystemUtils.deleteRecursively;
+import static org.eclipse.virgo.util.io.IOUtils.closeQuietly;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileReader;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.io.Writer;
+
+import org.eclipse.virgo.medic.dump.Dump;
+import org.eclipse.virgo.medic.dump.DumpContributionFailedException;
+import org.eclipse.virgo.medic.dump.DumpContributor;
+import org.eclipse.virgo.teststubs.osgi.service.cm.StubConfiguration;
+import org.eclipse.virgo.teststubs.osgi.service.cm.StubConfigurationAdmin;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.osgi.framework.InvalidSyntaxException;
+import org.osgi.service.cm.Configuration;
+import org.osgi.service.cm.ConfigurationAdmin;
+
+/**
+ * This class is for testing {@link ConfigurationAdminDumpContributor} class,
+ * an implementation of the {@link DumpContributor} interface.
+ */
+public class ConfigurationAdminDumpContributorTests {
+
+ private static final String TEST_OUTPUT_FILE_NAME = "Test-dump.out";
+ private static final String NORMAL_OUTPUT_FILE_NAME = "configurationAdmin.properties";
+ private static final String DC_NAME = "configurationAdmin";
+
+ private final File testDumpOutputDir = new File(new File("target"), "ConfigurationAdminDumpTests");
+ private File testDumpOutputFile;
+
+ @Before
+ public void setupTestDir() {
+ deleteRecursively(this.testDumpOutputDir);
+ this.testDumpOutputDir.mkdir();
+ this.testDumpOutputFile = new File(new File("target"), TEST_OUTPUT_FILE_NAME);
+ }
+
+ @After
+ public void deleteTestDir() {
+ deleteRecursively(this.testDumpOutputDir);
+ }
+
+ @Test
+ public void testGetName() {
+ DumpContributor configAdminDC = new ConfigurationAdminDumpContributor(
+ new StubConfigurationAdmin());
+ assertEquals(DC_NAME, configAdminDC.getName());
+ }
+
+ @Test
+ public void testContribute() throws DumpContributionFailedException, IOException, InvalidSyntaxException {
+ Dump mockDump = createMock(Dump.class);
+
+ StubConfiguration testConfig = new StubConfiguration("test");
+ testConfig.addProperty("service.key", "value");
+
+ FileWriter dumpFileWriter = new FileWriter(testDumpOutputFile);
+ expect(mockDump.createFileWriter(NORMAL_OUTPUT_FILE_NAME)).andReturn(dumpFileWriter);
+
+ ConfigurationAdmin mockAdmin = createMock(ConfigurationAdmin.class);
+ expect(mockAdmin.listConfigurations(null)).andReturn(new Configuration[]{testConfig});
+
+ replay(mockDump, mockAdmin);
+
+ DumpContributor configAdminDC = new ConfigurationAdminDumpContributor(mockAdmin);
+ configAdminDC.contribute(mockDump);
+
+ verify(mockAdmin, mockDump);
+
+ assertWriterClosed(dumpFileWriter);
+ assertCorrectContribution(this.testDumpOutputFile);
+ }
+
+ private static void assertCorrectContribution(File dumpFile) throws IOException {
+ String sb = getFileAsString(dumpFile);
+
+ assertTrue("Unexpected dump contribution output.",
+ sb.equals("########\n# test #\n########\n\nservice.pid:\ttest\nservice.key:\tvalue\n\n\n")
+ || sb.equals("########\n# test #\n########\n\nservice.key:\tvalue\nservice.pid:\ttest\n\n\n")
+ );
+ }
+
+ private static String getFileAsString(File dumpFile) throws IOException {
+ StringBuilder sb = new StringBuilder();
+ BufferedReader in = null;
+ try {
+ in = new BufferedReader(new FileReader(dumpFile));
+ while (in.ready())
+ sb.append(in.readLine()).append("\n");
+ } finally {
+ closeQuietly(in);
+ }
+ return sb.toString();
+ }
+
+ private static void assertWriterClosed(Writer writer) {
+ try {
+ writer.flush();
+ fail("Writer not already flushed, therefore not already closed.");
+ } catch (IOException _) {
+ // expected and ignored
+ }
+ }
+} \ No newline at end of file