blob: 9201dcb6b6a777d5499ed9fef09711c73c49d768 [file] [log] [blame]
Vinay Vishal57171472018-09-18 20:22:00 +05301/*
2 * Copyright (c) 2010, 2018 Oracle and/or its affiliates. All rights reserved.
3 *
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v. 2.0, which is available at
6 * http://www.eclipse.org/legal/epl-2.0.
7 *
8 * This Source Code may also be made available under the following Secondary
9 * Licenses when the conditions for such availability set forth in the
10 * Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
11 * version 2 with the GNU Classpath Exception, which is available at
12 * https://www.gnu.org/software/classpath/license.html.
13 *
14 * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
15 */
16
17import java.io.*;
18import java.net.*;
19import com.sun.ejte.ccl.reporter.*;
20
21/*
22 * Unit test for @WebServlet
23 */
24public class WebTest {
25
26 private static SimpleReporterAdapter stat
27 = new SimpleReporterAdapter("appserv-tests");
28 private static final String TEST_NAME = "unsatisfied-dependencies-tests";
29 private static final String EXPECTED_RESPONSE = "Hello from Servlet 3.0. initParams: n1=v1, n2=v2";
30
31 private String host;
32 private String port;
33 private String contextRoot;
34
35 public WebTest(String[] args) {
36 host = args[0];
37 port = args[1];
38 contextRoot = args[2];
39 }
40
41 public static void main(String[] args) {
42 stat.addDescription("Testing Unsatisfied dependencies");
43 WebTest webTest = new WebTest(args);
44 webTest.doTest();
45 stat.printSummary(TEST_NAME);
46 }
47
48 public void doTest() {
49 try {
50 invoke();
51 } catch (Exception ex) {
52 System.out.println(TEST_NAME + " test failed");
53 stat.addStatus(TEST_NAME, stat.FAIL);
54 ex.printStackTrace();
55 }
56 }
57
58 private void invoke() throws Exception {
59
60 String url = "http://" + host + ":" + port + contextRoot
61 + "/myurl";
62 System.out.println("opening connection to " + url);
63 HttpURLConnection conn = (HttpURLConnection)
64 (new URL(url)).openConnection();
65 System.out.println("TESTNAME " + TEST_NAME);
66
67 int code = conn.getResponseCode();
68 if (code != 200) {
69 System.out.println("Unexpected return code: " + code);
70 stat.addStatus(TEST_NAME, stat.FAIL);
71 } else {
72 InputStream is = null;
73 BufferedReader input = null;
74 String line = null;
75 try {
76 is = conn.getInputStream();
77 input = new BufferedReader(new InputStreamReader(is));
78 line = input.readLine();
79 System.out.println("line = " + line);
80 } finally {
81 try {
82 if (is != null) {
83 is.close();
84 }
85 } catch(IOException ioe) {
86 // ignore
87 }
88 try {
89 if (input != null) {
90 input.close();
91 }
92 } catch(IOException ioe) {
93 // ignore
94 }
95 }
96 if (EXPECTED_RESPONSE.equals(line)) {
97 stat.addStatus(TEST_NAME, stat.PASS);
98 } else {
99 System.out.println("Wrong response. Expected: " +
100 EXPECTED_RESPONSE + ", received: " + line);
101 stat.addStatus(TEST_NAME, stat.FAIL);
102 }
103 }
104 }
105}