blob: 9eccfad67b5c1b1b7a3f99ddd4274f23b83f82ca [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.*;
19
20import com.sun.ejte.ccl.reporter.*;
21
22/*
23 * Unit test for @WebServlet
24 */
25public class WebTest {
26
27 private static SimpleReporterAdapter stat
28 = new SimpleReporterAdapter("appserv-tests");
29 private static final String TEST_NAME = "cdi-servlet-annotation-with-web-inf-lib-javaee-injection";
30 private static final String EXPECTED_RESPONSE = "Hello from Servlet 3.0. initParams: n1=v1, n2=v2";
31
32 private String host;
33 private String port;
34 private String contextRoot;
35
36 public WebTest(String[] args) {
37 host = args[0];
38 port = args[1];
39 contextRoot = args[2];
40 }
41
42 public static void main(String[] args) {
43 stat.addDescription("Unit test for @WebServlet");
44 WebTest webTest = new WebTest(args);
45 webTest.doTest();
46 stat.printSummary(TEST_NAME);
47 }
48
49 public void doTest() {
50 try {
51 invoke("llinit");
52 invoke("llquery");
53 invoke("llfind");
54 invoke("llinj");
55 } catch (Exception ex) {
56 System.out.println(TEST_NAME + " test failed");
57 stat.addStatus(TEST_NAME, stat.FAIL);
58 ex.printStackTrace();
59 }
60 }
61
62 private void invoke(String testCase) throws Exception {
63
64 String url = "http://" + host + ":" + port + contextRoot + "/myurl"
65 + "?testcase=" + testCase;
66 System.out.println("opening connection to " + url);
67 HttpURLConnection conn = (HttpURLConnection) (new URL(url))
68 .openConnection();
69
70 int code = conn.getResponseCode();
71 if (code != 200) {
72 System.out.println("Unexpected return code: " + code);
73 stat.addStatus(TEST_NAME + testCase, stat.FAIL);
74 } else {
75 InputStream is = null;
76 BufferedReader input = null;
77 String line = null;
78 try {
79 is = conn.getInputStream();
80 input = new BufferedReader(new InputStreamReader(is));
81 line = input.readLine();
82 System.out.println("line = " + line);
83 } finally {
84 try {
85 if (is != null) {
86 is.close();
87 }
88 } catch (IOException ioe) {
89 // ignore
90 }
91 try {
92 if (input != null) {
93 input.close();
94 }
95 } catch (IOException ioe) {
96 // ignore
97 }
98 }
99 if (EXPECTED_RESPONSE.equals(line)) {
100 stat.addStatus(TEST_NAME + testCase, stat.PASS);
101 } else {
102 System.out.println("Wrong response. Expected: "
103 + EXPECTED_RESPONSE + ", received: " + line);
104 stat.addStatus(TEST_NAME + testCase, stat.FAIL);
105 }
106 }
107 }
108}