-
Notifications
You must be signed in to change notification settings - Fork 57
/
MockContext.java
191 lines (170 loc) · 5.25 KB
/
MockContext.java
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
/*
* JBoss, Home of Professional Open Source
* Copyright 2011 Red Hat Inc. and/or its affiliates and other contributors
* as indicated by the @author tags. All rights reserved.
* See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* This copyrighted material is made available to anyone wishing to use,
* modify, copy, or redistribute it subject to the terms and conditions
* of the GNU Lesser General Public License, v. 2.1.
* This program is distributed in the hope that it will be useful, but WITHOUT A
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General Public License,
* v.2.1 along with this distribution; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*/
package org.switchyard;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
/**
* MockContext.
*
* @author David Ward <<a href="mailto:dward@jboss.org">dward@jboss.org</a>> (C) 2011 Red Hat Inc.
*/
public class MockContext implements Context {
private final Map<String,Property> _exchangeProperties = Collections.synchronizedMap(new HashMap<String,Property>());
private final Map<String,Property> _inProperties = Collections.synchronizedMap(new HashMap<String,Property>());
private final Map<String,Property> _outProperties = Collections.synchronizedMap(new HashMap<String,Property>());
public MockContext() {}
private MockContext(Map<String, Property> exchangeProps,
Map<String, Property> inProps,
Map<String, Property> outProps) {
_exchangeProperties.putAll(exchangeProps);
_inProperties.putAll(inProps);
_outProperties.putAll(outProps);
}
private Map<String,Property> getPropertiesMap(Scope scope) {
switch (scope) {
case IN:
return _inProperties;
case OUT:
return _outProperties;
case EXCHANGE:
default:
return _exchangeProperties;
}
}
/**
* {@inheritDoc}
*/
@Override
public Property getProperty(String name) {
return getProperty(name, Scope.EXCHANGE);
}
/**
* {@inheritDoc}
*/
@Override
public Property getProperty(String name, Scope scope) {
return getPropertiesMap(scope).get(name);
}
/**
* {@inheritDoc}
*/
@Override
public Object getPropertyValue(String name) {
Property property = getProperty(name);
return property != null ? property.getValue() : null;
}
/**
* {@inheritDoc}
*/
@Override
public Set<Property> getProperties() {
return getProperties(Scope.EXCHANGE);
}
/**
* {@inheritDoc}
*/
@Override
public Set<Property> getProperties(Scope scope) {
Set<Property> properties = new HashSet<Property>();
for (Property property : getPropertiesMap(scope).values()) {
properties.add(property);
}
return properties;
}
/**
* {@inheritDoc}
*/
@Override
public void removeProperty(Property property) {
getPropertiesMap(Scope.EXCHANGE).remove(property.getName());
}
/**
* {@inheritDoc}
*/
@Override
public void removeProperties() {
removeProperties(Scope.EXCHANGE);
}
/**
* {@inheritDoc}
*/
@Override
public void removeProperties(Scope scope) {
getPropertiesMap(scope).clear();
}
/**
* {@inheritDoc}
*/
@Override
public Property setProperty(String name, Object val) {
return setProperty(name, val, Scope.EXCHANGE);
}
/**
* {@inheritDoc}
*/
@Override
public Property setProperty(String name, Object val, Scope scope) {
Map<String,Property> properties = getPropertiesMap(scope);
if (val != null) {
Property p = new MockProperty(name, val, scope);
properties.put(name, p);
return p;
} else {
properties.remove(name);
return null;
}
}
/**
* {@inheritDoc}
*/
@Override
public Context setProperties(Set<Property> properties) {
for (Property property : properties) {
setProperty(property.getName(), property.getValue(), property.getScope());
}
return this;
}
@Override
public Context copy() {
MockContext ctx = new MockContext(_exchangeProperties, _inProperties, _outProperties);
ctx.removeProperties(Labels.TRANSIENT);
return ctx;
}
@Override
public Set<Property> getProperties(String label) {
Set<Property> props = new HashSet<Property>();
for (Property p : getProperties()) {
if (p.hasLabel(label)) {
props.add(p);
}
}
return props;
}
@Override
public void removeProperties(String label) {
for (Property p : getProperties()) {
if (p.hasLabel(label)) {
removeProperty(p);
}
}
}
}