package com.izforge.izpack.util;

import java.io.IOException;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.util.Map;

import junit.framework.Assert;
import junit.framework.TestCase;

import com.izforge.izpack.panels.ProcessingClient;


public class PortProcessorTest extends TestCase
{
    public void testProcessGenericBoundPort() throws IOException {
        // create a ServerSocket on any free port (for all available network interfaces)
        ServerSocket use = new ServerSocket(0); // create a serversocket on any free port
        int usedPort = use.getLocalPort();
        ProcessingClient pc = new ProcessingClientStub(usedPort);
        PortProcessor pp = new PortProcessor();
        String result = pp.process(pc);
        Assert.assertTrue((Integer.toString(usedPort)).equals(result));
        try {
            use.close();
        } catch (Throwable t) {
            //ignore cleanup errors
        }
    }

    public void testProcessSpecificBoundPort() throws IOException {
        // create a ServerSocket for localhost on any free port
        ServerSocket use = new ServerSocket(0,0,InetAddress.getByName("localhost"));
        int usedPort = use.getLocalPort();
        ProcessingClient pc = new ProcessingClientStub("localhost", usedPort);
        PortProcessor pp = new PortProcessor();
        String result = pp.process(pc);
        Assert.assertFalse((Integer.toString(usedPort)).equals(result));
        try {
            use.close();
        } catch (Throwable t) {
            //ignore cleanup errors
        }
    }
    
    public void testProcessGenericOnGenericBoundPortIPv6() throws IOException {
        // create a ServerSocket for localhost on any free port
        ServerSocket use = new ServerSocket(0,0,InetAddress.getByName("::"));
        int usedPort = use.getLocalPort();
        ProcessingClient pc = new ProcessingClientStub("::", usedPort);
        PortProcessor pp = new PortProcessor();
        String result = pp.process(pc);
        Assert.assertFalse((Integer.toString(usedPort)).equals(result));
        try {
            use.close();
        } catch (Throwable t) {
            //ignore cleanup errors
        }
    }

    public void testProcessGenericOnGenericBoundPortIPv4() throws IOException {
        // create a ServerSocket for localhost on any free port
        ServerSocket use = new ServerSocket(0,0,InetAddress.getByName("0.0.0.0"));
        int usedPort = use.getLocalPort();
        ProcessingClient pc = new ProcessingClientStub("::", usedPort);
        PortProcessor pp = new PortProcessor();
        String result = pp.process(pc);
        Assert.assertFalse((Integer.toString(usedPort)).equals(result));
        try {
            use.close();
        } catch (Throwable t) {
            //ignore cleanup errors
        }
    }

    class ProcessingClientStub implements ProcessingClient {

        String[] fields;
        
        public ProcessingClientStub(String host, int port) {
            fields = new String[2];
            fields[0] = host;
            fields[1] = Integer.toString(port);
        }
        
        public ProcessingClientStub(int port) {
            fields = new String[1];
            fields[0] = Integer.toString(port);
        }
        
        public String getFieldContents(int index)
        {
            if (index < fields.length) {
                return fields[index];
            } else {
                throw new IndexOutOfBoundsException();
            }
        }

        public int getNumFields()
        {
            return fields.length;
        }

        public String getText()
        {
            return null;
        }

        public Map<String, String> getValidatorParams()
        {
            return null;
        }

        public boolean hasParams()
        {
            return false;
        }
        
    }
}

@@ -1363,32 +1363,68 @@
 pp.process(pc);%0A
+        System.out.println(result);%0A
         Assert.a
@@ -1427,32 +1427,45 @@
 rt.assertFalse((
+%22localhost*%22+
 Integer.toString
@@ -2066,32 +2066,38 @@
 rt.assertFalse((
+%22::*%22+
 Integer.toString
@@ -2567,34 +2567,39 @@
 singClientStub(%22
-::
+0.0.0.0
 %22, usedPort);%0A  
@@ -2712,16 +2712,27 @@
 tFalse((
+%220.0.0.0*%22+
 Integer.
@@ -2888,24 +2888,663 @@
    %7D%0A    %7D%0A%0A
+    public void testProcessSpecificOnGenericBoundPortIPv4() throws IOException %7B%0A        // create a ServerSocket for localhost on any free port%0A        ServerSocket use = new ServerSocket(0,0,InetAddress.getByName(%220.0.0.0%22));%0A        int usedPort = use.getLocalPort();%0A        ProcessingClient pc = new ProcessingClientStub(%22127.0.0.1%22, usedPort);%0A        PortProcessor pp = new PortProcessor();%0A        String result = pp.process(pc);%0A        Assert.assertEquals(%22127.0.0.1*%22+Integer.toString(usedPort),result);%0A        try %7B%0A            use.close();%0A        %7D catch (Throwable t) %7B%0A            //ignore cleanup errors%0A        %7D%0A    %7D%0A%0A
     class Pr

Corrected the 'expected' part of the 2-parameter PortProcessor tests