summaryrefslogtreecommitdiffstats
path: root/sandbox/ant/tuscany-container-rhino/src/test/java/org/apache/tuscany/container/rhino/e4x/E4XPolicyBuilderTestCase.java
blob: 09638e7eaeaa6d0c6804230d94662fe58ceb1708 (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
320
321
322
323
324
325
/*
 * Copyright 2004,2005 The Apache Software Foundation.
 *
 * Licensed 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.container.rhino.e4x;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.wsdl.Input;
import javax.wsdl.Message;
import javax.wsdl.Operation;
import javax.wsdl.Part;
import javax.wsdl.PortType;
import javax.xml.namespace.QName;

import junit.framework.TestCase;

import org.apache.tuscany.common.resource.impl.ResourceLoaderImpl;
import org.apache.tuscany.container.rhino.assembly.JavaScriptImplementation;
import org.apache.tuscany.core.wire.Interceptor;
import org.apache.tuscany.core.wire.TargetInvocationConfiguration;
import org.apache.tuscany.core.wire.WireTargetConfiguration;
import org.apache.tuscany.model.assembly.AssemblyContext;
import org.apache.tuscany.model.assembly.AssemblyInitializationException;
import org.apache.tuscany.model.assembly.AssemblyVisitor;
import org.apache.tuscany.model.assembly.AtomicComponent;
import org.apache.tuscany.model.assembly.AtomicImplementation;
import org.apache.tuscany.model.assembly.ComponentType;
import org.apache.tuscany.model.assembly.Composite;
import org.apache.tuscany.model.assembly.ConfiguredProperty;
import org.apache.tuscany.model.assembly.ConfiguredReference;
import org.apache.tuscany.model.assembly.ConfiguredService;
import org.apache.tuscany.model.assembly.Property;
import org.apache.tuscany.model.assembly.Reference;
import org.apache.tuscany.model.assembly.Service;
import org.apache.tuscany.model.assembly.ServiceContract;
import org.apache.tuscany.model.types.wsdl.impl.WSDLServiceContractImpl;

import com.ibm.wsdl.InputImpl;
import com.ibm.wsdl.MessageImpl;
import com.ibm.wsdl.OperationImpl;
import com.ibm.wsdl.PartImpl;
import com.ibm.wsdl.PortTypeImpl;

/**
 * Tests for the E4XPolicyBuilder
 */
public class E4XPolicyBuilderTestCase extends TestCase {

    public E4XPolicyBuilderTestCase() {

    }

    protected void setUp() throws Exception {
        super.setUp();
    }

    public void testGetElementQName() {
        E4XPolicyBuilder builder = new E4XPolicyBuilder();
        QName qn = new QName("foo");
        JavaScriptImplementation jsImpl = createMockJSImpl("foo", qn);
        QName qn2 = builder.getElementQName(jsImpl, "foo");
        assertEquals(qn, qn2);
    }

    public void testBuild() throws SecurityException, NoSuchMethodException {
        E4XPolicyBuilder builder = new E4XPolicyBuilder();
        ConfiguredService service = createMockConfiguredService();
        WireTargetConfiguration config = createMockWireTargetConfiguration();
        builder.build(service, config);
        Map<Method, TargetInvocationConfiguration> configs = config.getInvocationConfigurations();
        assertNotNull(configs);
        assertEquals(1, configs.size());
        TargetInvocationConfiguration tic = configs.values().iterator().next();
        Interceptor interceptor = tic.getHeadInterceptor();
        assertTrue(interceptor instanceof E4XInterceptor);
    }

    private WireTargetConfiguration createMockWireTargetConfiguration() throws SecurityException, NoSuchMethodException {
        Map<Method, TargetInvocationConfiguration> configs = new HashMap<Method, TargetInvocationConfiguration>();
        Method foo = Foo.class.getMethod("foo", new Class[0]);
        TargetInvocationConfiguration config = new TargetInvocationConfiguration(foo);
        configs.put(foo, config);
        WireTargetConfiguration wtf = new WireTargetConfiguration(null, configs, null, null);
        return wtf;
    }

    interface Foo {
        public void foo();
    }

    private ConfiguredService createMockConfiguredService() {
        ConfiguredService service = new ConfiguredService() {

            public String getName() {
                return null;
            }

            public void setName(String name) {
            }

            public Service getPort() {
                return null;
            }

            public void setPort(Service port) {
            }

            public org.apache.tuscany.model.assembly.Part getPart() {
                return createPart();
            }

            public void setPart(org.apache.tuscany.model.assembly.Part part) {
            }

            public void initialize(AssemblyContext modelContext) throws AssemblyInitializationException {
            }

            public void freeze() {
            }

            public boolean accept(AssemblyVisitor visitor) {
                return false;
            }

            public void setProxyFactory(Object proxyFactory) {
            }

            public Object getProxyFactory() {
                return null;
            }
        };

        return service;
    }

    private org.apache.tuscany.model.assembly.Part createPart() {
        org.apache.tuscany.model.assembly.Part part = new AtomicComponent() {

            public AtomicImplementation getImplementation() {
                return createMockJSImpl("foo", new QName("foo"));
            }

            public void setImplementation(AtomicImplementation value) {
            }

            public List<ConfiguredProperty> getConfiguredProperties() {
                return null;
            }

            public ConfiguredProperty getConfiguredProperty(String name) {
                return null;
            }

            public List<ConfiguredReference> getConfiguredReferences() {
                return null;
            }

            public ConfiguredReference getConfiguredReference(String name) {
                return null;
            }

            public List<ConfiguredService> getConfiguredServices() {
                return null;
            }

            public ConfiguredService getConfiguredService(String name) {
                return null;
            }

            public String getName() {
                return null;
            }

            public void setName(String value) {
            }

            public Composite getComposite() {
                return null;
            }

            public void setComposite(Composite composite) {
            }

            public List<Object> getExtensibilityElements() {
                return null;
            }

            public List<Object> getExtensibilityAttributes() {
                return null;
            }

            public void initialize(AssemblyContext modelContext) throws AssemblyInitializationException {
            }

            public void freeze() {
            }

            public boolean accept(AssemblyVisitor visitor) {
                return false;
            }

            public void setContextFactory(Object contextFactory) {
            }

            public Object getContextFactory() {
                return null;
            }
        };
        return part;
    }

    private JavaScriptImplementation createMockJSImpl(final String name, final QName qn) {
        JavaScriptImplementation jsImpl = new JavaScriptImplementation();

        jsImpl.setComponentType(new ComponentType() {

            public List<Service> getServices() {
                return Arrays.asList(new Service[] { createMockService(name, qn) });
            }

            public Service getService(String name) {
                return null;
            }

            public List<Reference> getReferences() {
                return null;
            }

            public Reference getReference(String name) {
                return null;
            }

            public List<Property> getProperties() {
                return new ArrayList<Property>();
            }

            public Property getProperty(String name) {
                return null;
            }

            public List<Object> getExtensibilityElements() {
                return null;
            }

            public List<Object> getExtensibilityAttributes() {
                return null;
            }

            public void initialize(AssemblyContext modelContext) throws AssemblyInitializationException {
            }

            public void freeze() {
            }

            public boolean accept(AssemblyVisitor visitor) {
                return false;
            }
        });

        jsImpl.setResourceLoader(new ResourceLoaderImpl(getClass().getClassLoader()));

        return jsImpl;
    }

    private Service createMockService(final String name, final QName qn) {
        Service service = new Service() {

            public ServiceContract getServiceContract() {
                WSDLServiceContractImpl sc = new WSDLServiceContractImpl();
                PortType pt = new PortTypeImpl();
                Operation op = new OperationImpl();
                op.setName(name);
                Input input = new InputImpl();
                Message msg = new MessageImpl();
                Part p = new PartImpl();
                p.setElementName(qn);
                msg.addPart(p);
                input.setMessage(msg);
                op.setInput(input);
                pt.addOperation(op);
                sc.setPortType(pt);
                return sc;
            }

            public void setServiceContract(ServiceContract contract) {
            }

            public String getName() {
                return null;
            }

            public void setName(String name) {
            }

            public void initialize(AssemblyContext modelContext) throws AssemblyInitializationException {
            }

            public void freeze() {
            }

            public boolean accept(AssemblyVisitor visitor) {
                return false;
            }
        };
        return service;
    }

}