You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tamaya.apache.org by an...@apache.org on 2017/10/20 08:48:59 UTC

incubator-tamaya-extensions git commit: TAMAYA-300 Added additional tests, fixed some minor issues.

Repository: incubator-tamaya-extensions
Updated Branches:
  refs/heads/master c1b2da96d -> 85582e3f2


TAMAYA-300 Added additional tests, fixed some minor issues.


Project: http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/commit/85582e3f
Tree: http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/tree/85582e3f
Diff: http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/diff/85582e3f

Branch: refs/heads/master
Commit: 85582e3f2beec3ad9546dc4019932643af74a70e
Parents: c1b2da9
Author: Anatole Tresch <an...@apache.org>
Authored: Fri Oct 20 10:48:34 2017 +0200
Committer: Anatole Tresch <an...@apache.org>
Committed: Fri Oct 20 10:48:55 2017 +0200

----------------------------------------------------------------------
 .../apache/tamaya/osgi/injection/Activator.java |  4 +-
 .../injection/OSGIConfigurationInjector.java    |  2 +
 .../osgi/injection/TamayaOSGIInjector.java      | 27 ++++--
 .../tamaya/osgi/injection/ActivatorTest.java    | 92 ++++++++++++++++++++
 .../apache/tamaya/osgi/injection/Example.java   | 51 +++++++++++
 .../OSGIConfigAdminPropertySourceTest.java      | 25 ++++++
 .../OSGIConfigurationInjectorTest.java          | 51 +++--------
 .../tamaya/osgi/injection/TemplateExample.java  | 36 ++++++++
 8 files changed, 244 insertions(+), 44 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/85582e3f/modules/osgi/injection/src/main/java/org/apache/tamaya/osgi/injection/Activator.java
