blob: 343d171347550e71f3b62107d7935cbc2dca46e1 [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 = "simple-managed-bean-interceptor";
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("Unit test for @WebServlet");
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
66 int code = conn.getResponseCode();
67 if (code != 200) {
68 System.out.println("Unexpected return code: " + code);
69 stat.addStatus(TEST_NAME, stat.FAIL);
70 } else {
71 InputStream is = null;
72 BufferedReader input = null;
73 String line = null;
74 try {
75 is = conn.getInputStream();
76 input = new BufferedReader(new InputStreamReader(is));
77 line = input.readLine();
78 System.out.println("line = " + line);
79 } finally {
80 try {
81 if (is != null) {
82 is.close();
83 }
84 } catch(IOException ioe) {
85 // ignore
86 }
87 try {
88 if (input != null) {
89 input.close();
90 }
91 } catch(IOException ioe) {
92 // ignore
93 }
94 }
95 if (EXPECTED_RESPONSE.equals(line)) {
96 stat.addStatus(TEST_NAME, stat.PASS);
97 } else {
98 System.out.println("Wrong response. Expected: " +
99 EXPECTED_RESPONSE + ", received: " + line);
100 stat.addStatus(TEST_NAME, stat.FAIL);
101 }
102 }
103 }
104}