KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > latka > TestStatusCodes


1 /*
2  * Copyright 1999-2002,2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.commons.latka;
18
19 import org.apache.commons.latka.http.*;
20
21 import java.io.IOException JavaDoc;
22 import java.net.MalformedURLException JavaDoc;
23 import java.net.SocketException JavaDoc;
24 import java.net.UnknownHostException JavaDoc;
25 import java.net.URL JavaDoc;
26
27 import junit.framework.Test;
28 import junit.framework.TestCase;
29 import junit.framework.TestSuite;
30
31 public class TestStatusCodes extends TestCase {
32
33   public TestStatusCodes(String JavaDoc testName) {
34     super(testName);
35   }
36
37   public static Test suite() {
38     return new TestSuite(TestStatusCodes.class);
39   }
40
41   /**
42   public void testXmlFile() {
43     Latka latka = new Latka();
44     try {
45       java.io.FileReader reader =
46         new java.io.FileReader("d:\\cvs\\bcom\\latka-lib\\test\\data\\StatusCodeSuite.xml");
47       org.apache.commons.latka.Suite suite = new org.apache.commons.latka.Suite(reader);
48       latka.runTests(suite);
49     } catch (Exception e) {
50       fail(e.toString());
51     }
52   }
53   */

54
55   public void testBadURL() {
56     SessionImpl session = new SessionImpl();
57
58     URL JavaDoc url = null;
59     try {
60       url = new URL JavaDoc("http://whatabadwolf.com");
61     } catch (MalformedURLException JavaDoc e) {
62       fail(e.toString());
63     }
64
65     Request request =
66     session.createRequest(url, Request.HTTP_METHOD_GET, "1.1");
67
68     try {
69       request.execute();
70     } catch (IOException JavaDoc e) {
71       // good
72
return;
73     }
74
75     fail("Bad URL should have thrown IOException");
76   }
77
78   public void testGoodURL() {
79     SessionImpl session = new SessionImpl();
80
81     URL JavaDoc url = null;
82     try {
83       url = new URL JavaDoc("http://www.britannica.com");
84     } catch (MalformedURLException JavaDoc e) {
85       fail(e.toString());
86     }
87
88     Request request =
89     session.createRequest(url, Request.HTTP_METHOD_GET, "1.1");
90
91     try {
92       request.execute();
93     } catch (SocketException JavaDoc se) {
94         if (se.getMessage().indexOf("unreachable") == -1) {
95             fail("Network error: " + se.toString());
96         }
97         return;
98     } catch (UnknownHostException JavaDoc e) {
99         // ignore for offline tests
100
} catch (IOException JavaDoc e) {
101       fail(e.toString());
102     }
103
104     
105   }
106
107
108
109 }
110
Popular Tags