mirror of
https://github.com/nasa/trick.git
synced 2025-03-22 12:05:20 +00:00
Created initial headless test cases
This commit is contained in:
parent
6cc93ecb19
commit
5c9615ddad
@ -0,0 +1,32 @@
|
||||
package trick.simcontrol;
|
||||
|
||||
|
||||
import org.jdesktop.application.Application;
|
||||
|
||||
import trick.simcontrol.utils.SimControlActionController;
|
||||
|
||||
import java.awt.*;
|
||||
import java.awt.event.ActionEvent;
|
||||
|
||||
import javax.swing.Action;
|
||||
import javax.swing.AbstractAction;
|
||||
|
||||
|
||||
public class HeadlessSimControlApplication extends SimControlApplication {
|
||||
|
||||
public HeadlessSimControlApplication() {
|
||||
this("", -1);
|
||||
}
|
||||
|
||||
public HeadlessSimControlApplication(String hostname, int portNum) {
|
||||
super.setHostPort(hostname, portNum);
|
||||
actionController = new SimControlActionController();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Action getAction(String key) {
|
||||
return new AbstractAction() {
|
||||
public void actionPerformed(ActionEvent UNUSED) {}
|
||||
};
|
||||
}
|
||||
}
|
@ -1,6 +1,14 @@
|
||||
package trick.simcontrol;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.beans.Transient;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.io.PrintStream;
|
||||
import java.util.Arrays;
|
||||
import java.util.Scanner;
|
||||
|
||||
import org.jdesktop.application.Application;
|
||||
import org.junit.After;
|
||||
@ -8,8 +16,12 @@ import org.junit.AfterClass;
|
||||
import org.junit.Before;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assume.assumeTrue;
|
||||
|
||||
import trick.common.CheckApplicationProperties;
|
||||
import trick.common.SimulationInterface;
|
||||
import trick.common.utils.VariableServerConnection;
|
||||
|
||||
/**
|
||||
*
|
||||
@ -20,68 +32,144 @@ import trick.common.CheckApplicationProperties;
|
||||
*
|
||||
*/
|
||||
public class SimControlApplicationTest {
|
||||
private final static String SIM_DIR = "/home/cacistudent/trick/test/SIM_gui_testing";
|
||||
private final ByteArrayOutputStream stdContent = new ByteArrayOutputStream();
|
||||
private final PrintStream originalStd = System.out;
|
||||
private final ByteArrayOutputStream errContent = new ByteArrayOutputStream();
|
||||
private final PrintStream originalErr = System.err;
|
||||
|
||||
private final int MODE_FREEZE = 1,
|
||||
MODE_RUN = 5;
|
||||
|
||||
private static HeadlessSimControlApplication simControl;
|
||||
private static VariableServerConnection varserv;
|
||||
private static Process simProc;
|
||||
private static String host;
|
||||
private static int port;
|
||||
|
||||
@BeforeClass
|
||||
public static void setUpBeforeClass() throws Exception {
|
||||
WaitForSimControlApplication.launchAndWait(WaitForSimControlApplication.class);
|
||||
public static void setUpBeforeClass() {
|
||||
SimulationInterface.cleanSim(SIM_DIR);
|
||||
SimulationInterface.compileSim(SIM_DIR);
|
||||
simProc = SimulationInterface.startSim(SIM_DIR, "RUN_test/input.py");
|
||||
|
||||
sleep(1000);
|
||||
|
||||
try {
|
||||
Scanner infoReader = new Scanner(new File(SIM_DIR + "/socket_info"));
|
||||
String line = infoReader.nextLine();
|
||||
|
||||
host = line.split(":")[0];
|
||||
port = Integer.parseInt(line.split(":")[1]);
|
||||
|
||||
varserv = new VariableServerConnection(host, port);
|
||||
} catch (Exception e) {
|
||||
System.err.println(e.getMessage());
|
||||
host = "";
|
||||
port = -1;
|
||||
varserv = null;
|
||||
}
|
||||
|
||||
assumeTrue("Did not connect to the variable server", varserv != null);
|
||||
assumeTrue("Did not find the host name.", !host.isEmpty());
|
||||
assumeTrue("Did not find the port numbber", port >= 0);
|
||||
}
|
||||
|
||||
@AfterClass
|
||||
public static void tearDownAfterClass() throws Exception {
|
||||
public static void tearDownAfterClass() {
|
||||
// if(simControl != null) {
|
||||
// simControl.freezeSim();
|
||||
// simControl.shutdown();
|
||||
// }
|
||||
if(simProc != null && simProc.isAlive())
|
||||
simProc.destroy();
|
||||
}
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
public void setUp() {
|
||||
// System.setOut(new PrintStream(stdContent));
|
||||
System.setErr(new PrintStream(errContent));
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() throws Exception {
|
||||
public void tearDown() {
|
||||
// System.setOut(originalStd);
|
||||
System.setErr(originalErr);
|
||||
|
||||
// stdContent.reset();
|
||||
errContent.reset();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStartSim() throws IOException {
|
||||
// ARRANGE
|
||||
System.out.println("START TEST");
|
||||
simControl = new HeadlessSimControlApplication(host, port);
|
||||
sleep(1000);
|
||||
System.out.println("CONNECT");
|
||||
simControl.connect();
|
||||
String out;
|
||||
int mode = 1, count = 0;
|
||||
|
||||
// ACT
|
||||
varserv.put("trick.var_send_once(\"trick_sys.sched.mode\")");
|
||||
mode = Integer.parseInt(varserv.get().split("\t")[1]);
|
||||
assumeTrue("Sim Mode was not MODE_FREEZE at test start", mode == MODE_FREEZE);
|
||||
simControl.startSim();
|
||||
do {
|
||||
count ++;
|
||||
varserv.put("trick.var_send_once(\"trick_sys.sched.mode\")");
|
||||
mode = Integer.parseInt(varserv.get().split("\t")[1]);
|
||||
} while (mode != 5 && count < 100000);
|
||||
|
||||
// ASSERT
|
||||
System.out.println(errContent.toString());
|
||||
assertTrue("Sim Mode is not MODE_RUN(5)\nMODE_ID=" + mode, mode == MODE_RUN);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReady() {
|
||||
assertTrue("SimControlApplicationTest is not ready yet!", application().isReady());
|
||||
public void testConnectionFail() {
|
||||
// ARRANGE
|
||||
String[] expOutput = { "Sim Control Error at Initialization: ",
|
||||
"Error: SimControlApplication:getInitializationPacket()",
|
||||
" Invalid TCP/IP port number \"0\"",
|
||||
" Please check the server and enter a proper port number!",
|
||||
" IOException ...java.net.ConnectException: Connection refused (Connection refused)",
|
||||
" If there is no connection, please make sure SIM is up running properly!" },
|
||||
output;
|
||||
|
||||
String badH = "localhost";
|
||||
int badP = 0;
|
||||
simControl = new HeadlessSimControlApplication();
|
||||
|
||||
// ACT
|
||||
simControl.setHostPort(badH, badP);
|
||||
simControl.connect();
|
||||
|
||||
// ASSERT
|
||||
output = errContent.toString().split("\n");
|
||||
assertTrue("Did not recieve the expected error message: \n"
|
||||
+ Arrays.toString(output),
|
||||
compStringArray(expOutput, output));
|
||||
}
|
||||
|
||||
/**
|
||||
* Verify that all implemented actions exist as well as their text, shortDescription properties.
|
||||
*/
|
||||
@Test
|
||||
public void testDefinedActions() {
|
||||
String[] actionNames = {"saveStatusMsgs", "clearStatusMsgs", "startTV", "startMTV", "freezeAt", "freezeIn",
|
||||
"checkpointObjects", "throttle", "connect", "stepSim", "recordingSim", "startSim",
|
||||
"realtime", "freezeSim", "dumpChkpntASCII", "shutdownSim", "loadChkpnt", "lite"};
|
||||
String[] actionTexts = {"Save Status Msgs...", "Clear Status Msgs...", "Start Trick View",
|
||||
"Start Event/Malfunction Trick View", "Freeze At...", "Freeze In...",
|
||||
"Checkpoint Objects...", "Throttle...", "Connect", "Step", "Data Rec On",
|
||||
"Start", "RealTime On", "Freeze", "Dump Chkpnt", "Shutdown", "Load Chkpnt", "Lite"};
|
||||
String[] actionShortDescriptions = {"Save Status Messages", "Clear Status Messages", "Start Trick View (TV)",
|
||||
"Start Event/Malfunction Trick View (MTV)", "Freeze At", "Freeze In",
|
||||
"Checkpoint the specified objects", "Throttle", "Connect to the specified server at specified port",
|
||||
"Step through sim initialization", "Data Recording On/Off", "Start the simulation",
|
||||
"Realtime on/off", "Freeze/Stop the simulation", "Dump ASCII Checkpoint",
|
||||
"Shutdown Simulation", "Load ASCII Checkpoint", "Lite/Full"};
|
||||
|
||||
for (int i = 0; i < actionNames.length; i++) {
|
||||
CheckApplicationProperties.checkAction(application().actionMap, actionNames[i]);
|
||||
CheckApplicationProperties.checkActionText(application().actionMap, actionNames[i], actionTexts[i]);
|
||||
CheckApplicationProperties.checkActionShortDescription(application().actionMap, actionNames[i], actionShortDescriptions[i]);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDefinedKeyText() {
|
||||
CheckApplicationProperties.checkKeyText(application().resourceMap, "fileMenu.text", "&File");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testExit() {
|
||||
application().removeExitListener(application().exitListener);
|
||||
application().exit();
|
||||
assertTrue(application().isEnded);
|
||||
|
||||
public boolean compStringArray(String[] a, String[] b) {
|
||||
boolean same = true;
|
||||
for(int i = 0; i < a.length && i < b.length; i++) {
|
||||
same = same && sameLetters(a[i], b[i]);
|
||||
if(!same) return same;
|
||||
}
|
||||
return same;
|
||||
}
|
||||
|
||||
public boolean sameLetters(String str1, String str2) {
|
||||
String a = str1.replaceAll("\\s+", "").toLowerCase(),
|
||||
b = str2.replaceAll("\\s+", "").toLowerCase();
|
||||
return a.equals(b);
|
||||
}
|
||||
|
||||
public static void sleep(long ms) {
|
||||
try {Thread.sleep(ms);} catch(Exception ignored) {}
|
||||
}
|
||||
|
||||
private static WaitForSimControlApplication application() {
|
||||
return Application.getInstance(WaitForSimControlApplication.class);
|
||||
}
|
||||
}
|
@ -1,50 +0,0 @@
|
||||
package trick.simcontrol;
|
||||
|
||||
|
||||
import org.jdesktop.application.Application;
|
||||
|
||||
|
||||
public class WaitForSimControlApplication extends SimControlApplication {
|
||||
static Object lock = new Object();
|
||||
|
||||
boolean isEnded;
|
||||
|
||||
@Override
|
||||
protected void end() {
|
||||
isEnded = true;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void ready() {
|
||||
super.ready();
|
||||
synchronized(lock) {
|
||||
lock.notifyAll();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Launch the specified subclsas of SimControlApplication and block
|
||||
* (wait) until it's startup() method has run.
|
||||
*/
|
||||
public static void launchAndWait(Class<? extends WaitForSimControlApplication> applicationClass) {
|
||||
synchronized(lock) {
|
||||
Application.launch(applicationClass, new String[]{});
|
||||
while(true) {
|
||||
try {
|
||||
lock.wait();
|
||||
}
|
||||
catch (InterruptedException e) {
|
||||
System.err.println("launchAndWait interrupted!");
|
||||
break;
|
||||
}
|
||||
Application app = Application.getInstance(WaitForSimControlApplication.class);
|
||||
if (app instanceof WaitForSimControlApplication) {
|
||||
if (((WaitForSimControlApplication)app).isReady()) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user