Repository: incubator-tamaya-sandbox Updated Branches: refs/heads/master 55b1f20ed -> e60d9ddb9
TAMAYA-300 Added tests and fixes. 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/e60d9ddb Tree: http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/tree/e60d9ddb Diff: http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/diff/e60d9ddb Branch: refs/heads/master Commit: e60d9ddb9986cf8491377a8440d478d8a7103651 Parents: 55b1f20 Author: Anatole Tresch <[email protected]> Authored: Sat Sep 30 23:45:30 2017 +0200 Committer: Anatole Tresch <[email protected]> Committed: Sat Sep 30 23:45:30 2017 +0200 ---------------------------------------------------------------------- osgi/gogo-shell/pom.xml | 4 + .../tamaya/gogo/shell/AbstractOSGITest.java | 99 ++++++++++++++++++++ .../apache/tamaya/gogo/shell/ActivatorTest.java | 46 +++++++++ 3 files changed, 149 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/e60d9ddb/osgi/gogo-shell/pom.xml ---------------------------------------------------------------------- diff --git a/osgi/gogo-shell/pom.xml b/osgi/gogo-shell/pom.xml index 1b61576..2b7bb7d 100644 --- a/osgi/gogo-shell/pom.xml +++ b/osgi/gogo-shell/pom.xml @@ -68,6 +68,10 @@ <groupId>junit</groupId> <artifactId>junit</artifactId> </dependency> + <dependency> + <groupId>org.mockito</groupId> + <artifactId>mockito-core</artifactId> + </dependency> </dependencies> </project> http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/e60d9ddb/osgi/gogo-shell/src/test/java/org/apache/tamaya/gogo/shell/AbstractOSGITest.java ---------------------------------------------------------------------- diff --git a/osgi/gogo-shell/src/test/java/org/apache/tamaya/gogo/shell/AbstractOSGITest.java b/osgi/gogo-shell/src/test/java/org/apache/tamaya/gogo/shell/AbstractOSGITest.java new file mode 100644 index 0000000..511769a --- /dev/null +++ b/osgi/gogo-shell/src/test/java/org/apache/tamaya/gogo/shell/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.gogo.shell; + +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/e60d9ddb/osgi/gogo-shell/src/test/java/org/apache/tamaya/gogo/shell/ActivatorTest.java ---------------------------------------------------------------------- diff --git a/osgi/gogo-shell/src/test/java/org/apache/tamaya/gogo/shell/ActivatorTest.java b/osgi/gogo-shell/src/test/java/org/apache/tamaya/gogo/shell/ActivatorTest.java new file mode 100644 index 0000000..03608c3 --- /dev/null +++ b/osgi/gogo-shell/src/test/java/org/apache/tamaya/gogo/shell/ActivatorTest.java @@ -0,0 +1,46 @@ +/* + * 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.gogo.shell; + +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 30.09.2017. + */ +@RunWith(MockitoJUnitRunner.class) +public class ActivatorTest extends AbstractOSGITest{ + + @Test + public void startStop() throws Exception { + Activator activator = new Activator(); + activator.start(super.bundleContext); + verify(bundleContext).registerService(eq(BackupCommands.class), anyObject(), anyObject()); + verify(bundleContext).registerService(eq(ConfigCommands.class), anyObject(), anyObject()); + verify(bundleContext).registerService(eq(HistoryCommands.class), anyObject(), anyObject()); + verify(bundleContext).registerService(eq(SettingsCommands.class), anyObject(), anyObject()); + activator.stop(super.bundleContext); + } + +} \ No newline at end of file
