blob: 831c4cf1369bf695adbfe02fe5d93c5591d96f65 [file] [log] [blame]
Vinay Vishal57171472018-09-18 20:22:00 +05301/*
2 * Copyright (c) 2017, 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
17package util;
18
19import java.io.*;
20import java.net.*;
21import java.util.*;
22
23import com.sun.ejte.ccl.reporter.SimpleReporterAdapter;
24
25/** WebTestUtil.java
David Matějčekf4dc06a2021-05-17 12:10:57 +020026 * This program opens HttpURLconnection,sends the request to the
Vinay Vishal57171472018-09-18 20:22:00 +053027 * servlet , & receives the response from the servlet.
28 * Using commandline args the user can specify for WebTestUtil
29 * 1. test suite name
30 * 2. host name
31 * 3. port no
32 * 4. context root of the servlet that is defined in web.xml
33 * 5. url pattern of the servlet that is defined in web.xml
34 *
35 * @author Sarada Kommalapati
36 */
37
38
39public class WebTestUtil {
40
41 private SimpleReporterAdapter stat;
42
43 private String testSuiteID;
44 private String TEST_NAME;
45 private String host;
46 private String port;
47 private String contextRoot;
48 private String urlPattern;
49
50
51 public WebTestUtil( String host, String port, String contextRoot , String urlPattern, String testSuiteID, SimpleReporterAdapter stat) {
52 this.testSuiteID = testSuiteID;
53 TEST_NAME = testSuiteID;
54 this.host = host;
55 this.port = port;
56 this.contextRoot = contextRoot;
57 this.urlPattern = urlPattern;
58 this.stat = stat;
59 }
David Matějčekf4dc06a2021-05-17 12:10:57 +020060
Vinay Vishal57171472018-09-18 20:22:00 +053061
62 public void test( String c) throws Exception {
63 this.test( c, "");
64 }
65
66
67 public void test( String c, String params) throws Exception {
68 String EXPECTED_RESPONSE = c + ":pass";
69 String TEST_CASE = TEST_NAME + c;
70 String url = "http://" + host + ":" + port + contextRoot + "/";
71 url = url + urlPattern + "?case=" + c;
72 if ( (params != null) & (!params.trim().equals("")) ) {
73 url = url + "&" + params.trim();
74 }
75
76 System.out.println("url="+url);
77
78 HttpURLConnection conn = (HttpURLConnection)
79 (new URL(url)).openConnection();
80 int code = conn.getResponseCode();
81 if (code != 200) {
82 System.err.println("Unexpected return code: " + code);
83 stat.addStatus(TEST_CASE, stat.FAIL);
84 } else {
85 InputStream is = conn.getInputStream();
86 BufferedReader input = new BufferedReader(new InputStreamReader(is));
David Matějčekf4dc06a2021-05-17 12:10:57 +020087 String line = null;
88 while ((line = input.readLine()) != null) {
Vinay Vishal57171472018-09-18 20:22:00 +053089 // System.out.println("line="+line);
David Matějčekf4dc06a2021-05-17 12:10:57 +020090 if (line.contains(EXPECTED_RESPONSE)) {
91 stat.addStatus(TEST_CASE, stat.PASS);
92 break;
93 }
94 }
95
96 if (line == null) {
97 System.out.println("Unable to find " + EXPECTED_RESPONSE +
98 " in the response");
99 }
100 stat.addStatus(TEST_CASE, stat.FAIL);
101 }
Vinay Vishal57171472018-09-18 20:22:00 +0530102 }
103
104}
105
106