// Collection of code snippets by Arne Vajhøj 
// (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) 
package client;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.net.URL;
import java.util.List;

import com.googlecode.jsonrpc4j.JsonRpcHttpClient;

import common.Data;

public class TestClient {
	private static void test(String urlstr) throws Throwable {
		System.out.println(urlstr);
		JsonRpcHttpClient client = new JsonRpcHttpClient(new URL(urlstr));
		int v = client.invoke("getInt", new Object[0], Integer.class);
		System.out.println(v);
		String s = client.invoke("getString", new Object[0], String.class);
		System.out.println(s);
		Data d = client.invoke("getData", new Object[0], Data.class);
		System.out.println(d);
		@SuppressWarnings("unchecked")
		List<Integer> lstv = client.invoke("getListOfInts", new Object[0], List.class);
		System.out.println(lstv);
		@SuppressWarnings("unchecked")
		List<String> lsts = client.invoke("getListOfStrings", new Object[0], List.class);
		System.out.println(lsts);
		Type lstdata = new ParameterizedType() {
			@Override
			public Type[] getActualTypeArguments() {
				return new Type[] { Data.class };
			}
			@Override
			public Type getRawType() {
				return List.class;
			}
			@Override
			public Type getOwnerType() {
				return null;
			}
		};
		@SuppressWarnings("unchecked")
		List<Data> lstd = (List<Data>)client.invoke("getListOfData", new Object[0], lstdata);
		System.out.println(lstd);
		int v1 = 123;
		int v2 = 456;
		int v3 = client.invoke("add", new Object[] { v1,  v2 }, Integer.class);
		System.out.println(v3);
        String s1 = "ABC";
        String s2 = "DEF";
        String s3 = client.invoke("concat", new Object[] { s1, s2 }, String.class);
        System.out.println(s3);
        Data d1 = new Data(123, "ABC");
        Data d2 = client.invoke("modify", new Object[] { d1 }, Data.class);
        System.out.println(d2);
	}
	public static void main(String[] args) throws Throwable {
		test("http://localhost:8001/test");
		test("http://localhost:8002/test");
		test("http://localhost:8003/test");
		test("http://localhost:8004/test");
		test("http://localhost:8005/test");
		test("http://localhost:8006/server.php");
		test("http://localhost:8007/server.php");
	}
}
