Dead
[official-gcc.git] / gomp-20050608-branch / libjava / classpath / test / java.net / URLTest.java
blob726bfa5177e77d89159f8206df1e402b76ab7749
1 /* Test URL's */
3 import java.net.*;
4 import java.io.*;
6 public class URLTest
9 public static void
10 main(String argv[])
12 System.out.println("Starting URL tests");
14 /* Simple URL test */
16 System.out.println("Test 1: Simple URL test");
18 try
20 URL url = new URL("http", "www.fsf.org", 80, "/");
22 if (!url.getProtocol().equals("http") ||
23 !url.getHost().equals("www.fsf.org") ||
24 url.getPort() != 80 ||
25 !url.getFile().equals("/"))
26 System.out.println("FAILED: Simple URL test");
28 System.out.println("URL is: " + url.toString());
30 URLConnection uc = url.openConnection();
32 if (uc instanceof HttpURLConnection)
33 System.out.println("Got the expected connection type");
35 HttpURLConnection hc = (HttpURLConnection)uc;
37 hc.connect();
39 System.out.flush();
40 System.out.println("Dumping response headers");
41 for (int i = 0; ; i++)
43 String key = hc.getHeaderFieldKey(i);
44 if (key == null)
45 break;
47 System.out.println(key + ": " + hc.getHeaderField(i));
50 System.out.flush();
51 System.out.println("Dumping contents");
53 BufferedReader br = new BufferedReader(new
54 InputStreamReader(hc.getInputStream()));
56 for (String str = br.readLine(); str != null; str = br.readLine())
58 System.out.println(str);
60 System.out.flush();
62 hc.disconnect();
64 System.out.println("Content Type: " + hc.getContentType());
65 System.out.println("Content Encoding: " + hc.getContentEncoding());
66 System.out.println("Content Length: " + hc.getContentLength());
67 System.out.println("Date: " + hc.getDate());
68 System.out.println("Expiration: " + hc.getExpiration());
69 System.out.println("Last Modified: " + hc.getLastModified());
71 System.out.println("PASSED: Simple URL test");
73 catch(IOException e)
75 System.out.println("FAILED: Simple URL test: " + e);
78 // Parsing test
79 System.out.println("Test 2: URL parsing test");
80 try
82 URL url = new URL("http://www.urbanophile.com/arenn/trans/trans.html#mis");
83 if (!url.toString().equals(
84 "http://www.urbanophile.com/arenn/trans/trans.html#mis"))
85 System.out.println("FAILED: Parse URL test: " + url.toString());
86 else {
87 System.out.println("Parsed ok: " + url.toString());
88 url = new URL("http://www.foo.com:8080/#");
89 if (!url.toString().equals("http://www.foo.com:8080/#"))
90 System.out.println("FAILED: Parse URL test: " + url.toString());
91 else {
92 System.out.println("Parsed ok: " + url.toString());
93 url = new URL("http://www.bar.com/test:file/");
94 if (!url.toString().equals("http://www.bar.com/test:file/"))
95 System.out.println("FAILED: Parse URL test: " + url.toString());
96 else {
97 System.out.println("Parsed ok: " + url.toString());
98 url = new URL("http://www.gnu.org");
99 if (!url.toString().equals("http://www.gnu.org/"))
100 System.out.println("FAILED: Parse URL test: " + url.toString());
101 else {
102 System.out.println("Parsed ok: " + url.toString());
103 url = new URL("HTTP://www.fsf.org/");
104 if (!url.toString().equals("http://www.fsf.org/"))
105 System.out.println("FAILED: Parse URL test: " + url.toString());
106 else {
107 System.out.println("Parsed ok: " + url.toString());
108 System.out.println("PASSED: URL parse test");
115 catch (IOException e)
117 System.out.println("FAILED: URL parsing test: " + e);
120 // getContent test
121 System.out.println("Test 3: getContent test");
124 URL url = new URL("http://localhost/~arenn/services.txt");
126 Object obj = url.getContent();
127 System.out.println("Object type is: " + obj.getClass().getName());
129 if (obj instanceof InputStream)
131 System.out.println("Got InputStream, so dumping contents");
132 BufferedReader br = new BufferedReader(new
133 InputStreamReader((InputStream)obj));
135 for (String str = br.readLine(); str != null; str = br.readLine())
136 System.out.println(str);
138 br.close();
140 else
142 System.out.println("FAILED: Object is not an InputStream");
145 System.out.println("PASSED: getContent test");
147 catch (IOException e)
149 System.out.println("FAILED: getContent test: " + e);
152 System.out.println("URL test complete");