Vinay Vishal | 5717147 | 2018-09-18 20:22:00 +0530 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package test.web.jsp.hello; |
| 18 | import org.testng.annotations.Configuration; |
| 19 | import org.testng.annotations.ExpectedExceptions; |
| 20 | import org.testng.annotations.Test; |
| 21 | import org.testng.annotations.*; |
| 22 | import org.testng.Assert; |
| 23 | |
| 24 | import java.io.*; |
| 25 | import java.net.*; |
| 26 | import 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 | */ |
| 33 | public class HelloJSPTestNG { |
| 34 | |
| 35 | private static final String TEST_NAME = |
| 36 | "simple-webapp-jspservlet-noresource"; |
David Matějček | f4dc06a | 2021-05-17 12:10:57 +0200 | [diff] [blame^] | 37 | |
Vinay Vishal | 5717147 | 2018-09-18 20:22:00 +0530 | [diff] [blame] | 38 | 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ček | f4dc06a | 2021-05-17 12:10:57 +0200 | [diff] [blame^] | 43 | |
Vinay Vishal | 5717147 | 2018-09-18 20:22:00 +0530 | [diff] [blame] | 44 | /* |
| 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ček | f4dc06a | 2021-05-17 12:10:57 +0200 | [diff] [blame^] | 55 | |
Vinay Vishal | 5717147 | 2018-09-18 20:22:00 +0530 | [diff] [blame] | 56 | try{ |
David Matějček | f4dc06a | 2021-05-17 12:10:57 +0200 | [diff] [blame^] | 57 | |
Vinay Vishal | 5717147 | 2018-09-18 20:22:00 +0530 | [diff] [blame] | 58 | |
| 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ček | f4dc06a | 2021-05-17 12:10:57 +0200 | [diff] [blame^] | 73 | String testLine = null; |
| 74 | String EXPECTED_RESPONSE ="JSP Test Page"; |
Vinay Vishal | 5717147 | 2018-09-18 20:22:00 +0530 | [diff] [blame] | 75 | 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ček | f4dc06a | 2021-05-17 12:10:57 +0200 | [diff] [blame^] | 81 | |
| 82 | } |
| 83 | |
Vinay Vishal | 5717147 | 2018-09-18 20:22:00 +0530 | [diff] [blame] | 84 | Assert.assertEquals(result, true,"Unexpected HTML"); |
David Matějček | f4dc06a | 2021-05-17 12:10:57 +0200 | [diff] [blame^] | 85 | |
| 86 | |
Vinay Vishal | 5717147 | 2018-09-18 20:22:00 +0530 | [diff] [blame] | 87 | }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ček | f4dc06a | 2021-05-17 12:10:57 +0200 | [diff] [blame^] | 96 | //Your server is up and running! |
| 97 | // |
| 98 | String testurl = "http://" + host + ":" + port; |
Vinay Vishal | 5717147 | 2018-09-18 20:22:00 +0530 | [diff] [blame] | 99 | 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ček | f4dc06a | 2021-05-17 12:10:57 +0200 | [diff] [blame^] | 106 | InputStream is = conn.getInputStream(); |
Vinay Vishal | 5717147 | 2018-09-18 20:22:00 +0530 | [diff] [blame] | 107 | BufferedReader input = new BufferedReader(new InputStreamReader(is)); |
| 108 | |
| 109 | String line = null; |
| 110 | boolean result=false; |
David Matějček | f4dc06a | 2021-05-17 12:10:57 +0200 | [diff] [blame^] | 111 | String testLine = null; |
Vinay Vishal | 5717147 | 2018-09-18 20:22:00 +0530 | [diff] [blame] | 112 | 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ček | f4dc06a | 2021-05-17 12:10:57 +0200 | [diff] [blame^] | 119 | |
| 120 | } |
| 121 | |
Vinay Vishal | 5717147 | 2018-09-18 20:22:00 +0530 | [diff] [blame] | 122 | Assert.assertEquals(result, true,"Unexpected HTML"); |
| 123 | } |
David Matějček | f4dc06a | 2021-05-17 12:10:57 +0200 | [diff] [blame^] | 124 | |
| 125 | |
Vinay Vishal | 5717147 | 2018-09-18 20:22:00 +0530 | [diff] [blame] | 126 | @Test(groups ={ "pulse"} ) // test method |
| 127 | public void staticHTMLPageTest() throws Exception{ |
| 128 | try{ |
David Matějček | f4dc06a | 2021-05-17 12:10:57 +0200 | [diff] [blame^] | 129 | |
Vinay Vishal | 5717147 | 2018-09-18 20:22:00 +0530 | [diff] [blame] | 130 | |
| 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ček | f4dc06a | 2021-05-17 12:10:57 +0200 | [diff] [blame^] | 146 | String testLine = null; |
Vinay Vishal | 5717147 | 2018-09-18 20:22:00 +0530 | [diff] [blame] | 147 | 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ček | f4dc06a | 2021-05-17 12:10:57 +0200 | [diff] [blame^] | 153 | |
| 154 | } |
| 155 | |
Vinay Vishal | 5717147 | 2018-09-18 20:22:00 +0530 | [diff] [blame] | 156 | Assert.assertEquals(result, true,"Unexpected HTML"); |
David Matějček | f4dc06a | 2021-05-17 12:10:57 +0200 | [diff] [blame^] | 157 | |
| 158 | |
Vinay Vishal | 5717147 | 2018-09-18 20:22:00 +0530 | [diff] [blame] | 159 | }catch(Exception e){ |
| 160 | e.printStackTrace(); |
| 161 | throw new Exception(e); |
| 162 | } |
David Matějček | f4dc06a | 2021-05-17 12:10:57 +0200 | [diff] [blame^] | 163 | |
Vinay Vishal | 5717147 | 2018-09-18 20:22:00 +0530 | [diff] [blame] | 164 | } |
David Matějček | f4dc06a | 2021-05-17 12:10:57 +0200 | [diff] [blame^] | 165 | |
Vinay Vishal | 5717147 | 2018-09-18 20:22:00 +0530 | [diff] [blame] | 166 | @Test(groups ={ "pulse"} ) // test method |
| 167 | public void simpleServletTest() throws Exception{ |
| 168 | try{ |
David Matějček | f4dc06a | 2021-05-17 12:10:57 +0200 | [diff] [blame^] | 169 | |
Vinay Vishal | 5717147 | 2018-09-18 20:22:00 +0530 | [diff] [blame] | 170 | |
| 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ček | f4dc06a | 2021-05-17 12:10:57 +0200 | [diff] [blame^] | 185 | String testLine = null; |
Vinay Vishal | 5717147 | 2018-09-18 20:22:00 +0530 | [diff] [blame] | 186 | 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ček | f4dc06a | 2021-05-17 12:10:57 +0200 | [diff] [blame^] | 192 | |
| 193 | } |
| 194 | |
Vinay Vishal | 5717147 | 2018-09-18 20:22:00 +0530 | [diff] [blame] | 195 | Assert.assertEquals(result, true,"Unexpected HTML"); |
David Matějček | f4dc06a | 2021-05-17 12:10:57 +0200 | [diff] [blame^] | 196 | |
| 197 | |
Vinay Vishal | 5717147 | 2018-09-18 20:22:00 +0530 | [diff] [blame] | 198 | }catch(Exception e){ |
| 199 | e.printStackTrace(); |
| 200 | throw new Exception(e); |
| 201 | } |
David Matějček | f4dc06a | 2021-05-17 12:10:57 +0200 | [diff] [blame^] | 202 | |
Vinay Vishal | 5717147 | 2018-09-18 20:22:00 +0530 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | public static void echo(String msg) { |
| 206 | System.out.println(msg); |
| 207 | } |
| 208 | |
| 209 | } |