/*
 * Copyright (C) 2014 Dell, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.dell.doradus.testprocessor;

import com.dell.doradus.testprocessor.common.*;
import com.dell.doradus.testprocessor.diff.*;

import java.io.FileOutputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.List;

public class Program
{
    static public void main(String args[])
    {
        try {
            Data.predefine();

            if (Data.configFilePath == null || Data.configFilePath.trim().isEmpty()) {
                System.out.println("Config file is not define: Using predefined data\"");
            }
            else {
                System.out.println("Loading config \"" + Data.configFilePath + "\"");
                Config.load(Data.configFilePath);
                Config.modifyData();
            }

            Log.toFile(Data.logFilePath);
            if (Log.isOpened()) {
                System.out.println("Log: \"" + Data.configFilePath + "\"");
            }

            Log.println("*** Program: Test Processor Data:");
            Log.println(Data.toString("    "));

            if (Data.testSuiteInfo == null) {
                String msg = "Test suite is not defined";
                throw new Exception(msg);
            }

            Log.println("*** Program: Running Tests");

            System.out.println("Running Tests");
            for (TestDirInfo testDirInfo : Data.testSuiteInfo.getTestDirInfoList()) {
                System.out.println("Directory: " + testDirInfo.path());

                for (TestInfo testInfo : testDirInfo.testInfoList()) {
                    if (testInfo.isExcluded()) continue;
                    System.out.print("   " + testInfo.name() + ": ");
                    try { TestProcessor.execute(testInfo); }
                    catch(Exception ex) { System.out.println(); }
                    System.out.println(testInfo.resultToString());
                }
            }

            writeHtmlTestReport();
            writeXmlTestTestSummaryForCcnet();
        }
        catch (Exception ex) {
            Log.println("!!! Exception: " + Utils.unwind(ex));
            System.out.println("!!! Exception: " + Utils.unwind(ex));
        }
        finally {
            Log.close();
        }
    }

    static private void writeHtmlTestReport()
    throws Exception
    {
        if (Data.reportFilePath == null)
            return;

        Log.println("*** Program: Generating tests report:  " + Data.reportFilePath);
        System.out.println("Report: \"" + Data.reportFilePath + "\"");

        if (FileUtils.fileExists(Data.reportFilePath))
            FileUtils.deleteFile(Data.reportFilePath);

        OutputStream reportOutputStream = new FileOutputStream(Data.reportFilePath);
        Writer reportWriter = new OutputStreamWriter(reportOutputStream);

        String report = Reporter.generateHtmlReport(Data.testSuiteInfo);

        reportWriter.write(report);
        reportWriter.flush();
        reportWriter.close();
    }

    static private void writeXmlTestTestSummaryForCcnet()
    throws Exception
    {
        if (Data.ccnetSummaryFilePath == null)
            return;

        Log.println("*** Program: Generating tests summary for ccnet:  " + Data.ccnetSummaryFilePath);

        if (FileUtils.fileExists(Data.ccnetSummaryFilePath))
            FileUtils.deleteFile(Data.ccnetSummaryFilePath);

        OutputStream summaryOutputStream = new FileOutputStream(Data.ccnetSummaryFilePath);
        Writer summaryWriter = new OutputStreamWriter(summaryOutputStream);

        String summary = Reporter.generateXmlSummaryForCCNet(Data.testSuiteInfo);

        summaryWriter.write(summary);
        summaryWriter.flush();
        summaryWriter.close();
    }
}

@@ -2939,24 +2939,1400 @@
 %0D%0A        %7D%0D
+%0A%0D%0A        System.out.println(Utils.EOL + %22Summary:%22);%0D%0A        displaySummary(%22  %22);%0D%0A    %7D%0D%0A%0D%0A    static public void displaySummary(String prefix)%0D%0A    %7B%0D%0A        if (prefix == null) prefix = %22%22;%0D%0A%0D%0A        int cntSucceeded    = 0;%0D%0A        int cntFailed       = 0;%0D%0A        int cntInterrupted  = 0;%0D%0A        int cntNotExecuted  = 0;%0D%0A%0D%0A        for (TestDirInfo testDirInfo : Data.testSuiteInfo.getTestDirInfoList()) %7B%0D%0A            if (testDirInfo.isExcluded()) continue;%0D%0A%0D%0A            for (TestInfo testInfo : testDirInfo.testInfoList()) %7B%0D%0A                if (testInfo.isExcluded())%0D%0A                    continue;%0D%0A%0D%0A                if (testInfo.isInterrupted())%0D%0A                    %7B cntInterrupted += 1; continue; %7D%0D%0A                if (testInfo.isSucceeded())%0D%0A                    %7B cntSucceeded += 1; continue; %7D%0D%0A                if (!testInfo.isExecuted())%0D%0A                    %7B cntNotExecuted += 1; continue; %7D%0D%0A                cntFailed += 1;%0D%0A            %7D%0D%0A        %7D%0D%0A%0D%0A        System.out.println(prefix + %22Succeeded:    %22 + cntSucceeded);%0D%0A        if (cntFailed %3E 0)%0D%0A            System.out.println(prefix + %22Failed:       %22 + cntFailed);%0D%0A        if (cntInterrupted %3E 0)%0D%0A            System.out.println(prefix + %22Interrupted:  %22 + cntInterrupted);%0D%0A        if (cntNotExecuted %3E 0)%0D%0A            System.out.println(prefix + %22Not executed: %22 + cntNotExecuted);%0D
 %0A    %7D%0D%0A%0D%0A  
@@ -4584,18 +4584,40 @@
 println(
-%22R
+Utils.EOL + %22Generated r
 eport: %5C

Test Processor modified