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.util.Properties;
import java.util.logging.Logger;
import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertTrue;
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 ParamicsLogFileHandlerTest extends TestCase
{

    public ParamicsLogFileHandlerTest(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
        {
            new CADSimulator("config/testConfig/cad_simulator_console_config.properties");
        } catch (Exception e)
        {
            fail("Couldn't launch CADSimulator");
        }
        //ParamicsLog plog = ParamicsLog.getInstance();
        Properties paramicsLogProp = new Properties();
        paramicsLogProp.load(new FileInputStream(propsfile));
        String logFilename = paramicsLogProp.getProperty("LogFile");
        // This test requires a CADSimulator running.
        Logger logger = Logger.getLogger("tmcsim.parmicsLog");
        ParamicsLogFileHandler fh = ParamicsLogFileHandler.getInstance();
        logger.addHandler(fh);
        // write a message to the log.
        logger.info("Hello friendly log.");
        // Read from the log file and see if the desired msg is there.
        File logFile = new File(logFilename);
        assertTrue("log file doesn't exist", logFile.exists());
        String expected = "\n<!-- Time written to file: 00:00:00 -->\nHello friendly log.\n";
        String actual = fh.getLog();
        assertEquals("log written incorrectly", expected, actual);
    }
}
