summaryrefslogtreecommitdiffstats
path: root/sca-java-2.x/tags/2.0.1-RC1/testing/itest/builder/src/test/java/org/apache/tuscany/sca/itest/builder/TestUtils.java
blob: 9a5aa5582d6f42ea8663fc2438b513b01cc13ec4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.    
 */
package org.apache.tuscany.sca.itest.builder;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;

import javax.wsdl.Definition;
import javax.wsdl.Port;
import javax.wsdl.WSDLException;
import javax.wsdl.extensions.soap.SOAPAddress;
import javax.wsdl.extensions.soap12.SOAP12Address;
import javax.wsdl.factory.WSDLFactory;
import javax.wsdl.xml.WSDLWriter;
import javax.xml.namespace.QName;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.stream.XMLStreamWriter;

import org.apache.tuscany.sca.assembly.Binding;
import org.apache.tuscany.sca.assembly.Component;
import org.apache.tuscany.sca.assembly.ComponentReference;
import org.apache.tuscany.sca.assembly.ComponentService;
import org.apache.tuscany.sca.assembly.Composite;
import org.apache.tuscany.sca.assembly.Contract;
import org.apache.tuscany.sca.assembly.Endpoint;
import org.apache.tuscany.sca.assembly.EndpointReference;
import org.apache.tuscany.sca.assembly.Implementation;
import org.apache.tuscany.sca.assembly.Reference;
import org.apache.tuscany.sca.assembly.Service;
import org.apache.tuscany.sca.assembly.impl.EndpointImpl;
import org.apache.tuscany.sca.assembly.impl.EndpointReferenceImpl;
import org.apache.tuscany.sca.binding.ws.WebServiceBinding;
import org.apache.tuscany.sca.contribution.processor.ProcessorContext;
import org.apache.tuscany.sca.monitor.Problem;
import org.apache.xml.serialize.OutputFormat;
import org.apache.xml.serialize.XMLSerializer;
import org.w3c.dom.Document;

/**
 * Static utility methods for use by test cases.
 * 
 * @version $Rev$ $Date$
 */
public class TestUtils {

    protected static void checkProblems(CustomCompositeBuilder customBuilder) throws Exception {
        boolean problems = false;
        for (Problem problem : customBuilder.getMonitor().getProblems()) {
            if (problem.getCause() != null) {
                problem.getCause().printStackTrace();
            }
            if (problem.getSeverity() == Problem.Severity.ERROR){
                problems = true;
            }
        }
        assert !problems;
    }

    protected static String getPortAddress(Port port) {
        Object ext = port.getExtensibilityElements().get(0);
        String returnAddress = null;
        if (ext instanceof SOAPAddress) {
            returnAddress = ((SOAPAddress)ext).getLocationURI();
        }
        if (ext instanceof SOAP12Address) {
            returnAddress = ((SOAP12Address)ext).getLocationURI();
        }
        
        returnAddress = returnAddress.substring(returnAddress.indexOf("//") + 2);
        returnAddress = returnAddress.substring(returnAddress.indexOf("/"));
        
        return returnAddress;
    }

    protected static Component getComponent(Composite composite, String name) {
        for (Component component : composite.getComponents()) {
            if (name.equals(component.getName())) {
                return component;
            }
            // process implementation composites recursively
            Implementation impl = component.getImplementation();
            if (impl instanceof Composite) {
                Component comp = getComponent((Composite)impl, name);
                if (comp != null) {
                    return comp;
                }
            }
        }
        return null;
    }

    protected static Composite getComposite(Composite composite, QName name) {
        if (name.equals(composite.getName())) {
            return composite;
        }
        for (Component component : composite.getComponents()) {
            // process implementation composites recursively
            Implementation impl = component.getImplementation();
            if (impl instanceof Composite) {
                Composite comp = getComposite((Composite)impl, name);
                if (comp != null) {
                    return comp;
                }
            }
        }
        return null;
    }

    protected static void printResults(CustomCompositeBuilder customBuilder) throws Exception {
        for (Problem problem : customBuilder.getMonitor().getProblems()) {
            if (problem.getCause() != null) {
                problem.getCause().printStackTrace();
            }
        }
        Composite domainComposite = customBuilder.getDomainComposite();
        printComposite(domainComposite, customBuilder);
    }

    private static void printComposite(Composite composite, CustomCompositeBuilder customBuilder) throws Exception {
        // process implementation composites recursively
        for (Component component : composite.getComponents()) {
            Implementation implementation = component.getImplementation();
            if (implementation instanceof Composite) {
                printComposite((Composite)implementation, customBuilder);
            }
        }

        // write out the SCDL
        writeSCDL(composite, customBuilder);

        // find all the component service and reference bindings     
        for (Component component : composite.getComponents()) {
            for (ComponentService componentService : component.getServices()) {
                for (Binding binding : componentService.getBindings()) {
                    if (binding instanceof WebServiceBinding) {
                        writeWSDL(component, componentService, ((WebServiceBinding)binding).getGeneratedWSDLDocument());
                    }
                }
            }
            for (ComponentReference componentReference : component.getReferences()) {
                for (Binding binding : componentReference.getBindings()) {
                    if (binding instanceof WebServiceBinding) {
                        writeWSDL(component, componentReference, ((WebServiceBinding)binding).getGeneratedWSDLDocument());
                    }
                }
            }
        }

        // find all the composite service and reference bindings     
        for (Service service : composite.getServices()) {
            for (Binding binding : service.getBindings()) {
                if (binding instanceof WebServiceBinding) {
                    writeWSDL(null, service, ((WebServiceBinding)binding).getGeneratedWSDLDocument());
                }
            }
        }
        for (Reference reference : composite.getReferences()) {
            for (Binding binding : reference.getBindings()) {
                if (binding instanceof WebServiceBinding) {
                    writeWSDL(null, reference, ((WebServiceBinding)binding).getGeneratedWSDLDocument());
                }
            }
        }
    }

