mirror of
https://github.com/eclipse-sailing-analytics/sailing-analytics.git
synced 2026-11-02 17:19:37 +00:00
bug 2775: Extended Settings serialization test to also test
SettingsToStringMapSerializer with the same set of test cases by introducing a abstract test class
This commit is contained in:
1 parent
c651316031
commit
65e6802fb2
4 files changed
+162
-146
No files matched your search
+2
-2
@@ -78,14 +78,14 @@ public abstract class AbstractSettingsToJsonSerializer<OBJECT, ARRAY> {
|
||||
Setting setting = entry.getValue();
|
||||
if(hasProperty(json, entry.getKey())) {
|
||||
Object jsonValue = get(json, entry.getKey());
|
||||
deserialize(setting, jsonValue);
|
||||
deserializeSetting(setting, jsonValue);
|
||||
}
|
||||
}
|
||||
}
|
||||
return settings;
|
||||
}
|
||||
|
||||
private void deserialize(Setting setting, Object jsonValue) {
|
||||
private void deserializeSetting(Setting setting, Object jsonValue) {
|
||||
if (setting instanceof ValueSetting) {
|
||||
deserializeValueSetting(jsonValue, (ValueSetting<?>) setting);
|
||||
} else if (setting instanceof ListSetting) {
|
||||
|
||||
+129
@@ -0,0 +1,129 @@
|
||||
package com.sap.sse.shared.android.test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.util.Arrays;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import com.sap.sse.common.filter.TextOperator;
|
||||
import com.sap.sse.common.filter.TextOperator.Operators;
|
||||
import com.sap.sse.common.settings.AbstractSettings;
|
||||
import com.sap.sse.common.settings.DecimalListSetting;
|
||||
import com.sap.sse.common.settings.DecimalSetting;
|
||||
import com.sap.sse.common.settings.EnumListSetting;
|
||||
import com.sap.sse.common.settings.EnumSetting;
|
||||
import com.sap.sse.common.settings.Settings;
|
||||
import com.sap.sse.common.settings.StringSetting;
|
||||
import com.sap.sse.common.settings.StringToEnumConverter;
|
||||
|
||||
public abstract class AbstractJsonSerializationTest<SOT> {
|
||||
|
||||
private static class TestOuterSettings extends AbstractSettings {
|
||||
private TestSettings nested = new TestSettings("nested", this);
|
||||
public TestOuterSettings() {
|
||||
}
|
||||
}
|
||||
|
||||
private static class TestSettings extends AbstractSettings {
|
||||
private final StringSetting humba = new StringSetting("humba", this);
|
||||
// TODO user lambda: TextOperator.Operators::valueOf
|
||||
private final EnumSetting<TextOperator.Operators> trala = new EnumSetting<>("trala", this, new StringToEnumConverter<TextOperator.Operators>() {
|
||||
@Override
|
||||
public Operators fromString(String stringValue) {
|
||||
return TextOperator.Operators.valueOf(stringValue);
|
||||
}
|
||||
});
|
||||
private final DecimalSetting num = new DecimalSetting("num", this);
|
||||
private final DecimalListSetting l = new DecimalListSetting("l", this);
|
||||
|
||||
public TestSettings() {
|
||||
}
|
||||
public TestSettings(String name, AbstractSettings settings) {
|
||||
super(name, settings);
|
||||
}
|
||||
}
|
||||
|
||||
private static class TestEnumListSettings extends AbstractSettings {
|
||||
private final EnumListSetting<TextOperator.Operators> l = new EnumListSetting<>("l", this, new StringToEnumConverter<TextOperator.Operators>() {
|
||||
@Override
|
||||
public Operators fromString(String stringValue) {
|
||||
return TextOperator.Operators.valueOf(stringValue);
|
||||
}
|
||||
});
|
||||
public TestEnumListSettings() {
|
||||
}
|
||||
}
|
||||
|
||||
private static class DuplicateFieldSettings extends AbstractSettings {
|
||||
@SuppressWarnings("unused")
|
||||
private final StringSetting humba = new StringSetting("humba", this);
|
||||
@SuppressWarnings("unused")
|
||||
private final DecimalSetting bumba = new DecimalSetting("humba", this);
|
||||
}
|
||||
|
||||
protected abstract <T extends Settings> SOT serialize(T settings) throws Exception;
|
||||
|
||||
protected abstract <T extends Settings> T deserialize(SOT serializedObject, T settings) throws Exception;
|
||||
|
||||
private <T extends Settings> T serializeAndDeserialize(T objectToSerialize) {
|
||||
try {
|
||||
SOT serialized = serialize(objectToSerialize);
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
Class<T> settingsType = (Class<T>) objectToSerialize.getClass();
|
||||
T deserializedInstance = settingsType.newInstance();
|
||||
|
||||
return deserialize(serialized, deserializedInstance);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFlatJsonSerialization() {
|
||||
final TestSettings settings = new TestSettings();
|
||||
settings.humba.setValue("trala");
|
||||
settings.trala.setValue(TextOperator.Operators.Contains);
|
||||
settings.num.setValue(BigDecimal.TEN);
|
||||
settings.l.setValues(Arrays.asList(BigDecimal.ONE, BigDecimal.valueOf(2), BigDecimal.valueOf(3)));
|
||||
|
||||
final TestSettings deserializedSettings = serializeAndDeserialize(settings);
|
||||
assertEquals(settings, deserializedSettings);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNestedJsonSerialization() {
|
||||
final TestOuterSettings outerSettings = new TestOuterSettings();
|
||||
final TestSettings settings = outerSettings.nested;
|
||||
settings.humba.setValue("trala");
|
||||
settings.trala.setValue(TextOperator.Operators.Contains);
|
||||
settings.num.setValue(BigDecimal.TEN);
|
||||
settings.l.setValues(Arrays.asList(BigDecimal.ONE, BigDecimal.valueOf(2), BigDecimal.valueOf(3)));
|
||||
|
||||
final TestOuterSettings deserializedSettings = serializeAndDeserialize(outerSettings);
|
||||
assertEquals(outerSettings, deserializedSettings);
|
||||
}
|
||||
|
||||
/**
|
||||
* Verifies that it is not possible to have two equally named child settings that would cause conflicts on serialization.
|
||||
*/
|
||||
@Test(expected = RuntimeException.class)
|
||||
public void testDuplicateSetting() {
|
||||
new DuplicateFieldSettings();
|
||||
}
|
||||
|
||||
/**
|
||||
* A white-box test that tests property name escaping in case the property name conflicts with how type
|
||||
* names (used particularly for enum settings) are represented in JSON.
|
||||
*/
|
||||
@Test
|
||||
public void testJsonSerializationWithEnumTypedList() throws ClassNotFoundException {
|
||||
final TestEnumListSettings settings = new TestEnumListSettings();
|
||||
settings.l.setValues(Arrays.asList(Operators.Contains, Operators.EndsWith));
|
||||
|
||||
final TestEnumListSettings deserializedSettings = serializeAndDeserialize(settings);
|
||||
assertEquals(settings, deserializedSettings);
|
||||
}
|
||||
}
|
||||
+10
-144
@@ -1,158 +1,24 @@
|
||||
package com.sap.sse.shared.android.test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.util.Arrays;
|
||||
|
||||
import org.json.simple.JSONObject;
|
||||
import org.json.simple.parser.JSONParser;
|
||||
import org.json.simple.parser.ParseException;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.sap.sse.common.filter.TextOperator;
|
||||
import com.sap.sse.common.filter.TextOperator.Operators;
|
||||
import com.sap.sse.common.settings.AbstractSettings;
|
||||
import com.sap.sse.common.settings.DecimalListSetting;
|
||||
import com.sap.sse.common.settings.DecimalSetting;
|
||||
import com.sap.sse.common.settings.EnumListSetting;
|
||||
import com.sap.sse.common.settings.EnumSetting;
|
||||
import com.sap.sse.common.settings.StringSetting;
|
||||
import com.sap.sse.common.settings.StringToEnumConverter;
|
||||
import com.sap.sse.common.settings.Settings;
|
||||
import com.sap.sse.shared.settings.SettingsToJsonSerializer;
|
||||
|
||||
public class SettingsJsonSerializationTest {
|
||||
|
||||
private static class TestOuterSettings extends AbstractSettings {
|
||||
private TestSettings nested = new TestSettings("nested", this);
|
||||
|
||||
}
|
||||
|
||||
private static class TestSettings extends AbstractSettings {
|
||||
private final StringSetting humba = new StringSetting("humba", this);
|
||||
// TODO user lambda: TextOperator.Operators::valueOf
|
||||
private final EnumSetting<TextOperator.Operators> trala = new EnumSetting<>("trala", this, new StringToEnumConverter<TextOperator.Operators>() {
|
||||
@Override
|
||||
public Operators fromString(String stringValue) {
|
||||
return TextOperator.Operators.valueOf(stringValue);
|
||||
}
|
||||
});
|
||||
private final DecimalSetting num = new DecimalSetting("num", this);
|
||||
private final DecimalListSetting l = new DecimalListSetting("l", this);
|
||||
|
||||
public TestSettings() {
|
||||
}
|
||||
public TestSettings(String name, AbstractSettings settings) {
|
||||
super(name, settings);
|
||||
}
|
||||
}
|
||||
|
||||
private static class TestEnumListSettings extends AbstractSettings {
|
||||
private final EnumListSetting<TextOperator.Operators> l = new EnumListSetting<>("l", this, new StringToEnumConverter<TextOperator.Operators>() {
|
||||
@Override
|
||||
public Operators fromString(String stringValue) {
|
||||
return TextOperator.Operators.valueOf(stringValue);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private static class DuplicateFieldSettings extends AbstractSettings {
|
||||
@SuppressWarnings("unused")
|
||||
private final StringSetting humba = new StringSetting("humba", this);
|
||||
@SuppressWarnings("unused")
|
||||
private final DecimalSetting bumba = new DecimalSetting("humba", this);
|
||||
}
|
||||
public class SettingsJsonSerializationTest extends AbstractJsonSerializationTest<String> {
|
||||
|
||||
private final SettingsToJsonSerializer serializer = new SettingsToJsonSerializer();
|
||||
|
||||
private JSONObject serializeAndDeserialize(JSONObject jsonObject) {
|
||||
String serializedJson = jsonObject.toJSONString();
|
||||
try {
|
||||
return (JSONObject) new JSONParser().parse(serializedJson);
|
||||
} catch (ParseException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
@Override
|
||||
protected <T extends Settings> String serialize(T settings) throws Exception {
|
||||
// Do not change this to return a JSONOBject instance.
|
||||
// JSONOBject is just a HashMap and you only know if it is really JSON serializable if you do it!
|
||||
return serializer.serialize(settings).toJSONString();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFlatJsonSerialization() {
|
||||
final TestSettings settings = new TestSettings();
|
||||
settings.humba.setValue("trala");
|
||||
settings.trala.setValue(TextOperator.Operators.Contains);
|
||||
settings.num.setValue(BigDecimal.TEN);
|
||||
settings.l.setValues(Arrays.asList(BigDecimal.ONE, BigDecimal.valueOf(2), BigDecimal.valueOf(3)));
|
||||
|
||||
final TestSettings deserializedSettings = serializer.deserialize(new TestSettings(), serializeAndDeserialize(serializer.serialize(settings)));
|
||||
assertEquals(settings, deserializedSettings);
|
||||
@Override
|
||||
protected <T extends Settings> T deserialize(String serializedObject, T settings) throws Exception {
|
||||
return serializer.deserialize(settings, (JSONObject) new JSONParser().parse(serializedObject));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNestedJsonSerialization() {
|
||||
final TestOuterSettings outerSettings = new TestOuterSettings();
|
||||
final TestSettings settings = outerSettings.nested;
|
||||
settings.humba.setValue("trala");
|
||||
settings.trala.setValue(TextOperator.Operators.Contains);
|
||||
settings.num.setValue(BigDecimal.TEN);
|
||||
settings.l.setValues(Arrays.asList(BigDecimal.ONE, BigDecimal.valueOf(2), BigDecimal.valueOf(3)));
|
||||
|
||||
final TestOuterSettings deserializedSettings = serializer.deserialize(new TestOuterSettings(), serializeAndDeserialize(serializer.serialize(outerSettings)));
|
||||
assertEquals(outerSettings, deserializedSettings);
|
||||
}
|
||||
|
||||
/**
|
||||
* Verifies that it is not possible to have two equally named child settings that would cause conflicts on serialization.
|
||||
*/
|
||||
@Test(expected = RuntimeException.class)
|
||||
public void testDuplicateSetting() {
|
||||
new DuplicateFieldSettings();
|
||||
}
|
||||
|
||||
// /**
|
||||
// * A white-box test that tests property name escaping in case the property name conflicts with how type
|
||||
// * names (used particularly for enum settings) are represented in JSON.
|
||||
// */
|
||||
// @Test
|
||||
// public void testJsonSerializationWithFunnyPropertyName() throws ClassNotFoundException {
|
||||
// final Map<String, Setting> settingsMap = new HashMap<>();
|
||||
// settingsMap.put("humba___TYPE", new StringSetting("trala"));
|
||||
// settingsMap.put("trala___TYPE___TYPE", new EnumSetting<>(TextOperator.Operators.Contains));
|
||||
// Settings settings = new MapSettings(settingsMap);
|
||||
// SettingsToJsonSerializer serializer = new SettingsToJsonSerializer();
|
||||
// JSONObject json = serializer.serialize(settings);
|
||||
// Settings deserializedSettings = serializer.deserialize(json);
|
||||
// assertEquals(settingsMap, deserializedSettings.getNonDefaultSettings());
|
||||
// }
|
||||
|
||||
/**
|
||||
* A white-box test that tests property name escaping in case the property name conflicts with how type
|
||||
* names (used particularly for enum settings) are represented in JSON.
|
||||
*/
|
||||
@Test
|
||||
public void testJsonSerializationWithEnumTypedList() throws ClassNotFoundException {
|
||||
final TestEnumListSettings settings = new TestEnumListSettings();
|
||||
settings.l.setValues(Arrays.asList(Operators.Contains, Operators.EndsWith));
|
||||
|
||||
final TestEnumListSettings deserializedSettings = serializer.deserialize(new TestEnumListSettings(), serializeAndDeserialize(serializer.serialize(settings)));
|
||||
assertEquals(settings, deserializedSettings);
|
||||
}
|
||||
|
||||
// /**
|
||||
// * A white-box test that tests property name escaping in case the property name conflicts with how type
|
||||
// * names (used particularly for enum settings) are represented in JSON.
|
||||
// */
|
||||
// @Test
|
||||
// public void testJsonSerializationWithStringifiedEnum() throws ClassNotFoundException {
|
||||
// final Map<String, Setting> settingsMap = new HashMap<>();
|
||||
// final List<StringSetting> list = new ArrayList<>();
|
||||
// list.add(new StringSetting(Operators.Contains));
|
||||
// list.add(new StringSetting(Operators.EndsWith));
|
||||
// settingsMap.put("l", new ListSetting<StringSetting>(list));
|
||||
// Settings settings = new MapSettings(settingsMap);
|
||||
// SettingsToJsonSerializer serializer = new SettingsToJsonSerializer();
|
||||
// JSONObject json = serializer.serialize(settings);
|
||||
// Settings deserializedSettings = serializer.deserialize(json);
|
||||
// ListSetting<?> l = (ListSetting<?>) deserializedSettings.getNonDefaultSettings().get("l");
|
||||
// assertEquals(Arrays.asList(Operators.Contains, Operators.EndsWith), l.getEnumList(Operators.class));
|
||||
// }
|
||||
|
||||
}
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
package com.sap.sse.shared.android.test;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import com.sap.sse.common.settings.Settings;
|
||||
import com.sap.sse.common.settings.serializer.SettingsToStringMapSerializer;
|
||||
|
||||
public class SettingsToStringMapSerializationTest extends AbstractJsonSerializationTest<Map<String,Iterable<String>>> {
|
||||
|
||||
private final SettingsToStringMapSerializer serializer = new SettingsToStringMapSerializer();
|
||||
|
||||
@Override
|
||||
protected <T extends Settings> Map<String,Iterable<String>> serialize(T settings) throws Exception {
|
||||
return serializer.serialize(settings);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected <T extends Settings> T deserialize(Map<String,Iterable<String>> serializedObject, T settings) throws Exception {
|
||||
return serializer.deserialize(settings, serializedObject);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user