OpenCores
URL https://opencores.org/ocsvn/scarts/scarts/trunk

Subversion Repositories scarts

[/] [scarts/] [trunk/] [toolchain/] [scarts-gcc/] [gcc-4.1.1/] [libjava/] [classpath/] [test/] [java.net/] [URLTest.java] - Blame information for rev 14

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 14 jlechner
/* Test URL's */
2
 
3
import java.net.*;
4
import java.io.*;
5
 
6
public class URLTest
7
{
8
 
9
public static void
10
main(String argv[])
11
{
12
  System.out.println("Starting URL tests");
13
 
14
  /* Simple URL test */
15
 
16
  System.out.println("Test 1: Simple URL test");
17
 
18
  try
19
    {
20
      URL url = new URL("http", "www.fsf.org", 80, "/");
21
 
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");
27
 
28
      System.out.println("URL is: " + url.toString());
29
 
30
      URLConnection uc = url.openConnection();
31
 
32
      if (uc instanceof HttpURLConnection)
33
         System.out.println("Got the expected connection type");
34
 
35
      HttpURLConnection hc = (HttpURLConnection)uc;
36
 
37
      hc.connect();
38
 
39
      System.out.flush();
40
      System.out.println("Dumping response headers");
41
      for (int i = 0; ; i++)
42
        {
43
          String key = hc.getHeaderFieldKey(i);
44
          if (key == null)
45
            break;
46
 
47
          System.out.println(key + ": " + hc.getHeaderField(i));
48
        }
49
 
50
      System.out.flush();
51
      System.out.println("Dumping contents");
52
 
53
      BufferedReader br = new BufferedReader(new
54
                              InputStreamReader(hc.getInputStream()));
55
 
56
      for (String str = br.readLine(); str != null; str = br.readLine())
57
        {
58
          System.out.println(str);
59
        }
60
      System.out.flush();
61
 
62
      hc.disconnect();
63
 
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());
70
 
71
      System.out.println("PASSED: Simple URL test");
72
    }
73
  catch(IOException e)
74
    {
75
      System.out.println("FAILED: Simple URL test: " + e);
76
    }
77
 
78
  // Parsing test
79
  System.out.println("Test 2: URL parsing test");
80
  try
81
    {
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");
109
              }
110
            }
111
          }
112
        }
113
      }
114
    }
115
  catch (IOException e)
116
    {
117
      System.out.println("FAILED: URL parsing test: " + e);
118
    }
119
 
120
  // getContent test
121
  System.out.println("Test 3: getContent test");
122
  try
123
    {
124
      URL url = new URL("http://localhost/~arenn/services.txt");
125
 
126
      Object obj = url.getContent();
127
      System.out.println("Object type is: " + obj.getClass().getName());
128
 
129
      if (obj instanceof InputStream)
130
        {
131
          System.out.println("Got InputStream, so dumping contents");
132
          BufferedReader br = new BufferedReader(new
133
                                  InputStreamReader((InputStream)obj));
134
 
135
          for (String str = br.readLine(); str != null; str = br.readLine())
136
             System.out.println(str);
137
 
138
          br.close();
139
        }
140
      else
141
        {
142
          System.out.println("FAILED: Object is not an InputStream");
143
        }
144
 
145
      System.out.println("PASSED: getContent test");
146
    }
147
  catch (IOException e)
148
    {
149
      System.out.println("FAILED: getContent test: " + e);
150
    }
151
 
152
  System.out.println("URL test complete");
153
}
154
 
155
}
156
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.