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
|
/**
*
* Copyright 2005 The Apache Software Foundation or its licensors, as applicable.
*
* 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.binding.jsonrpc.mocks;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import org.apache.tuscany.binding.jsonrpc.assembly.JSONRPCBinding;
import org.apache.tuscany.binding.jsonrpc.mocks.servlet.MockServletConfig;
import org.apache.tuscany.binding.jsonrpc.mocks.tuscany.MockBinding;
import org.apache.tuscany.binding.jsonrpc.mocks.tuscany.MockCompositeContextImpl;
import org.apache.tuscany.binding.jsonrpc.mocks.tuscany.MockEntryPointContext;
import org.apache.tuscany.binding.jsonrpc.mocks.tuscany.MockScopeContext;
import org.apache.tuscany.core.context.CompositeContext;
import org.apache.tuscany.core.context.Context;
import org.apache.tuscany.core.context.ScopeContext;
import org.apache.tuscany.core.webapp.TuscanyServletListener;
import org.apache.tuscany.model.assembly.Binding;
import org.apache.tuscany.model.assembly.EntryPoint;
import org.apache.tuscany.model.assembly.Module;
import org.apache.tuscany.model.assembly.impl.EntryPointImpl;
public class MockConfigUtils {
public static ServletConfig createMockServletConfig(String entryPointName, Object instance) {
ServletConfig servletConfig = new MockServletConfig();
ServletContext context = servletConfig.getServletContext();
context.setAttribute(TuscanyServletListener.MODULE_COMPONENT_NAME, createModuleWithJSONRPCEntryPoint(entryPointName, instance));
return servletConfig;
}
public static CompositeContext createModuleWithJSONRPCEntryPoint(String entryPointName, Object instance) {
MockCompositeContextImpl cci = new MockCompositeContextImpl();
Module module = (Module) cci.getComposite();
List<EntryPoint> entryPoints = module.getEntryPoints();
EntryPoint ep = createMockEntryPoint(entryPointName);
addJSONRPCBinding(ep);
entryPoints.add(ep);
cci.start();
Map<String, Context> ics = new HashMap<String, Context>();
ics.put(entryPointName, new MockEntryPointContext(instance));
ScopeContext sc = new MockScopeContext(ics);
sc.start();
Map<String, ScopeContext> scopeIndex = cci.getScopeIndex();
scopeIndex.put(entryPointName, sc);
return cci;
}
public static EntryPoint createMockEntryPoint(String name) {
EntryPoint entryPoint = new EntryPointImpl() {
};
entryPoint.setName(name);
return entryPoint;
}
public static void addJSONRPCBinding(EntryPoint entryPoint) {
List<Binding> bindings = entryPoint.getBindings();
bindings.add(new JSONRPCBinding());
}
public static void addNonJSONRPCBinding(EntryPoint entryPoint) {
List<Binding> bindings = entryPoint.getBindings();
bindings.add(new MockBinding());
}
}
|