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/03 12:50:38 UTC
incubator-tamaya-sandbox git commit: TAMAYA-301 Implemented OSGI
Configuration injection.
Repository: incubator-tamaya-sandbox
Updated Branches:
refs/heads/master 3bcac1b82 -> 3e5225120
TAMAYA-301 Implemented OSGI Configuration injection.
Project: http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/commit/3e522512
Tree: http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/tree/3e522512
Diff: http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/diff/3e522512
Branch: refs/heads/master
Commit: 3e5225120fcfb28074c8744a66d2bbefe729b653
Parents: 3bcac1b
Author: Anatole Tresch <an...@apache.org>
Authored: Tue Oct 3 14:50:29 2017 +0200
Committer: Anatole Tresch <an...@apache.org>
Committed: Tue Oct 3 14:50:29 2017 +0200
----------------------------------------------------------------------
.../OSGIConfigAdminPropertySource.java | 91 +++++++++++++++
.../injection/OSGIConfigurationInjector.java | 58 ++++++++++
.../osgi/injection/TamayaOSGIInjector.java | 113 +++++++++++++++++++
.../tamaya/osgi/injection/AbstractOSGITest.java | 99 ++++++++++++++++
.../tamaya/osgi/injection/ActivatorTest.java | 43 +++++++
.../OSGIConfigAdminPropertySourceTest.java | 65 +++++++++++
.../OSGIConfigurationInjectorTest.java | 97 ++++++++++++++++
osgi/pom.xml | 1 +
8 files changed, 567 insertions(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/3e522512/osgi/injection/src/main/java/org/apache/tamaya/osgi/injection/OSGIConfigAdminPropertySource.java
----------------------------------------------------------------------
diff --git a/osgi/injection/src/main/java/org/apache/tamaya/osgi/injection/OSGIConfigAdminPropertySource.java b/osgi/injection/src/main/java/org/apache/tamaya/osgi/injection/OSGIConfigAdminPropertySource.java
new file mode 100644
index 0000000..6689b31
--- /dev/null
+++ b/osgi/injection/src/main/java/org/apache/tamaya/osgi/injection/OSGIConfigAdminPropertySource.java
@@ -0,0 +1,91 @@
+package org.apache.tamaya.osgi.injection;
+
+import org.apache.tamaya.spi.PropertySource;
+import org.apache.tamaya.spi.PropertyValue;
+import org.apache.tamaya.spisupport.BasePropertySource;
+import org.osgi.service.cm.Configuration;
+import org.osgi.service.cm.ConfigurationAdmin;
+
+import java.io.IOException;
+import java.util.*;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+/**
+ * This is a Tamaya PropertySource, which internally wraps the OSGI ConfigAdmin service, preconfigured
+ * for a PID and (optionally) location.
+ */
+public class OSGIConfigAdminPropertySource extends BasePropertySource{
+
+ private static final Logger LOG = Logger.getLogger(OSGIConfigAdminPropertySource.class.getName());
+ private ConfigurationAdmin configurationAdmin;
+ private String pid;
+ private String location;
+
+ public OSGIConfigAdminPropertySource(ConfigurationAdmin configurationAdmin, String pid){
+ this.configurationAdmin = Objects.requireNonNull(configurationAdmin);
+ this.pid = Objects.requireNonNull(pid);
+ }
+
+ public OSGIConfigAdminPropertySource(ConfigurationAdmin configurationAdmin, String pid, String location){
+ this.configurationAdmin = Objects.requireNonNull(configurationAdmin);
+ this.pid = Objects.requireNonNull(pid);
+ this.location = location;
+ }
+
+ /**
+ * Get the configured OSGI service PID.
+ * @return the pid, nnever null.
+ */
+ public String getPid() {
+ return pid;
+ }
+
+ /**
+ * Get the configured OSGI config location, may be null.
+ * @return the location, or null.
+ */
+ public String getLocation() {
+ return location;
+ }
+
+ @Override
+ public PropertyValue get(String key) {
+ try {
+ Configuration osgiConfig = configurationAdmin.getConfiguration(pid, location);
+ Dictionary<String,Object> props = osgiConfig.getProperties();
+ if(props!=null){
+ Object value = props.get(key);
+ if(value!=null) {
+ return PropertyValue.of(key, String.valueOf(value), "OSGI ConfigAdmin: " + pid);
+ }
+ }
+ } catch (IOException e) {
+ LOG.log(Level.FINEST, e, () -> "No config for PID: " + pid);
+ }
+ return null;
+ }
+
+ @Override
+ public Map<String, PropertyValue> getProperties() {
+ try {
+ Configuration osgiConfig = configurationAdmin.getConfiguration(pid);
+ Dictionary<String,Object> props = osgiConfig.getProperties();
+ if(props!=null){
+ Map<String, PropertyValue> result = new HashMap<>();
+ Enumeration<String> keys = props.keys();
+ while(keys.hasMoreElements()){
+ String key = keys.nextElement();
+ Object value = props.get(key);
+ result.put(key, PropertyValue.of(key, String.valueOf(value), "OSGI ConfigAdmin: " + pid));
+ }
+ return result;
+ }
+ } catch (IOException e) {
+ LOG.log(Level.FINEST, e, () -> "No config for PID: " + pid);
+ }
+ return Collections.emptyMap();
+ }
+}
+
+
http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/3e522512/osgi/injection/src/main/java/org/apache/tamaya/osgi/injection/OSGIConfigurationInjector.java
----------------------------------------------------------------------
diff --git a/osgi/injection/src/main/java/org/apache/tamaya/osgi/injection/OSGIConfigurationInjector.java b/osgi/injection/src/main/java/org/apache/tamaya/osgi/injection/OSGIConfigurationInjector.java
new file mode 100644
index 0000000..ddec058
--- /dev/null
+++ b/osgi/injection/src/main/java/org/apache/tamaya/osgi/injection/OSGIConfigurationInjector.java
@@ -0,0 +1,58 @@
+package org.apache.tamaya.osgi.injection;
+
+import org.apache.tamaya.Configuration;
+import org.apache.tamaya.ConfigurationProvider;
+import org.apache.tamaya.inject.ConfigurationInjection;
+import org.osgi.framework.BundleContext;
+import org.osgi.service.cm.ConfigurationAdmin;
+
+import java.util.Objects;
+import java.util.function.Supplier;
+
+/**
+ * Created by atsti on 03.10.2017.
+ */
+final class OSGIConfigurationInjector{
+
+ private ConfigurationAdmin cm;
+ private Configuration tamayaOSGIConfiguration;
+ private String pid;
+ private String location;
+
+
+ public OSGIConfigurationInjector(ConfigurationAdmin cm, String pid){
+ this(cm, pid, null);
+ }
+
+ public OSGIConfigurationInjector(ConfigurationAdmin cm, String pid, String location){
+ this.cm = Objects.requireNonNull(cm);
+ tamayaOSGIConfiguration = ConfigurationProvider.createConfiguration(
+ ConfigurationProvider.getConfigurationContextBuilder()
+ .addDefaultPropertyConverters()
+ .addDefaultPropertyFilters()
+ .addPropertySources(new OSGIConfigAdminPropertySource(cm, pid, location))
+ .build());
+ }
+
+ public <T> T configure(T instance){
+ return ConfigurationInjection.getConfigurationInjector()
+ .configure(instance, tamayaOSGIConfiguration);
+ }
+
+ public <T> Supplier<T> getConfiguredSupplier(java.util.function.Supplier<T> supplier){
+ return ConfigurationInjection.getConfigurationInjector()
+ .getConfiguredSupplier(supplier, tamayaOSGIConfiguration);
+ }
+
+ /**
+ * Creates a template implementing the annotated methods based on current configuration data.
+ *
+ * @param <T> the type of the template.
+ * @param templateType the type of the template to be created.
+ * @return the configured template.
+ */
+ public <T> T createTemplate(Class<T> templateType){
+ return ConfigurationInjection.getConfigurationInjector()
+ .createTemplate(templateType, tamayaOSGIConfiguration);
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/3e522512/osgi/injection/src/main/java/org/apache/tamaya/osgi/injection/TamayaOSGIInjector.java
----------------------------------------------------------------------
diff --git a/osgi/injection/src/main/java/org/apache/tamaya/osgi/injection/TamayaOSGIInjector.java b/osgi/injection/src/main/java/org/apache/tamaya/osgi/injection/TamayaOSGIInjector.java
new file mode 100644
index 0000000..4c04d41
--- /dev/null
+++ b/osgi/injection/src/main/java/org/apache/tamaya/osgi/injection/TamayaOSGIInjector.java
@@ -0,0 +1,113 @@
+/*
+ * 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.osgi.TamayaConfigPlugin;
+import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.Constants;
+import org.osgi.framework.ServiceReference;
+import org.osgi.service.cm.ConfigurationAdmin;
+import org.osgi.util.tracker.ServiceTracker;
+
+import java.util.Map;
+import java.util.Objects;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.logging.Logger;
+
+/**
+ * Class that monitors services and configures them if they have {@code Tamaya-Config = true} in the
+ * service settings.
+ */
+public class TamayaOSGIInjector {
+
+ public static final String TAMAYA_INJECTION_ENABLED_MANIFEST = "Tamaya-Config-Inject";
+ public static final String TAMAYA_INJECTION_ENABLED_PROP = "tamaya-config-inject";
+ private static final Logger log = Logger.getLogger(TamayaConfigPlugin.class.getName());
+ private BundleContext context;
+ private ConfigurationAdmin cm;
+ private ServiceTracker<Object, Object> tracker;
+ private static final Map<String, OSGIConfigurationInjector> INJECTORS = new ConcurrentHashMap<>();
+
+ public TamayaOSGIInjector(BundleContext context) {
+ this.context = Objects.requireNonNull(context);
+ }
+
+ public void start(){
+ tracker = new ServiceTracker<Object, Object>(context, Object.class.getName(), null) {
+ @Override
+ public Object addingService(ServiceReference reference) {
+ log.info("Checking service for configuration: " + reference);
+ Object service = super.addingService(reference);
+ if(isInjectionEnabled(reference)) {
+ String pid = (String)reference.getProperty(Constants.SERVICE_PID);
+ if(pid==null){
+ pid = reference.getBundle().getSymbolicName();
+ }
+ OSGIConfigurationInjector injector = getInjector(pid, reference.getBundle().getLocation());
+ injector.configure(service);
+ }
+ return service;
+ }
+
+ @Override
+ public void modifiedService(ServiceReference<Object> reference, Object service) {
+ super.modifiedService(reference, service);
+ if(isInjectionEnabled(reference)) {
+ String pid = (String)reference.getProperty(Constants.SERVICE_PID);
+ if(pid==null){
+ pid = reference.getBundle().getSymbolicName();
+ }
+ OSGIConfigurationInjector injector = getInjector(pid, reference.getBundle().getLocation());
+ injector.configure(service);
+ }
+ }
+ };
+ tracker.open(true);
+ }
+
+ public void stop(){
+ if(tracker!=null){
+ tracker.close();
+ tracker = null;
+ }
+ }
+
+ private OSGIConfigurationInjector getInjector(String pid, String location){
+ String key = location==null?pid.trim():pid.trim()+"::"+location.trim();
+ OSGIConfigurationInjector injector = INJECTORS.get(key);
+ if(injector==null){
+ injector = new OSGIConfigurationInjector(cm, pid, location);
+ INJECTORS.put(key, injector);
+ }
+ return injector;
+ }
+
+ public static boolean isInjectionEnabled(ServiceReference reference){
+ String enabledVal = (String)reference.getProperty(TAMAYA_INJECTION_ENABLED_PROP);
+ if(enabledVal!=null){
+ return Boolean.parseBoolean(enabledVal);
+ }
+ enabledVal = reference.getBundle().getHeaders().get(TAMAYA_INJECTION_ENABLED_MANIFEST);
+ if(enabledVal!=null){
+ return Boolean.parseBoolean(enabledVal);
+ }
+ return false;
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/3e522512/osgi/injection/src/test/java/org/apache/tamaya/osgi/injection/AbstractOSGITest.java
----------------------------------------------------------------------
diff --git a/osgi/injection/src/test/java/org/apache/tamaya/osgi/injection/AbstractOSGITest.java b/osgi/injection/src/test/java/org/apache/tamaya/osgi/injection/AbstractOSGITest.java
new file mode 100644
index 0000000..e3f73c3
--- /dev/null
+++ b/osgi/injection/src/test/java/org/apache/tamaya/osgi/injection/AbstractOSGITest.java
@@ -0,0 +1,99 @@
+/*
+ * 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.osgi.TamayaConfigPlugin;
+import org.junit.Before;
+import org.mockito.Mock;
+import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.ServiceReference;
+import org.osgi.service.cm.Configuration;
+import org.osgi.service.cm.ConfigurationAdmin;
+
+import java.util.Dictionary;
+import java.util.Hashtable;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+
+import static org.mockito.Matchers.any;
+import static org.mockito.Mockito.*;
+
+/**
+ * Created by atsticks on 27.09.17.
+ */
+public abstract class AbstractOSGITest {
+
+ private Map<String,Hashtable<String, Object>> properties = new ConcurrentHashMap<>();
+
+ @Mock
+ protected BundleContext bundleContext;
+
+ @Mock
+ protected ConfigurationAdmin cm;
+
+ @Mock
+ private ServiceReference<ConfigurationAdmin> cmRef;
+ @Mock
+ private ServiceReference<TamayaConfigPlugin> tamayaRef;
+
+ protected TamayaConfigPlugin tamayaConfigPlugin;
+
+ protected Dictionary<String,Object> getProperties(String pid){
+ return this.properties.get(pid);
+ }
+
+ @Before
+ public void setup()throws Exception{
+ doAnswer(invocation -> {
+ return initConfigurationMock((String)invocation.getArguments()[0]);
+ }).when(cm).getConfiguration(any());
+ doAnswer(invocation -> {
+ return initConfigurationMock((String)invocation.getArguments()[0]);
+ }).when(cm).getConfiguration(any(), any());
+ doReturn(new Bundle[0]).when(bundleContext).getBundles();
+ doReturn(cmRef).when(bundleContext).getServiceReference(ConfigurationAdmin.class);
+ doReturn(cm).when(bundleContext).getService(cmRef);
+ doReturn(tamayaRef).when(bundleContext).getServiceReference(TamayaConfigPlugin.class);
+ tamayaConfigPlugin = new TamayaConfigPlugin(bundleContext);
+ doReturn(tamayaConfigPlugin).when(bundleContext).getService(tamayaRef);
+ }
+
+ protected Configuration initConfigurationMock(final String pid)throws Exception{
+ Configuration config = mock(Configuration.class);
+ doAnswer(invocation -> {
+ Hashtable<String,Object> props = properties.get(pid);
+ props.clear();
+ props.putAll((Map<? extends String, ?>) invocation.getArguments()[0]);
+ return null;
+ }).when(config).update(any(Dictionary.class));
+ doAnswer(invocation -> {
+ Hashtable<String,Object> props = properties.get(pid);
+ if(props==null){
+ props = new Hashtable<>();
+ properties.put(pid, props);
+ for(Map.Entry en:System.getProperties().entrySet()){
+ props.put(en.getKey().toString(), en.getValue());
+ }
+ }
+ return new Hashtable<>(props);
+ }).when(config).getProperties();
+ return config;
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/3e522512/osgi/injection/src/test/java/org/apache/tamaya/osgi/injection/ActivatorTest.java
----------------------------------------------------------------------
diff --git a/osgi/injection/src/test/java/org/apache/tamaya/osgi/injection/ActivatorTest.java b/osgi/injection/src/test/java/org/apache/tamaya/osgi/injection/ActivatorTest.java
new file mode 100644
index 0000000..9cd587f
--- /dev/null
+++ b/osgi/injection/src/test/java/org/apache/tamaya/osgi/injection/ActivatorTest.java
@@ -0,0 +1,43 @@
+/*
+ * 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.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.runners.MockitoJUnitRunner;
+
+import static org.mockito.Matchers.anyObject;
+import static org.mockito.Matchers.eq;
+import static org.mockito.Mockito.verify;
+
+/**
+ * Created by atsti on 03.10.2017.
+ */
+@RunWith(MockitoJUnitRunner.class)
+public class ActivatorTest extends AbstractOSGITest{
+
+ @Test
+ public void startStop() throws Exception {
+ Activator activator = new Activator();
+ activator.start(this.bundleContext);
+ verify(bundleContext).registerService(eq(TamayaOSGIInjector.class), anyObject(), anyObject());
+ activator.stop(this.bundleContext);
+ }
+
+}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/3e522512/osgi/injection/src/test/java/org/apache/tamaya/osgi/injection/OSGIConfigAdminPropertySourceTest.java
----------------------------------------------------------------------
diff --git a/osgi/injection/src/test/java/org/apache/tamaya/osgi/injection/OSGIConfigAdminPropertySourceTest.java b/osgi/injection/src/test/java/org/apache/tamaya/osgi/injection/OSGIConfigAdminPropertySourceTest.java
new file mode 100644
index 0000000..f83ac56
--- /dev/null
+++ b/osgi/injection/src/test/java/org/apache/tamaya/osgi/injection/OSGIConfigAdminPropertySourceTest.java
@@ -0,0 +1,65 @@
+/*
+ * 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.spi.PropertyValue;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.runners.MockitoJUnitRunner;
+
+import java.util.Map;
+
+import static org.junit.Assert.*;
+
+/**
+ * Created by atsti on 03.10.2017.
+ */
+@RunWith(MockitoJUnitRunner.class)
+public class OSGIConfigAdminPropertySourceTest extends AbstractOSGITest{
+
+ OSGIConfigAdminPropertySource propertySource;
+
+ @Before
+ public void init(){
+ propertySource = new OSGIConfigAdminPropertySource(cm, "tamaya");
+ }
+
+ @Test
+ public void get() throws Exception {
+ PropertyValue val = propertySource.get("java.home");
+ assertNotNull(val);
+ assertEquals(val.getKey(), "java.home");
+ assertEquals(val.getValue(), System.getProperty("java.home"));
+ val = propertySource.get("foo.bar");
+ assertNull(val);
+ }
+
+ @Test
+ public void getProperties() throws Exception {
+ Map<String,PropertyValue> props = propertySource.getProperties();
+ assertNotNull(props);
+ PropertyValue val = props.get("java.home");
+ assertEquals(val.getKey(), "java.home");
+ assertEquals(val.getValue(), System.getProperty("java.home"));
+ val = props.get("foo.bar");
+ assertNull(val);
+ }
+
+}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/3e522512/osgi/injection/src/test/java/org/apache/tamaya/osgi/injection/OSGIConfigurationInjectorTest.java
----------------------------------------------------------------------
diff --git a/osgi/injection/src/test/java/org/apache/tamaya/osgi/injection/OSGIConfigurationInjectorTest.java b/osgi/injection/src/test/java/org/apache/tamaya/osgi/injection/OSGIConfigurationInjectorTest.java
new file mode 100644
index 0000000..6735e7a
--- /dev/null
+++ b/osgi/injection/src/test/java/org/apache/tamaya/osgi/injection/OSGIConfigurationInjectorTest.java
@@ -0,0 +1,97 @@
+/*
+ * 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 org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.runners.MockitoJUnitRunner;
+
+import java.util.function.Supplier;
+
+import static org.junit.Assert.*;
+
+/**
+ * Created by atsti on 03.10.2017.
+ */
+@RunWith(MockitoJUnitRunner.class)
+public class OSGIConfigurationInjectorTest extends AbstractOSGITest{
+
+
+ @Test
+ public void configure() throws Exception {
+ OSGIConfigurationInjector injector = new OSGIConfigurationInjector(cm, "OSGIConfigurationInjectorTest");
+ Example example = new Example();
+ Example result = injector.configure(example);
+ assertNotNull(result);
+ assertTrue(result==example);
+ checkExampleConfig(example);
+ }
+
+ @Test
+ public void getConfiguredSupplier() throws Exception {
+ OSGIConfigurationInjector injector = new OSGIConfigurationInjector(cm, "OSGIConfigurationInjectorTest");
+ Supplier<Example> supplier = injector.getConfiguredSupplier(Example::new);
+ assertNotNull(supplier);
+ Example example = supplier.get();
+ 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);
+ }
+
+ 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-sandbox/blob/3e522512/osgi/pom.xml
----------------------------------------------------------------------
diff --git a/osgi/pom.xml b/osgi/pom.xml
index 40ba101..18df6ee 100644
--- a/osgi/pom.xml
+++ b/osgi/pom.xml
@@ -101,6 +101,7 @@
<module>karaf-shell</module>
<module>karaf-features</module>
<module>gogo-shell</module>
+ <module>injection</module>
</modules>
</project>