package net.hyperic.sigar.test;

import net.hyperic.sigar.Sigar;
import net.hyperic.sigar.SigarLoader;
import net.hyperic.sigar.SigarProxy;
import net.hyperic.sigar.SigarProxyCache;
import net.hyperic.sigar.jmx.SigarInvokerJMX;

public class TestInvoker extends SigarTestCase {

    private static final String[][] OK_QUERIES = {
        { "sigar:Type=Mem", "Free" },
        { "sigar:Type=Mem", "Total" },
        { "sigar:Type=Cpu", "User" },
        { "sigar:Type=Cpu", "Sys" },
        { "sigar:Type=CpuPerc", "User" },
        { "sigar:Type=CpuPerc", "Sys" },
        { "sigar:Type=Swap", "Free" },
        { "sigar:Type=Swap", "Used" },
        { "sigar:Type=Uptime", "Uptime" },
        { "sigar:Type=LoadAverage", "0" },
        { "sigar:Type=LoadAverage", "1" },
        { "sigar:Type=LoadAverage", "2" },
        { "sigar:Type=ProcMem,Arg=$$", "Size" },
        { "sigar:Type=ProcMem,Arg=$$", "Vsize" },
        { "sigar:Type=ProcTime,Arg=$$", "Stime" },
        { "sigar:Type=ProcTime,Arg=$$", "Utime" },
        { "sigar:Type=CpuPercList,Arg=0", "Idle" },
    };

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

    public void testCreate() throws Exception {
        Sigar sigar = new Sigar();

        SigarProxy proxy =
            SigarProxyCache.newInstance(sigar);

        traceln("");

        testOK(proxy);
    }

    private void testOK(SigarProxy proxy) throws Exception {
        for (int i=0; i<OK_QUERIES.length; i++) {
            String[] query = OK_QUERIES[i];
            SigarInvokerJMX invoker =
                SigarInvokerJMX.getInstance(proxy, query[0]);
            if (SigarLoader.IS_WIN32 &&
                invoker.getType().equals("LoadAverage"))
            {
                /* XXX not implemented on win32
                 * invoke should throw SigarNotImplementedException
                 */
                continue;
            }
            Object o = invoker.invoke(query[1]);
            traceln(query[0] + ":" + query[1] + "=" + o);
        }
    }
}

@@ -58,16 +58,57 @@
 .Sigar;%0A
+import net.hyperic.sigar.SigarException;%0A
 import n
@@ -137,16 +137,16 @@
 Loader;%0A
-
 import n
@@ -1110,16 +1110,323 @@
     %7D;%0A%0A
+    private static final String%5B%5D%5B%5D BROKEN_QUERIES = %7B%0A        %7B %22sigar:Type=BREAK%22, %22Free%22 %7D,%0A        %7B %22sigar:Type=Mem%22, %22BREAK%22 %7D,%0A        %7B %22sigar:Type=ProcTime,Arg=BREAK%22, %22Stime%22 %7D,%0A        %7B %22sigar:Type=CpuPercList,Arg=1000%22, %22Idle%22 %7D,%0A        %7B %22sigar:Type=CpuPercList,Arg=BREAK%22, %22Idle%22 %7D,%0A    %7D;%0A%0A
     publ
@@ -2233,24 +2233,46 @@
           %7D%0A
+            try %7B%0A    
             
@@ -2304,24 +2304,28 @@
 (query%5B1%5D);%0A
+    
             
@@ -2370,16 +2370,623 @@
 %22 + o);%0A
+                assertTrue(true);%0A            %7D catch (SigarException e) %7B%0A                assertTrue(false);%0A            %7D%0A        %7D%0A%0A        for (int i=0; i%3CBROKEN_QUERIES.length; i++) %7B%0A            String%5B%5D query = BROKEN_QUERIES%5Bi%5D;%0A            SigarInvokerJMX invoker =%0A                SigarInvokerJMX.getInstance(proxy, query%5B0%5D);%0A            try %7B%0A                Object o = invoker.invoke(query%5B1%5D);%0A                assertTrue(false);%0A            %7D catch (SigarException e) %7B%0A                traceln(query%5B0%5D + %22:%22 + query%5B1%5D + %22=%22 + e.getMessage());%0A                assertTrue(true);%0A            %7D%0A
         

test bogus queries too