package tmcsim.paramicscommunicator;

import java.io.File;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Observable;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import static junit.framework.Assert.assertTrue;
import junit.framework.TestCase;
import org.w3c.dom.Document;

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

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

    @Override
    protected void setUp() throws Exception
    {
        super.setUp();
    }

    @Override
    public void tearDown() throws java.io.IOException
    {
        File removeMe = new File("inputfile.txt");
        removeMe.delete();
    }

    private static void writedata(String filename, String data)
    {
        PrintWriter writer = null;
        try
        {
            writer = new PrintWriter(new FileWriter(filename));
            writer.println(data);
            writer.close();
        } catch (Exception ex)
        {
            ex.printStackTrace();
        }
    }
    /* Convert a DOM to a string, thank you internet.  */

    private static String toString(Document newDoc) throws Exception
    {
        DOMSource domSource = new DOMSource(newDoc);
        Transformer transformer = TransformerFactory.newInstance().newTransformer();
        StringWriter sw = new StringWriter();
        StreamResult sr = new StreamResult(sw);
        transformer.transform(domSource, sr);
        return sw.toString();
    }
    String kFileContents = "Hello Jupiter";

    public void testConstructor() throws Exception
    {
        writedata("inputfile.txt", kFileContents);
        ParamicsFileReader pfr = new ParamicsFileReader("", "1", 2, "inputfile.txt");
        ReaderObserver watcher = new ReaderObserver();
        pfr.addObserver(watcher);
        try
        {
            Thread.sleep(2000);
        } catch (Exception e)
        {
            fail();
        }
        assertTrue(watcher.count > 0);
        String actual = "";
        actual = toString(watcher.result);
        assertTrue("Reader produced wrong output", actual.contains(kFileContents));
    }

    class ReaderObserver implements java.util.Observer
    {

        int count = 0;
        Document result;

        @Override
        public void update(Observable obs, Object obj)
        {
            count += 1;
            if (obj instanceof FileIOUpdate)
            {
                FileIOUpdate msg = (FileIOUpdate) obj;
                assertEquals("1", msg.ioID);
                // Windows and Unix have different EOL characters, so we have
                // to approximate the answer here.
                boolean result = kFileContents.length() - msg.ioBytes < 2;
                assertTrue(result);
            } else if (obj instanceof Document)
            {
                result = (Document) obj;
            } else
            {
                fail("unrecognized update object " + obj.getClass());
            }
        }
    }
}
