blob: e3ca54b3b061c42accfdac7908b2317ae6965ccc [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
17import java.io.*;
18import java.net.*;
19import 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 */
30public 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čekf4dc06a2021-05-17 12:10:57 +020048
Vinay Vishal57171472018-09-18 20:22:00 +053049 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čekf4dc06a2021-05-17 12:10:57 +020066 stat.printSummary();
Vinay Vishal57171472018-09-18 20:22:00 +053067 }
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čekf4dc06a2021-05-17 12:10:57 +020075 * the word "SUCCESS" to /tmp/mytest. Otherwise, it will write FAIL to
Vinay Vishal57171472018-09-18 20:22:00 +053076 * 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}