Vinay Vishal | 5717147 | 2018-09-18 20:22:00 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2017, 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 | /* |
| 18 | * TestProgressObjectImpl.java |
| 19 | * |
| 20 | * Created on January 15, 2004, 10:10 AM |
| 21 | */ |
| 22 | |
| 23 | import javax.enterprise.deploy.spi.status.ProgressListener; |
| 24 | import org.glassfish.deployapi.ProgressObjectImpl; |
| 25 | import org.glassfish.deployapi.TargetImpl; |
| 26 | import org.glassfish.deployment.client.DeploymentFacilityFactory; |
| 27 | import org.glassfish.deployment.client.DeploymentFacility; |
| 28 | import org.glassfish.deployment.client.AbstractDeploymentFacility; |
| 29 | import javax.enterprise.deploy.shared.StateType; |
| 30 | |
| 31 | import java.io.File; |
| 32 | import java.io.PrintStream; |
| 33 | import java.io.FileOutputStream; |
| 34 | import java.io.FileNotFoundException; |
| 35 | |
| 36 | /** |
| 37 | *Makes sure that the ProgressObjectImpl class functions correctly. |
| 38 | *<p> |
David Matějček | f4dc06a | 2021-05-17 12:10:57 +0200 | [diff] [blame^] | 39 | *In particular, bug 4977764 reported that the ProgressObjectImpl class was susceptible to |
Vinay Vishal | 5717147 | 2018-09-18 20:22:00 +0530 | [diff] [blame] | 40 | *concurrent update failures in the vector that holds registered progress listeners. The |
| 41 | *fireProgressEvent method worked with the vector of listeners itself rather than a clone of the vector. |
| 42 | *One of the listeners unregistered itself from the progress object, so when the iterator tried to |
| 43 | *get the next element it detected the concurrent update. So, the progress object now clones the vector |
| 44 | *temporarily in fireProgressEvent and iterates through the clone. |
| 45 | * |
| 46 | * @author tjquinn |
| 47 | */ |
| 48 | public class TestProgressObjectImpl { |
| 49 | |
| 50 | private static final String here = "devtests/deployment/jsr88/misc"; |
David Matějček | f4dc06a | 2021-05-17 12:10:57 +0200 | [diff] [blame^] | 51 | |
Vinay Vishal | 5717147 | 2018-09-18 20:22:00 +0530 | [diff] [blame] | 52 | /** |
| 53 | *Provides a concrete implementation of the progress object for testing. Note that the behavior being |
| 54 | *tested is actually that of the superclass ProgressObjectImpl. |
| 55 | */ |
| 56 | public class MyProgressObjectImpl extends ProgressObjectImpl { |
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 | public MyProgressObjectImpl(TargetImpl target) { |
| 59 | super(target); |
| 60 | } |
David Matějček | f4dc06a | 2021-05-17 12:10:57 +0200 | [diff] [blame^] | 61 | |
Vinay Vishal | 5717147 | 2018-09-18 20:22:00 +0530 | [diff] [blame] | 62 | /** |
| 63 | *Required by the abstract class definition but not used during testing. |
| 64 | */ |
| 65 | public void run() {} |
David Matějček | f4dc06a | 2021-05-17 12:10:57 +0200 | [diff] [blame^] | 66 | |
Vinay Vishal | 5717147 | 2018-09-18 20:22:00 +0530 | [diff] [blame] | 67 | /** |
| 68 | *Stands in as an operation that fires an event to registered listeners. |
| 69 | */ |
| 70 | public void act() { |
| 71 | fireProgressEvent(StateType.RUNNING, "starting"); |
| 72 | /* |
| 73 | *This is where any real work would be done. It's useful to test with two events just in case |
| 74 | *that would uncover any problems. |
| 75 | */ |
| 76 | fireProgressEvent(StateType.COMPLETED, "done"); |
| 77 | } |
| 78 | } |
David Matějček | f4dc06a | 2021-05-17 12:10:57 +0200 | [diff] [blame^] | 79 | |
Vinay Vishal | 5717147 | 2018-09-18 20:22:00 +0530 | [diff] [blame] | 80 | /** |
| 81 | *Adds a new listener during the event handling. |
| 82 | */ |
| 83 | public class MeddlingListenerAdder implements ProgressListener { |
David Matějček | f4dc06a | 2021-05-17 12:10:57 +0200 | [diff] [blame^] | 84 | |
| 85 | |
Vinay Vishal | 5717147 | 2018-09-18 20:22:00 +0530 | [diff] [blame] | 86 | public void handleProgressEvent(javax.enterprise.deploy.spi.status.ProgressEvent progressEvent) { |
| 87 | /* |
| 88 | *Meddle in the listener list by adding a new listener to the list. This should trigger the error |
| 89 | *in the original version of ProgressObjectImpl but should not in the fixed version. |
| 90 | */ |
| 91 | TestProgressObjectImpl.this.theProgressObjectImpl.addProgressListener(new TestProgressObjectImpl.MeddlingListenerRemover()); |
| 92 | } |
David Matějček | f4dc06a | 2021-05-17 12:10:57 +0200 | [diff] [blame^] | 93 | |
Vinay Vishal | 5717147 | 2018-09-18 20:22:00 +0530 | [diff] [blame] | 94 | } |
David Matějček | f4dc06a | 2021-05-17 12:10:57 +0200 | [diff] [blame^] | 95 | |
Vinay Vishal | 5717147 | 2018-09-18 20:22:00 +0530 | [diff] [blame] | 96 | /** |
| 97 | *Removes itself as a listener during the event handling. |
| 98 | */ |
| 99 | public class MeddlingListenerRemover implements ProgressListener { |
David Matějček | f4dc06a | 2021-05-17 12:10:57 +0200 | [diff] [blame^] | 100 | |
| 101 | |
Vinay Vishal | 5717147 | 2018-09-18 20:22:00 +0530 | [diff] [blame] | 102 | public void handleProgressEvent(javax.enterprise.deploy.spi.status.ProgressEvent progressEvent) { |
| 103 | /* |
| 104 | *Meddle in the listener list by removing itself from the list. This should trigger the error |
| 105 | *in the original version of ProgressObjectImpl but should not in the fixed version. |
| 106 | */ |
| 107 | TestProgressObjectImpl.this.theProgressObjectImpl.removeProgressListener(this); |
| 108 | } |
David Matějček | f4dc06a | 2021-05-17 12:10:57 +0200 | [diff] [blame^] | 109 | |
Vinay Vishal | 5717147 | 2018-09-18 20:22:00 +0530 | [diff] [blame] | 110 | } |
David Matějček | f4dc06a | 2021-05-17 12:10:57 +0200 | [diff] [blame^] | 111 | |
Vinay Vishal | 5717147 | 2018-09-18 20:22:00 +0530 | [diff] [blame] | 112 | /* Local progress object implementation to be tested. */ |
| 113 | private MyProgressObjectImpl theProgressObjectImpl; |
David Matějček | f4dc06a | 2021-05-17 12:10:57 +0200 | [diff] [blame^] | 114 | |
Vinay Vishal | 5717147 | 2018-09-18 20:22:00 +0530 | [diff] [blame] | 115 | /** Creates a new instance of TestProgressObjectImpl */ |
| 116 | public TestProgressObjectImpl() { |
| 117 | } |
David Matějček | f4dc06a | 2021-05-17 12:10:57 +0200 | [diff] [blame^] | 118 | |
Vinay Vishal | 5717147 | 2018-09-18 20:22:00 +0530 | [diff] [blame] | 119 | /** |
| 120 | * @param args the command line arguments |
| 121 | */ |
| 122 | public static void main(String[] args) { |
| 123 | TestProgressObjectImpl test = new TestProgressObjectImpl(); |
| 124 | try { |
| 125 | test.run(args); |
| 126 | test.pass(); |
| 127 | } catch (Throwable th) { |
| 128 | th.printStackTrace(System.out); |
| 129 | test.fail(); |
| 130 | } |
| 131 | } |
David Matějček | f4dc06a | 2021-05-17 12:10:57 +0200 | [diff] [blame^] | 132 | |
Vinay Vishal | 5717147 | 2018-09-18 20:22:00 +0530 | [diff] [blame] | 133 | public void run(String[] args) { |
| 134 | addNewListenerDuringEventHandling(); |
| 135 | } |
| 136 | |
| 137 | /** |
David Matějček | f4dc06a | 2021-05-17 12:10:57 +0200 | [diff] [blame^] | 138 | * Tamper with the listener list by adding a new listener during event handling. |
Vinay Vishal | 5717147 | 2018-09-18 20:22:00 +0530 | [diff] [blame] | 139 | */ |
| 140 | private void addNewListenerDuringEventHandling() { |
| 141 | /* |
| 142 | *Create a TargetImpl just to satisfy the signature of the constructor for the progress object implementation. |
| 143 | */ |
| 144 | DeploymentFacility df = DeploymentFacilityFactory.getDeploymentFacility(); |
| 145 | TargetImpl target = new TargetImpl((AbstractDeploymentFacility)df, "test", "test"); |
| 146 | theProgressObjectImpl = new MyProgressObjectImpl(target); |
| 147 | TestProgressObjectImpl.MeddlingListenerAdder meddlingListener1 = new TestProgressObjectImpl.MeddlingListenerAdder(); |
| 148 | TestProgressObjectImpl.MeddlingListenerAdder meddlingListener2 = new TestProgressObjectImpl.MeddlingListenerAdder(); |
David Matějček | f4dc06a | 2021-05-17 12:10:57 +0200 | [diff] [blame^] | 149 | |
Vinay Vishal | 5717147 | 2018-09-18 20:22:00 +0530 | [diff] [blame] | 150 | theProgressObjectImpl.addProgressListener(meddlingListener1); |
| 151 | theProgressObjectImpl.addProgressListener(meddlingListener2); |
David Matějček | f4dc06a | 2021-05-17 12:10:57 +0200 | [diff] [blame^] | 152 | |
Vinay Vishal | 5717147 | 2018-09-18 20:22:00 +0530 | [diff] [blame] | 153 | /* |
| 154 | *Fire an event that will change the listener set. |
| 155 | */ |
| 156 | theProgressObjectImpl.act(); |
David Matějček | f4dc06a | 2021-05-17 12:10:57 +0200 | [diff] [blame^] | 157 | |
Vinay Vishal | 5717147 | 2018-09-18 20:22:00 +0530 | [diff] [blame] | 158 | } |
David Matějček | f4dc06a | 2021-05-17 12:10:57 +0200 | [diff] [blame^] | 159 | |
Vinay Vishal | 5717147 | 2018-09-18 20:22:00 +0530 | [diff] [blame] | 160 | private void log(String message) { |
| 161 | System.out.println("[TestProgressObjectImpl]:: " + message); |
| 162 | } |
| 163 | |
| 164 | private void pass() { |
| 165 | log("PASSED: " + here); |
| 166 | System.exit(0); |
| 167 | } |
| 168 | |
| 169 | private void fail() { |
| 170 | log("FAILED: " + here); |
| 171 | System.exit(-1); |
| 172 | } |
| 173 | } |