package org.hyperic.sigar.test;

import org.hyperic.sigar.NetConnection;
import org.hyperic.sigar.NetFlags;
import org.hyperic.sigar.NetStat;
import org.hyperic.sigar.Sigar;
import org.hyperic.sigar.SigarNotImplementedException;

public class TestNetStat extends SigarTestCase {
    private static boolean printListeners =
        "true".equals(System.getProperty("sigar.netstat.listeners"));

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

    public void testCreate() throws Exception {
        Sigar sigar = getSigar();
        NetStat netstat;
        
        try {
            netstat = sigar.getNetStat();
        } catch (SigarNotImplementedException e) {
            return;
        }

        traceln("");
        assertGtEqZeroTrace("Outbound", netstat.getTcpOutboundTotal());
        assertGtEqZeroTrace("Inbound", netstat.getTcpInboundTotal());
        assertGtEqZeroTrace("Listen", netstat.getTcpListen());
        assertGtEqZeroTrace("Established", netstat.getTcpEstablished());

        if (!printListeners) {
            return;
        }

        int flags = NetFlags.CONN_SERVER | NetFlags.CONN_TCP;

        NetConnection[] connections =
            sigar.getNetConnectionList(flags);

        for (int i=0; i<connections.length; i++) {
            long port = connections[i].getLocalPort();
            traceln("Listen " +
                    sigar.getNetListenAddress(port) + ":" + port);
        }
    }
}

@@ -882,37 +882,22 @@
     
-assertGtEqZeroTrace(%22Listen%22,
+int%5B%5D states =
  net
@@ -911,19 +911,75 @@
 tTcp
-Listen
+States
 ()
-)
 ;%0A
+        for (int i=0; i%3CNetFlags.TCP_UNKNOWN; i++) %7B%0A    
     
@@ -1006,52 +1006,62 @@
 ace(
-%22Established%22, netstat.getTcpEstablished());
+NetConnection.getStateString(i), states%5Bi%5D);%0A        %7D
 %0A%0A  

check all states