----------------------------------------------------------------------
diff --git a/modules/osgi/injection/src/main/java/org/apache/tamaya/osgi/injection/Activator.java b/modules/osgi/injection/src/main/java/org/apache/tamaya/osgi/injection/Activator.java
index b344415..57b8010 100644
--- a/modules/osgi/injection/src/main/java/org/apache/tamaya/osgi/injection/Activator.java
+++ b/modules/osgi/injection/src/main/java/org/apache/tamaya/osgi/injection/Activator.java
@@ -53,12 +53,12 @@ public class Activator implements BundleActivator {
     public void stop(BundleContext context) throws Exception {
         LOG.finest("Unregistering Tamaya Config injector...");
         if (serviceReg != null) {
-            injector.stop();
             serviceReg.unregister();
         }
+        injector.stop();
     }
 
-    private final class TamayaConfigInjectionService implements ConfigInjectionService{
+    static final class TamayaConfigInjectionService implements ConfigInjectionService{
 
         private TamayaOSGIInjector injector;
 

http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/85582e3f/modules/osgi/injection/src/main/java/org/apache/tamaya/osgi/injection/OSGIConfigurationInjector.java
----------------------------------------------------------------------
diff --git a/modules/osgi/injection/src/main/java/org/apache/tamaya/osgi/injection/OSGIConfigurationInjector.java b/modules/osgi/injection/src/main/java/org/apache/tamaya/osgi/injection/OSGIConfigurationInjector.java
index 4ac520d..dc90449 100644
--- a/modules/osgi/injection/src/main/java/org/apache/tamaya/osgi/injection/OSGIConfigurationInjector.java
+++ b/modules/osgi/injection/src/main/java/org/apache/tamaya/osgi/injection/OSGIConfigurationInjector.java
@@ -57,6 +57,8 @@ final class OSGIConfigurationInjector{
      */
     public OSGIConfigurationInjector(ConfigurationAdmin cm, String pid, String location){
         this.cm = Objects.requireNonNull(cm);
+        this.pid = Objects.requireNonNull(pid);
+        this.location = location;
         tamayaOSGIConfiguration = ConfigurationProvider.createConfiguration(
                 ConfigurationProvider.getConfigurationContextBuilder()
                 .addDefaultPropertyConverters()

http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/85582e3f/modules/osgi/injection/src/main/java/org/apache/tamaya/osgi/injection/TamayaOSGIInjector.java
----------------------------------------------------------------------
diff --git a/modules/osgi/injection/src/main/java/org/apache/tamaya/osgi/injection/TamayaOSGIInjector.java b/modules/osgi/injection/src/main/java/org/apache/tamaya/osgi/injection/TamayaOSGIInjector.java
index 2b245d3..fe0fe78 100644
--- a/modules/osgi/injection/src/main/java/org/apache/tamaya/osgi/injection/TamayaOSGIInjector.java
+++ b/modules/osgi/injection/src/main/java/org/apache/tamaya/osgi/injection/TamayaOSGIInjector.java
@@ -18,6 +18,7 @@
  */
 package org.apache.tamaya.osgi.injection;
 
+import org.apache.tamaya.Configuration;
 import org.apache.tamaya.osgi.TamayaConfigPlugin;
 import org.osgi.framework.Bundle;
 import org.osgi.framework.BundleContext;
@@ -26,6 +27,7 @@ import org.osgi.framework.ServiceReference;
 import org.osgi.service.cm.ConfigurationAdmin;
 import org.osgi.util.tracker.ServiceTracker;
 
+import java.util.Locale;
 import java.util.Map;
 import java.util.Objects;
 import java.util.concurrent.ConcurrentHashMap;
@@ -54,6 +56,8 @@ public class TamayaOSGIInjector{
      */
     public TamayaOSGIInjector(BundleContext context) {
         this.context = Objects.requireNonNull(context);
+        ServiceReference<ConfigurationAdmin> cmRef = context.getServiceReference(ConfigurationAdmin.class);
+        this.cm = Objects.requireNonNull(context.getService(cmRef));
     }
 
     /**
@@ -127,10 +131,15 @@ public class TamayaOSGIInjector{
     static boolean isInjectionEnabled(ServiceReference reference){
         String enabledVal = (String)reference.getProperty(ConfigInjectionService.TAMAYA_INJECTION_ENABLED_PROP);
         if(enabledVal!=null){
-            return Boolean.parseBoolean(enabledVal);
-        }
-        if(enabledVal!=null){
-            return Boolean.parseBoolean(enabledVal);
+            enabledVal = enabledVal.toLowerCase(Locale.ENGLISH);
+            switch(enabledVal){
+                case "true":
+                case "yes":
+                case "on":
+                    return true;
+                default:
+                    return false;
+            }
         }
         return isInjectionEnabled(reference.getBundle());
     }
@@ -143,7 +152,15 @@ public class TamayaOSGIInjector{
     static boolean isInjectionEnabled(Bundle bundle){
         String enabledVal = (String)bundle.getHeaders().get(ConfigInjectionService.TAMAYA_INJECTION_ENABLED_MANIFEST);
         if(enabledVal!=null){
-            return Boolean.parseBoolean(enabledVal);
+            enabledVal = enabledVal.toLowerCase(Locale.ENGLISH);
+            switch(enabledVal){
+                case "true":
+                case "yes":
+                case "on":
+                    return true;
+                default:
+                    return false;
+            }
         }
         return false;
     }

http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/85582e3f/modules/osgi/injection/src/test/java/org/apache/tamaya/osgi/injection/ActivatorTest.java
----------------------------------------------------------------------
diff --git a/modules/osgi/injection/src/test/java/org/apache/tamaya/osgi/injection/ActivatorTest.java b/modules/osgi/injection/src/test/java/org/apache/tamaya/osgi/injection/ActivatorTest.java
index 2a3e5d4..1ec4f0a 100644
--- a/modules/osgi/injection/src/test/java/org/apache/tamaya/osgi/injection/ActivatorTest.java
+++ b/modules/osgi/injection/src/test/java/org/apache/tamaya/osgi/injection/ActivatorTest.java
@@ -18,12 +18,23 @@
  */
 package org.apache.tamaya.osgi.injection;
 
+import org.junit.Before;
 import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.mockito.runners.MockitoJUnitRunner;
+import org.osgi.framework.Bundle;
+import org.osgi.framework.ServiceReference;
 
+import java.util.Hashtable;
+import java.util.function.Supplier;
+
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
 import static org.mockito.Matchers.anyObject;
 import static org.mockito.Matchers.eq;
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.verify;
 
 /**
@@ -32,6 +43,17 @@ import static org.mockito.Mockito.verify;
 @RunWith(MockitoJUnitRunner.class)
 public class ActivatorTest extends AbstractOSGITest{
 
+    private TamayaOSGIInjector injector;
+    private  Activator.TamayaConfigInjectionService injectionService;
+
+    @Before
+    public void init(){
+        injector = new TamayaOSGIInjector(this.bundleContext);
+        injectionService = new Activator.TamayaConfigInjectionService(
+                injector
+        );
+    }
+
     @Test
     public void startStop() throws Exception {
         Activator activator = new Activator();
@@ -40,4 +62,74 @@ public class ActivatorTest extends AbstractOSGITest{
         activator.stop(this.bundleContext);
     }
 
+    @Test
+    public void isInjectionEnabled_Service(){
+        ServiceReference reference = mock(ServiceReference.class);
+        Bundle bundle = mock(Bundle.class);
+        doReturn(bundle).when(reference).getBundle();
+        doReturn(new Hashtable<>()).when(bundle).getHeaders();
+        doReturn("true").when(reference).getProperty(ConfigInjectionService.TAMAYA_INJECTION_ENABLED_PROP);
+        assertTrue(injectionService.isInjectionEnabled(reference));
+        doReturn("yes").when(reference).getProperty(ConfigInjectionService.TAMAYA_INJECTION_ENABLED_PROP);
+        assertTrue(injectionService.isInjectionEnabled(reference));
+        doReturn("no").when(reference).getProperty(ConfigInjectionService.TAMAYA_INJECTION_ENABLED_PROP);
+        assertFalse(injectionService.isInjectionEnabled(reference));
+        doReturn(null).when(reference).getProperty(ConfigInjectionService.TAMAYA_INJECTION_ENABLED_PROP);
+        assertFalse(injectionService.isInjectionEnabled(reference));
+        doReturn("foo").when(reference).getProperty(ConfigInjectionService.TAMAYA_INJECTION_ENABLED_PROP);
+        assertFalse(injectionService.isInjectionEnabled(reference));
+        // service undefined, but bundle enabled
+        doReturn(null).when(reference).getProperty(ConfigInjectionService.TAMAYA_INJECTION_ENABLED_PROP);
+        doReturn(singleHashtable(ConfigInjectionService.TAMAYA_INJECTION_ENABLED_MANIFEST, "true")).when(bundle).getHeaders();
+        assertTrue(injectionService.isInjectionEnabled(reference));
+    }
+
+    private Hashtable singleHashtable(String key, String value) {
+        Hashtable t = new Hashtable();
+        t.put(key, value);
+        return t;
+    }
+
+    @Test
+    public void isInjectionEnabled_Bundle() {
+        Bundle bundle = mock(Bundle.class);
+        doReturn(singleHashtable(ConfigInjectionService.TAMAYA_INJECTION_ENABLED_MANIFEST, "true")).when(bundle).getHeaders();
+        assertTrue(injectionService.isInjectionEnabled(bundle));
+        doReturn(singleHashtable(ConfigInjectionService.TAMAYA_INJECTION_ENABLED_MANIFEST, "YeS")).when(bundle).getHeaders();
+        assertTrue(injectionService.isInjectionEnabled(bundle));
+        doReturn(singleHashtable(ConfigInjectionService.TAMAYA_INJECTION_ENABLED_MANIFEST, "on")).when(bundle).getHeaders();
+        assertTrue(injectionService.isInjectionEnabled(bundle));
+        doReturn(singleHashtable(ConfigInjectionService.TAMAYA_INJECTION_ENABLED_MANIFEST, "TRUE")).when(bundle).getHeaders();
+        assertTrue(injectionService.isInjectionEnabled(bundle));
+        doReturn(singleHashtable(ConfigInjectionService.TAMAYA_INJECTION_ENABLED_MANIFEST, "no")).when(bundle).getHeaders();
+        assertFalse(injectionService.isInjectionEnabled(bundle));
+        doReturn(singleHashtable(ConfigInjectionService.TAMAYA_INJECTION_ENABLED_MANIFEST, "foo")).when(bundle).getHeaders();
+        assertFalse(injectionService.isInjectionEnabled(bundle));
+        doReturn(new Hashtable<>()).when(bundle).getHeaders();
+        assertFalse(injectionService.isInjectionEnabled(bundle));
+    }
+
+    @Test
+    public void configure() throws Exception {
+        Example example = new Example();
+        Example result = injectionService.configure("tamaya", null, example);
+        assertNotNull(result);
+        assertTrue(result==example);
+        Example.checkExampleConfig(example);
+    }
+
+    @Test
+    public void getConfiguredSupplier() throws Exception {
+        Supplier<Example> supplier = injectionService.getConfiguredSupplier("tamaya", null, Example::new);
+        assertNotNull(supplier);
+        Example example = supplier.get();
+        Example.checkExampleConfig(example);
+    }
+
+    @Test
+    public void createTemplate() throws Exception {
+        TemplateExample template = injectionService.createTemplate("tamaya", null, TemplateExample.class);
+        Example.checkExampleConfig(template);
+    }
+
 }
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/85582e3f/modules/osgi/injection/src/test/java/org/apache/tamaya/osgi/injection/Example.java
----------------------------------------------------------------------
diff --git a/modules/osgi/injection/src/test/java/org/apache/tamaya/osgi/injection/Example.java b/modules/osgi/injection/src/test/java/org/apache/tamaya/osgi/injection/Example.java
new file mode 100644
index 0000000..b5726ad
--- /dev/null
+++ b/modules/osgi/injection/src/test/java/org/apache/tamaya/osgi/injection/Example.java
@@ -0,0 +1,51 @@
+/*
+ * 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.tamaya.osgi.injection;
+
+import org.apache.tamaya.inject.api.Config;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+
+/**
+ * Example class to be configured with injection.
+ */
+final class Example {
+    @Config("java.home")
+    String javaHome;
+    @Config("java.version")
+    String javaVersion;
+    @Config(value = "java.used", defaultValue = "true")
+    boolean javaUsed;
+
+    static void checkExampleConfig(Example example) {
+        assertNotNull(example);
+        assertEquals(example.javaHome, System.getProperty("java.home"));
+        assertEquals(example.javaVersion, System.getProperty("java.version"));
+        assertEquals(example.javaUsed, true);
+    }
+
+    static void checkExampleConfig(TemplateExample template) {
+        assertNotNull(template);
+        assertEquals(template.getJavaHome(), System.getProperty("java.home"));
+        assertEquals(template.javaVersion(), System.getProperty("java.version"));
+        assertEquals(template.isJavaUsed(), true);
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/85582e3f/modules/osgi/injection/src/test/java/org/apache/tamaya/osgi/injection/OSGIConfigAdminPropertySourceTest.java
----------------------------------------------------------------------
diff --git a/modules/osgi/injection/src/test/java/org/apache/tamaya/osgi/injection/OSGIConfigAdminPropertySourceTest.java b/modules/osgi/injection/src/test/java/org/apache/tamaya/osgi/injection/OSGIConfigAdminPropertySourceTest.java
index f83ac56..cff8c3f 100644
--- a/modules/osgi/injection/src/test/java/org/apache/tamaya/osgi/injection/OSGIConfigAdminPropertySourceTest.java
+++ b/modules/osgi/injection/src/test/java/org/apache/tamaya/osgi/injection/OSGIConfigAdminPropertySourceTest.java
@@ -42,6 +42,31 @@ public class OSGIConfigAdminPropertySourceTest extends AbstractOSGITest{
     }
 
     @Test
+    public void getPID() throws Exception {
+        assertEquals("tamaya", propertySource.getPid());
+    }
+
+    @Test
+    public void getLocation() throws Exception {
+        assertNull(propertySource.getLocation());
+    }
+
+    @Test
+    public void getDefaultOrdinal() throws Exception {
+        assertEquals(0, propertySource.getDefaultOrdinal());
+    }
+
+    @Test
+    public void getOrdinal() throws Exception {
+        assertEquals(0, propertySource.getOrdinal());
+    }
+
+    @Test
+    public void isScannable() throws Exception {
+        assertEquals(true, propertySource.isScannable());
+    }
+
+    @Test
     public void get() throws Exception {
         PropertyValue val = propertySource.get("java.home");
         assertNotNull(val);

http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/85582e3f/modules/osgi/injection/src/test/java/org/apache/tamaya/osgi/injection/OSGIConfigurationInjectorTest.java
----------------------------------------------------------------------
diff --git a/modules/osgi/injection/src/test/java/org/apache/tamaya/osgi/injection/OSGIConfigurationInjectorTest.java b/modules/osgi/injection/src/test/java/org/apache/tamaya/osgi/injection/OSGIConfigurationInjectorTest.java
index 6735e7a..cb9c22b 100644
--- a/modules/osgi/injection/src/test/java/org/apache/tamaya/osgi/injection/OSGIConfigurationInjectorTest.java
+++ b/modules/osgi/injection/src/test/java/org/apache/tamaya/osgi/injection/OSGIConfigurationInjectorTest.java
@@ -18,7 +18,6 @@
  */
 package org.apache.tamaya.osgi.injection;
 
-import org.apache.tamaya.inject.api.Config;
 import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.mockito.runners.MockitoJUnitRunner;
@@ -33,6 +32,17 @@ import static org.junit.Assert.*;
 @RunWith(MockitoJUnitRunner.class)
 public class OSGIConfigurationInjectorTest extends AbstractOSGITest{
 
+    @Test
+    public void getPid() throws Exception {
+        OSGIConfigurationInjector injector = new OSGIConfigurationInjector(cm, "getPid");
+        assertEquals("getPid", injector.getPid());
+    }
+
+    @Test
+    public void getLocation() throws Exception {
+        OSGIConfigurationInjector injector = new OSGIConfigurationInjector(cm, "getLocation", "/test");
+        assertEquals("/test", injector.getLocation());
+    }
 
     @Test
     public void configure() throws Exception {
@@ -41,7 +51,7 @@ public class OSGIConfigurationInjectorTest extends AbstractOSGITest{
         Example result = injector.configure(example);
         assertNotNull(result);
         assertTrue(result==example);
-        checkExampleConfig(example);
+        Example.checkExampleConfig(example);
     }
 
     @Test
@@ -50,48 +60,15 @@ public class OSGIConfigurationInjectorTest extends AbstractOSGITest{
         Supplier<Example> supplier = injector.getConfiguredSupplier(Example::new);
         assertNotNull(supplier);
         Example example = supplier.get();
-        checkExampleConfig(example);
+        Example.checkExampleConfig(example);
     }
 
     @Test
     public void createTemplate() throws Exception {
         OSGIConfigurationInjector injector = new OSGIConfigurationInjector(cm, "OSGIConfigurationInjectorTest");
         TemplateExample template = injector.createTemplate(TemplateExample.class);
-        checkExampleConfig(template);
-    }
-
-    private void checkExampleConfig(Example example) {
-        assertNotNull(example);
-        assertEquals(example.javaHome, System.getProperty("java.home"));
-        assertEquals(example.javaVersion, System.getProperty("java.version"));
-        assertEquals(example.javaUsed, true);
+        Example.checkExampleConfig(template);
     }
 
-    private void checkExampleConfig(TemplateExample template) {
-        assertNotNull(template);
-        assertEquals(template.getJavaHome(), System.getProperty("java.home"));
-        assertEquals(template.javaVersion(), System.getProperty("java.version"));
-        assertEquals(template.isJavaUsed(), true);
-    }
-
-    private static final class Example {
-        @Config("java.home")
-        String javaHome;
-        @Config("java.version")
-        String javaVersion;
-        @Config(value = "java.used", defaultValue = "true")
-        boolean javaUsed;
-    }
-
-    private interface TemplateExample {
 
-        @Config("java.home")
-        String getJavaHome();
-
-        @Config("java.version")
-        String javaVersion();
-
-        @Config(value = "java.used", defaultValue = "true")
-        boolean isJavaUsed();
-    }
 }
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/85582e3f/modules/osgi/injection/src/test/java/org/apache/tamaya/osgi/injection/TemplateExample.java
----------------------------------------------------------------------
diff --git a/modules/osgi/injection/src/test/java/org/apache/tamaya/osgi/injection/TemplateExample.java b/modules/osgi/injection/src/test/java/org/apache/tamaya/osgi/injection/TemplateExample.java
new file mode 100644
index 0000000..4605b4d
--- /dev/null
+++ b/modules/osgi/injection/src/test/java/org/apache/tamaya/osgi/injection/TemplateExample.java
@@ -0,0 +1,36 @@
+/*
+ * 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.tamaya.osgi.injection;
+
+import org.apache.tamaya.inject.api.Config;
+
+/**
+ * Example template interface.
+ */
+interface TemplateExample {
+
+    @Config("java.home")
+    String getJavaHome();
+
+    @Config("java.version")
+    String javaVersion();
+
+    @Config(value = "java.used", defaultValue = "true")
+    boolean isJavaUsed();
+}