| 1 | package tmcsim.paramicslog; |
|---|
| 2 | |
|---|
| 3 | import java.io.File; |
|---|
| 4 | import java.io.FileInputStream; |
|---|
| 5 | import java.io.FileNotFoundException; |
|---|
| 6 | import java.io.IOException; |
|---|
| 7 | import java.rmi.RemoteException; |
|---|
| 8 | import java.text.ParseException; |
|---|
| 9 | import java.text.SimpleDateFormat; |
|---|
| 10 | import java.util.Date; |
|---|
| 11 | import java.util.Properties; |
|---|
| 12 | import static junit.framework.Assert.assertEquals; |
|---|
| 13 | import static junit.framework.Assert.fail; |
|---|
| 14 | import junit.framework.TestCase; |
|---|
| 15 | import tmcsim.cadsimulator.CADServer; |
|---|
| 16 | import tmcsim.common.ScriptException; |
|---|
| 17 | import tmcsim.common.SimulationException; |
|---|
| 18 | |
|---|
| 19 | /** |
|---|
| 20 | * |
|---|
| 21 | * @author jdalbey |
|---|
| 22 | */ |
|---|
| 23 | public class ParamicsLogRMITestSkeleton extends TestCase |
|---|
| 24 | { |
|---|
| 25 | |
|---|
| 26 | public ParamicsLogRMITestSkeleton(String testName) |
|---|
| 27 | { |
|---|
| 28 | super(testName); |
|---|
| 29 | } |
|---|
| 30 | |
|---|
| 31 | @Override |
|---|
| 32 | protected void setUp() throws Exception |
|---|
| 33 | { |
|---|
| 34 | super.setUp(); |
|---|
| 35 | } |
|---|
| 36 | String propsfile = "config/paramics_communicator_logging.properties"; |
|---|
| 37 | |
|---|
| 38 | /** |
|---|
| 39 | * Test of getInstance method, of class ParamicsLog. |
|---|
| 40 | */ |
|---|
| 41 | public void testGetInstance() throws ScriptException, SimulationException, RemoteException, FileNotFoundException, IOException |
|---|
| 42 | { |
|---|
| 43 | System.out.println("getInstance"); |
|---|
| 44 | try |
|---|
| 45 | { |
|---|
| 46 | CADServer engine = new CADServer("config/cad_simulator_console_config.properties"); |
|---|
| 47 | } catch (Exception e) |
|---|
| 48 | { |
|---|
| 49 | fail("Couldn't launch CADSimulator"); |
|---|
| 50 | } |
|---|
| 51 | try |
|---|
| 52 | { |
|---|
| 53 | Thread.sleep(2000); |
|---|
| 54 | } catch (InterruptedException ex) |
|---|
| 55 | { |
|---|
| 56 | } |
|---|
| 57 | // This seems to fail because the ParamicsLog static initializer gets |
|---|
| 58 | // run before the CADSimulator is created, so no RMI connection can be made. |
|---|
| 59 | ParamicsLog plog = ParamicsLog.getInstance(); |
|---|
| 60 | Properties paramicsLogProp = new Properties(); |
|---|
| 61 | paramicsLogProp.load(new FileInputStream(propsfile)); |
|---|
| 62 | String logFilename = paramicsLogProp.getProperty("LogFile"); |
|---|
| 63 | File logFile = new File(logFilename); |
|---|
| 64 | assertTrue("log file doesn't exist", logFile.exists()); |
|---|
| 65 | |
|---|
| 66 | plog.writeToLog("Hello friendly log."); |
|---|
| 67 | String expected = "\n<!-- Time written to file: ? -->\nHello friendly log.\n"; |
|---|
| 68 | String actual = plog.getLog(); |
|---|
| 69 | assertEquals("log written incorrectly", expected, actual); |
|---|
| 70 | } |
|---|
| 71 | |
|---|
| 72 | public void testFormatEasyTime() |
|---|
| 73 | { |
|---|
| 74 | ParamicsLog plog = ParamicsLog.getInstance(); |
|---|
| 75 | String expected = "00:01:02"; |
|---|
| 76 | String actual = plog.formatTime(62l); |
|---|
| 77 | assertEquals("time format wrong", expected, actual); |
|---|
| 78 | |
|---|
| 79 | expected = "1:00:01"; |
|---|
| 80 | actual = plog.formatTime(3601l); |
|---|
| 81 | assertEquals("time format wrong", expected, actual); |
|---|
| 82 | } |
|---|
| 83 | |
|---|
| 84 | public void testFormatDate() throws ParseException |
|---|
| 85 | { |
|---|
| 86 | ParamicsLog plog = ParamicsLog.getInstance(); |
|---|
| 87 | SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss"); |
|---|
| 88 | String expected = "01:01:01"; |
|---|
| 89 | Date one = formatter.parse(expected); |
|---|
| 90 | long millis = one.getTime(); |
|---|
| 91 | String actual = plog.formatTime(millis); |
|---|
| 92 | assertEquals("time format wrong", expected, actual); |
|---|
| 93 | } |
|---|
| 94 | } |
|---|