mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-04-29 19:45:01 +02:00
Revert "Bug 462353 - [visualizer] Add support for persistent List<T> and
Map<String,T> parameters in PersistentSettingsManager"
This reverts commit e329e6a518
.
Change-Id: I8aef5384e73a62a3efc21ea046a71a1bf5f5e1d6
This commit is contained in:
parent
a2cff345b1
commit
20328a9961
5 changed files with 63 additions and 811 deletions
|
@ -37,19 +37,20 @@ import org.xml.sax.helpers.DefaultHandler;
|
||||||
|
|
||||||
/** encodes and decodes memento to and from different data types; list, map, String*/
|
/** encodes and decodes memento to and from different data types; list, map, String*/
|
||||||
public class MementoUtils {
|
public class MementoUtils {
|
||||||
|
|
||||||
protected static final String ROOT_ELEMENT_TAGNAME = "root_element"; //$NON-NLS-1$
|
protected static final String ROOT_ELEMENT_TAGNAME = "root_element"; //$NON-NLS-1$
|
||||||
protected static final String ELEMENT_TAGNAME = "elem"; //$NON-NLS-1$
|
protected static final String ELEMENT_TAGNAME = "elem"; //$NON-NLS-1$
|
||||||
protected static final String ATTRIBUTE_KEY = "key"; //$NON-NLS-1$
|
protected static final String ATTRIBUTE_NAME = "value"; //$NON-NLS-1$
|
||||||
protected static final String ATTRIBUTE_VALUE = "value"; //$NON-NLS-1$
|
|
||||||
|
|
||||||
|
|
||||||
/** Returns a XML memento, that encodes a single String parameter */
|
/** Returns a XML memento, that encodes a single String parameter */
|
||||||
public static String encodeStringIntoMemento(String str) {
|
public static String encodeStringIntoMemento(String str) {
|
||||||
List<String> list = new ArrayList<>();
|
List<String> list = new ArrayList<String>();
|
||||||
list.add(str);
|
list.add(str);
|
||||||
return encodeListIntoMemento(list);
|
return encodeListIntoMemento(list);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/** Returns a single String parameter, decoded from a XML memento */
|
/** Returns a single String parameter, decoded from a XML memento */
|
||||||
public static String decodeStringFromMemento(String memento) {
|
public static String decodeStringFromMemento(String memento) {
|
||||||
return decodeListFromMemento(memento).get(0);
|
return decodeListFromMemento(memento).get(0);
|
||||||
|
@ -70,9 +71,7 @@ public class MementoUtils {
|
||||||
// create one XML element per map entry
|
// create one XML element per map entry
|
||||||
for (String key : keyPairValues.keySet()) {
|
for (String key : keyPairValues.keySet()) {
|
||||||
Element elem = doc.createElement(ELEMENT_TAGNAME);
|
Element elem = doc.createElement(ELEMENT_TAGNAME);
|
||||||
// store key and value as values of 2 attributes
|
elem.setAttribute(key, keyPairValues.get(key));
|
||||||
elem.setAttribute(ATTRIBUTE_KEY, key);
|
|
||||||
elem.setAttribute(ATTRIBUTE_VALUE, keyPairValues.get(key));
|
|
||||||
rootElement.appendChild(elem);
|
rootElement.appendChild(elem);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -97,7 +96,7 @@ public class MementoUtils {
|
||||||
|
|
||||||
/** Returns a Map of Strings, decoded from a XML memento */
|
/** Returns a Map of Strings, decoded from a XML memento */
|
||||||
public static Map<String, String> decodeMapFromMemento(String memento) {
|
public static Map<String, String> decodeMapFromMemento(String memento) {
|
||||||
Map<String, String> keyPairValues = new HashMap<>();
|
Map<String, String> keyPairValues = new HashMap<String, String>();
|
||||||
|
|
||||||
Element root = null;
|
Element root = null;
|
||||||
DocumentBuilder parser;
|
DocumentBuilder parser;
|
||||||
|
@ -111,26 +110,20 @@ public class MementoUtils {
|
||||||
if (node.getNodeType() == Node.ELEMENT_NODE) {
|
if (node.getNodeType() == Node.ELEMENT_NODE) {
|
||||||
Element elem = (Element) node;
|
Element elem = (Element) node;
|
||||||
NamedNodeMap nodeMap = elem.getAttributes();
|
NamedNodeMap nodeMap = elem.getAttributes();
|
||||||
String key = null;
|
|
||||||
String value = null;
|
|
||||||
for(int idx = 0; idx < nodeMap.getLength(); idx++) {
|
for(int idx = 0; idx < nodeMap.getLength(); idx++) {
|
||||||
Node attrNode = nodeMap.item(idx);
|
Node attrNode = nodeMap.item(idx);
|
||||||
if (attrNode.getNodeType() == Node.ATTRIBUTE_NODE) {
|
if (attrNode.getNodeType() == Node.ATTRIBUTE_NODE) {
|
||||||
Attr attr = (Attr) attrNode;
|
Attr attr = (Attr) attrNode;
|
||||||
if (attr.getName().equals(ATTRIBUTE_KEY)) {
|
String key = attr.getName();
|
||||||
key = attr.getValue();
|
String value = attr.getValue();
|
||||||
|
if (key != null && value != null) {
|
||||||
|
keyPairValues.put(key, value);
|
||||||
}
|
}
|
||||||
else if (attr.getName().equals(ATTRIBUTE_VALUE)) {
|
else {
|
||||||
value = attr.getValue();
|
throw new Exception();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (key != null && value != null) {
|
|
||||||
keyPairValues.put(key, value);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
throw new Exception();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
|
@ -155,7 +148,7 @@ public class MementoUtils {
|
||||||
// create one XML element per list entry to save
|
// create one XML element per list entry to save
|
||||||
for (String lbl : labels) {
|
for (String lbl : labels) {
|
||||||
Element elem = doc.createElement(ELEMENT_TAGNAME);
|
Element elem = doc.createElement(ELEMENT_TAGNAME);
|
||||||
elem.setAttribute(ATTRIBUTE_VALUE, lbl);
|
elem.setAttribute(ATTRIBUTE_NAME, lbl);
|
||||||
rootElement.appendChild(elem);
|
rootElement.appendChild(elem);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -179,7 +172,7 @@ public class MementoUtils {
|
||||||
|
|
||||||
/** Returns a List of Strings, decoded from a XML memento */
|
/** Returns a List of Strings, decoded from a XML memento */
|
||||||
public static List<String> decodeListFromMemento(String memento) {
|
public static List<String> decodeListFromMemento(String memento) {
|
||||||
List<String> list = new ArrayList<>();
|
List<String> list = new ArrayList<String>();
|
||||||
|
|
||||||
Element root = null;
|
Element root = null;
|
||||||
DocumentBuilder parser;
|
DocumentBuilder parser;
|
||||||
|
@ -192,7 +185,7 @@ public class MementoUtils {
|
||||||
Node node = nodeList.item(i);
|
Node node = nodeList.item(i);
|
||||||
if (node.getNodeType() == Node.ELEMENT_NODE) {
|
if (node.getNodeType() == Node.ELEMENT_NODE) {
|
||||||
Element elem = (Element) node;
|
Element elem = (Element) node;
|
||||||
String value = elem.getAttribute(ATTRIBUTE_VALUE);
|
String value = elem.getAttribute(ATTRIBUTE_NAME);
|
||||||
if (value != null) {
|
if (value != null) {
|
||||||
list.add(value);
|
list.add(value);
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,100 +7,30 @@
|
||||||
*
|
*
|
||||||
* Contributors:
|
* Contributors:
|
||||||
* Marc Dumais (Ericsson) - initial API and implementation (bug 460837)
|
* Marc Dumais (Ericsson) - initial API and implementation (bug 460837)
|
||||||
* Marc Dumais (Ericsson) - Bug 462353
|
|
||||||
*******************************************************************************/
|
*******************************************************************************/
|
||||||
|
|
||||||
package org.eclipse.cdt.dsf.gdb.multicorevisualizer.internal.utils;
|
package org.eclipse.cdt.dsf.gdb.multicorevisualizer.internal.utils;
|
||||||
|
|
||||||
|
|
||||||
import java.security.InvalidParameterException;
|
import java.security.InvalidParameterException;
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
import org.eclipse.cdt.dsf.gdb.multicorevisualizer.internal.ui.MulticoreVisualizerUIPlugin;
|
import org.eclipse.cdt.dsf.gdb.multicorevisualizer.internal.ui.MulticoreVisualizerUIPlugin;
|
||||||
import org.eclipse.core.runtime.preferences.IEclipsePreferences;
|
import org.eclipse.core.runtime.preferences.IEclipsePreferences;
|
||||||
import org.osgi.service.prefs.BackingStoreException;
|
import org.osgi.service.prefs.BackingStoreException;
|
||||||
|
|
||||||
|
/** This class manages one or more PersistentSetting objects, using a commmon
|
||||||
/**
|
* name-space and optionally an instance id so that multiple instances can
|
||||||
* This class manages one or more PersistentParameter, PersistentListParameter,
|
* each have their own version of the parameter persisted */
|
||||||
* PersistentMapParameter objects, using a common name-space and optionally an
|
|
||||||
* instance id so that multiple instances can each have their own version of
|
|
||||||
* the parameter persisted
|
|
||||||
*/
|
|
||||||
public class PersistentSettingsManager {
|
public class PersistentSettingsManager {
|
||||||
|
// TODO: add a way to notify clients that the value of a global (shared) parameter
|
||||||
|
// has been updated, and that they should re-read it.
|
||||||
|
|
||||||
/** Base class for a persistent parameter */
|
/** Class for a specific persistent parameter */
|
||||||
private abstract class AbstractPersistentParameter<T> {
|
public class PersistentParameter<T> {
|
||||||
protected final Class<T> myClazz;
|
private String m_storeKey;
|
||||||
protected final boolean m_perInstance;
|
|
||||||
protected final String m_storeKey;
|
|
||||||
|
|
||||||
/** Constructor */
|
|
||||||
public AbstractPersistentParameter(Class<T> clazz, boolean perInstance, String storeKey) {
|
|
||||||
myClazz = clazz;
|
|
||||||
m_perInstance = perInstance;
|
|
||||||
m_storeKey = storeKey;
|
|
||||||
}
|
|
||||||
|
|
||||||
// accessors
|
|
||||||
|
|
||||||
/** Returns whether this parameter is persisted independently for each client instance */
|
|
||||||
public boolean isPerInstance() {
|
|
||||||
return m_perInstance;
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Returns the class of the parameter */
|
|
||||||
public Class<T> getClazz() {
|
|
||||||
return myClazz;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected String getStoreKey() {
|
|
||||||
return m_storeKey;
|
|
||||||
}
|
|
||||||
|
|
||||||
// misc
|
|
||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
|
||||||
/** Converts a value from a String to its expected generic type. This is a base
|
|
||||||
* implementation that converts some base types - Use/Override as needed for more complex
|
|
||||||
* types, such as List or Map of these types */
|
|
||||||
protected T convertToT(String val) {
|
|
||||||
// TODO: Add other types? Float, etc
|
|
||||||
if (String.class.isAssignableFrom(getClazz())) {
|
|
||||||
return (T) val;
|
|
||||||
}
|
|
||||||
else if (Integer.class.isAssignableFrom(getClazz())) {
|
|
||||||
return (T) Integer.valueOf(val);
|
|
||||||
}
|
|
||||||
else if (Boolean.class.isAssignableFrom(getClazz())) {
|
|
||||||
return (T) Boolean.valueOf(val);
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Returns whether the wanted Class type is supported, to use as a persistent parameter */
|
|
||||||
protected boolean isTypeSupported(Class<T> clazz) {
|
|
||||||
// TODO: Add other types? Float, etc
|
|
||||||
if (String.class.isAssignableFrom(clazz) ||
|
|
||||||
Integer.class.isAssignableFrom(clazz) ||
|
|
||||||
Boolean.class.isAssignableFrom(clazz))
|
|
||||||
{
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO: add a way to notify clients that the value of a global (shared) parameter
|
|
||||||
// has been updated, and that they should re-read it.
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Class for a persistent parameter */
|
|
||||||
public class PersistentParameter<T> extends AbstractPersistentParameter<T> {
|
|
||||||
private T m_value;
|
private T m_value;
|
||||||
private T m_defaultValue;
|
private T m_defaultValue;
|
||||||
|
private Class<T> myClazz;
|
||||||
|
private boolean m_perInstance;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructor
|
* Constructor
|
||||||
|
@ -110,7 +40,9 @@ public class PersistentSettingsManager {
|
||||||
* @param storeKey : The key used to store the parameter in the store
|
* @param storeKey : The key used to store the parameter in the store
|
||||||
*/
|
*/
|
||||||
public PersistentParameter(Class<T> clazz, boolean perInstance, String storeKey) {
|
public PersistentParameter(Class<T> clazz, boolean perInstance, String storeKey) {
|
||||||
super(clazz, perInstance, storeKey);
|
myClazz = clazz;
|
||||||
|
m_perInstance = perInstance;
|
||||||
|
m_storeKey = storeKey;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Sets the default value to use if no persistent
|
/** Sets the default value to use if no persistent
|
||||||
|
@ -119,46 +51,36 @@ public class PersistentSettingsManager {
|
||||||
m_defaultValue = defaultValue;
|
m_defaultValue = defaultValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Sets the value to persist */
|
/** Sets the persistent value to set */
|
||||||
public void set(T value) {
|
public void set(T value) {
|
||||||
m_value = value;
|
m_value = value;
|
||||||
// save value in preference store
|
// save value in preference store
|
||||||
persistParameter(value);
|
persistParameter(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Returns the persistent value, if found, else the default value */
|
/** Gets the persistent value, if found, else the default value */
|
||||||
public T value() {
|
public T value() {
|
||||||
if (m_value == null) {
|
if (m_value == null) {
|
||||||
// attempt to get the value from the preference store
|
// attempt to get the value from the preference store
|
||||||
m_value = restoreParameter();
|
m_value = restoreParameter();
|
||||||
}
|
}
|
||||||
// parameter has one value for any/all instances
|
// parameter has one value for any/all instances
|
||||||
else if(!isPerInstance()) {
|
else if(!m_perInstance) {
|
||||||
// do not rely on cached value, since another instance might have
|
// do not rely on cached value, since another instance might have
|
||||||
// changed it - reread from data store
|
// changed it - reread from data store
|
||||||
m_value = restoreParameter();
|
m_value = restoreParameter();
|
||||||
}
|
}
|
||||||
return (m_value == null)? m_defaultValue : m_value;
|
return (m_value!=null)? m_value : m_defaultValue;
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Gets the persistent value, optionally forcing re-reading stored value
|
|
||||||
* @param forceRefresh whether to force to re-read memento in case value changed
|
|
||||||
*/
|
|
||||||
public T value(boolean forceRefresh) {
|
|
||||||
if (forceRefresh) {
|
|
||||||
m_value = null;
|
|
||||||
}
|
|
||||||
return value();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Attempts to find the parameter in the preference store. Returns null if not found */
|
/** Attempts to find the parameter in the preference store. Returns null if not found */
|
||||||
private T restoreParameter() {
|
private T restoreParameter() {
|
||||||
IEclipsePreferences store = MulticoreVisualizerUIPlugin.getEclipsePreferenceStore();
|
IEclipsePreferences store = MulticoreVisualizerUIPlugin.getEclipsePreferenceStore();
|
||||||
String memento = store.get(getStoreKey(), null);
|
String memento = store.get(m_storeKey, null);
|
||||||
if (memento == null) return null;
|
if (memento == null) return null;
|
||||||
|
|
||||||
String val = MementoUtils.decodeStringFromMemento(memento);
|
String val = MementoUtils.decodeStringFromMemento(memento);
|
||||||
|
|
||||||
T convertedVal = convertToT(val);
|
T convertedVal = convertToT(val);
|
||||||
return convertedVal;
|
return convertedVal;
|
||||||
}
|
}
|
||||||
|
@ -171,87 +93,7 @@ public class PersistentSettingsManager {
|
||||||
// save memento in store
|
// save memento in store
|
||||||
if (memento != null) {
|
if (memento != null) {
|
||||||
IEclipsePreferences store = MulticoreVisualizerUIPlugin.getEclipsePreferenceStore();
|
IEclipsePreferences store = MulticoreVisualizerUIPlugin.getEclipsePreferenceStore();
|
||||||
store.put(getStoreKey(), memento);
|
store.put(m_storeKey, memento);
|
||||||
try {
|
|
||||||
store.flush();
|
|
||||||
} catch (BackingStoreException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Class for a persistent {@literal List<T>} parameter */
|
|
||||||
public class PersistentListParameter<T> extends AbstractPersistentParameter<T> {
|
|
||||||
private List<T> m_value;
|
|
||||||
private List<T> m_defaultValue;
|
|
||||||
|
|
||||||
public PersistentListParameter(Class<T> clazz, boolean perInstance, String storeKey) {
|
|
||||||
super(clazz, perInstance, storeKey);
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Sets the default value to use if no persistent
|
|
||||||
* value is found for this parameter */
|
|
||||||
public void setDefault(List<T> defaultValues) {
|
|
||||||
m_defaultValue = defaultValues;
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Sets the value to persist */
|
|
||||||
public void set(List<T> values) {
|
|
||||||
m_value = values;
|
|
||||||
// save value in preference store
|
|
||||||
persistParameter(values);
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Returns the persistent value, if found, else the default value */
|
|
||||||
public List<T> value() {
|
|
||||||
if (m_value == null) {
|
|
||||||
// attempt to get the value from the preference store
|
|
||||||
m_value = restoreParameter();
|
|
||||||
}
|
|
||||||
// parameter has one value for any/all instances
|
|
||||||
else if(!isPerInstance()) {
|
|
||||||
// do not rely on cached value, since another instance might have
|
|
||||||
// changed it - reread from data store
|
|
||||||
m_value = restoreParameter();
|
|
||||||
}
|
|
||||||
return (m_value == null)? m_defaultValue : m_value ;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Gets the persistent value, optionally forcing re-reading stored value
|
|
||||||
* @param forceRefresh whether to force to re-read memento in case value changed
|
|
||||||
*/
|
|
||||||
public List<T> value(boolean forceRefresh) {
|
|
||||||
if (forceRefresh) {
|
|
||||||
m_value = null;
|
|
||||||
}
|
|
||||||
return value();
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Attempts to find the parameter in the preference store. Returns null if not found */
|
|
||||||
private List<T> restoreParameter() {
|
|
||||||
IEclipsePreferences store = MulticoreVisualizerUIPlugin.getEclipsePreferenceStore();
|
|
||||||
String memento = store.get(getStoreKey(), null);
|
|
||||||
if (memento == null) return null;
|
|
||||||
|
|
||||||
List<String> vals = MementoUtils.decodeListFromMemento(memento);
|
|
||||||
// convert from List<String> to List<T>
|
|
||||||
List<T> convertedVal = convertToT(vals);
|
|
||||||
return convertedVal;
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Saves parameter's value in preference store */
|
|
||||||
private void persistParameter(List<T> values) {
|
|
||||||
// Convert List<T> to List<String>
|
|
||||||
List<String> strList = convertTListToStringList(values);
|
|
||||||
// create memento from List<String>
|
|
||||||
String memento = MementoUtils.encodeListIntoMemento(strList);
|
|
||||||
|
|
||||||
// save memento in store
|
|
||||||
if (memento != null) {
|
|
||||||
IEclipsePreferences store = MulticoreVisualizerUIPlugin.getEclipsePreferenceStore();
|
|
||||||
store.put(getStoreKey(), memento);
|
|
||||||
try {
|
try {
|
||||||
store.flush();
|
store.flush();
|
||||||
} catch (BackingStoreException e) {
|
} catch (BackingStoreException e) {
|
||||||
|
@ -260,127 +102,21 @@ public class PersistentSettingsManager {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/** For list parameters, converts the restored values from String
|
@SuppressWarnings("unchecked")
|
||||||
* to its expected generic type */
|
/** Converts the stored value from a String to its expected type */
|
||||||
private List<T> convertToT(List<String> vals) {
|
private T convertToT(String val) {
|
||||||
List<T> convertedList = new ArrayList<>();
|
// TODO: Add other types? Float, etc
|
||||||
|
if (String.class.isAssignableFrom(myClazz)) {
|
||||||
for(String val : vals) {
|
return (T) val;
|
||||||
convertedList.add(convertToT(val));
|
|
||||||
}
|
}
|
||||||
return convertedList;
|
else if (Integer.class.isAssignableFrom(myClazz)) {
|
||||||
}
|
return (T) Integer.valueOf(val);
|
||||||
|
|
||||||
/** Converts a list of generic type to a list of String */
|
|
||||||
private List<String> convertTListToStringList(List<T> tList) {
|
|
||||||
List<String> strList = new ArrayList<>();
|
|
||||||
// convert list to list of String
|
|
||||||
for(T elem : tList) {
|
|
||||||
strList.add(elem.toString());
|
|
||||||
}
|
}
|
||||||
return strList;
|
else if (Boolean.class.isAssignableFrom(myClazz)) {
|
||||||
}
|
return (T) Boolean.valueOf(val);
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/** Class for a persistent {@literal Map<String,T>} parameter */
|
|
||||||
public class PersistentMapParameter<T> extends AbstractPersistentParameter<T> {
|
|
||||||
private Map<String,T> m_value;
|
|
||||||
private Map<String,T> m_defaultValue;
|
|
||||||
|
|
||||||
public PersistentMapParameter(Class<T> clazz, boolean perInstance, String storeKey) {
|
|
||||||
super(clazz, perInstance, storeKey);
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Sets the default value to use if no persistent
|
|
||||||
* value is found for this parameter */
|
|
||||||
public void setDefault(Map<String,T> defaultValues) {
|
|
||||||
m_defaultValue = defaultValues;
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Sets the value to persist */
|
|
||||||
public void set(Map<String,T> values) {
|
|
||||||
m_value = values;
|
|
||||||
// save value in preference store
|
|
||||||
persistParameter(values);
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Returns the persistent value, if found, else the default value */
|
|
||||||
public Map<String,T> value() {
|
|
||||||
if (m_value == null) {
|
|
||||||
// attempt to get the value from the preference store
|
|
||||||
m_value = restoreParameter();
|
|
||||||
}
|
}
|
||||||
// parameter has one value for any/all instances
|
|
||||||
else if(!isPerInstance()) {
|
|
||||||
// do not rely on cached value, since another instance might have
|
|
||||||
// changed it - reread from data store
|
|
||||||
m_value = restoreParameter();
|
|
||||||
}
|
|
||||||
return (m_value == null)? m_defaultValue : m_value ;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
return null;
|
||||||
* Gets the persistent value, optionally forcing re-reading stored value
|
|
||||||
* @param forceRefresh whether to force to re-read memento in case value changed
|
|
||||||
*/
|
|
||||||
public Map<String,T> value(boolean forceRefresh) {
|
|
||||||
if (forceRefresh) {
|
|
||||||
m_value = null;
|
|
||||||
}
|
|
||||||
return value();
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Attempts to find the parameter in the preference store. Returns null if not found */
|
|
||||||
private Map<String,T> restoreParameter() {
|
|
||||||
IEclipsePreferences store = MulticoreVisualizerUIPlugin.getEclipsePreferenceStore();
|
|
||||||
String memento = store.get(getStoreKey(), null);
|
|
||||||
if (memento == null) return null;
|
|
||||||
|
|
||||||
Map<String,String> vals = MementoUtils.decodeMapFromMemento(memento);
|
|
||||||
// convert from Map<String,String> to Map<String,T>
|
|
||||||
Map<String,T> convertedVal = convertToT(vals);
|
|
||||||
return convertedVal;
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Saves parameter's value in preference store */
|
|
||||||
private void persistParameter(Map<String,T> values) {
|
|
||||||
// Convert Map<String,T> to Map<String,String>
|
|
||||||
Map<String,String> strMap = convertTMapToStringMap(values);
|
|
||||||
// create memento from Map
|
|
||||||
String memento = MementoUtils.encodeMapIntoMemento(strMap);
|
|
||||||
|
|
||||||
// save memento in store
|
|
||||||
if (memento != null) {
|
|
||||||
IEclipsePreferences store = MulticoreVisualizerUIPlugin.getEclipsePreferenceStore();
|
|
||||||
store.put(getStoreKey(), memento);
|
|
||||||
try {
|
|
||||||
store.flush();
|
|
||||||
} catch (BackingStoreException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/** For Map parameters, converts the restored values from {@literal Map<String,String>}
|
|
||||||
* to {@literal Map<String, T>} */
|
|
||||||
private Map<String,T> convertToT(Map<String,String> vals) {
|
|
||||||
Map<String,T> convertedMap = new HashMap<>();
|
|
||||||
|
|
||||||
for(String key : vals.keySet()) {
|
|
||||||
convertedMap.put(key, convertToT(vals.get(key)));
|
|
||||||
}
|
|
||||||
return convertedMap;
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Converts a {@literal Map<String,T>} to a {@literal Map<String,String>} */
|
|
||||||
private Map<String,String> convertTMapToStringMap(Map<String,T> map) {
|
|
||||||
Map<String,String> strMap = new HashMap<>();
|
|
||||||
// convert each entry
|
|
||||||
for(String key : map.keySet()) {
|
|
||||||
strMap.put(key, map.get(key).toString());
|
|
||||||
}
|
|
||||||
return strMap;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -429,12 +165,17 @@ public class PersistentSettingsManager {
|
||||||
* @param defaultValue: default value to use (mandatory)
|
* @param defaultValue: default value to use (mandatory)
|
||||||
*/
|
*/
|
||||||
public <T> PersistentParameter<T> getNewParameter(Class<T> clazz, String label, boolean perInstance, T defaultValue) {
|
public <T> PersistentParameter<T> getNewParameter(Class<T> clazz, String label, boolean perInstance, T defaultValue) {
|
||||||
PersistentParameter<T> setting;
|
|
||||||
// build the final store key with category, parameter label and specific instance, if applicable
|
|
||||||
setting = new PersistentParameter<T>(clazz, perInstance, getStorageKey(perInstance) + "." + label); //$NON-NLS-1$
|
|
||||||
// check that we're dealing with one of a few supported types
|
// check that we're dealing with one of a few supported types
|
||||||
if (setting.isTypeSupported(clazz)) {
|
// TODO: Add other types? Float, etc
|
||||||
|
if (String.class.isAssignableFrom(clazz) ||
|
||||||
|
Integer.class.isAssignableFrom(clazz) ||
|
||||||
|
Boolean.class.isAssignableFrom(clazz))
|
||||||
|
{
|
||||||
|
PersistentParameter<T> setting;
|
||||||
|
// build the final store key with category, parameter label and specific instance, if applicable
|
||||||
|
setting = new PersistentParameter<T>(clazz, perInstance, getStorageKey(perInstance) + "." + label); //$NON-NLS-1$
|
||||||
setting.setDefault(defaultValue);
|
setting.setDefault(defaultValue);
|
||||||
|
|
||||||
return setting;
|
return setting;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
@ -442,55 +183,11 @@ public class PersistentSettingsManager {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Creates a new persistent {@literal List<T>} parameter, using the namespace and instance id of this manager.
|
|
||||||
* @param clazz: the class of the persistent parameter List (e.g. List of that type). Supported types: String, Integer, Boolean
|
|
||||||
* @param label: unique label that identifies this parameter.
|
|
||||||
* @param perInstance: whether the parameter's value should be persisted per client instance or
|
|
||||||
* globally (one common shared stored value for all instances)
|
|
||||||
* @param defaultValue: default value to use (mandatory).
|
|
||||||
*/
|
|
||||||
public <T> PersistentListParameter<T> getNewListParameter(Class<T> clazz, String label, boolean perInstance, List<T> defaultValue) {
|
|
||||||
PersistentListParameter<T> setting;
|
|
||||||
// build the final store key with category, parameter label and specific instance, if applicable
|
|
||||||
setting = new PersistentListParameter<T>(clazz, perInstance, getStorageKey(perInstance) + "." + label); //$NON-NLS-1$
|
|
||||||
// check that we're dealing with one of a few supported types
|
|
||||||
if (setting.isTypeSupported(clazz)) {
|
|
||||||
setting.setDefault(defaultValue);
|
|
||||||
return setting;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
throw new InvalidParameterException("Unsupported class type: " + clazz.toString()); //$NON-NLS-1$
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Creates a new persistent {@literal Map<String,T>} parameter, using the namespace and instance id of this manager.
|
|
||||||
* @param clazz: the class of the persistent parameter List (e.g. List of that type). Supported types: String, Integer, Boolean
|
|
||||||
* @param label: unique label that identifies this parameter.
|
|
||||||
* @param perInstance: whether the parameter's value should be persisted per client instance or
|
|
||||||
* globally (one common shared stored value for all instances)
|
|
||||||
* @param defaultValue: default value to use (mandatory).
|
|
||||||
*/
|
|
||||||
public <T> PersistentMapParameter<T> getNewMapParameter(Class<T> clazz, String label, boolean perInstance, Map<String,T> defaultValue) {
|
|
||||||
PersistentMapParameter<T> setting;
|
|
||||||
// build the final store key with category, parameter label and specific instance, if applicable
|
|
||||||
setting = new PersistentMapParameter<T>(clazz, perInstance, getStorageKey(perInstance) + "." + label); //$NON-NLS-1$
|
|
||||||
// check that we're dealing with one of a few supported types
|
|
||||||
if (setting.isTypeSupported(clazz)) {
|
|
||||||
setting.setDefault(defaultValue);
|
|
||||||
return setting;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
throw new InvalidParameterException("Unsupported class type: " + clazz.toString()); //$NON-NLS-1$
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// ---- misc ----
|
|
||||||
|
|
||||||
/** Returns the key to be used to save parameter, taking into account the
|
/** Returns the key to be used to save parameter, taking into account the
|
||||||
* instance id, if applicable */
|
* instance id, if applicable */
|
||||||
private String getStorageKey(boolean perInstance) {
|
private String getStorageKey(boolean perInstance) {
|
||||||
return (perInstance ? m_instance : "") + (!m_category.equals("") ? "." + m_category : ""); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
|
return (perInstance ? m_instance : "") + (!m_category.equals("") ? "." + m_category : ""); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
|
@ -8,4 +8,3 @@ Bundle-RequiredExecutionEnvironment: JavaSE-1.7
|
||||||
Require-Bundle: org.junit;bundle-version="4.8.1",
|
Require-Bundle: org.junit;bundle-version="4.8.1",
|
||||||
org.eclipse.cdt.visualizer.ui,
|
org.eclipse.cdt.visualizer.ui,
|
||||||
org.eclipse.swt
|
org.eclipse.swt
|
||||||
Import-Package: org.eclipse.cdt.dsf.gdb.multicorevisualizer.internal.utils
|
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
/*******************************************************************************
|
/*******************************************************************************
|
||||||
* Copyright (c) 2014, 2015 Ericsson AB and others.
|
* Copyright (c) 2014 Ericsson AB and others.
|
||||||
* All rights reserved. This program and the accompanying materials
|
* All rights reserved. This program and the accompanying materials
|
||||||
* are made available under the terms of the Eclipse Public License v1.0
|
* are made available under the terms of the Eclipse Public License v1.0
|
||||||
* which accompanies this distribution, and is available at
|
* which accompanies this distribution, and is available at
|
||||||
|
@ -28,7 +28,6 @@ import org.junit.runners.Suite.SuiteClasses;
|
||||||
MIStringHandlerTests.class,
|
MIStringHandlerTests.class,
|
||||||
ProcStatParserTest.class,
|
ProcStatParserTest.class,
|
||||||
VisualizerVirtualBoundsGraphicObjectTest.class,
|
VisualizerVirtualBoundsGraphicObjectTest.class,
|
||||||
PersistentSettingsManagerTest.class,
|
|
||||||
})
|
})
|
||||||
public class AllTests {
|
public class AllTests {
|
||||||
// Often overriding BeforeClass method here
|
// Often overriding BeforeClass method here
|
||||||
|
|
|
@ -1,436 +0,0 @@
|
||||||
/*******************************************************************************
|
|
||||||
* Copyright (c) 2015 Ericsson
|
|
||||||
* All rights reserved. This program and the accompanying materials
|
|
||||||
* are made available under the terms of the Eclipse Public License v1.0
|
|
||||||
* which accompanies this distribution, and is available at
|
|
||||||
* http://www.eclipse.org/legal/epl-v10.html
|
|
||||||
*
|
|
||||||
* Contributors:
|
|
||||||
* Marc Dumais (Ericsson) - Initial API and implementation
|
|
||||||
*******************************************************************************/
|
|
||||||
|
|
||||||
package org.eclipse.cdt.dsf.gdb.tests;
|
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
|
||||||
import java.util.Random;
|
|
||||||
|
|
||||||
import org.eclipse.cdt.dsf.gdb.multicorevisualizer.internal.utils.PersistentSettingsManager;
|
|
||||||
import org.eclipse.cdt.dsf.gdb.multicorevisualizer.internal.utils.PersistentSettingsManager.PersistentListParameter;
|
|
||||||
import org.eclipse.cdt.dsf.gdb.multicorevisualizer.internal.utils.PersistentSettingsManager.PersistentMapParameter;
|
|
||||||
import org.eclipse.cdt.dsf.gdb.multicorevisualizer.internal.utils.PersistentSettingsManager.PersistentParameter;
|
|
||||||
import org.junit.Test;
|
|
||||||
|
|
||||||
@SuppressWarnings("restriction")
|
|
||||||
public class PersistentSettingsManagerTest {
|
|
||||||
|
|
||||||
// ---- variables ----
|
|
||||||
|
|
||||||
/** persistent settings manager */
|
|
||||||
private PersistentSettingsManager m_persistentSettingsManager1;
|
|
||||||
private PersistentSettingsManager m_persistentSettingsManager2;
|
|
||||||
private PersistentSettingsManager m_persistentSettingsManager3;
|
|
||||||
|
|
||||||
// set of persistent parameters
|
|
||||||
private PersistentParameter<String> m_stringParam;
|
|
||||||
private PersistentParameter<Boolean> m_booleanParam;
|
|
||||||
private PersistentParameter<Integer> m_integerParam;
|
|
||||||
private PersistentListParameter<String> m_listOfStringParam;
|
|
||||||
private PersistentListParameter<Boolean> m_listOfBooleanParam;
|
|
||||||
private PersistentListParameter<Integer> m_listOfIntegerParam;
|
|
||||||
private PersistentMapParameter<String> m_mapOfStringParam;
|
|
||||||
private PersistentMapParameter<Boolean> m_mapOfBooleanParam;
|
|
||||||
private PersistentMapParameter<Integer> m_mapOfIntegerParam;
|
|
||||||
|
|
||||||
// set of per-instance parameters
|
|
||||||
private PersistentParameter<String> m_stringParamInstance1;
|
|
||||||
private PersistentParameter<String> m_stringParamInstance2;
|
|
||||||
private PersistentParameter<String> m_stringParamInstance3;
|
|
||||||
|
|
||||||
// set of global parameters
|
|
||||||
private PersistentParameter<String> m_stringGlobalParamInstance1;
|
|
||||||
private PersistentParameter<String> m_stringGlobalParamInstance2;
|
|
||||||
private PersistentParameter<String> m_stringGlobalParamInstance3;
|
|
||||||
|
|
||||||
// random number generator
|
|
||||||
private Random m_random = new Random();
|
|
||||||
|
|
||||||
|
|
||||||
// ---- constants ----
|
|
||||||
private final static int NUM_LIST_ELEMENTS = 10;
|
|
||||||
|
|
||||||
private final static String INSTANCE_ID_1 = "instance1";
|
|
||||||
private final static String INSTANCE_ID_2 = "instance2";
|
|
||||||
private final static String INSTANCE_ID_3 = "instance3";
|
|
||||||
|
|
||||||
private final static String DEFAULT_STRING = "Default String";
|
|
||||||
private final static Boolean DEFAULT_BOOLEAN = false;
|
|
||||||
private final static Integer DEFAULT_INTEGER = 1234321;
|
|
||||||
|
|
||||||
private final static String DEFAULT_STRING_VAL_INSTANCE1 = "Default String Instance 1";
|
|
||||||
private final static String DEFAULT_STRING_VAL_INSTANCE2 = "Default String Instance 2";
|
|
||||||
private final static String DEFAULT_STRING_VAL_INSTANCE3 = "Default String Instance 3";
|
|
||||||
private final static String DEFAULT_STRING_VAL_SHARED = "Default String Shared Instance";
|
|
||||||
|
|
||||||
|
|
||||||
public PersistentSettingsManagerTest() {
|
|
||||||
// 3 instances of managers - to simulate, for example, 3 views each having one manager
|
|
||||||
m_persistentSettingsManager1 = new PersistentSettingsManager("PersistentSettingsManagerTest", INSTANCE_ID_1);
|
|
||||||
m_persistentSettingsManager2 = new PersistentSettingsManager("PersistentSettingsManagerTest", INSTANCE_ID_2);
|
|
||||||
m_persistentSettingsManager3 = new PersistentSettingsManager("PersistentSettingsManagerTest", INSTANCE_ID_3);
|
|
||||||
|
|
||||||
// one persistent parameter for each supported type:
|
|
||||||
// simple types
|
|
||||||
m_stringParam = m_persistentSettingsManager1.getNewParameter(String.class, "String Parameter", false, DEFAULT_STRING);
|
|
||||||
m_booleanParam = m_persistentSettingsManager1.getNewParameter(Boolean.class, "Boolean Parameter", false, DEFAULT_BOOLEAN);
|
|
||||||
m_integerParam = m_persistentSettingsManager1.getNewParameter(Integer.class, "Integer Parameter", false, DEFAULT_INTEGER);
|
|
||||||
// List<T>
|
|
||||||
m_listOfStringParam = m_persistentSettingsManager1.getNewListParameter(String.class, "List of String Parameter", false, new ArrayList<String>());
|
|
||||||
m_listOfBooleanParam = m_persistentSettingsManager1.getNewListParameter(Boolean.class, "List of Boolean Parameter", false, new ArrayList<Boolean>());
|
|
||||||
m_listOfIntegerParam = m_persistentSettingsManager1.getNewListParameter(Integer.class, "List of Integer Parameter", false, new ArrayList<Integer>());
|
|
||||||
// Map<String,T>
|
|
||||||
m_mapOfStringParam = m_persistentSettingsManager1.getNewMapParameter(String.class, "Map of String Parameter", true, new HashMap<String,String>());
|
|
||||||
m_mapOfBooleanParam = m_persistentSettingsManager1.getNewMapParameter(Boolean.class, "Map of Boolean Parameter", true, new HashMap<String,Boolean>());
|
|
||||||
m_mapOfIntegerParam = m_persistentSettingsManager1.getNewMapParameter(Integer.class, "Map of Integer Parameter", true, new HashMap<String,Integer>());
|
|
||||||
|
|
||||||
// simulate 3 instances using the same parameter, using "per instance" persistence (i.e. they'll be persisted independently)
|
|
||||||
m_stringParamInstance1 = m_persistentSettingsManager1.getNewParameter(String.class, "Per-instance String Parameter", true, DEFAULT_STRING_VAL_INSTANCE1);
|
|
||||||
m_stringParamInstance2 = m_persistentSettingsManager2.getNewParameter(String.class, "Per-instance String Parameter", true, DEFAULT_STRING_VAL_INSTANCE2);
|
|
||||||
m_stringParamInstance3 = m_persistentSettingsManager3.getNewParameter(String.class, "Per-instance String Parameter", true, DEFAULT_STRING_VAL_INSTANCE3);
|
|
||||||
|
|
||||||
// This is to simulate a persistent parameter, being "shared" by 3 instances (e.g. views). So, the 3 instances are persisted as a single parameter.
|
|
||||||
m_stringGlobalParamInstance1 = m_persistentSettingsManager1.getNewParameter(String.class, "Global String Parameter", false, DEFAULT_STRING_VAL_SHARED);
|
|
||||||
m_stringGlobalParamInstance2 = m_persistentSettingsManager2.getNewParameter(String.class, "Global String Parameter", false, DEFAULT_STRING_VAL_SHARED);
|
|
||||||
m_stringGlobalParamInstance3 = m_persistentSettingsManager3.getNewParameter(String.class, "Global String Parameter", false, DEFAULT_STRING_VAL_SHARED);
|
|
||||||
}
|
|
||||||
|
|
||||||
// testcases
|
|
||||||
|
|
||||||
/** Test Un-supported base type - we expect an Exception */
|
|
||||||
@Test(expected = Exception.class)
|
|
||||||
public void testUnsupportedBaseType() throws Exception {
|
|
||||||
m_persistentSettingsManager1.getNewParameter(Float.class, "Float Parameter", false, 1.0f);
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Test Un-supported List type - we expect an Exception */
|
|
||||||
@Test(expected = Exception.class)
|
|
||||||
public void testUnsupportedListType() throws Exception {
|
|
||||||
m_persistentSettingsManager1.getNewListParameter(Float.class, "List of Float Parameter", false, new ArrayList<Float>());
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Test Un-supported Map type - we expect an Exception */
|
|
||||||
@Test(expected = Exception.class)
|
|
||||||
public void testUnsupportedMapType() throws Exception {
|
|
||||||
m_persistentSettingsManager1.getNewMapParameter(Float.class, "Map of Float Parameter", false, new HashMap<String,Float>());
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Test persisting one String value */
|
|
||||||
@Test
|
|
||||||
public void testPersistentParamString() throws Exception {
|
|
||||||
// no value persisted yet - should return default
|
|
||||||
assertEquals(DEFAULT_STRING, m_stringParam.value());
|
|
||||||
|
|
||||||
// set a value
|
|
||||||
String randomString = getRandomString();
|
|
||||||
m_stringParam.set(randomString);
|
|
||||||
// get cached value
|
|
||||||
assertEquals(randomString, m_stringParam.value());
|
|
||||||
// force re-read from storage
|
|
||||||
assertEquals(randomString, m_stringParam.value(true));
|
|
||||||
|
|
||||||
// set a different value
|
|
||||||
randomString = getRandomString();
|
|
||||||
m_stringParam.set(randomString);
|
|
||||||
// get cached value
|
|
||||||
assertEquals(randomString, m_stringParam.value());
|
|
||||||
// force re-read from storage
|
|
||||||
assertEquals(randomString, m_stringParam.value(true));
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Test persisting one Boolean value */
|
|
||||||
@Test
|
|
||||||
public void testPersistentParamBoolean() throws Exception {
|
|
||||||
// no value persisted yet - should return default
|
|
||||||
assertEquals(DEFAULT_BOOLEAN, m_booleanParam.value());
|
|
||||||
|
|
||||||
// set a value
|
|
||||||
m_booleanParam.set(!DEFAULT_BOOLEAN);
|
|
||||||
// get cached value
|
|
||||||
assertEquals(!DEFAULT_BOOLEAN, m_booleanParam.value());
|
|
||||||
// force re-read from storage
|
|
||||||
assertEquals(!DEFAULT_BOOLEAN, m_booleanParam.value(true));
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Test persisting one Integer value */
|
|
||||||
@Test
|
|
||||||
public void testPersistentParamInteger() throws Exception {
|
|
||||||
// no value persisted yet - should return default
|
|
||||||
assertEquals(DEFAULT_INTEGER, m_integerParam.value());
|
|
||||||
|
|
||||||
// set a value
|
|
||||||
Integer randomInt = getRandomInt();
|
|
||||||
m_integerParam.set(randomInt);
|
|
||||||
// get cached value
|
|
||||||
assertEquals(randomInt, m_integerParam.value());
|
|
||||||
// force re-read from storage
|
|
||||||
assertEquals(randomInt, m_integerParam.value(true));
|
|
||||||
|
|
||||||
// set a different value
|
|
||||||
randomInt = getRandomInt();
|
|
||||||
m_integerParam.set(randomInt);
|
|
||||||
// get cached value
|
|
||||||
assertEquals(randomInt, m_integerParam.value());
|
|
||||||
// force re-read from storage
|
|
||||||
assertEquals(randomInt, m_integerParam.value(true));
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Test persisting a List of String */
|
|
||||||
@Test
|
|
||||||
public void testPersistentParamListOfString() throws Exception {
|
|
||||||
// no value persisted yet - should return default (empty list)
|
|
||||||
List<String> list = m_listOfStringParam.value();
|
|
||||||
assertEquals(0,list.size());
|
|
||||||
// generate list of random elements
|
|
||||||
for(int i = 0; i < NUM_LIST_ELEMENTS; i++) {
|
|
||||||
list.add(getRandomString());
|
|
||||||
}
|
|
||||||
m_listOfStringParam.set(list);
|
|
||||||
|
|
||||||
// get cached value
|
|
||||||
List<String> list2 = m_listOfStringParam.value();
|
|
||||||
for(int i = 0; i < NUM_LIST_ELEMENTS; i++) {
|
|
||||||
assertEquals(list.get(i), list2.get(i));
|
|
||||||
}
|
|
||||||
// force re-read from storage
|
|
||||||
list2 = m_listOfStringParam.value(true);
|
|
||||||
for(int i = 0; i < NUM_LIST_ELEMENTS; i++) {
|
|
||||||
assertEquals(list.get(i), list2.get(i));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Test persisting a List of Boolean */
|
|
||||||
@Test
|
|
||||||
public void testPersistentParamListOfBoolean() throws Exception {
|
|
||||||
// no value persisted yet - should return default (empty list)
|
|
||||||
List<Boolean> list = m_listOfBooleanParam.value();
|
|
||||||
assertEquals(0,list.size());
|
|
||||||
// generate list of random elements
|
|
||||||
for(int i = 0; i < NUM_LIST_ELEMENTS; i++) {
|
|
||||||
list.add(getRandomBoolean());
|
|
||||||
}
|
|
||||||
m_listOfBooleanParam.set(list);
|
|
||||||
|
|
||||||
// get cached value
|
|
||||||
List<Boolean> list2 = m_listOfBooleanParam.value();
|
|
||||||
for(int i = 0; i < NUM_LIST_ELEMENTS; i++) {
|
|
||||||
assertEquals(list.get(i), list2.get(i));
|
|
||||||
}
|
|
||||||
// force re-read from storage
|
|
||||||
list2 = m_listOfBooleanParam.value(true);
|
|
||||||
for(int i = 0; i < NUM_LIST_ELEMENTS; i++) {
|
|
||||||
assertEquals(list.get(i), list2.get(i));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Test persisting a List of Integer */
|
|
||||||
@Test
|
|
||||||
public void testPersistentParamListofInteger() throws Exception {
|
|
||||||
// no value persisted yet - should return default (empty list)
|
|
||||||
List<Integer> list = m_listOfIntegerParam.value();
|
|
||||||
assertEquals(0,list.size());
|
|
||||||
// generate list of random elements
|
|
||||||
for(int i = 0; i < NUM_LIST_ELEMENTS; i++) {
|
|
||||||
list.add(getRandomInt());
|
|
||||||
}
|
|
||||||
m_listOfIntegerParam.set(list);
|
|
||||||
|
|
||||||
List<Integer> list2;
|
|
||||||
// get cached value
|
|
||||||
list2 = m_listOfIntegerParam.value();
|
|
||||||
for(int i = 0; i < NUM_LIST_ELEMENTS; i++) {
|
|
||||||
assertEquals(list.get(i), list2.get(i));
|
|
||||||
}
|
|
||||||
// force re-read from storage
|
|
||||||
list2 = m_listOfIntegerParam.value(true);
|
|
||||||
for(int i = 0; i < NUM_LIST_ELEMENTS; i++) {
|
|
||||||
assertEquals(list.get(i), list2.get(i));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/** Test persisting a Map of String */
|
|
||||||
@Test
|
|
||||||
public void testPersistentParamMapOfString() throws Exception {
|
|
||||||
// no value persisted yet - should return default (empty Map)
|
|
||||||
Map<String,String> map = m_mapOfStringParam.value();
|
|
||||||
assertEquals(0,map.size());
|
|
||||||
|
|
||||||
// generate random elements
|
|
||||||
for(int i = 0; i < NUM_LIST_ELEMENTS; i++) {
|
|
||||||
map.put(getRandomString(), getRandomString());
|
|
||||||
}
|
|
||||||
m_mapOfStringParam.set(map);
|
|
||||||
|
|
||||||
// get cached value
|
|
||||||
Map<String,String> map2 = m_mapOfStringParam.value();
|
|
||||||
assertEquals(map.size(), map2.size());
|
|
||||||
for(String key : map2.keySet()) {
|
|
||||||
assertEquals(map2.get(key), map.get(key));
|
|
||||||
}
|
|
||||||
|
|
||||||
// force re-read from storage
|
|
||||||
map2 = m_mapOfStringParam.value(true);
|
|
||||||
assertEquals(map.size(), map2.size());
|
|
||||||
for(String key : map2.keySet()) {
|
|
||||||
assertEquals(map2.get(key), map.get(key));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Test persisting a Map of Boolean*/
|
|
||||||
@Test
|
|
||||||
public void testPersistentParamMapOfBoolean() throws Exception {
|
|
||||||
// no value persisted yet - should return default (empty Map)
|
|
||||||
Map<String,Boolean> map = m_mapOfBooleanParam.value();
|
|
||||||
assertEquals(0,map.size());
|
|
||||||
|
|
||||||
// generate random elements
|
|
||||||
for(int i = 0; i < NUM_LIST_ELEMENTS; i++) {
|
|
||||||
map.put(getRandomString(), getRandomBoolean());
|
|
||||||
}
|
|
||||||
m_mapOfBooleanParam.set(map);
|
|
||||||
|
|
||||||
// get cached value
|
|
||||||
Map<String,Boolean> map2 = m_mapOfBooleanParam.value();
|
|
||||||
assertEquals(map.size(), map2.size());
|
|
||||||
for(String key : map2.keySet()) {
|
|
||||||
assertEquals(map2.get(key), map.get(key));
|
|
||||||
}
|
|
||||||
|
|
||||||
// force re-read from storage
|
|
||||||
map2 = m_mapOfBooleanParam.value(true);
|
|
||||||
assertEquals(map.size(), map2.size());
|
|
||||||
for(String key : map2.keySet()) {
|
|
||||||
assertEquals(map2.get(key), map.get(key));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Test persisting a List of Integer */
|
|
||||||
@Test
|
|
||||||
public void testPersistentParamMapOfInteger() throws Exception {
|
|
||||||
// no value persisted yet - should return default (empty Map)
|
|
||||||
Map<String,Integer> map = m_mapOfIntegerParam.value();
|
|
||||||
assertEquals(0,map.size());
|
|
||||||
|
|
||||||
// generate random elements
|
|
||||||
for(int i = 0; i < NUM_LIST_ELEMENTS; i++) {
|
|
||||||
map.put(getRandomString(), getRandomInt());
|
|
||||||
}
|
|
||||||
m_mapOfIntegerParam.set(map);
|
|
||||||
|
|
||||||
// get cached value
|
|
||||||
Map<String,Integer> map2 = m_mapOfIntegerParam.value();
|
|
||||||
assertEquals(map.size(), map2.size());
|
|
||||||
for(String key : map2.keySet()) {
|
|
||||||
assertEquals(map2.get(key), map.get(key));
|
|
||||||
}
|
|
||||||
|
|
||||||
// force re-read from storage
|
|
||||||
map2 = m_mapOfIntegerParam.value(true);
|
|
||||||
assertEquals(map.size(), map2.size());
|
|
||||||
for(String key : map2.keySet()) {
|
|
||||||
assertEquals(map2.get(key), map.get(key));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/** This test simulates 3 different instances (e.g. views) writing and reading the same persistent
|
|
||||||
* parameter. In this case the parameter is defined as "per-instance" (vs "global"), so each
|
|
||||||
* instance has it's own independent copy of the persistent parameter. */
|
|
||||||
@Test
|
|
||||||
public void testMultipleInstances() throws Exception {
|
|
||||||
// no values persisted yet - should return defaults
|
|
||||||
assertEquals(DEFAULT_STRING_VAL_INSTANCE1, m_stringParamInstance1.value());
|
|
||||||
assertEquals(DEFAULT_STRING_VAL_INSTANCE2, m_stringParamInstance2.value());
|
|
||||||
assertEquals(DEFAULT_STRING_VAL_INSTANCE3, m_stringParamInstance3.value());
|
|
||||||
|
|
||||||
// set values - since the parameters were defined to save values per-instance, they should be
|
|
||||||
// persisted independently (i.e. not overwrite each other)
|
|
||||||
String randomString1 = getRandomString();
|
|
||||||
String randomString2 = getRandomString();
|
|
||||||
String randomString3 = getRandomString();
|
|
||||||
m_stringParamInstance1.set(randomString1);
|
|
||||||
m_stringParamInstance2.set(randomString2);
|
|
||||||
m_stringParamInstance3.set(randomString3);
|
|
||||||
assertEquals(randomString1, m_stringParamInstance1.value(true));
|
|
||||||
assertEquals(randomString2, m_stringParamInstance2.value(true));
|
|
||||||
assertEquals(randomString3, m_stringParamInstance3.value(true));
|
|
||||||
|
|
||||||
// set different values
|
|
||||||
randomString1 = getRandomString();
|
|
||||||
randomString2 = getRandomString();
|
|
||||||
randomString3 = getRandomString();
|
|
||||||
m_stringParamInstance1.set(randomString1);
|
|
||||||
m_stringParamInstance2.set(randomString2);
|
|
||||||
m_stringParamInstance3.set(randomString3);
|
|
||||||
assertEquals(randomString1, m_stringParamInstance1.value(true));
|
|
||||||
assertEquals(randomString2, m_stringParamInstance2.value(true));
|
|
||||||
assertEquals(randomString3, m_stringParamInstance3.value(true));
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/** This test simulates 3 different instances (e.g. views) writing and reading the same persistent
|
|
||||||
* parameter. In this case the parameter is defined as "global" (vs "per-instance"), so only one
|
|
||||||
* copy is shared between the instances. */
|
|
||||||
@Test
|
|
||||||
public void testGlobalParamsWithMultipleInstances() throws Exception {
|
|
||||||
// no values persisted yet - should return defaults
|
|
||||||
assertEquals(DEFAULT_STRING_VAL_SHARED, m_stringGlobalParamInstance1.value());
|
|
||||||
assertEquals(DEFAULT_STRING_VAL_SHARED, m_stringGlobalParamInstance2.value());
|
|
||||||
assertEquals(DEFAULT_STRING_VAL_SHARED, m_stringGlobalParamInstance3.value());
|
|
||||||
|
|
||||||
// set values - since the parameters were defined to save values per-instance, they should be
|
|
||||||
// persisted independently
|
|
||||||
String randomString1 = getRandomString();
|
|
||||||
String randomString2 = getRandomString();
|
|
||||||
String randomString3 = getRandomString();
|
|
||||||
m_stringGlobalParamInstance1.set(randomString1);
|
|
||||||
m_stringGlobalParamInstance2.set(randomString2);
|
|
||||||
m_stringGlobalParamInstance3.set(randomString3);
|
|
||||||
// since the parameters are global, they share the same storage... So the last value written
|
|
||||||
// will be persisted
|
|
||||||
assertEquals(randomString3, m_stringGlobalParamInstance1.value(true));
|
|
||||||
assertEquals(randomString3, m_stringGlobalParamInstance2.value(true));
|
|
||||||
assertEquals(randomString3, m_stringGlobalParamInstance3.value(true));
|
|
||||||
|
|
||||||
// set different values
|
|
||||||
randomString1 = getRandomString();
|
|
||||||
randomString2 = getRandomString();
|
|
||||||
randomString3 = getRandomString();
|
|
||||||
m_stringGlobalParamInstance1.set(randomString1);
|
|
||||||
m_stringGlobalParamInstance2.set(randomString2);
|
|
||||||
m_stringGlobalParamInstance3.set(randomString3);
|
|
||||||
// since the parameters are global, they share the same storage... So the last value written
|
|
||||||
// will be persisted
|
|
||||||
assertEquals(randomString3, m_stringGlobalParamInstance1.value(true));
|
|
||||||
assertEquals(randomString3, m_stringGlobalParamInstance2.value(true));
|
|
||||||
assertEquals(randomString3, m_stringGlobalParamInstance3.value(true));
|
|
||||||
}
|
|
||||||
|
|
||||||
// utility methods
|
|
||||||
|
|
||||||
private int getRandomInt() {
|
|
||||||
return m_random.nextInt();
|
|
||||||
}
|
|
||||||
|
|
||||||
private String getRandomString() {
|
|
||||||
return Integer.toString(getRandomInt(), 16) + Integer.toString(getRandomInt(), 16);
|
|
||||||
}
|
|
||||||
|
|
||||||
private boolean getRandomBoolean() {
|
|
||||||
return getRandomInt() % 2 == 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
Loading…
Add table
Reference in a new issue