//------------------------------------------------------------------------------
// <copyright file="TestUtil.java" company="Microsoft">
//      Copyright (c) Microsoft Corporation.  All rights reserved.
// </copyright>
// <summary>
//      Test utility functions
// </summary>
//------------------------------------------------------------------------------

import java.net.*;
import java.util.*;


public class TestUtil {
    static TestLog Log = null;
    private static int nTestItems = 0;

    static void InitLog(String filename)
    {
        if (Log == null){
            Log = new TestLog(filename);
        }
        try{
            Log.WriteLine("%n========================================================");
            Log.WriteLine("Cluster: %s; StartTime: %s%n",
                    InetAddress.getLocalHost().getHostName(), new Date().toString());
        } catch (Exception e){
            e.printStackTrace();
        }
    }
    
    static int GetTestItemsCount()
    {
        return nTestItems;
    }
    
    static void IntoAPI()
    {
        StackTraceElement[] st = new Throwable().getStackTrace();
        TestUtil.Log.WriteLine("IntoAPI: %s:%s%n",
                st[1].getClassName(), 
                st[1].getMethodName());
    }
    
    static void LogTestItem(String description)
    {
        nTestItems++;
        try{
            TestUtil.Log.WriteLine("%sTestItem %d: %s%n",
                    InetAddress.getLocalHost().getHostName(), nTestItems, description);
        }
        catch (Exception e){
            e.printStackTrace();
        }
    }
    
    static void LogTestItem(String format, Object... arg)
    {
        nTestItems++;
        TestUtil.Log.WriteLine(format, arg);
    }
    
    static void EndAPI(int nerrs)
    {
        StackTraceElement[] st = new Throwable().getStackTrace();
        TestUtil.Log.WriteLine("EndAPI: %s:%s TotalErrors=%d%n",
                st[1].getClassName(),
                st[1].getMethodName(),
                nerrs);
        TestUtil.Log.Flush();
    }
    
    static int ValidateString(String expected, String actual, Boolean ignoreCase, String name)
    {
        int compareResult;
        if(ignoreCase){
           compareResult = expected.compareToIgnoreCase(actual); 
        } else{
           compareResult = expected.compareTo(actual);
        }
        if(compareResult != 0){
            TestUtil.Log.WriteLine("Error: %s differs: expected=%s;actual=%s",
                    name, expected, actual);
            return 1;
        }
        return 0;
    }
    
    static int ValidateObject(Object expected, Object actual, String name)
    {
        if (expected == null) {
            if (actual != null){
                TestUtil.Log.WriteLine("Error: %s differs: expected=%s;actual=%s",
                        name, "null", actual.toString());
                return 1;
            }
        }
        else if (!expected.equals(actual)){
            TestUtil.Log.WriteLine("Error: %s differs: expected=%s;actual=%s",
                    name, expected.toString(), actual.toString());
            return 1;
        }
        return 0;
    }
    
    static int RunCmd(String cmd, String args)
    {
        TestUtil.Log.WriteLine("RunCmd: %s %s", cmd, args);
        try{
            Process ps = Runtime.getRuntime().exec(cmd, new String[] {args});
            //ps.wait(120 * 1000);
            //ps.destroy();
            return 0;
        } catch (Exception e){
            e.printStackTrace();
            return 1;
        }
        
    }
    
    static int RunCmd(String cmd)
    {
        return RunCmd(cmd, "");
    }

    
    static int GetJobInfo(String schedulername, String jobType, int jobId)
    {
        /*
        int nerrs = 0;
        if (jobId < 1){
            nerrs++;
            TestUtil.Log.WriteLine("Error: invalid job Id %i", jobId);
            return nerrs;
        }
       
        return nerrs;
        */
        TestUtil.Log.WriteConsole("GetJobInfo not supported in Java (Scheduler implementation in .Net)");
        return 0;
    }
    
    static String GetActionString(String SvcInterfaceName, String api)
    {
        String orgname = "http://tempuri.org";
        String action = orgname + "/" + SvcInterfaceName + "/" + api;
        return action;
    }
    
    
}
