<%@page contentType="text/html;charset=UTF-8" pageEncoding="ISO-8859-1" session="true"
%><%@page errorPage="Exception.jsp"
%><%@page import="com.fujitsu.iflow.model.bpm.BPMSession"
%><%@page import="com.fujitsu.iflow.model.bpmcore.BPMSessionImpl"
%><%@page import="com.fujitsu.iflow.model.util.QuickForm"
%><%@page import="com.fujitsu.iflow.server.dataobjects.FilterEnum"
%><%@page import="com.fujitsu.iflow.server.dataobjects.ProcessInstanceStatesEnum"
%><%@page import="com.fujitsu.iflowqa.testframe.TestDriver"
%><%@page import="com.fujitsu.iflowqa.testframe.TestRecorder"
%><%@page import="com.fujitsu.iflowqa.testframe.TestRecorderText"
%><%@page import="java.io.File"
%><%@page import="java.io.FileReader"
%><%@page import="java.io.Writer"
%><%@page import="java.io.BufferedReader"
%><%@page import="java.text.SimpleDateFormat"
%><%@page import="java.util.Date"
%><%@page import="java.util.Enumeration"
%><%@page import="java.util.Vector"
%><%

    request.setCharacterEncoding("UTF-8");request.getParameterNames(); //Need dummy call for WL9.1
    BPMSession bps = (BPMSession) session.getAttribute("BPMSession");
    if (bps == null) {
        %><jsp:include page="PasswordPanel.jsp" flush="true"/><%
        return;
    }

    String testDir = request.getParameter("testDir");
    if (testDir == null) {
        throw new Exception("The parameters 'testDir' must be set for the page RunTestResult.jsp");
    }
    testDir = testDir.replace('\\','/');
    session.setAttribute("testDir", testDir);

    String testList = request.getParameter("testList");
    if (testList == null) {
        throw new Exception("The parameters 'testList' must be set for the page RunTestResult.jsp");
    }

    session.setAttribute("testList", testList);
    File testDirFile = new File(testDir);
    if (!testDirFile.exists()) {
        throw new Exception("The directory '"+testDir+"' does not exist!  Please check your configuration");
    }


    String[] bcolor = {"evenrow","oddrow"};
    int nextColor = 0;
    String rowColor = bcolor[nextColor];

%>
<html>
<head>
<title>Interstage Business Process Manager | Process Instance List</title>
<jsp:include page="StyleSheet.jsp" flush="true"/>
<script language="javascript" src="styles/clockscripts.js"></script>
</head>

<body class="bodyClass">


<jsp:include page="Header.jsp" flush="true"/><br>

<h1>Display List <%=testList%></h1>
<a href="RunTest.jsp">Return to RunTest</a><br>
<hr>

<table>
<%

    int lineCount = 0;
    String[] mainLineArgs = new String[0];
    try {
        String[] testClassNames = readTestListFile(testDirFile, testList);
        for (int i=0; i < testClassNames.length; i++) {
            String name = testClassNames[i];
            if (name.length() < 5) {
                continue;
            }
            if (name.charAt(0) == '#') {
                continue;
            }
            lineCount = i;

            //parse out the args from this line
            int index = 0;
            int startPos = 0;
            Vector newArgs = new Vector();
            int pos = name.indexOf(" ");
            while (pos >= 0 && startPos < name.length()) {
                String token = name.substring(startPos, pos);
                newArgs.add(token);
                startPos = pos + 1;
                pos = name.indexOf(" ", startPos);
            }
            if (startPos < name.length()) {
                newArgs.add(name.substring(startPos));
            }

            // append whatever args were on the command line (except the first)
            for (int k=1; k<mainLineArgs.length; k++) {
                newArgs.add(name.substring(startPos));
            }

            %>
            <tr><td><form action="RunTestResult.jsp" method="get">
            <input type="hidden" name="testDir", value="<%=testDir%>">
            <input type="hidden" name="className", value="<%=newArgs.elementAt(0)%>">
            <%
            for (int j=1; j<newArgs.size(); j++) {
                %>
                <input type="hidden" name="param<%=(j-1)%>", value="<%=newArgs.elementAt(j)%>">
                <%
            }

            %>
            <input type="submit" name="go", value="Run"><%=name%></form></td></tr>
            <%
        }
    }
    catch (Exception e) {
        throw new Exception("Line '"+lineCount+"' of file '"+testList+"' contains a entry that caused an exception"+e.toString());
    }

%>
</table>
<hr>

</body>
</html>


<%!

    private String[] readTestListFile(File dir, String fileName)
        throws Exception
    {
        File testListFile = new File(dir, fileName);
        if (!testListFile.exists()) {
            throw new Exception("The test list file does not exist at "+testListFile.getCanonicalPath());
        }
        FileReader fr = new FileReader(testListFile);
        Vector v = new Vector();
        BufferedReader in = new BufferedReader(fr);
        String inputLine = null;
        while((inputLine = in.readLine()) != null){
            v.addElement(inputLine);
        }
        String testList[] = new String[v.size()];
        v.copyInto(testList);
        return testList;
     }


%>
