/*
 * Copyright (C) [2004, 2005, 2006], Hyperic, Inc.
 * This file is part of SIGAR.
 * 
 * SIGAR is free software; you can redistribute it and/or modify
 * it under the terms version 2 of the GNU General Public License as
 * published by the Free Software Foundation. This program is distributed
 * in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
 * even the implied warranty of MERCHANTABILITY or FITNESS FOR A
 * PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 * USA.
 */

package org.hyperic.sigar.win32.test;

import java.util.Iterator;
import java.util.Map;

import org.hyperic.sigar.test.SigarTestCase;
import org.hyperic.sigar.win32.Pdh;

public class TestPdh extends SigarTestCase {

    public TestPdh(String name) {
        super(name);
    }

    private void getValue(String key) throws Exception {
        Pdh pdh = new Pdh();

        traceln(key);
        assertGtEqZeroTrace("raw",
                            (long)pdh.getRawValue(key));
        assertGtEqZeroTrace("fmt",
                            (long)pdh.getFormattedValue(key));
    }

    public void testGetValue() throws Exception {
        Pdh.enableTranslation();
        final String DL = "\\";
        String[][] keys = {
            { "System", "System Up Time" },
            { "Memory", "Available Bytes" },
            { "Memory", "Pages/sec" },
            { "Processor(_Total)", "% User Time" },
        };

        for (int i=0; i<keys.length; i++) {
            String path =
                DL + keys[i][0] + DL + keys[i][1];

            String trans = Pdh.translate(path);
            if (!trans.equals(path)) {
                traceln(path + "-->" + trans);
            }
            traceln(path + " validate: " + Pdh.validate(path));
            getValue(path);
        }

        String bogusKey = "\\Does Not\\Exist";
        assertTrue(Pdh.validate(bogusKey) != 0);
    }

    public void testCounterMap() throws Exception {
        Map counters = Pdh.getEnglishPerflibCounterMap();

        assertGtZeroTrace("counters", counters.size());
        int dups = 0;
        for (Iterator it=counters.entrySet().iterator(); it.hasNext();) {
            Map.Entry entry = (Map.Entry)it.next();
            String name = (String)entry.getKey();
            int[] ix = (int[])entry.getValue();
            if (ix.length > 1) {
                dups++;
                //traceln(name + " has dups: " + ix.length);
            }
        }
        traceln(dups + " names have dups");

        String[] keys = {
            "System", "System Up Time"
        };
        int last = -1;
        for (int i=0; i<keys.length; i++) {
            String name = keys[i];
            int[] ix = (int[])counters.get(name);
            assertFalse(ix[0] == last);
            traceln(name + "=" + ix[0]);
            last = ix[0];
            String lookupName =
                Pdh.getCounterName(ix[0]);
            traceln(name + "=" + lookupName);
        }
    }

    public void testPdh() throws Exception {

        String[] iface = Pdh.getKeys("Thread");
        
        assertTrue(iface.length > 0);
    }
}

@@ -2022,105 +2022,8 @@
    %7D
-%0A%0A        String bogusKey = %22%5C%5CDoes Not%5C%5CExist%22;%0A        assertTrue(Pdh.validate(bogusKey) != 0);
 %0A   
@@ -2818,32 +2818,48 @@
       int%5B%5D ix =
+%0A               
  (int%5B%5D)counters
@@ -2863,24 +2863,38 @@
 ers.get(name
+.toLowerCase()
 );%0A         
@@ -3058,32 +3058,32 @@
 terName(ix%5B0%5D);%0A
-
             trac
@@ -3121,32 +3121,1133 @@
        %7D%0A    %7D%0A%0A
+    public void testValidate() %7B%0A        Object%5B%5D%5B%5D tests = %7B%0A            %7B %22%5C%5CDoes Not%5C%5CExist%22, new Integer(Pdh.NO_OBJECT) %7D,%0A            %7B %22Does Not Exist%22, new Integer(Pdh.BAD_COUNTERNAME) %7D,%0A            %7B %22%5C%5CSystem%5C%5CDoesNotExist%22, new Integer(Pdh.NO_COUNTER) %7D,%0A            %7B %22%5C%5CProcessor(666)%5C%5C%25 User Time%22, new Integer(Pdh.NO_INSTANCE) %7D,%0A            %7B %22%5C%5CSystem%5C%5CThreads%22, new Integer(Pdh.VALID_DATA) %7D,%0A            //slow%0A            //%7B %22%5C%5C%5C%5C-%5C%5CSystem%5C%5CThreads%22, new Integer(Pdh.NO_MACHINE) %7D,%0A        %7D;%0A%0A        for (int i=0; i%3Ctests.length; i++) %7B%0A            String path = (String)tests%5Bi%5D%5B0%5D;%0A            int expect = ((Integer)tests%5Bi%5D%5B1%5D).intValue();%0A            int status = Pdh.validate(path);%0A            boolean expectedResult = (status == expect);%0A%0A            if (!expectedResult) %7B%0A                traceln(%22%5Bvalidate%5D %22 + path + %22--%3E%22 +%0A                        Integer.toHexString(status).toUpperCase() +%0A                        %22 != %22 +%0A                        Integer.toHexString(expect).toUpperCase());%0A            %7D%0A%0A            assertTrue(expectedResult);%0A        %7D%0A    %7D%0A%0A
     public void 

test validate method