use of jmri.MemoryManager in project JMRI by JMRI.
the class AbstractMemoryManagerConfigXML method store.
/**
* Default implementation for storing the contents of a MemoryManager
*
* @param o Object to store, of type MemoryManager
* @return Element containing the complete info
*/
@Override
public Element store(Object o) {
Element memories = new Element("memories");
setStoreElementClass(memories);
MemoryManager tm = (MemoryManager) o;
if (tm != null) {
java.util.Iterator<String> iter = tm.getSystemNameList().iterator();
// don't return an element if there are not memories to include
if (!iter.hasNext()) {
return null;
}
// store the memories
while (iter.hasNext()) {
String sname = iter.next();
if (sname == null) {
log.error("System name null during store");
break;
}
log.debug("system name is " + sname);
Memory m = tm.getBySystemName(sname);
Element elem = new Element("memory");
elem.addContent(new Element("systemName").addContent(sname));
// store common part
storeCommon(m, elem);
// store value if non-null; null values omitted
Object obj = m.getValue();
if (obj != null) {
if (obj instanceof jmri.jmrit.roster.RosterEntry) {
String valueClass = obj.getClass().getName();
String value = ((RosterEntry) obj).getId();
elem.setAttribute("value", value);
elem.setAttribute("valueClass", valueClass);
} else {
String value = obj.toString();
elem.setAttribute("value", value);
}
}
log.debug("store Memory " + sname);
memories.addContent(elem);
}
}
return memories;
}
use of jmri.MemoryManager in project JMRI by JMRI.
the class JsonMemorySocketServiceTest method testMemoryChange.
public void testMemoryChange() {
try {
JsonMockConnection connection = new JsonMockConnection((DataOutputStream) null);
JsonNode message = connection.getObjectMapper().createObjectNode().put(JSON.NAME, "IM1");
JsonMemorySocketService service = new JsonMemorySocketService(connection);
MemoryManager manager = InstanceManager.getDefault(MemoryManager.class);
Memory memory1 = manager.provideMemory("IM1");
service.onMessage(JsonMemory.MEMORY, message, Locale.ENGLISH);
// TODO: test that service is listener in MemoryManager
// default null value of memory1 has text representation "null" in JSON
Assert.assertEquals("null", connection.getMessage().path(JSON.DATA).path(JSON.VALUE).asText());
memory1.setValue("throw");
JUnitUtil.waitFor(() -> {
return memory1.getValue().equals("throw");
}, "Memory to throw");
Assert.assertEquals("throw", connection.getMessage().path(JSON.DATA).path(JSON.VALUE).asText());
memory1.setValue("close");
JUnitUtil.waitFor(() -> {
return memory1.getValue().equals("close");
}, "Memory to close");
Assert.assertEquals("close", memory1.getValue());
Assert.assertEquals("close", connection.getMessage().path(JSON.DATA).path(JSON.VALUE).asText());
service.onClose();
// TODO: test that service is no longer a listener in MemoryManager
} catch (IOException | JmriException | JsonException ex) {
Assert.fail(ex.getMessage());
}
}
use of jmri.MemoryManager in project JMRI by JMRI.
the class JsonMemoryHttpServiceTest method testDoGetList.
public void testDoGetList() {
try {
ObjectMapper mapper = new ObjectMapper();
JsonMemoryHttpService service = new JsonMemoryHttpService(mapper);
MemoryManager manager = InstanceManager.getDefault(MemoryManager.class);
JsonNode result;
result = service.doGetList(JsonMemory.MEMORY, Locale.ENGLISH);
Assert.assertNotNull(result);
Assert.assertEquals(0, result.size());
manager.provideMemory("IM1");
manager.provideMemory("IM2");
result = service.doGetList(JsonMemory.MEMORY, Locale.ENGLISH);
Assert.assertNotNull(result);
Assert.assertEquals(2, result.size());
} catch (JsonException ex) {
Assert.fail(ex.getMessage());
}
}
use of jmri.MemoryManager in project JMRI by JMRI.
the class JsonMemoryHttpServiceTest method testDoPost.
public void testDoPost() throws JmriException {
ObjectMapper mapper = new ObjectMapper();
JsonMemoryHttpService service = new JsonMemoryHttpService(mapper);
MemoryManager manager = InstanceManager.getDefault(MemoryManager.class);
Memory memory1 = manager.provideMemory("IM1");
JsonNode result;
JsonNode message;
try {
// set off
message = mapper.createObjectNode().put(JSON.NAME, "IM1").put(JSON.VALUE, "close");
result = service.doPost(JsonMemory.MEMORY, "IM1", message, Locale.ENGLISH);
Assert.assertEquals("close", memory1.getValue());
Assert.assertNotNull(result);
Assert.assertEquals("close", result.path(JSON.DATA).path(JSON.VALUE).asText());
// set on
message = mapper.createObjectNode().put(JSON.NAME, "IM1").put(JSON.VALUE, "throw");
result = service.doPost(JsonMemory.MEMORY, "IM1", message, Locale.ENGLISH);
Assert.assertEquals("throw", memory1.getValue());
Assert.assertNotNull(result);
Assert.assertEquals("throw", result.path(JSON.DATA).path(JSON.VALUE).asText());
// set null
message = mapper.createObjectNode().put(JSON.NAME, "IM1").putNull(JSON.VALUE);
result = service.doPost(JsonMemory.MEMORY, "IM1", message, Locale.ENGLISH);
Assert.assertNull(memory1.getValue());
Assert.assertEquals("null", result.path(JSON.DATA).path(JSON.VALUE).asText());
} catch (JsonException ex) {
Assert.fail(ex.getMessage());
}
}
Aggregations