Improve test to actually test the append scenario

Project: http://git-wip-us.apache.org/repos/asf/karaf/repo
Commit: http://git-wip-us.apache.org/repos/asf/karaf/commit/562019cb
Tree: http://git-wip-us.apache.org/repos/asf/karaf/tree/562019cb
Diff: http://git-wip-us.apache.org/repos/asf/karaf/diff/562019cb

Branch: refs/heads/model_features
Commit: 562019cbd2ffea35508d2cb30c29028130bd56de
Parents: 935006e
Author: Christian Schneider <ch...@die-schneider.net>
Authored: Fri Aug 11 11:00:11 2017 +0200
Committer: Christian Schneider <ch...@die-schneider.net>
Committed: Fri Aug 11 11:00:11 2017 +0200

----------------------------------------------------------------------
 .../org/apache/karaf/features/AppendTest.java   | 110 +++++++++++--------
 1 file changed, 63 insertions(+), 47 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/karaf/blob/562019cb/features/core/src/test/java/org/apache/karaf/features/AppendTest.java
----------------------------------------------------------------------
diff --git 
a/features/core/src/test/java/org/apache/karaf/features/AppendTest.java 
b/features/core/src/test/java/org/apache/karaf/features/AppendTest.java
index d6d5d20..662ad2d 100644
--- a/features/core/src/test/java/org/apache/karaf/features/AppendTest.java
+++ b/features/core/src/test/java/org/apache/karaf/features/AppendTest.java
@@ -16,74 +16,90 @@
  */
 package org.apache.karaf.features;
 
+import static org.easymock.EasyMock.eq;
+import static org.easymock.EasyMock.expect;
+import static org.easymock.EasyMock.expectLastCall;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+import java.io.IOException;
+import java.util.Dictionary;
 import java.util.Hashtable;
 import java.util.Properties;
 
-import junit.framework.TestCase;
 import org.apache.karaf.features.internal.service.FeatureConfigInstaller;
 import org.apache.karaf.features.internal.service.RepositoryImpl;
+import org.easymock.Capture;
 import org.easymock.EasyMock;
+import org.easymock.IMocksControl;
+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;
 
-public class AppendTest extends TestCase {
-
-    public void testLoad() throws Exception {
+public class AppendTest {
 
+    private IMocksControl c;
+    private Feature feature;
+    private ConfigurationAdmin admin;
+    private FeatureConfigInstaller installer;
+    
+    @Before
+    public void before() throws Exception {
         System.setProperty("karaf.data", "data");
         System.setProperty("karaf.etc", "etc");
-
         RepositoryImpl r = new 
RepositoryImpl(getClass().getResource("internal/service/f08.xml").toURI());
-        // Check repo
         Feature[] features = r.getFeatures();
-        assertNotNull(features);
-        assertEquals(1, features.length);
-        Feature feature = features[0];
-
-        ConfigInfo configInfo = feature.getConfigurations().get(0);
-        assertNotNull(configInfo);
-        assertTrue(configInfo.isAppend());
-
-        Properties properties = configInfo.getProperties();
-        assertNotNull(properties);
-        String property = 
properties.getProperty("javax.servlet.context.tempdir");
-        assertNotNull(property);
-        assertFalse(property.contains("${"));
-        assertEquals(property, "data/pax-web-jsp");
+        feature = features[0];
+        checkFeature(feature);
+        c = EasyMock.createControl();
+        admin = c.createMock(ConfigurationAdmin.class);
+        installer = new FeatureConfigInstaller(admin);
+    }
 
-        ConfigurationAdmin admin = 
EasyMock.createMock(ConfigurationAdmin.class);
-        Configuration config = EasyMock.createMock(Configuration.class);
-        
EasyMock.expect(admin.listConfigurations(EasyMock.eq("(service.pid=org.ops4j.pax.web)")))
-            .andReturn(new Configuration[] {
-                                            config
-        });
+    @Test
+    public void testNoChange() throws Exception {
         Hashtable<String, Object> original = new Hashtable<>();
-        original.put("javax.servlet.context.tempdir", "data/pax-web-jsp");
-        EasyMock.expect(config.getProperties()).andReturn(original);
+        original.put("javax.servlet.context.tempdir", "bar");
+        expectConfig(admin, original);
 
-        Hashtable<String, Object> expected = new Hashtable<>();
-        expected.put("org.ops4j.pax.web", "data/pax-web-jsp");
-        expected.put("org.apache.karaf.features.configKey", 
"org.ops4j.pax.web");
-        expected.put("foo", "bar");
-        EasyMock.expectLastCall();
-        EasyMock.replay(admin, config);
+        c.replay();
+        installer.installFeatureConfigs(feature);
+        c.verify();
+    }
 
-        FeatureConfigInstaller installer = new FeatureConfigInstaller(admin);
+    @Test
+    public void testAppend() throws Exception {
+        Hashtable<String, Object> original = new Hashtable<>();
+        original.put("foo", "bar");
+        Configuration config = expectConfig(admin, original);
+        Capture<Dictionary<String, ?>> captured = EasyMock.newCapture();
+        config.update(EasyMock.capture(captured));
+        expectLastCall();
+        c.replay();
         installer.installFeatureConfigs(feature);
-        EasyMock.verify(admin, config);
+        c.verify();
+        assertEquals("data/pax-web-jsp", 
captured.getValue().get("javax.servlet.context.tempdir"));
+    }
 
-        EasyMock.reset(admin, config);
-        
EasyMock.expect(admin.listConfigurations(EasyMock.eq("(service.pid=org.ops4j.pax.web)")))
+    private Configuration expectConfig(ConfigurationAdmin admin, 
Hashtable<String, Object> original)
+        throws IOException, InvalidSyntaxException {
+        Configuration config = c.createMock(Configuration.class);
+        expect(admin.listConfigurations(eq("(service.pid=org.ops4j.pax.web)")))
             .andReturn(new Configuration[] {
                                             config
-        });
-        original = new Hashtable<>();
-        original.put("org.apache.karaf.features.configKey", 
"org.ops4j.pax.web");
-        original.put("javax.servlet.context.tempdir", "value");
-        original.put("foo", "bar");
-        EasyMock.expect(config.getProperties()).andReturn(original);
-        EasyMock.replay(admin, config);
-        installer.installFeatureConfigs(feature);
-        EasyMock.verify(admin, config);
+        }).atLeastOnce();
+        expect(config.getProperties()).andReturn(original).atLeastOnce();
+        return config;
+    }
+
+    private void checkFeature(Feature feature) {
+        ConfigInfo configInfo = feature.getConfigurations().get(0);
+        assertTrue(configInfo.isAppend());
+
+        Properties properties = configInfo.getProperties();
+        String tempDir = 
properties.getProperty("javax.servlet.context.tempdir");
+        assertEquals("data/pax-web-jsp", tempDir);
     }
 }

Reply via email to