package de.ugoe.cs.quest.plugin.jfc;

import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import java.io.File;
import java.util.Collection;
import java.util.List;

import org.junit.Before;
import org.junit.Test;

import de.ugoe.cs.quest.eventcore.Event;
import de.ugoe.cs.quest.eventcore.guimodel.GUIModel;
import de.ugoe.cs.quest.eventcore.guimodel.IGUIElement;
import de.ugoe.cs.quest.plugin.jfc.guimodel.JFCGUIElement;

/**
 * TODO comment
 * 
 * @version $Revision: $ $Date: $
 * @author 2011, last modified by $Author: $
 */
public class JFCLogParserTest {

    /**
     *
     */
    @Before
    public void setUp() {
        // Logger.getLogger("").getHandlers()[0].setFormatter(new SimpleLogFormatter());
    }

    /**
     *
     */
    @Test
    public void test() throws Exception {
        JFCLogParser parser = new JFCLogParser(null);
        parser.parseFile(new File(ClassLoader.getSystemResource("trace.xml").getFile()));
        Collection<List<Event>> events = parser.getSequences();

        assertNotNull(events);
        assertTrue(events.size() > 0);

        System.err.println("{");
        for (List<Event> session : events) {
            System.err.println("  {");
            for (Event event : session) {
                System.err.print("    ");
                System.err.print(event);
                System.err.println(",");
            }
            System.err.println("  }");
        }
        System.err.println("}");
        System.err.println("\n\n");

        GUIModel guiModel = parser.getGuiModel();
        assertNotNull(guiModel);

        for (IGUIElement root : guiModel.getRootElements()) {
            dumpGUIElement(root, guiModel, "");
        }
    }

    /**
     * TODO: comment
     * 
     * @param root
     * @param guiModel
     */
    private void dumpGUIElement(IGUIElement guiElement, GUIModel guiModel, String indent) {
        assertTrue(guiElement instanceof JFCGUIElement);

        System.err.print(indent);
        System.err.print(guiElement);

        List<IGUIElement> children = guiModel.getChildren(guiElement);

        if ((children != null) && (children.size() > 0)) {
            System.err.println(" {");

            for (IGUIElement child : children) {
                dumpGUIElement(child, guiModel, indent + "  ");
            }

            System.err.print(indent);
            System.err.print("}");
        }

        System.err.println();
    }

}
