forked from citrusframework/citrus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TypeConversionUtils.java
234 lines (209 loc) · 9.55 KB
/
TypeConversionUtils.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
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
/*
* Copyright 2006-2016 the original author or authors.
*
* 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 com.consol.citrus.util;
import com.consol.citrus.Citrus;
import com.consol.citrus.context.TestContext;
import com.consol.citrus.exceptions.CitrusRuntimeException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.ConversionNotSupportedException;
import org.springframework.beans.SimpleTypeConverter;
import org.springframework.core.io.InputStreamSource;
import org.springframework.util.*;
import org.springframework.xml.transform.StringSource;
import org.w3c.dom.Node;
import javax.xml.transform.Source;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamSource;
import java.io.*;
import java.nio.ByteBuffer;
import java.util.*;
/**
* @author Christoph Deppisch
* @since 2.5
*/
public abstract class TypeConversionUtils {
/** Logger */
private static Logger log = LoggerFactory.getLogger(TypeConversionUtils.class);
/**
* Prevent instantiation.
*/
private TypeConversionUtils() {
super();
}
/**
* Converts target object to required type if necessary.
*
* @param target
* @param type
* @param <T>
* @return
*/
public static <T> T convertIfNecessary(Object target, Class<T> type) {
if (type.isInstance(target)) {
return type.cast(target);
}
if (Source.class.isAssignableFrom(type)) {
if (target.getClass().isAssignableFrom(String.class)) {
return (T) new StringSource(String.valueOf(target));
} else if (target.getClass().isAssignableFrom(Node.class)) {
return (T) new DOMSource((Node) target);
} else if (target.getClass().isAssignableFrom(InputStreamSource.class)) {
try {
return (T) new StreamSource(((InputStreamSource)target).getInputStream());
} catch (IOException e) {
log.warn("Failed to create stream source from object", e);
}
}
}
if (MultiValueMap.class.isAssignableFrom(type)) {
String mapString = String.valueOf(target);
Properties props = new Properties();
try {
props.load(new StringReader(mapString.substring(1, mapString.length() - 1).replaceAll("\\]\\s*", "]\n")));
} catch (IOException e) {
throw new CitrusRuntimeException("Failed to reconstruct object of type map", e);
}
MultiValueMap<String, Object> map = new LinkedMultiValueMap<>();
for (Map.Entry<Object, Object> entry : props.entrySet()) {
String arrayString = String.valueOf(entry.getValue()).replaceAll("^\\[", "").replaceAll("\\]$", "").replaceAll(",\\s", ",");
map.add(entry.getKey().toString(), StringUtils.commaDelimitedListToStringArray(String.valueOf(arrayString)));
}
return (T) map;
}
if (Map.class.isAssignableFrom(type)) {
String mapString = String.valueOf(target);
Properties props = new Properties();
try {
props.load(new StringReader(mapString.substring(1, mapString.length() - 1).replaceAll(",\\s*", "\n")));
} catch (IOException e) {
throw new CitrusRuntimeException("Failed to reconstruct object of type map", e);
}
Map<String, Object> map = new LinkedHashMap<>();
for (Map.Entry<Object, Object> entry : props.entrySet()) {
map.put(entry.getKey().toString(), entry.getValue());
}
return (T) map;
}
if (String[].class.isAssignableFrom(type)) {
String arrayString = String.valueOf(target).replaceAll("^\\[", "").replaceAll("\\]$", "").replaceAll(",\\s", ",");
return (T) StringUtils.commaDelimitedListToStringArray(String.valueOf(arrayString));
}
if (List.class.isAssignableFrom(type)) {
String listString = String.valueOf(target).replaceAll("^\\[", "").replaceAll("\\]$", "").replaceAll(",\\s", ",");
return (T) Arrays.asList(StringUtils.commaDelimitedListToStringArray(String.valueOf(listString)));
}
if (byte[].class.isAssignableFrom(type)) {
if (target instanceof String) {
try {
return (T) String.valueOf(target).getBytes(Citrus.CITRUS_FILE_ENCODING);
} catch (UnsupportedEncodingException e) {
return (T) String.valueOf(target).getBytes();
}
} else if (target instanceof ByteBuffer) {
return (T) ((ByteBuffer) target).array();
} else if (target instanceof ByteArrayInputStream) {
try {
return (T) StreamUtils.copyToByteArray((ByteArrayInputStream) target);
} catch (IOException e) {
throw new CitrusRuntimeException("Failed to convert input stream to byte[]");
}
}
}
if (InputStream.class.isAssignableFrom(type)) {
if (target instanceof InputStream) {
return (T) target;
} else if (target instanceof byte[]) {
return (T) new ByteArrayInputStream((byte[]) target);
} else if (target instanceof String) {
try {
return (T) new ByteArrayInputStream(String.valueOf(target).getBytes(Citrus.CITRUS_FILE_ENCODING));
} catch (UnsupportedEncodingException e) {
return (T) new ByteArrayInputStream(String.valueOf(target).getBytes());
}
} else {
try {
return (T) new ByteArrayInputStream(target.toString().getBytes(Citrus.CITRUS_FILE_ENCODING));
} catch (UnsupportedEncodingException e) {
return (T) new ByteArrayInputStream(target.toString().getBytes());
}
}
}
if (type.equals(String.class)) {
if (ByteBuffer.class.isAssignableFrom(target.getClass())) {
return (T) new String(((ByteBuffer) target).array());
} else if (byte[].class.isAssignableFrom(target.getClass())) {
return (T) new String((byte[]) target);
}
}
try {
return new SimpleTypeConverter().convertIfNecessary(target, type);
} catch (ConversionNotSupportedException e) {
if (String.class.equals(type)) {
log.warn(String.format("Using object toString representation: %s", e.getMessage()));
return (T) target.toString();
}
throw e;
}
}
/**
* Convert value string to required type.
* @param value
* @param type
* @return
*/
public static <T> T convertStringToType(String value, Class<T> type) {
if (type.isAssignableFrom(String.class)) {
return (T) value;
} else if (type.isAssignableFrom(int.class) || type.isAssignableFrom(Integer.class)) {
return (T) Integer.valueOf(value);
} else if (type.isAssignableFrom(short.class) || type.isAssignableFrom(Short.class)) {
return (T) Short.valueOf(value);
} else if (type.isAssignableFrom(byte.class) || type.isAssignableFrom(Byte.class)) {
return (T) Byte.valueOf(value);
} else if (type.isAssignableFrom(long.class) || type.isAssignableFrom(Long.class)) {
return (T) Long.valueOf(value);
} else if (type.isAssignableFrom(boolean.class) || type.isAssignableFrom(Boolean.class)) {
return (T) Boolean.valueOf(value);
} else if (type.isAssignableFrom(float.class) || type.isAssignableFrom(Float.class)) {
return (T) Float.valueOf(value);
} else if (type.isAssignableFrom(double.class) || type.isAssignableFrom(Double.class)) {
return (T) Double.valueOf(value);
}
throw new CitrusRuntimeException(String.format("Unable to convert '%s' to required type '%s'", value, type.getName()));
}
/**
* Convert value string to required type or read bean of type from application context.
* @param value
* @param type
* @param context
* @return
*/
public static <T> T convertStringToType(String value, Class<T> type, TestContext context) {
try {
return convertStringToType(value, type);
} catch (CitrusRuntimeException e) {
// try to resolve bean in application context
if (context.getApplicationContext() != null && context.getApplicationContext().containsBean(value)) {
Object bean = context.getApplicationContext().getBean(value);
if (type.isAssignableFrom(bean.getClass())) {
return (T) bean;
}
}
throw new CitrusRuntimeException(String.format("Unable to convert '%s' to required type '%s' - also no bean of required type available in application context", value, type.getName()), e.getCause());
}
}
}