// Collection of code snippets by Arne Vajhøj 
// (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) 
import java.net.URL;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.apache.xmlrpc.client.XmlRpcClient;
import org.apache.xmlrpc.client.XmlRpcClientConfigImpl;

public class TestClient {
    private static void test(String urlstr) throws Exception {
        System.out.println(urlstr);
        XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl();
        config.setServerURL(new URL(urlstr));
        XmlRpcClient client = new XmlRpcClient();
        client.setConfig(config);
        {
            @SuppressWarnings("unchecked")
            int v = (Integer)client.execute("Test.getInt", new Object[0]);
            System.out.println(v);
        }
        {
            @SuppressWarnings("unchecked")
            String s = (String)client.execute("Test.getString", new Object[0]);
            System.out.println(s);
        }
        {
            @SuppressWarnings("unchecked")
            Data d = Util.fromMap((Map<String,Object>)client.execute("Test.getData", new Object[0]), Data.class);
            System.out.println(d);
        }
        {
            @SuppressWarnings("unchecked")
            Object[] lst = (Object[])client.execute("Test.getListOfInts", new Object[0]);
            System.out.print("[");
            boolean first = true;
            for(Object o : lst) {
                if(first) first = false; else System.out.print(",");
                int v = (Integer)o;
                System.out.print(v);
            }
            System.out.println("]");
        }
        {
            @SuppressWarnings("unchecked")
            Object[] lst = (Object[])client.execute("Test.getListOfStrings", new Object[0]);
            System.out.print("[");
            boolean first = true;
            for(Object o : lst) {
                if(first) first = false; else System.out.print(",");
                String s = (String)o;
                System.out.print(s);
            }
            System.out.println("]");
        }
        {
            @SuppressWarnings("unchecked")
            Object[] lst = (Object[])client.execute("Test.getListOfData", new Object[0]);
            System.out.print("[");
            boolean first = true;
            for(Object o : lst) {
                if(first) first = false; else System.out.print(",");
                @SuppressWarnings("unchecked")
                Data obj = Util.fromMap((Map<String,Object>)o, Data.class);
                System.out.print(obj);
            }
            System.out.println("]");
        }
        {
            int v1 = 123;
            int v2 = 456;
            @SuppressWarnings("unchecked")
            int v3 = (Integer)client.execute("Test.add", new Object[] { v1, v2 });
            System.out.println(v3);
        }
        {
            String s1 = "ABC";
            String s2 = "DEF";
            @SuppressWarnings("unchecked")
            String s3 = (String)client.execute("Test.concat", new Object[] { s1, s2 });
            System.out.println(s3);
        }
        {
            Data d = new Data(123, "ABC");
            @SuppressWarnings("unchecked")
            Data d2 = Util.fromMap((Map<String,Object>)client.execute("Test.modify", new Object[] { Util.toMap(d) }), Data.class);
            System.out.println(d2);
        }
    }
    public static void main(String[] args) throws Exception {
        test("http://localhost:8001/");
        test("http://localhost:8002/");
    }
}
