Transferring to new fork

This commit is contained in:
Marcus Rockwell 2024-03-25 15:42:58 -05:00
parent 5c9615ddad
commit f75d26a4a8
4 changed files with 106 additions and 45 deletions

View File

@ -1245,7 +1245,7 @@ public class SimControlApplication extends TrickApplication implements PropertyC
/**
* Updates the GUI as needed if SIM states are changed.
*/
private void updateGUI() {
protected void updateGUI() {
String newStatusDesc = SimState.SIM_MODE_DESCRIPTION[simState.getMode()];
recTime.setText(simState.getTwoFractionFormatted(simState.getExecOutTime()));

View File

@ -0,0 +1,42 @@
package trick.common;
public class ApplicationTest {
public static String getTrickHome() {
String path;
// Getting TRICK_HOME environment variable
path = System.getenv("TRICK_HOME");
// Getting Trick's home directory if TRICK_HOME isn't set
if(path != null) {
if(path.endsWith("/"))
path.substring(0, path.length() - 1);
} else {
path = System.getProperty("user.dir");
int cutoff = path.indexOf("/trick_source");
path = (cutoff > 0) ? path.substring(0, cutoff) : "";
}
return path;
}
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) {}
}
}

View File

@ -29,4 +29,7 @@ public class HeadlessSimControlApplication extends SimControlApplication {
public void actionPerformed(ActionEvent UNUSED) {}
};
}
@Override
protected void updateGUI() { /* UNUSED */ }
}

View File

@ -19,7 +19,7 @@ import org.junit.Test;
import static org.junit.Assert.assertTrue;
import static org.junit.Assume.assumeTrue;
import trick.common.CheckApplicationProperties;
import trick.common.ApplicationTest;
import trick.common.SimulationInterface;
import trick.common.utils.VariableServerConnection;
@ -31,8 +31,8 @@ import trick.common.utils.VariableServerConnection;
* @intern mrockwell2
*
*/
public class SimControlApplicationTest {
private final static String SIM_DIR = "/home/cacistudent/trick/test/SIM_gui_testing";
public class SimControlApplicationTest extends ApplicationTest {
private final static String SIM_DIR = "/test/SIM_gui_testing";
private final ByteArrayOutputStream stdContent = new ByteArrayOutputStream();
private final PrintStream originalStd = System.out;
private final ByteArrayOutputStream errContent = new ByteArrayOutputStream();
@ -49,14 +49,16 @@ public class SimControlApplicationTest {
@BeforeClass
public static void setUpBeforeClass() {
SimulationInterface.cleanSim(SIM_DIR);
SimulationInterface.compileSim(SIM_DIR);
simProc = SimulationInterface.startSim(SIM_DIR, "RUN_test/input.py");
String fullDir = getTrickHome() + SIM_DIR;
SimulationInterface.cleanSim(fullDir);
SimulationInterface.compileSim(fullDir);
simProc = SimulationInterface.startSim(fullDir, "RUN_test/input.py");
sleep(1000);
try {
Scanner infoReader = new Scanner(new File(SIM_DIR + "/socket_info"));
Scanner infoReader = new Scanner(new File(fullDir + "/socket_info"));
String line = infoReader.nextLine();
host = line.split(":")[0];
@ -72,7 +74,7 @@ public class SimControlApplicationTest {
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);
assumeTrue("Did not find the port number", port >= 0);
}
@AfterClass
@ -88,28 +90,29 @@ public class SimControlApplicationTest {
@Before
public void setUp() {
// System.setOut(new PrintStream(stdContent));
System.setErr(new PrintStream(errContent));
// System.setErr(new PrintStream(errContent));
}
@After
public void tearDown() {
// System.setOut(originalStd);
System.setErr(originalErr);
// System.setErr(originalErr);
// stdContent.reset();
errContent.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;
simControl = new HeadlessSimControlApplication(host, port);
sleep(1000);
simControl.connect();
sleep(1000);
// ACT
varserv.put("trick.var_send_once(\"trick_sys.sched.mode\")");
@ -126,50 +129,63 @@ public class SimControlApplicationTest {
System.out.println(errContent.toString());
assertTrue("Sim Mode is not MODE_RUN(5)\nMODE_ID=" + mode, mode == MODE_RUN);
// CLEAN UP
varserv.put("trick.exec_freeze()\n");
}
@Test
public void testFreezeSim() throws IOException {
// ARRANGE
String out;
int mode = 1, count = 0;
simControl = new HeadlessSimControlApplication(host, port);
sleep(1000);
simControl.connect();
sleep(1000);
simControl.startSim();
sleep(1000);
varserv.put("trick.var_send_once(\"trick_sys.sched.mode\")");
mode = Integer.parseInt(varserv.get().split("\t")[1]);
assumeTrue("Sim Mode was not MODE_RUN at test start", mode == MODE_RUN);
// ACT
simControl.freezeSim();
do {
count ++;
varserv.put("trick.var_send_once(\"trick_sys.sched.mode\")");
mode = Integer.parseInt(varserv.get().split("\t")[1]);
} while (mode != 1 && count < 100000);
// ASSERT
System.out.println(errContent.toString());
assertTrue("Sim Mode is not MODE_FREEZE (1)\nMODE_ID=" + mode, mode == MODE_FREEZE);
}
@Test
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 expOutput = "Invalid TCP/IP port number \"0\"", output;
String badH = "localhost";
int badP = 0;
simControl = new HeadlessSimControlApplication();
System.setErr(new PrintStream(errContent));
// ACT
simControl.setHostPort(badH, badP);
simControl.connect();
sleep(500);
// ASSERT
output = errContent.toString().split("\n");
output = errContent.toString();
assertTrue("Did not recieve the expected error message: \n"
+ Arrays.toString(output),
compStringArray(expOutput, output));
}
+ output, output.indexOf(expOutput) >= 0);
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) {}
// CLEAN UP
System.setErr(originalErr);
}
}