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