KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > wsdl > model > extensions > bpel > SyncUpdateTest


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.xml.wsdl.model.extensions.bpel;
21
22 import java.util.ArrayList JavaDoc;
23 import javax.swing.text.Document JavaDoc;
24 import junit.framework.TestCase;
25 import org.netbeans.modules.xml.wsdl.model.PortType;
26 import org.netbeans.modules.xml.wsdl.model.ReferenceableWSDLComponent;
27 import org.netbeans.modules.xml.wsdl.model.WSDLModel;
28 import org.netbeans.modules.xml.wsdl.model.extensions.NamespaceLocation;
29 import org.netbeans.modules.xml.wsdl.model.extensions.TestCatalogModel;
30 import org.netbeans.modules.xml.wsdl.model.extensions.Util;
31 import org.netbeans.modules.xml.wsdl.model.visitor.FindReferencedVisitor;
32 import org.netbeans.modules.xml.xam.ComponentEvent;
33 import org.netbeans.modules.xml.xam.ComponentListener;
34 import org.netbeans.modules.xml.xam.dom.AbstractDocumentModel;
35 import org.netbeans.modules.xml.xam.dom.NamedComponentReference;
36
37 /**
38  *
39  * @author Nam Nguyen
40  */

41 public class SyncUpdateTest extends TestCase {
42     private TestComponentListener listener;
43     
44     public SyncUpdateTest(String JavaDoc testName) {
45         super(testName);
46     }
47
48     public static class TestComponentListener implements ComponentListener {
49         ArrayList JavaDoc<ComponentEvent> events = new ArrayList JavaDoc<ComponentEvent>();
50         
51         public void valueChanged(ComponentEvent evt) {
52             events.add(evt);
53         }
54         public void childrenDeleted(ComponentEvent evt) {
55             events.add(evt);
56         }
57         public void childrenAdded(ComponentEvent evt) {
58             events.add(evt);
59         }
60
61         public void assertChangeEvent(Object JavaDoc source) {
62             for (ComponentEvent e : events) {
63                 if (e.getEventType() == ComponentEvent.EventType.VALUE_CHANGED &&
64                     e.getSource() == source) {
65                     return;
66                 }
67             }
68             assertFalse("Failed to receive ATTRIBUTE event on " + source, true);
69         }
70         
71         public void assertChildAddedEvent(Object JavaDoc source) {
72             for (ComponentEvent e : events) {
73                 if (e.getEventType() == ComponentEvent.EventType.CHILD_ADDED &&
74                     e.getSource() == source) {
75                     return;
76                 }
77             }
78             assertFalse("Failed to receive CHILD_ADDED event on " + source, true);
79         }
80
81         public void assertChildRemovedEvent(Object JavaDoc source) {
82             for (ComponentEvent e : events) {
83                 if (e.getEventType() == ComponentEvent.EventType.CHILD_REMOVED &&
84                     e.getSource() == source) {
85                     return;
86                 }
87             }
88             assertFalse("Failed to receive CHILD_REMOVED event on " + source, true);
89         }
90         
91         public void reset() { events.clear(); events = new ArrayList JavaDoc<ComponentEvent>(); }
92     }
93     
94     protected void setUp() throws Exception JavaDoc {
95         TestCatalogModel.getDefault().setDocumentPooling(true);
96     }
97
98     private WSDLModel setup(NamespaceLocation wsdl) throws Exception JavaDoc {
99         WSDLModel m = TestCatalogModel.getDefault().getWSDLModel(wsdl);
100         listener = new TestComponentListener();
101         m.addComponentListener(listener);
102         return m;
103     }
104
105     protected void tearDown() throws Exception JavaDoc {
106         listener.reset();
107         TestCatalogModel.getDefault().setDocumentPooling(false);
108     }
109
110     public <T extends ReferenceableWSDLComponent> T find(WSDLModel model, String JavaDoc name, Class JavaDoc<T> type) {
111         return new FindReferencedVisitor<T>(model.getDefinitions()).find(name, type);
112     }
113     
114     public void testSyncAddRemoveBPEL() throws Exception JavaDoc {
115         WSDLModel model = setup(NamespaceLocation.VEHICLE);
116         
117         PartnerLinkType plt = find(model, "VehicleReservationPartnerLinkType", PartnerLinkType.class);
118         Role role1 = plt.getRole1();
119         Role role2 = plt.getRole2();
120         
121         Document JavaDoc doc = ((AbstractDocumentModel)model).getBaseDocument();
122         Util.setDocumentContentTo(doc, "resources/Vehicle_PartnerLinkChanged.wsdl");
123     model.sync();
124         
125         listener.assertChildRemovedEvent(plt);
126         assertNull("role2 removed", plt.getRole2());
127         
128         // now do the reverse
129
Util.setDocumentContentTo(doc, "resources/VehicleReservationService.wsdl");
130         model.sync();
131         
132         listener.assertChildAddedEvent(plt);
133         assertEquals("role2 added", "VehicleReservationCallbackServiceRole", plt.getRole2().getName());
134     }
135
136     public void testSyncUpdateRenameAndReferences() throws Exception JavaDoc {
137         WSDLModel model = setup(NamespaceLocation.VEHICLE);
138         
139         PartnerLinkType plt = find(model, "VehicleReservationPartnerLinkType", PartnerLinkType.class);
140         Role role1 = plt.getRole1();
141         assertEquals("VehicleReservationServiceRole", role1.getName());
142         NamedComponentReference<PortType> ptRef1 = role1.getPortType();
143         assertNotNull(ptRef1);
144         
145         Util.setDocumentContentTo(model, "resources/Vehicle_PartnerLinkChanged2.wsdl");
146         model.sync();
147         
148         listener.assertChangeEvent(role1);
149         NamedComponentReference<PortType> ptRef = role1.getPortType();
150         assertNotNull(ptRef);
151         assertEquals("VehicleReservationPortType2", ptRef.get().getName());
152     }
153 }
154
Popular Tags