blob: b14e39cba7e9e1878b6670ea49837e92359809a7 [file] [log] [blame]
Vinay Vishal57171472018-09-18 20:22:00 +05301/*
2 * Copyright (c) 1997, 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 test.web.jsp.hello;
18import org.testng.annotations.Configuration;
19import org.testng.annotations.ExpectedExceptions;
20import org.testng.annotations.Test;
21import org.testng.annotations.*;
22import org.testng.Assert;
23
24import java.io.*;
25import java.net.*;
26import java.util.*;
27
28/**
29 * Simple TestNG client for basic WAR containing one JSP,one Servlet and one static
30 *HTML resource.Each resources (HTML,JSP,Servlet) is invoked as a separate test.
31 *
32 */
33public class HelloJSPTestNG {
34
35 private static final String TEST_NAME =
36 "simple-webapp-jspservlet-noresource";
David Matějčekf4dc06a2021-05-17 12:10:57 +020037
Vinay Vishal57171472018-09-18 20:22:00 +053038 private String strContextRoot="hellojsp";
39
40 static String result = "";
41 String host=System.getProperty("http.host");
42 String port=System.getProperty("http.port");
David Matějčekf4dc06a2021-05-17 12:10:57 +020043
Vinay Vishal57171472018-09-18 20:22:00 +053044 /*
45 *If two asserts are mentioned in one method, then last assert is taken in
46 *to account.
47 *Each method can act as one test within one test suite
48 */
49
50
51 //@Parameters({ "host", "port", "contextroot" })
52 @Test(groups ={ "pulse"} ) // test method
53 //public void webtest(String host, String port, String contextroot) throws Exception{
54 public void simpleJSPTestPage() throws Exception{
David Matějčekf4dc06a2021-05-17 12:10:57 +020055
Vinay Vishal57171472018-09-18 20:22:00 +053056 try{
David Matějčekf4dc06a2021-05-17 12:10:57 +020057
Vinay Vishal57171472018-09-18 20:22:00 +053058
59 String testurl = "http://" + host + ":" + port + "/"+ strContextRoot + "/hello.jsp";
60 System.out.println("URL is: "+testurl);
61 URL url = new URL(testurl);
62 echo("Connecting to: " + url.toString());
63 HttpURLConnection conn = (HttpURLConnection) url.openConnection();
64 conn.connect();
65 int responseCode = conn.getResponseCode();
66
67
68 InputStream is = conn.getInputStream();
69 BufferedReader input = new BufferedReader(new InputStreamReader(is));
70
71 String line = null;
72 boolean result=false;
David Matějčekf4dc06a2021-05-17 12:10:57 +020073 String testLine = null;
74 String EXPECTED_RESPONSE ="JSP Test Page";
Vinay Vishal57171472018-09-18 20:22:00 +053075 while ((line = input.readLine()) != null) {
76 if(line.indexOf(EXPECTED_RESPONSE)!=-1){
77 result=true;
78 testLine = line;
79 System.out.println(testLine);
80 }
David Matějčekf4dc06a2021-05-17 12:10:57 +020081
82 }
83
Vinay Vishal57171472018-09-18 20:22:00 +053084 Assert.assertEquals(result, true,"Unexpected HTML");
David Matějčekf4dc06a2021-05-17 12:10:57 +020085
86
Vinay Vishal57171472018-09-18 20:22:00 +053087 }catch(Exception e){
88 e.printStackTrace();
89 throw new Exception(e);
90 }
91
92 }
93
94 @Test(groups={"pulse"}) //test method for server
95 public void testServerRunning() throws Exception{
David Matějčekf4dc06a2021-05-17 12:10:57 +020096 //Your server is up and running!
97 //
98 String testurl = "http://" + host + ":" + port;
Vinay Vishal57171472018-09-18 20:22:00 +053099 System.out.println("URL is: "+testurl);
100 URL url = new URL(testurl);
101 echo("Connecting to: " + url.toString());
102 HttpURLConnection conn = (HttpURLConnection) url.openConnection();
103 conn.connect();
104 int responseCode = conn.getResponseCode();
105
David Matějčekf4dc06a2021-05-17 12:10:57 +0200106 InputStream is = conn.getInputStream();
Vinay Vishal57171472018-09-18 20:22:00 +0530107 BufferedReader input = new BufferedReader(new InputStreamReader(is));
108
109 String line = null;
110 boolean result=false;
David Matějčekf4dc06a2021-05-17 12:10:57 +0200111 String testLine = null;
Vinay Vishal57171472018-09-18 20:22:00 +0530112 while ((line = input.readLine()) != null) {
113 echo(line);
114 if(line.indexOf("Your Application Server is now running")!=-1){
115 result=true;
116 testLine = line;
117 echo(testLine);
118 }
David Matějčekf4dc06a2021-05-17 12:10:57 +0200119
120 }
121
Vinay Vishal57171472018-09-18 20:22:00 +0530122 Assert.assertEquals(result, true,"Unexpected HTML");
123 }
David Matějčekf4dc06a2021-05-17 12:10:57 +0200124
125
Vinay Vishal57171472018-09-18 20:22:00 +0530126 @Test(groups ={ "pulse"} ) // test method
127 public void staticHTMLPageTest() throws Exception{
128 try{
David Matějčekf4dc06a2021-05-17 12:10:57 +0200129
Vinay Vishal57171472018-09-18 20:22:00 +0530130
131 String testurl = "http://" + host + ":" + port + "/"+ strContextRoot + "/first.html";
132 System.out.println("URL is: "+testurl);
133 URL url = new URL(testurl);
134 echo("Connecting to: " + url.toString());
135 HttpURLConnection conn = (HttpURLConnection) url.openConnection();
136 conn.connect();
137 int responseCode = conn.getResponseCode();
138
139 //Assert.assertEquals(responseCode, 200);
140
141 InputStream is = conn.getInputStream();
142 BufferedReader input = new BufferedReader(new InputStreamReader(is));
143
144 String line = null;
145 boolean result=false;
David Matějčekf4dc06a2021-05-17 12:10:57 +0200146 String testLine = null;
Vinay Vishal57171472018-09-18 20:22:00 +0530147 while ((line = input.readLine()) != null) {
148 if(line.indexOf("Welcome to HTML Test Program")!=-1){
149 result=true;
150 testLine = line;
151 System.out.println(testLine);
152 }
David Matějčekf4dc06a2021-05-17 12:10:57 +0200153
154 }
155
Vinay Vishal57171472018-09-18 20:22:00 +0530156 Assert.assertEquals(result, true,"Unexpected HTML");
David Matějčekf4dc06a2021-05-17 12:10:57 +0200157
158
Vinay Vishal57171472018-09-18 20:22:00 +0530159 }catch(Exception e){
160 e.printStackTrace();
161 throw new Exception(e);
162 }
David Matějčekf4dc06a2021-05-17 12:10:57 +0200163
Vinay Vishal57171472018-09-18 20:22:00 +0530164 }
David Matějčekf4dc06a2021-05-17 12:10:57 +0200165
Vinay Vishal57171472018-09-18 20:22:00 +0530166 @Test(groups ={ "pulse"} ) // test method
167 public void simpleServletTest() throws Exception{
168 try{
David Matějčekf4dc06a2021-05-17 12:10:57 +0200169
Vinay Vishal57171472018-09-18 20:22:00 +0530170
171 String testurl = "http://" + host + ":" + port + "/"+ strContextRoot + "/simpleservlet";
172 System.out.println("URL is: "+testurl);
173 URL url = new URL(testurl);
174 echo("Connecting to: " + url.toString());
175 HttpURLConnection conn = (HttpURLConnection) url.openConnection();
176 conn.connect();
177 int responseCode = conn.getResponseCode();
178
179
180 InputStream is = conn.getInputStream();
181 BufferedReader input = new BufferedReader(new InputStreamReader(is));
182
183 String line = null;
184 boolean result=false;
David Matějčekf4dc06a2021-05-17 12:10:57 +0200185 String testLine = null;
Vinay Vishal57171472018-09-18 20:22:00 +0530186 while ((line = input.readLine()) != null) {
187 if(line.indexOf("Sample Application Servlet")!=-1){
188 result=true;
189 testLine = line;
190 echo(testLine);
191 }
David Matějčekf4dc06a2021-05-17 12:10:57 +0200192
193 }
194
Vinay Vishal57171472018-09-18 20:22:00 +0530195 Assert.assertEquals(result, true,"Unexpected HTML");
David Matějčekf4dc06a2021-05-17 12:10:57 +0200196
197
Vinay Vishal57171472018-09-18 20:22:00 +0530198 }catch(Exception e){
199 e.printStackTrace();
200 throw new Exception(e);
201 }
David Matějčekf4dc06a2021-05-17 12:10:57 +0200202
Vinay Vishal57171472018-09-18 20:22:00 +0530203 }
204
205 public static void echo(String msg) {
206 System.out.println(msg);
207 }
208
209}