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 | import java.io.*; |
| 18 | import java.net.*; |
| 19 | import com.sun.ejte.ccl.reporter.*; |
| 20 | |
| 21 | /* |
| 22 | * Unit test for: |
| 23 | * |
| 24 | * https://glassfish.dev.java.net/issues/show_bug.cgi?id=6442 |
| 25 | * ("IllegalStateException: No WebApplicationContext found") |
| 26 | * |
| 27 | * Make sure a ServletContextListener is called at its contextDestroyed() |
| 28 | * method before any ServletContext attributes are cleared |
| 29 | */ |
| 30 | public class WebTest { |
| 31 | |
| 32 | private static final String TEST_NAME = "servlet-context-destroyed-event-attributes-available"; |
| 33 | |
| 34 | private static SimpleReporterAdapter stat |
| 35 | = new SimpleReporterAdapter("appserv-tests"); |
| 36 | |
| 37 | private String host; |
| 38 | private String port; |
| 39 | private String contextRoot; |
| 40 | private String run; |
| 41 | |
| 42 | public WebTest(String[] args) { |
| 43 | host = args[0]; |
| 44 | port = args[1]; |
| 45 | contextRoot = args[2]; |
| 46 | run = args[3]; |
| 47 | } |
David Matějček | f4dc06a | 2021-05-17 12:10:57 +0200 | [diff] [blame^] | 48 | |
Vinay Vishal | 5717147 | 2018-09-18 20:22:00 +0530 | [diff] [blame] | 49 | public static void main(String[] args) { |
| 50 | |
| 51 | stat.addDescription("Unit test for Issue 6442"); |
| 52 | WebTest webTest = new WebTest(args); |
| 53 | |
| 54 | try { |
| 55 | if ("firstRun".equals(webTest.run)) { |
| 56 | webTest.firstRun(); |
| 57 | } else { |
| 58 | webTest.secondRun(); |
| 59 | } |
| 60 | stat.addStatus(TEST_NAME, stat.PASS); |
| 61 | } catch (Exception ex) { |
| 62 | ex.printStackTrace(); |
| 63 | stat.addStatus(TEST_NAME, stat.FAIL); |
| 64 | } |
| 65 | |
David Matějček | f4dc06a | 2021-05-17 12:10:57 +0200 | [diff] [blame^] | 66 | stat.printSummary(); |
Vinay Vishal | 5717147 | 2018-09-18 20:22:00 +0530 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Access servlet that adds ServletContext attribute. |
| 71 | * As the app is undeployed, its ServletContextListener will be invoked |
| 72 | * at its contextDestroyed() method and will attempt to access the |
| 73 | * ServletContext attribute that was added by the servlet. |
| 74 | * If the attribute is present, the ServletContextListener will write |
David Matějček | f4dc06a | 2021-05-17 12:10:57 +0200 | [diff] [blame^] | 75 | * the word "SUCCESS" to /tmp/mytest. Otherwise, it will write FAIL to |
Vinay Vishal | 5717147 | 2018-09-18 20:22:00 +0530 | [diff] [blame] | 76 | * that file. |
| 77 | */ |
| 78 | public void firstRun() throws Exception { |
| 79 | URL url = new URL("http://" + host + ":" + port + contextRoot + |
| 80 | "/TestServlet"); |
| 81 | System.out.println("Connecting to: " + url.toString()); |
| 82 | HttpURLConnection conn = (HttpURLConnection) url.openConnection(); |
| 83 | conn.setInstanceFollowRedirects(false); |
| 84 | conn.connect(); |
| 85 | |
| 86 | int responseCode = conn.getResponseCode(); |
| 87 | if (responseCode != HttpURLConnection.HTTP_OK) { |
| 88 | throw new Exception("Unexpected response code: Got " + |
| 89 | responseCode + ", expected: " + |
| 90 | HttpURLConnection.HTTP_OK); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Read the contents of /tmp/mytest |
| 96 | */ |
| 97 | public void secondRun() throws Exception { |
| 98 | |
| 99 | File inFile = new File("/tmp/mytest"); |
| 100 | FileInputStream fis = new FileInputStream(inFile); |
| 101 | BufferedReader br = new BufferedReader(new InputStreamReader(fis)); |
| 102 | String line = br.readLine(); |
| 103 | inFile.delete(); |
| 104 | |
| 105 | if (!line.equals("SUCCESS")) { |
| 106 | throw new Exception("No success"); |
| 107 | } |
| 108 | } |
| 109 | } |