001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.tomcat5.progress;
043:
044: import java.util.List;
045: import java.util.concurrent.CopyOnWriteArrayList;
046: import javax.enterprise.deploy.shared.ActionType;
047: import javax.enterprise.deploy.shared.CommandType;
048: import javax.enterprise.deploy.shared.StateType;
049: import javax.enterprise.deploy.spi.TargetModuleID;
050: import javax.enterprise.deploy.spi.exceptions.OperationUnsupportedException;
051: import javax.enterprise.deploy.spi.status.ClientConfiguration;
052: import javax.enterprise.deploy.spi.status.DeploymentStatus;
053: import javax.enterprise.deploy.spi.status.ProgressEvent;
054: import javax.enterprise.deploy.spi.status.ProgressListener;
055: import javax.enterprise.deploy.spi.status.ProgressObject;
056: import org.netbeans.junit.NbTestCase;
057:
058: /**
059: *
060: * @author Petr Hejl
061: */
062: public class MultiProgressObjectWrapperTest extends NbTestCase {
063:
064: public MultiProgressObjectWrapperTest(String testName) {
065: super (testName);
066: }
067:
068: public void testCompleted() {
069: TargetModuleID module1 = new TargetModuleIDImpl("name1",
070: "description1", "id1", "url1");
071: TargetModuleID module2 = new TargetModuleIDImpl("name2",
072: "description2", "id2", "url2");
073:
074: ProgressObjectImpl object1 = new ProgressObjectImpl(new Status(
075: ActionType.EXECUTE, CommandType.DISTRIBUTE, "start",
076: StateType.RUNNING), module1);
077: ProgressObjectImpl object2 = new ProgressObjectImpl(new Status(
078: ActionType.EXECUTE, CommandType.DISTRIBUTE, "start",
079: StateType.RUNNING), module2);
080:
081: MultiProgressObjectWrapper wrapper = new MultiProgressObjectWrapper(
082: new ProgressObject[] { object1, object2 });
083:
084: DeploymentStatus module1Status1 = new Status(
085: ActionType.EXECUTE, CommandType.DISTRIBUTE,
086: "still running", StateType.RUNNING);
087: ProgressEvent event1 = new ProgressEvent("fake", module1,
088: module1Status1);
089:
090: DeploymentStatus module1Status2 = new Status(
091: ActionType.EXECUTE, CommandType.DISTRIBUTE,
092: "completed", StateType.COMPLETED);
093: ProgressEvent event2 = new ProgressEvent("fake", module1,
094: module1Status2);
095:
096: DeploymentStatus module2Status1 = new Status(
097: ActionType.EXECUTE, CommandType.DISTRIBUTE,
098: "completed", StateType.COMPLETED);
099: ProgressEvent event3 = new ProgressEvent("fake", module2,
100: module2Status1);
101:
102: ProgressListener listener1 = new ProgressListenerImpl(
103: new ProgressEvent[] { event1, event2, event3 }, wrapper);
104:
105: ProgressListener listener2 = new ProgressListenerImpl(
106: new ProgressEvent[] { event1, event2, event3 }, wrapper);
107:
108: wrapper.addProgressListener(listener1);
109: wrapper.addProgressListener(listener2);
110:
111: // test itself
112: object1.setDeploymentStatus(module1Status1);
113: object1.setDeploymentStatus(module1Status2);
114: object2.setDeploymentStatus(module2Status1);
115:
116: assertEquals(StateType.COMPLETED, wrapper.getDeploymentStatus()
117: .getState());
118: assertTrue(wrapper.getDeploymentStatus().isCompleted());
119: assertFalse(wrapper.getDeploymentStatus().isRunning());
120: assertFalse(wrapper.getDeploymentStatus().isFailed());
121: }
122:
123: public void testFailed() {
124: TargetModuleID module1 = new TargetModuleIDImpl("name1",
125: "description1", "id1", "url1");
126: TargetModuleID module2 = new TargetModuleIDImpl("name2",
127: "description2", "id2", "url2");
128:
129: ProgressObjectImpl object1 = new ProgressObjectImpl(new Status(
130: ActionType.EXECUTE, CommandType.DISTRIBUTE, "start",
131: StateType.RUNNING), module1);
132: ProgressObjectImpl object2 = new ProgressObjectImpl(new Status(
133: ActionType.EXECUTE, CommandType.DISTRIBUTE, "start",
134: StateType.RUNNING), module2);
135:
136: MultiProgressObjectWrapper wrapper = new MultiProgressObjectWrapper(
137: new ProgressObject[] { object1, object2 });
138:
139: DeploymentStatus module1Status1 = new Status(
140: ActionType.EXECUTE, CommandType.DISTRIBUTE,
141: "completed", StateType.COMPLETED);
142: ProgressEvent event1 = new ProgressEvent("fake", module1,
143: module1Status1);
144:
145: DeploymentStatus module2Status1 = new Status(
146: ActionType.EXECUTE, CommandType.DISTRIBUTE,
147: "completed", StateType.FAILED);
148: ProgressEvent event2 = new ProgressEvent("fake", module2,
149: module2Status1);
150:
151: ProgressListener listener1 = new ProgressListenerImpl(
152: new ProgressEvent[] { event1, event2 }, wrapper);
153:
154: ProgressListener listener2 = new ProgressListenerImpl(
155: new ProgressEvent[] { event1, event2 }, wrapper);
156:
157: wrapper.addProgressListener(listener1);
158: wrapper.addProgressListener(listener2);
159:
160: // test itself
161: object1.setDeploymentStatus(module1Status1);
162: object2.setDeploymentStatus(module2Status1);
163:
164: assertEquals(StateType.FAILED, wrapper.getDeploymentStatus()
165: .getState());
166: assertFalse(wrapper.getDeploymentStatus().isCompleted());
167: assertFalse(wrapper.getDeploymentStatus().isRunning());
168: assertTrue(wrapper.getDeploymentStatus().isFailed());
169: }
170:
171: private static class ProgressObjectImpl implements ProgressObject {
172:
173: private List<ProgressListener> listeners = new CopyOnWriteArrayList<ProgressListener>();
174:
175: private DeploymentStatus status;
176:
177: private TargetModuleID targetModuleID;
178:
179: public ProgressObjectImpl(DeploymentStatus status,
180: TargetModuleID targetModuleID) {
181: this .status = status;
182: this .targetModuleID = targetModuleID;
183: }
184:
185: public ClientConfiguration getClientConfiguration(
186: TargetModuleID arg0) {
187: return null;
188: }
189:
190: public synchronized DeploymentStatus getDeploymentStatus() {
191: return status;
192: }
193:
194: public TargetModuleID[] getResultTargetModuleIDs() {
195: return new TargetModuleID[] { targetModuleID };
196: }
197:
198: public boolean isCancelSupported() {
199: return false;
200: }
201:
202: public void cancel() throws OperationUnsupportedException {
203: throw new UnsupportedOperationException(
204: "Not supported yet.");
205: }
206:
207: public boolean isStopSupported() {
208: return false;
209: }
210:
211: public void stop() throws OperationUnsupportedException {
212: throw new UnsupportedOperationException(
213: "Not supported yet.");
214: }
215:
216: public void removeProgressListener(ProgressListener listener) {
217: if (listener != null) {
218: listeners.remove(listener);
219: }
220: }
221:
222: public void addProgressListener(ProgressListener listener) {
223: if (listener != null) {
224: listeners.add(listener);
225: }
226: }
227:
228: public synchronized void setDeploymentStatus(
229: DeploymentStatus status) {
230: this .status = status;
231: fireProgressEvent(new ProgressEvent(this , targetModuleID,
232: status));
233: }
234:
235: private void fireProgressEvent(ProgressEvent evt) {
236: for (ProgressListener listener : listeners) {
237: listener.handleProgressEvent(evt);
238: }
239: }
240:
241: }
242: }
|