package tmcsim.paramicslog;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.rmi.RemoteException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;
import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.fail;
import junit.framework.TestCase;
import tmcsim.cadsimulator.CADSimulator;
import tmcsim.common.ScriptException;
import tmcsim.common.SimulationException;

/**
 *
 * @author jdalbey
 */
public class ParamicsLogRMITestSkeleton extends TestCase
{

    public ParamicsLogRMITestSkeleton(String testName)
    {
        super(testName);
    }

    @Override
    protected void setUp() throws Exception
    {
        super.setUp();
    }
    String propsfile = "config/paramics_communicator_logging.properties";

    /**
     * Test of getInstance method, of class ParamicsLog.
     */
    public void testGetInstance() throws ScriptException, SimulationException, RemoteException, FileNotFoundException, IOException
    {
        System.out.println("getInstance");
        try
        {
            CADSimulator engine = new CADSimulator("config/testConfig/cad_simulator_console_config.properties");
        } catch (Exception e)
        {
            fail("Couldn't launch CADSimulator");
        }
        try
        {
            Thread.sleep(2000);
        } catch (InterruptedException ex)
        {
        }
        // This seems to fail because the ParamicsLog static initializer gets
        // run before the CADSimulator is created, so no RMI connection can be made.
        ParamicsLog plog = ParamicsLog.getInstance();
        Properties paramicsLogProp = new Properties();
        paramicsLogProp.load(new FileInputStream(propsfile));
        String logFilename = paramicsLogProp.getProperty("LogFile");
        File logFile = new File(logFilename);
        assertTrue("log file doesn't exist", logFile.exists());

        plog.writeToLog("Hello friendly log.");
        String expected = "\n<!-- Time written to file: ? -->\nHello friendly log.\n";
        String actual = plog.getLog();
        assertEquals("log written incorrectly", expected, actual);
    }

    public void testFormatEasyTime()
    {
        ParamicsLog plog = ParamicsLog.getInstance();
        String expected = "00:01:02";
        String actual = plog.formatTime(62l);
        assertEquals("time format wrong", expected, actual);

        expected = "1:00:01";
        actual = plog.formatTime(3601l);
        assertEquals("time format wrong", expected, actual);
    }

    public void testFormatDate() throws ParseException
    {
        ParamicsLog plog = ParamicsLog.getInstance();
        SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
        String expected = "01:01:01";
        Date one = formatter.parse(expected);
        long millis = one.getTime();
        String actual = plog.formatTime(millis);
        assertEquals("time format wrong", expected, actual);
    }
}
