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/09/28 20:12:57 UTC
[43/45] incubator-tamaya-sandbox git commit: TAMAYA-297: Added
tests/small fixes.
TAMAYA-297: Added tests/small 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/a8029f4e
Tree: http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/tree/a8029f4e
Diff: http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/diff/a8029f4e
Branch: refs/heads/master
Commit: a8029f4ee1b79dd5ec1730a994a3dcfbd8df6890
Parents: 563437b
Author: anatole <an...@apache.org>
Authored: Tue Sep 26 09:20:03 2017 +0200
Committer: anatole <an...@apache.org>
Committed: Tue Sep 26 09:20:03 2017 +0200
----------------------------------------------------------------------
.../java/org/apache/tamaya/osgi/Backups.java | 50 ++++++++-
.../org/apache/tamaya/osgi/BackupsTest.java | 107 +++++++++++++++++++
2 files changed, 155 insertions(+), 2 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/a8029f4e/osgi/common/src/main/java/org/apache/tamaya/osgi/Backups.java
----------------------------------------------------------------------
diff --git a/osgi/common/src/main/java/org/apache/tamaya/osgi/Backups.java b/osgi/common/src/main/java/org/apache/tamaya/osgi/Backups.java
index 01074fd..6205895 100644
--- a/osgi/common/src/main/java/org/apache/tamaya/osgi/Backups.java
+++ b/osgi/common/src/main/java/org/apache/tamaya/osgi/Backups.java
@@ -28,7 +28,9 @@ import java.util.logging.Level;
import java.util.logging.Logger;
/**
- * Created by atsticks on 19.09.17.
+ * Singleton class to store OSGI configuration backups before change the OSGI
+ * Config with Tamaya settings. This allows to restore the configuration in
+ * case of issues.
*/
public final class Backups {
@@ -38,10 +40,20 @@ public final class Backups {
private Backups(){}
+ /**
+ * Sets the given backup for a PID.
+ * @param pid the PID, not null.
+ * @param config the config to store.
+ */
public static void set(String pid, Dictionary<String,?> config){
initialConfigState.put(pid, toHashtable(config));
}
+ /**
+ * Converts the dictionary to a hash table to enabled serialization.
+ * @param dictionary he config, not null.
+ * @return the correspoinding Hashtable
+ */
private static Hashtable<String, ?> toHashtable(Dictionary<String, ?> dictionary) {
if (dictionary == null) {
return null;
@@ -58,30 +70,60 @@ public final class Backups {
return map;
}
+ /**
+ * Removes a backup.
+ * @param pid the PID, not null.
+ * @return
+ */
public static Dictionary<String,?> remove(String pid){
return initialConfigState.remove(pid);
}
+ /**
+ * Removes all backups.
+ */
public static void removeAll(){
initialConfigState.clear();
}
+ /**
+ * Get a backup for a PID.
+ * @param pid the PID, not null.
+ * @return the backup found, or null.
+ */
public static Dictionary<String,?> get(String pid){
return initialConfigState.get(pid);
}
+ /**
+ * Get all current stored backups.
+ * @return The backups stored, by PID.
+ */
public static Map<String,Dictionary<String,?>> get(){
return new HashMap<>(initialConfigState);
}
+ /**
+ * Get all current kjnown PIDs.
+ * @return the PIDs, never null.
+ */
public static Set<String> getPids(){
return initialConfigState.keySet();
}
+ /**
+ * Checks if a backup exists for a given PID.
+ * @param pid the pid, not null.
+ * @return
+ */
public static boolean contains(String pid){
return initialConfigState.containsKey(pid);
}
+ /**
+ * Saves the bachups into the given config.
+ * @param config the config, not nul.
+ */
public static void save(Dictionary<String,Object> config){
try{
ByteArrayOutputStream bos = new ByteArrayOutputStream();
@@ -89,12 +131,16 @@ public final class Backups {
oos.writeObject(initialConfigState);
oos.flush();
Base64.getEncoder().encode(bos.toByteArray());
- config.put(TAMAYA_BACKUP, Base64.getEncoder().encode(bos.toByteArray()));
+ config.put(TAMAYA_BACKUP, Base64.getEncoder().encodeToString(bos.toByteArray()));
}catch(Exception e){
LOG.log(Level.SEVERE, "Failed to restore OSGI Backups.", e);
}
}
+ /**
+ * Restores the backups ino the given config.
+ * @param config the config, not null.
+ */
public static void restore(Dictionary<String,Object> config){
try{
String serialized = (String)config.get("tamaya.backup");
http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/a8029f4e/osgi/common/src/test/java/org/apache/tamaya/osgi/BackupsTest.java
----------------------------------------------------------------------
diff --git a/osgi/common/src/test/java/org/apache/tamaya/osgi/BackupsTest.java b/osgi/common/src/test/java/org/apache/tamaya/osgi/BackupsTest.java
new file mode 100644
index 0000000..9039332
--- /dev/null
+++ b/osgi/common/src/test/java/org/apache/tamaya/osgi/BackupsTest.java
@@ -0,0 +1,107 @@
+/*
+ * 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;
+
+import org.junit.Test;
+
+import java.util.Dictionary;
+import java.util.Hashtable;
+import java.util.Set;
+
+import static org.junit.Assert.*;
+
+
+/**
+ * Created by atsticks on 26.09.17.
+ */
+public class BackupsTest {
+
+
+ private Dictionary<String,Object> createConfig(String pid){
+ Hashtable<String,Object> config = new Hashtable<>();
+ config.put("test.id", pid);
+ return config;
+ }
+ @Test
+ public void setGet() throws Exception {
+ Dictionary<String,Object> cfg = createConfig("set");
+ Backups.set("set", cfg);
+ assertEquals(Backups.get("set"), cfg);
+ }
+
+ @Test
+ public void remove() throws Exception {
+ Dictionary<String,Object> cfg = createConfig("remove");
+ Backups.set("remove", cfg);
+ assertEquals(Backups.get("remove"), cfg);
+ Backups.remove("remove");
+ assertEquals(Backups.get("remove"), null);
+ }
+
+ @Test
+ public void removeAll() throws Exception {
+ Dictionary<String,Object> cfg = createConfig("remove");
+ Backups.set("remove", cfg);
+ assertEquals(Backups.get("remove"), cfg);
+ Backups.removeAll();
+ assertEquals(Backups.get("remove"), null);
+ }
+
+ @Test
+ public void get1() throws Exception {
+ }
+
+ @Test
+ public void getPids() throws Exception {
+ Dictionary<String,Object> cfg = createConfig("getPids");
+ Backups.set("getPids", cfg);
+ Set<String> pids = Backups.getPids();
+ assertNotNull(pids);
+ assertTrue(pids.contains("getPids"));
+ Backups.removeAll();
+ pids = Backups.getPids();
+ assertNotNull(pids);
+ assertTrue(pids.isEmpty());
+ }
+
+ @Test
+ public void contains() throws Exception {
+ Dictionary<String,Object> cfg = createConfig("contains");
+ Backups.set("contains", cfg);
+ assertTrue(Backups.contains("contains"));
+ assertFalse(Backups.contains("foo"));
+ Backups.removeAll();
+ assertFalse(Backups.contains("contains"));
+ assertFalse(Backups.contains("foo"));
+ }
+
+ @Test
+ public void saveRestore() throws Exception {
+ Dictionary<String,Object> store = new Hashtable<>();
+ Dictionary<String,Object> cfg = createConfig("contains");
+ Backups.set("saveRestore", cfg);
+ Backups.save(store);
+ Backups.removeAll();
+ assertFalse(Backups.contains("saveRestore"));
+ Backups.restore(store);
+ assertTrue(Backups.contains("saveRestore"));
+ assertEquals(Backups.get("saveRestore"), cfg);
+ }
+
+}
\ No newline at end of file