276 lines
12 KiB
Java
276 lines
12 KiB
Java
package jmri.jmrit.logixng.actions;
|
|
|
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
|
import static org.junit.jupiter.api.Assertions.assertNotEquals;
|
|
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
|
import static org.junit.jupiter.api.Assertions.assertNull;
|
|
import static org.junit.jupiter.api.Assertions.assertSame;
|
|
import static org.junit.jupiter.api.Assertions.assertThrows;
|
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
|
import static org.junit.jupiter.api.Assertions.fail;
|
|
|
|
import java.beans.PropertyChangeEvent;
|
|
import java.beans.PropertyVetoException;
|
|
import java.util.ArrayList;
|
|
|
|
import jmri.*;
|
|
import jmri.jmrit.logixng.*;
|
|
import jmri.jmrit.logixng.implementation.DefaultConditionalNGScaffold;
|
|
import jmri.util.JUnitAppender;
|
|
import jmri.util.JUnitUtil;
|
|
|
|
import org.junit.jupiter.api.AfterEach;
|
|
import org.junit.jupiter.api.BeforeEach;
|
|
import org.junit.jupiter.api.Test;
|
|
|
|
/**
|
|
* Test AnalogActionMemory
|
|
*
|
|
* @author Daniel Bergqvist 2018
|
|
*/
|
|
public class AnalogActionMemoryTest extends AbstractAnalogActionTestBase {
|
|
|
|
private LogixNG logixNG;
|
|
private ConditionalNG conditionalNG;
|
|
protected Memory _memory;
|
|
|
|
@Override
|
|
public ConditionalNG getConditionalNG() {
|
|
return conditionalNG;
|
|
}
|
|
|
|
@Override
|
|
public LogixNG getLogixNG() {
|
|
return logixNG;
|
|
}
|
|
|
|
@Override
|
|
public MaleSocket getConnectableChild() {
|
|
return null;
|
|
}
|
|
|
|
@Override
|
|
public String getExpectedPrintedTree() {
|
|
return String.format("Set memory IM1 ::: Use default%n");
|
|
}
|
|
|
|
@Override
|
|
public String getExpectedPrintedTreeFromRoot() {
|
|
return String.format(
|
|
"LogixNG: A new logix for test%n" +
|
|
" ConditionalNG: A conditionalNG%n" +
|
|
" ! A%n" +
|
|
" Read analog E and set analog A ::: Use default%n" +
|
|
" ?~ E%n" +
|
|
" Socket not connected%n" +
|
|
" !~ A%n" +
|
|
" Set memory IM1 ::: Use default%n");
|
|
}
|
|
|
|
@Override
|
|
public NamedBean createNewBean(String systemName) {
|
|
return new AnalogMany(systemName, null);
|
|
}
|
|
|
|
@Override
|
|
public boolean addNewSocket() {
|
|
return false;
|
|
}
|
|
|
|
@Test
|
|
public void testCtor() {
|
|
assertNotNull( _base, "object exists");
|
|
|
|
AnalogActionMemory action2;
|
|
assertNotNull( _memory, "memory is not null");
|
|
_memory.setValue(10.2);
|
|
|
|
action2 = new AnalogActionMemory("IQAA11", null);
|
|
assertNotNull( action2, "object exists");
|
|
assertNull( action2.getUserName(), "Username matches");
|
|
assertEquals( "Set memory ''", action2.getLongDescription(), "String matches");
|
|
|
|
action2 = new AnalogActionMemory("IQAA11", "My memory");
|
|
assertNotNull( action2, "object exists");
|
|
assertEquals( "My memory", action2.getUserName(), "Username matches");
|
|
assertEquals( "Set memory ''", action2.getLongDescription(), "String matches");
|
|
|
|
action2 = new AnalogActionMemory("IQAA11", null);
|
|
action2.getSelectNamedBean().setNamedBean(_memory);
|
|
assertNotNull( action2, "object exists");
|
|
assertNull( action2.getUserName(), "Username matches");
|
|
assertEquals( "Set memory IM1", action2.getLongDescription(), "String matches");
|
|
|
|
action2 = new AnalogActionMemory("IQAA11", "My memory");
|
|
action2.getSelectNamedBean().setNamedBean(_memory);
|
|
assertNotNull( action2, "object exists");
|
|
assertEquals( "My memory", action2.getUserName(), "Username matches");
|
|
assertEquals( "Set memory IM1", action2.getLongDescription(), "String matches");
|
|
|
|
IllegalArgumentException ex = assertThrows( IllegalArgumentException.class, () -> {
|
|
var t = new AnalogActionMemory("IQA55:12:XY11", null);
|
|
fail("should have thrown, not cxreated " + t);
|
|
}, "Illegal system name Expected exception thrown");
|
|
assertNotNull(ex);
|
|
|
|
ex = assertThrows( IllegalArgumentException.class, () -> {
|
|
var t = new AnalogActionMemory("IQA55:12:XY11", "A name");
|
|
fail("should have thrown, not cxreated " + t);
|
|
}, "Illegal system name Expected exception thrown");
|
|
assertNotNull(ex);
|
|
}
|
|
|
|
@Test
|
|
public void testAction() throws SocketAlreadyConnectedException, SocketAlreadyConnectedException, JmriException {
|
|
AnalogActionMemory action = (AnalogActionMemory)_base;
|
|
action.setValue(0.0d);
|
|
assertEquals( 0.0d, (Double)_memory.getValue(), "Memory has correct value");
|
|
action.setValue(1.0d);
|
|
assertEquals( 1.0d, (Double)_memory.getValue(), "Memory has correct value");
|
|
action.getSelectNamedBean().removeNamedBean();
|
|
action.setValue(2.0d);
|
|
assertEquals( 1.0d, (Double)_memory.getValue(), "Memory has correct value");
|
|
}
|
|
|
|
@Test
|
|
public void testMemory() {
|
|
AnalogActionMemory action = (AnalogActionMemory)_base;
|
|
action.getSelectNamedBean().removeNamedBean();
|
|
assertNull( action.getSelectNamedBean().getNamedBean(), "Memory is null");
|
|
((AnalogActionMemory)_base).getSelectNamedBean().setNamedBean(_memory);
|
|
assertSame( _memory, action.getSelectNamedBean().getNamedBean().getBean(), "Memory matches");
|
|
|
|
action.getSelectNamedBean().removeNamedBean();
|
|
assertNull( action.getSelectNamedBean().getNamedBean(), "Memory is null");
|
|
Memory otherMemory = InstanceManager.getDefault(MemoryManager.class).provide("IM99");
|
|
assertNotNull( otherMemory, "memory is not null");
|
|
NamedBeanHandle<Memory> memoryHandle = InstanceManager.getDefault(NamedBeanHandleManager.class)
|
|
.getNamedBeanHandle(otherMemory.getDisplayName(), otherMemory);
|
|
((AnalogActionMemory)_base).getSelectNamedBean().setNamedBean(memoryHandle);
|
|
assertSame( memoryHandle, action.getSelectNamedBean().getNamedBean(), "Memory matches");
|
|
assertSame( otherMemory, action.getSelectNamedBean().getNamedBean().getBean(), "Memory matches");
|
|
|
|
action.getSelectNamedBean().removeNamedBean();
|
|
assertNull( action.getSelectNamedBean().getNamedBean(), "Memory is null");
|
|
action.getSelectNamedBean().setNamedBean(memoryHandle.getName());
|
|
assertSame( memoryHandle, action.getSelectNamedBean().getNamedBean(), "Memory matches");
|
|
|
|
// Test getSelectNamedBean().setNamedBean with a memory name that doesn't exists
|
|
action.getSelectNamedBean().setNamedBean("Non existent memory");
|
|
assertNull( action.getSelectNamedBean().getNamedBean(), "Memory is null");
|
|
JUnitAppender.assertErrorMessage("Memory \"Non existent memory\" is not found");
|
|
}
|
|
|
|
@Test
|
|
public void testVetoableChange() throws PropertyVetoException {
|
|
// Get some other memory for later use
|
|
Memory otherMemory = InstanceManager.getDefault(MemoryManager.class).provide("IM99");
|
|
assertNotNull( otherMemory, "Memory is not null");
|
|
assertNotEquals( _memory, otherMemory, "Memory is not equal");
|
|
|
|
// Get the expression and set the memory
|
|
AnalogActionMemory action = (AnalogActionMemory)_base;
|
|
action.getSelectNamedBean().setNamedBean(_memory);
|
|
assertEquals( _memory, action.getSelectNamedBean().getNamedBean().getBean(), "Memory matches");
|
|
|
|
// Test vetoableChange() for some other propery
|
|
action.vetoableChange(new PropertyChangeEvent(this, "CanSomething", "test", null));
|
|
assertEquals( _memory, action.getSelectNamedBean().getNamedBean().getBean(), "Memory matches");
|
|
|
|
// Test vetoableChange() for a string
|
|
action.vetoableChange(new PropertyChangeEvent(this, "CanDelete", "test", null));
|
|
assertEquals( _memory, action.getSelectNamedBean().getNamedBean().getBean(), "Memory matches");
|
|
action.vetoableChange(new PropertyChangeEvent(this, "DoDelete", "test", null));
|
|
assertEquals( _memory, action.getSelectNamedBean().getNamedBean().getBean(), "Memory matches");
|
|
|
|
// Test vetoableChange() for another memory
|
|
action.vetoableChange(new PropertyChangeEvent(this, "CanDelete", otherMemory, null));
|
|
assertEquals( _memory, action.getSelectNamedBean().getNamedBean().getBean(), "Memory matches");
|
|
action.vetoableChange(new PropertyChangeEvent(this, "DoDelete", otherMemory, null));
|
|
assertEquals( _memory, action.getSelectNamedBean().getNamedBean().getBean(), "Memory matches");
|
|
|
|
// Test vetoableChange() for its own memory
|
|
PropertyVetoException ex = assertThrows( PropertyVetoException.class, () ->
|
|
action.getSelectNamedBean().vetoableChange(new PropertyChangeEvent(this, "CanDelete", _memory, null)),
|
|
"Expected exception thrown");
|
|
assertNotNull(ex);
|
|
|
|
assertEquals( _memory, action.getSelectNamedBean().getNamedBean().getBean(), "Memory matches");
|
|
action.getSelectNamedBean().vetoableChange(new PropertyChangeEvent(this, "DoDelete", _memory, null));
|
|
assertNull( action.getSelectNamedBean().getNamedBean(), "Memory is null");
|
|
}
|
|
|
|
@Test
|
|
public void testCategory() {
|
|
assertSame( LogixNG_Category.ITEM, _base.getCategory(), "Category matches");
|
|
}
|
|
|
|
@Test
|
|
public void testShortDescription() {
|
|
assertEquals( "Memory", _base.getShortDescription(), "String matches");
|
|
}
|
|
|
|
@Test
|
|
public void testLongDescription() {
|
|
assertEquals( "Set memory IM1", _base.getLongDescription(), "String matches");
|
|
}
|
|
|
|
@Test
|
|
public void testChild() {
|
|
assertEquals( 0, _base.getChildCount(), "Num children is zero");
|
|
UnsupportedOperationException ex = assertThrows( UnsupportedOperationException.class, () ->
|
|
_base.getChild(0), "Exception is thrown");
|
|
assertEquals( "Not supported.", ex.getMessage(), "Error message is correct");
|
|
}
|
|
|
|
@BeforeEach
|
|
public void setUp() throws SocketAlreadyConnectedException {
|
|
JUnitUtil.setUp();
|
|
JUnitUtil.resetInstanceManager();
|
|
JUnitUtil.resetProfileManager();
|
|
JUnitUtil.initConfigureManager();
|
|
JUnitUtil.initInternalSensorManager();
|
|
JUnitUtil.initInternalTurnoutManager();
|
|
JUnitUtil.initMemoryManager();
|
|
// JUnitUtil.initLogixNGManager();
|
|
|
|
logixNG = InstanceManager.getDefault(LogixNG_Manager.class)
|
|
.createLogixNG("A new logix for test"); // NOI18N
|
|
conditionalNG = new DefaultConditionalNGScaffold("IQC1", "A conditionalNG"); // NOI18N;
|
|
InstanceManager.getDefault(ConditionalNG_Manager.class).register(conditionalNG);
|
|
conditionalNG.setEnabled(true);
|
|
conditionalNG.setRunDelayed(false);
|
|
logixNG.addConditionalNG(conditionalNG);
|
|
|
|
DoAnalogAction doAnalogAction = new DoAnalogAction("IQDA321", null);
|
|
MaleSocket maleSocketDoAnalogAction =
|
|
InstanceManager.getDefault(DigitalActionManager.class)
|
|
.registerAction(doAnalogAction);
|
|
conditionalNG.getChild(0).connect(maleSocketDoAnalogAction);
|
|
_memory = InstanceManager.getDefault(MemoryManager.class).provide("IM1");
|
|
AnalogActionMemory analogActionMemory =
|
|
new AnalogActionMemory("IQAA321", null);
|
|
MaleSocket maleSocketAnalogActionMemory =
|
|
InstanceManager.getDefault(AnalogActionManager.class)
|
|
.registerAction(analogActionMemory);
|
|
doAnalogAction.getChild(1).connect(maleSocketAnalogActionMemory);
|
|
analogActionMemory.getSelectNamedBean().setNamedBean(_memory);
|
|
_base = analogActionMemory;
|
|
_baseMaleSocket = maleSocketAnalogActionMemory;
|
|
|
|
assertTrue( logixNG.setParentForAllChildren(new ArrayList<>()));
|
|
logixNG.setEnabled(true);
|
|
|
|
InstanceManager.getDefault(LogixNG_Manager.class)
|
|
.activateAllLogixNGs(false, false);
|
|
}
|
|
|
|
@AfterEach
|
|
public void tearDown() {
|
|
_base.dispose();
|
|
jmri.jmrit.logixng.util.LogixNG_Thread.stopAllLogixNGThreads();
|
|
JUnitUtil.tearDown();
|
|
}
|
|
|
|
}
|