    private static void writeSCDL(Composite composite, CustomCompositeBuilder customBuilder) throws Exception {
        // Print out a composite
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        XMLStreamWriter writer = customBuilder.getOutputFactory().createXMLStreamWriter(bos);
        customBuilder.getModelProcessor().write(composite, writer, new ProcessorContext());
        
        // Parse and write again to pretty format it
        DocumentBuilder documentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
        Document document = documentBuilder.parse(new ByteArrayInputStream(bos.toByteArray()));
        OutputFormat format = new OutputFormat();
        format.setIndenting(true);
        format.setIndent(2);
        XMLSerializer serializer = new XMLSerializer(System.out, format);
        System.out.println("-->Runtime SCDL model for composite " + composite.getName());
        serializer.serialize(document);
    }

    private static void writeWSDL(Component component, Contract contract, Definition definition) {
        if (definition == null) {
            System.out.println("-->No generated WSDL for " + (component != null ? component.getName() : "") + "/" + contract.getName());
        } else {
            try {
                System.out.println("-->Generated WSDL for " + (component != null ? component.getName() : "") + "/" + contract.getName());
                WSDLWriter writer =  WSDLFactory.newInstance().newWSDLWriter();
                writer.writeWSDL(definition, System.out);
            } catch (WSDLException e) {
                // ignore
            }
        }
    }
    
    protected static void writeWSDL(Definition definition) {
        try {
            WSDLWriter writer =  WSDLFactory.newInstance().newWSDLWriter();
            writer.writeWSDL(definition, System.out);
        } catch (WSDLException e) {
            // ignore
        }
    }
    
    protected static String printStructure(Composite composite, String indent){
        String structure = "";
        for (Component component : composite.getComponents()){
            structure += indent + "Component URI - " + component.getURI() + "\n";
            
            // recurse for composite implementations
            Implementation implementation = component.getImplementation();
            if (implementation instanceof Composite) {
                structure += printStructure((Composite)implementation, indent + "  ");
            }
            
            for (Service service : component.getServices()){
                for (Endpoint endpoint : service.getEndpoints()){
                    structure += indent + ((EndpointImpl)endpoint).toStringWithoutHash() + " " + endpoint.getBinding().getClass().getName() + "\n";
                }
            }
            for (Reference reference : component.getReferences()){
                for (EndpointReference endpointReference : reference.getEndpointReferences()){
                    structure += indent + ((EndpointReferenceImpl)endpointReference).toStringWithoutHash() + " " + endpointReference.getBinding().getClass().getName() + "\n";
                }
            }
        }
        return structure;
    }    
/*    
    protected static String printEndpoints(Composite composite){
        return printEndpoints(composite, "");
    }
    
    protected static String printEndpoints(Composite composite, String indent){
        
        String buffer = ""; 
        
        for (Component component : composite.getComponents()) {
            buffer += indent + "Component - " + component.getName() + "\n";
            
            // print component service endpoints
            for (ComponentService componentService : component.getServices()) {
                buffer += indent + "Service - " + componentService.getName() + "\n";
                for (Endpoint endpoint : componentService.getEndpoints()) {
                    if (endpoint.getBinding() != null){
                        buffer += printEndpoint(endpoint, indent);
                    }
                }
            }
            
            for (ComponentReference componentReference : component.getReferences()) {
                buffer += indent + "Reference - " + componentReference.getName() + "\n";
                for (EndpointReference endpointReference : componentReference.getEndpointReferences()) {
                    buffer += printEndpointReference(endpointReference, indent);
                }
            }
            
            // process implementation composites recursively
            Implementation implementation = component.getImplementation();
            if (implementation instanceof Composite) {
                buffer += indent + "Component - " + component.getName() + " has composite impl" + "\n";
                buffer += printEndpoints((Composite)implementation, indent + "   ");
            }
        }
        
        return buffer;        
    }
    
    protected static String printEndpoint(Endpoint endpoint, String indent){
        String buffer = ""; 
        
        buffer += indent + " Endpoint - Component: " + endpoint.getComponent().getName() +"\n";
        buffer += indent + "            Service:   " + endpoint.getService().getName() +"\n";
        buffer += indent + "            Binding:   " + endpoint.getBinding().getName() +"\n";
        
        return buffer;
    }
    
    protected static String printEndpointReference(EndpointReference endpointReference, String indent){
        String buffer = ""; 
        
        buffer += indent + " EndpointReference - Component:        " + endpointReference.getComponent().getName() +"\n";
        buffer += indent + "                     Reference:        " + endpointReference.getReference().getName() +"\n";
        if (endpointReference.getTargetEndpoint() != null){
            buffer += indent + "                     Wired:            " +"\n";
            buffer += indent + "                       Target:         " + endpointReference.getTargetEndpoint().getComponent().getName()+"\n";
            if (endpointReference.getTargetEndpoint() != null && 
                endpointReference.getTargetEndpoint().isUnresolved() == false){
                buffer += indent + "                       Binding:        " + endpointReference.getBinding().getName() +"\n";
                buffer += indent + "                       TargetEndpoint: " + endpointReference.getTargetEndpoint().getBinding().getName()+"\n";
            } else {
                buffer += indent + "                       Unresolved:     " +"\n";
            }
        } else {
            buffer += indent + "                     NonWired:         " +"\n";
        }
        
        return buffer;
    }    
*/
}