
import java.util.*;
import java.net.*;

class URLTest {

  private static class TestCase {
    public String context;
    public String input;
    public String result;
    TestCase(String context, String input, String result) {
      this.context = context;
      this.input = input;
      this.result = result;
    }
  };

  static final TestCase[] cases = {

    new TestCase(
	null,
	"jar:http://www.kaffe.org/foo/bar.jar!/float/boat",
	"jar:http://www.kaffe.org/foo/bar.jar!/float/boat"
    ),

    new TestCase(
	null,
	"http://www.kaffe.org",
	"http://www.kaffe.org/"
    ),

    new TestCase(
	null,
	"http://www.kaffe.org:8080#ref",
	"http://www.kaffe.org:8080/#ref"
    ),

    new TestCase(
	"http://www.kaffe.org",
	"foo/bar",
	"http://www.kaffe.org/foo/bar"
    ),

    new TestCase(
	"http://www.kaffe.org/foo/bar#baz",
	"jan/far",
	"http://www.kaffe.org/foo/jan/far"
    ),

    new TestCase(
	"http://www.kaffe.org/foo/bar",
	"/jan/far",
	"http://www.kaffe.org/jan/far"
    ),

    new TestCase(
	null,
	"foo/bar",
	"java.net.MalformedURLException: no protocol"
    ),

    new TestCase(
	"file:/foo/bar",
	"barf#jow",
	"file:/foo/barf#jow"
    ),

    new TestCase(
	null,
	"jar:file:/usr/local/share/kaffe/Klasses.jar!/kaffe/lang/unicode.tbl",
	"jar:file:/usr/local/share/kaffe/Klasses.jar!/kaffe/lang/unicode.tbl"
    ),

    new TestCase(
	null,
	"jar:http://www.kaffe.org/foo/bar.jar",
	"java.net.MalformedURLException: no !/ in spec"
    ),

    new TestCase(
	"jar:http://www.kaffe.org/foo/bar.jar!/path/name",
	"float/boat",
	"jar:http://www.kaffe.org/foo/bar.jar!/path/float/boat"
    ),

    new TestCase(
	"jar:http://www.kaffe.org/foo/bar.jar!/",
	"float/boat",
	"jar:http://www.kaffe.org/foo/bar.jar!/float/boat"
    ),

    new TestCase(
	"jar:http://www.kaffe.org/foo/bar.jar!/path/name",
	"/float/boat",
	"jar:http://www.kaffe.org/foo/bar.jar!/float/boat"
    ),

    new TestCase(
	"jar:http://www.kaffe.org/foo/bar.jar!/",
	"/float/boat",
	"jar:http://www.kaffe.org/foo/bar.jar!/float/boat"
    ),

    new TestCase(
	null,
	"foo/bar",
	"java.net.MalformedURLException: no protocol"
    ),

    new TestCase(
	null,
	"http://www.kaffe.org:99999/foo/bar",
	"java.net.MalformedURLException: bad port: 99999"
    ),

    new TestCase(
	null,
	"jar:http://www.kaffe.org:99999/foo/bar.jar!/eat/me",
	"java.net.MalformedURLException: invalid inner URL: bad port: 99999"
    ),

  };

  public static void main (String[] args) throws Exception {

    if (args.length > 0) {
      URL context = null;
      String input, result;

      if (args.length > 1) {
	context = new URL(args[0]);
	input = args[1];
      } else {
	input = args[0];
      }

      try {
	result = new URL(context, input).toString();
      } catch (MalformedURLException e) {
	result = e.toString();
      }

      System.out.println("  CONTEXT: " + context);
      System.out.println("    INPUT: " + input);
      System.out.println("   RESULT: " + result);
      System.exit(0);
    }

    boolean failed = false;
    for (int i = 0; i < cases.length; i++) {
      TestCase t = cases[i];
      URL context = null;
      String result;

      // Create context URL
      try {
	context = (t.context == null) ? null : new URL(t.context);
      } catch (MalformedURLException e) {
	System.out.println("Oops: " +t.context+ ": " + e);
	System.exit(1);
      }

      // Create new URL in context
      try {
	result = new URL(context, t.input).toString();
      } catch (MalformedURLException e) {
	result = e.toString();
      }

      // Check result
      if (!result.equals(t.result)) {
	System.out.println("Test failure (case " +i+ ")");
	System.out.println("  CONTEXT: " + t.context);
	System.out.println("    INPUT: " + t.input);
	System.out.println("   WANTED: " + t.result);
	System.out.println("  BUT GOT: " + result);
	failed = true;
      }
    }
    if (!failed) {
      System.out.println("Success.");
    }
  }
}

/* Expected Output:
Success.
*/


@@ -980,32 +980,128 @@
 an/far%22%0A    ),%0A%0A
+    new TestCase(%0A%09%22http://www.kaffe.org/foo/bar%22,%0A%09%22%22,%0A%09%22http://www.kaffe.org/foo/bar%22%0A    ),%0A%0A
     new TestCase
@@ -1247,32 +1247,115 @@
 rf#jow%22%0A    ),%0A%0A
+    new TestCase(%0A%09%22file:/foo/bar#fly%22,%0A%09%22jabawaba%22,%0A%09%22file:/foo/jabawaba%22%0A    ),%0A%0A
     new TestCase
@@ -2456,46 +2456,11 @@
 jar:
-http://www.kaffe.org:99999/foo/bar.jar
+abc
 !/ea
@@ -2519,31 +2519,27 @@
 er URL: 
-bad port: 99999
+no protocol
 %22%0A    ),

Add a couple more tests.