Skip to content

Commit

Permalink
more Mocking
Browse files Browse the repository at this point in the history
  • Loading branch information
pabender committed Mar 21, 2020
1 parent 01b9262 commit 8712fc7
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 31 deletions.
15 changes: 10 additions & 5 deletions java/src/jmri/jmris/srcp/JmriSRCPProgrammerServer.java
@@ -1,9 +1,9 @@
package jmri.jmris.srcp;

import java.io.DataOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import jmri.InstanceManagerDelegate;
import jmri.ProgListener;
import jmri.jmris.AbstractProgrammerServer;
import org.slf4j.Logger;
Expand All @@ -19,8 +19,12 @@ public class JmriSRCPProgrammerServer extends AbstractProgrammerServer {

private OutputStream output;

public JmriSRCPProgrammerServer(OutputStream outStream) {
super();
public JmriSRCPProgrammerServer(OutputStream outputStream){
this(outputStream,new InstanceManagerDelegate());
}

public JmriSRCPProgrammerServer(OutputStream outStream,InstanceManagerDelegate instanceManagerDelegate) {
super(instanceManagerDelegate);
output = outStream;
}

Expand All @@ -36,17 +40,18 @@ public void sendStatus(int CV, int value, int status) throws IOException {
if (status == ProgListener.OK) {
output.write(("100 INFO 1 SM " + CV + " CV " + value + "\n\r").getBytes());
} else {
output.write("416 ERROR no data\n\r".getBytes());
output.write(Bundle.getMessage("Error416").getBytes());
}
}

@Override
public void sendNotAvailableStatus() throws IOException {
output.write("499 ERROR unspecified error\n".getBytes());
output.write(Bundle.getMessage("Error499").getBytes());
}

@Override
public void parseRequest(String statusString) throws jmri.JmriException, java.io.IOException {
// SRCP requests are parsed through the common parser.
}

private static final Logger log = LoggerFactory.getLogger(JmriSRCPProgrammerServer.class);
Expand Down
1 change: 1 addition & 0 deletions java/src/jmri/jmris/srcp/JmriSRCPServerBundle.properties
Expand Up @@ -19,6 +19,7 @@ Error411 = 411 ERROR unknown value\n\r
Error412 = 412 ERROR wrong value\n\r
Error414 = 414 ERROR device locked\n\r
Error415 = 415 ERROR forbidden\n\r
Error416 = 416 ERROR no data\n\r
Error417 = 417 ERROR timeout\n\r
Error418 = 418 ERROR list too long\n\r
Error419 = 419 ERROR list too short\n\r
Expand Down
2 changes: 1 addition & 1 deletion java/test/jmri/jmris/srcp/JmriSRCPPowerServerTest.java
Expand Up @@ -108,7 +108,7 @@ public void checkUnknownStatusSent() {
@BeforeEach
@Override
public void setUp() {
JUnitUtil.setUp(); // ToDo refactor common setup so there is a version that doesn't init instance manager.
JUnitUtil.setUpForMockInstanceManager();
instanceManagerDelegate = Mockito.mock(InstanceManagerDelegate.class);
powerManager = Mockito.mock(PowerManager.class);
Mockito.when(instanceManagerDelegate.getDefault(PowerManager.class)).thenReturn(powerManager);
Expand Down
33 changes: 20 additions & 13 deletions java/test/jmri/jmris/srcp/JmriSRCPProgrammerServerTest.java
@@ -1,9 +1,16 @@
package jmri.jmris.srcp;

import jmri.AddressedProgrammerManager;
import jmri.GlobalProgrammerManager;
import jmri.InstanceManagerDelegate;
import jmri.Programmer;
import jmri.util.JUnitUtil;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;

import java.io.ByteArrayOutputStream;
import java.io.OutputStream;

import static org.assertj.core.api.Assertions.assertThat;
Expand All @@ -16,30 +23,30 @@
*/
public class JmriSRCPProgrammerServerTest{

private InstanceManagerDelegate instanceManagerDelegate;

@Test
public void testCtor() {
OutputStream output = new 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 {
}
};
JmriSRCPProgrammerServer a = new JmriSRCPProgrammerServer(output);
OutputStream output = new ByteArrayOutputStream();
GlobalProgrammerManager programmerManager = Mockito.mock(GlobalProgrammerManager.class);
Mockito.when(instanceManagerDelegate.getDefault(GlobalProgrammerManager.class)).thenReturn(programmerManager);
Mockito.when(instanceManagerDelegate.getNullableDefault(GlobalProgrammerManager.class)).thenReturn(programmerManager);
Programmer programmer = Mockito.mock(Programmer.class);
Mockito.when(programmerManager.getGlobalProgrammer()).thenReturn(programmer);
JmriSRCPProgrammerServer a = new JmriSRCPProgrammerServer(output,instanceManagerDelegate);
assertThat(a).isNotNull();
}

@BeforeEach
public void setUp() {
jmri.util.JUnitUtil.setUp();

jmri.util.JUnitUtil.resetInstanceManager();
jmri.InstanceManager.store(new jmri.NamedBeanHandleManager(), jmri.NamedBeanHandleManager.class);
JUnitUtil.setUpForMockInstanceManager();
instanceManagerDelegate = Mockito.mock(InstanceManagerDelegate.class);
//jmri.InstanceManager.store(new jmri.NamedBeanHandleManager(), jmri.NamedBeanHandleManager.class);
}

@AfterEach
public void tearDown() {
jmri.util.JUnitUtil.tearDown();
JUnitUtil.tearDown();
}

}
33 changes: 21 additions & 12 deletions java/test/jmri/util/JUnitUtil.java
Expand Up @@ -169,55 +169,64 @@ public class JUnitUtil {
static private String initPrefsDir = null;

/**
* JMRI standard setUp for tests. This should be the first line in the {@code @Before}
* annotated method.
* JMRI standard setUp for tests that mock the InstanceManager. This should be the first line in the {@code @Before}
* annotated method if the tests mock the InstanceManager.
*/
public static void setUp() {
public static void setUpForMockInstanceManager() {
if (!isLoggingInitialized) {
// init logging if needed
isLoggingInitialized = true;
String filename = System.getProperty("jmri.log4jconfigfilename", "tests.lcf");
Log4JUtil.initLogging(filename);
}

// need to do this each time
try {
JUnitAppender.start();

// reset warn _only_ once logic to make tests repeatable
Log4JUtil.restartWarnOnce();
// ensure logging of deprecated method calls;
// individual tests can turn off as needed
Log4JUtil.setDeprecatedLogging(true);

} catch (Throwable e) {
System.err.println("Could not start JUnitAppender, but test continues:\n" + e);
}
// clear the backlog and reset the UnexpectedMessageFlags so that

// clear the backlog and reset the UnexpectedMessageFlags so that
// errors from a previous test do not interfere with the current test.
JUnitAppender.clearBacklog();
JUnitAppender.resetUnexpectedMessageFlags(Level.INFO);
JUnitAppender.resetUnexpectedMessageFlags(Level.INFO);


// do not set the UncaughtExceptionHandler while unit testing
// individual tests can explicitly set it after calling this method
Thread.setDefaultUncaughtExceptionHandler(null);

// make sure the jmri.prefsdir property match the property passed
// make sure the jmri.prefsdir property match the property passed
// to the tests.
if (initPrefsDir == null) {
initPrefsDir = System.getProperty("jmri.prefsdir", "./temp");
}
System.setProperty("jmri.prefsdir",initPrefsDir);
System.setProperty("jmri.prefsdir", initPrefsDir);

// silence the Jemmy GUI unit testing framework
JUnitUtil.silenceGUITestOutput();

// ideally this would be resetWindows(false, true) to force an error if an earlier
// test left a window open, but different platforms seem to have just
// enough differences that this is, for now, turned off
resetWindows(false, false);
}

/**
* JMRI standard setUp for tests. This should be the first line in the {@code @Before}
* annotated method if the tests do not mock the InstanceManager.
*/
public static void setUp() {
// all the setup for a MockInstanceManager applies
setUpForMockInstanceManager();

// Do a minimal amount of de-novo setup
resetInstanceManager();
Expand Down

0 comments on commit 8712fc7

Please sign in to comment.