Skip to content

Commit

Permalink
start testing throttle servers
Browse files Browse the repository at this point in the history
  • Loading branch information
pabender committed Sep 28, 2018
1 parent a7094cd commit f4c16f7
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 6 deletions.
51 changes: 51 additions & 0 deletions java/test/jmri/jmris/AbstractThrottleServerTestBase.java
@@ -0,0 +1,51 @@
package jmri.jmris;

import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import jmri.LocoAddress;
import jmri.DccLocoAddress;
import jmri.DccThrottle;
import jmri.Throttle;
import jmri.ThrottleListener;

/**
* Common tests for classes derived from jmri.jmris.AbstractThrottleServer class
*
* @author Paul Bender Copyright (C) 2017
*/
abstract public class AbstractThrottleServerTestBase {

protected AbstractThrottleServer ats = null;

@Test
public void testCtor() {
Assert.assertNotNull(ats);
}

@Test
public void requestThrottleTest(){
ats.requestThrottle(new DccLocoAddress(42,false));
confirmThrottleRequestSucceeded();
}

/**
* confirm the throttle request succeeded and an appropirate response
* was forwarded to the client.
*/
abstract public void confirmThrottleRequestSucceeded();

@Before
// derived classes must configure the ThrottleServer variable (ats)
// and should also install a throttle manager.
abstract public void setUp();

@After
public void postTestReset(){
ats = null;
}


}
62 changes: 56 additions & 6 deletions java/test/jmri/jmris/srcp/JmriSRCPThrottleServerTest.java
@@ -1,27 +1,77 @@
package jmri.jmris.srcp;

import org.junit.Assert;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import jmri.util.JUnitUtil;

/**
* Tests for the jmri.jmris.srcp.JmriSRCPThrottleServer class
*
* @author Paul Bender copyright (C) 2016
*/
public class JmriSRCPThrottleServerTest {
public class JmriSRCPThrottleServerTest extends jmri.jmris.AbstractThrottleServerTestBase {

private StringBuilder sb = null;

@Test
@Override
public void requestThrottleTest(){
try {
((JmriSRCPThrottleServer)ats).initThrottle(1,42,false,128,28);
confirmThrottleRequestSucceeded();
} catch (java.io.IOException ioe) {
Assert.fail("failed requesting throttle");
}
}

/**
* confirm the throttle request succeeded and an appropirate response
* was forwarded to the client.
*/
public void confirmThrottleRequestSucceeded(){
Assert.assertTrue("Throttle notification sent", sb.toString().endsWith("101 INFO 1 GL 42 N 1 28\n\r"));
}

@Test
public void testCtor() {
public void requestThrottleBadBusTest(){
try {
((JmriSRCPThrottleServer)ats).initThrottle(44,42,false,128,28);
} catch (java.io.IOException ioe) {
Assert.fail("failed requesting throttle");
}
Assert.assertTrue("wrong value",sb.toString().endsWith("412 ERROR wrong value\n\r"));
}

@Before
public void setUp() {
JUnitUtil.setUp();
JUnitUtil.resetProfileManager();
// verify the Internal System Connection memo is available.
jmri.InstanceManager.getDefault(jmri.jmrix.internal.InternalSystemConnectionMemo.class);

JUnitUtil.initInternalTurnoutManager();
JUnitUtil.initInternalLightManager();
JUnitUtil.initInternalSensorManager();
JUnitUtil.initDebugThrottleManager();
sb = new StringBuilder();
java.io.DataOutputStream output = new java.io.DataOutputStream(
new java.io.OutputStream() {
// null output string drops characters
// could be replaced by one that checks for specific outputs
@Override
public void write(int b) throws java.io.IOException {
sb.append((char)b);
}
});
java.io.DataInputStream input = new java.io.DataInputStream(System.in);
JmriSRCPThrottleServer a = new JmriSRCPThrottleServer(input,output);
Assert.assertNotNull(a);
ats = new JmriSRCPThrottleServer(input,output);
}

@After
public void tearDown() {
sb = null;
ats = null;
JUnitUtil.tearDown();
}
}

0 comments on commit f4c16f7

Please sign in to comment.