Skip to content
Permalink
Browse files

Merge pull request #5854 from danielb987/master

Add more tests to DefaultConditionalActionTest
  • Loading branch information...
bobjacobsen committed Sep 23, 2018
2 parents ec2addd + 3c480d1 commit 60b163ce7f0c97c553e10378353f60cd5d5b03cc
@@ -42,6 +42,7 @@ public void testCtor() {
@Test
public void testBasicBeanOperations() {
final String deviceName = "3";
final String otherDeviceName = "8";
final String actionStr = "5";

ConditionalAction ix1 = new DefaultConditionalAction(ACTION_OPTION_ON_CHANGE_TO_TRUE, ACTION_SET_TURNOUT, deviceName, Turnout.THROWN, actionStr);
@@ -51,9 +52,11 @@ public void testBasicBeanOperations() {
ConditionalAction ix4 = new DefaultConditionalAction(ACTION_OPTION_ON_CHANGE_TO_TRUE, 0, deviceName, Turnout.THROWN, actionStr);
ConditionalAction ix5 = new DefaultConditionalAction(ACTION_OPTION_ON_CHANGE_TO_TRUE, ACTION_SET_TURNOUT, "0", Turnout.THROWN, actionStr);
ConditionalAction ix6 = new DefaultConditionalAction(ACTION_OPTION_ON_CHANGE_TO_TRUE, ACTION_SET_TURNOUT, deviceName, 0, actionStr);
ConditionalAction ix7 = new DefaultConditionalAction(ACTION_OPTION_ON_CHANGE_TO_TRUE, ACTION_SET_TURNOUT, deviceName, Turnout.THROWN,"0");
ConditionalAction ix7 = new DefaultConditionalAction(ACTION_OPTION_ON_CHANGE_TO_TRUE, ACTION_SET_TURNOUT, deviceName, Turnout.THROWN, "0");
ConditionalAction ix8 = new DefaultConditionalAction(ACTION_OPTION_ON_CHANGE_TO_TRUE, ACTION_SET_TURNOUT, otherDeviceName, Turnout.THROWN, "0");
ConditionalAction ix9 = new DefaultConditionalAction(ACTION_OPTION_ON_CHANGE_TO_TRUE, ACTION_SET_TURNOUT, deviceName, Turnout.THROWN, "1");

ConditionalAction ix8 = new DefaultConditionalAction(0, Conditional.ACTION_NONE, null, Turnout.THROWN, actionStr);
ConditionalAction ix10 = new DefaultConditionalAction(0, Conditional.ACTION_NONE, null, Turnout.THROWN, actionStr);

Assert.assertFalse(ix1.equals(null));

@@ -65,13 +68,15 @@ public void testBasicBeanOperations() {
Assert.assertTrue(!ix1.equals(ix5));
Assert.assertTrue(!ix1.equals(ix6));
Assert.assertTrue(!ix1.equals(ix7));
Assert.assertTrue(!ix1.equals(ix8));
Assert.assertTrue(!ix1.equals(ix9));

// Test equal with different class
Assert.assertTrue(!ix1.equals(new Object()));

// Test deviceName == null
Assert.assertTrue(!ix1.equals(ix8));
Assert.assertTrue(!ix8.equals(ix1));
Assert.assertTrue(!ix1.equals(ix10));
Assert.assertTrue(!ix10.equals(ix1));

Assert.assertTrue(ix1.hashCode() == ix2.hashCode());
}
@@ -462,12 +467,103 @@ public void testGetActionDataString() {
"".equals(DefaultConditionalAction.getActionDataString(-1, -1)));
}

@Test
public void testSetActionData() {
final String deviceName = "3";
final String actionStr = "5";

ConditionalAction ix1 = new DefaultConditionalAction(ACTION_OPTION_ON_CHANGE_TO_TRUE, ACTION_SET_TURNOUT, deviceName, Turnout.THROWN, actionStr);
Assert.assertTrue("getActionData() gets correct value", ix1.getActionData() == Turnout.THROWN);
ix1.setActionData(Turnout.CLOSED);
Assert.assertTrue("getActionData() gets correct value", ix1.getActionData() == Turnout.CLOSED);
ix1.setActionData("Thrown");
Assert.assertTrue("getActionData() gets correct value", ix1.getActionData() == Turnout.THROWN);
}

@Test
public void testGetActionBean() {
ConditionalAction ix1;
NamedBean bean;
String deviceName = "3";

// Start with testing the exception handling in getActionBean()
jmri.util.JUnitUtil.resetInstanceManager();
jmri.util.JUnitUtil.initInternalTurnoutManagerThrowException();
jmri.util.JUnitUtil.initLightManagerThrowException();
jmri.util.JUnitUtil.initMemoryManagerThrowException();
jmri.util.JUnitUtil.initInternalSensorManagerThrowException();
jmri.util.JUnitUtil.initSignalHeadManagerThrowException();
jmri.util.JUnitUtil.initSignalMastManagerThrowException();
jmri.util.JUnitUtil.initWarrantManagerThrowException();
jmri.util.JUnitUtil.initOBlockManagerThrowException();
jmri.util.JUnitUtil.initRouteManagerThrowException();

ix1 = new DefaultConditionalAction(ACTION_OPTION_ON_CHANGE_TO_TRUE, ACTION_SET_SENSOR, deviceName, 4, "5");
jmri.util.JUnitAppender.assertErrorMessage("invalid sensor name= \"3\" in conditional action");
// getBean() tries to set the bean if bean == null. This generates a new error message.
Assert.assertTrue("getActionBean() returns null", ix1.getBean() == null);
jmri.util.JUnitAppender.assertErrorMessage("invalid sensor name= \"3\" in conditional action");

ix1 = new DefaultConditionalAction(ACTION_OPTION_ON_CHANGE_TO_TRUE, ACTION_SET_TURNOUT, deviceName, 4, "5");
jmri.util.JUnitAppender.assertErrorMessage("invalid turnout name= \"3\" in conditional action");
// getBean() tries to set the bean if bean == null. This generates a new error message.
Assert.assertTrue("getActionBean() returns null", ix1.getBean() == null);
jmri.util.JUnitAppender.assertErrorMessage("invalid turnout name= \"3\" in conditional action");

ix1 = new DefaultConditionalAction(ACTION_OPTION_ON_CHANGE_TO_TRUE, ACTION_SET_MEMORY, deviceName, 4, "5");
jmri.util.JUnitAppender.assertErrorMessage("invalid memory name= \"3\" in conditional action");
// getBean() tries to set the bean if bean == null. This generates a new error message.
Assert.assertTrue("getActionBean() returns null", ix1.getBean() == null);
jmri.util.JUnitAppender.assertErrorMessage("invalid memory name= \"3\" in conditional action");

ix1 = new DefaultConditionalAction(ACTION_OPTION_ON_CHANGE_TO_TRUE, ACTION_SET_LIGHT, deviceName, 4, "5");
jmri.util.JUnitAppender.assertErrorMessage("invalid light name= \"3\" in conditional action");
// getBean() tries to set the bean if bean == null. This generates a new error message.
Assert.assertTrue("getActionBean() returns null", ix1.getBean() == null);
jmri.util.JUnitAppender.assertErrorMessage("invalid light name= \"3\" in conditional action");

ix1 = new DefaultConditionalAction(ACTION_OPTION_ON_CHANGE_TO_TRUE, ACTION_SET_SIGNAL_APPEARANCE, "IH1", 4, "5");
jmri.util.JUnitAppender.assertErrorMessage("invalid signal head name= \"IH1\" in conditional action");
// getBean() tries to set the bean if bean == null. This generates a new error message.
Assert.assertTrue("getActionBean() returns null", ix1.getBean() == null);
jmri.util.JUnitAppender.assertErrorMessage("invalid signal head name= \"IH1\" in conditional action");

ix1 = new DefaultConditionalAction(ACTION_OPTION_ON_CHANGE_TO_TRUE, ACTION_SET_SIGNALMAST_HELD, "IF$shsm:AAR-1946:CPL(IH1)", 4, "5");
jmri.util.JUnitAppender.assertErrorMessage("invalid signal mast name= \"IF$shsm:AAR-1946:CPL(IH1)\" in conditional action");
// getBean() tries to set the bean if bean == null. This generates a new error message.
Assert.assertTrue("getActionBean() returns null", ix1.getBean() == null);
jmri.util.JUnitAppender.assertErrorMessage("invalid signal mast name= \"IF$shsm:AAR-1946:CPL(IH1)\" in conditional action");

ix1 = new DefaultConditionalAction(ACTION_OPTION_ON_CHANGE_TO_TRUE, ACTION_MANUAL_RUN_WARRANT, "IW3", 4, "5");
jmri.util.JUnitAppender.assertErrorMessage("invalid Warrant name= \"IW3\" in conditional action");
// getBean() tries to set the bean if bean == null. This generates a new error message.
Assert.assertTrue("getActionBean() returns null", ix1.getBean() == null);
jmri.util.JUnitAppender.assertErrorMessage("invalid Warrant name= \"IW3\" in conditional action");

ix1 = new DefaultConditionalAction(ACTION_OPTION_ON_CHANGE_TO_TRUE, ACTION_SET_BLOCK_VALUE, "OB3", 4, "5");
jmri.util.JUnitAppender.assertErrorMessage("invalid OBlock name= \"OB3\" in conditional action");
// getBean() tries to set the bean if bean == null. This generates a new error message.
Assert.assertTrue("getActionBean() returns null", ix1.getBean() == null);
jmri.util.JUnitAppender.assertErrorMessage("invalid OBlock name= \"OB3\" in conditional action");

ix1 = new DefaultConditionalAction(ACTION_OPTION_ON_CHANGE_TO_TRUE, ACTION_TRIGGER_ROUTE, deviceName, 4, "5");
jmri.util.JUnitAppender.assertErrorMessage("invalid Route name= \"3\" in conditional action");
// getBean() tries to set the bean if bean == null. This generates a new error message.
Assert.assertTrue("getActionBean() returns null", ix1.getBean() == null);
jmri.util.JUnitAppender.assertErrorMessage("invalid Route name= \"3\" in conditional action");


jmri.util.JUnitUtil.resetInstanceManager();
jmri.util.JUnitUtil.initInternalTurnoutManager();
jmri.util.JUnitUtil.initInternalLightManager();
jmri.util.JUnitUtil.initInternalSensorManager();
jmri.util.JUnitUtil.initDefaultSignalMastManager();
jmri.util.JUnitUtil.initSignalMastLogicManager();
jmri.util.JUnitUtil.initWarrantManager();
jmri.util.JUnitUtil.initOBlockManager();
jmri.util.JUnitUtil.initRouteManager();
jmri.util.JUnitUtil.initIdTagManager();

ix1 = new DefaultConditionalAction(ACTION_OPTION_ON_CHANGE_TO_TRUE, ACTION_SET_SENSOR, deviceName, 4, "5");
bean = InstanceManager.getDefault(SensorManager.class).provideSensor(deviceName);
Assert.assertTrue("getActionBean() returns correct bean", ix1.getBean().equals(bean));
@@ -36,6 +36,7 @@
import jmri.util.managers.InternalLightManagerThrowExceptionScaffold;
import jmri.util.managers.MemoryManagerThrowExceptionScaffold;
import jmri.util.managers.OBlockManagerThrowExceptionScaffold;
import jmri.util.managers.RouteManagerThrowExceptionScaffold;
import jmri.util.managers.SensorManagerThrowExceptionScaffold;
import jmri.util.managers.SignalHeadManagerThrowExceptionScaffold;
import jmri.util.managers.SignalMastManagerThrowExceptionScaffold;
@@ -736,6 +737,10 @@ public static void initOBlockManagerThrowException() {
InstanceManager.setDefault(OBlockManager.class, new OBlockManagerThrowExceptionScaffold());
}

public static void initRouteManagerThrowException() {
InstanceManager.setDefault(RouteManager.class, new RouteManagerThrowExceptionScaffold());
}

/**
* Leaves ShutDownManager, if any, in place,
* but removes its contents.
@@ -0,0 +1,49 @@
package jmri.util.managers;

import jmri.Route;
import jmri.managers.DefaultRouteManager;

/**
* This manager always throws an IllegalArgumentException from provide and get methods.
* It is used for testing exception handling in tests.
*
* The class name ends with 'Scaffold' to exclude it from the coverage statistics,
* since it is part of the testing infrastructure.
*/
public class RouteManagerThrowExceptionScaffold extends DefaultRouteManager {

public RouteManagerThrowExceptionScaffold() {
super();
}

/** {@inheritDoc} */
@Override
public Route provideRoute(String systemName, String userName) {
throw new IllegalArgumentException("Illegal argument");
}

/** {@inheritDoc} */
@Override
public Route getRoute(String name) {
throw new IllegalArgumentException("Illegal argument");
}

/** {@inheritDoc} */
@Override
public Route getBySystemName(String name) {
throw new IllegalArgumentException("Illegal argument");
}

/** {@inheritDoc} */
@Override
public Route getByUserName(String key) {
throw new IllegalArgumentException("Illegal argument");
}

/** {@inheritDoc} */
@Override
public Route newRoute(String userName) {
throw new IllegalArgumentException("Illegal argument");
}

}

0 comments on commit 60b163c

Please sign in to comment.
You can’t perform that action at this time.