/
SmooksMapperTest.java
140 lines (123 loc) · 6.61 KB
/
SmooksMapperTest.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
/*
* JBoss, Home of Professional Open Source.
* Copyright 2013, Red Hat, Inc., and individual contributors
* as indicated by the @author tags. See the copyright.txt file in the
* distribution for a full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY 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 along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.jboss.wise.core.mapper;
import static org.hamcrest.core.IsNot.not;
import static org.hamcrest.collection.IsMapContaining.hasEntry;
import static org.hamcrest.core.Is.is;
import static org.hamcrest.core.IsEqual.equalTo;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import java.net.URL;
import java.net.URLClassLoader;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import javax.xml.datatype.XMLGregorianCalendar;
import org.jboss.wise.core.client.WSDynamicClient;
import org.jboss.wise.core.mapper.mappingObject.ExternalObject;
import org.jboss.wise.core.mapper.mappingObject.InternalObject;
import org.junit.Test;
import org.smooks.Smooks;
import org.smooks.api.ExecutionContext;
/**
* @author stefano.maestri@javalinux.it
*/
public class SmooksMapperTest {
public static final String DEFAULT_DATE_FORMAT = "yyyy-MM-dd'T'HH:mm:ss";
@Test
public void shouldMapComplexObjectModel() throws Exception {
WSDynamicClient client = mock(WSDynamicClient.class);
when(client.getSmooksInstance()).thenReturn(new Smooks());
when(client.getClassLoader()).thenReturn(
new URLClassLoader(new URL[] {}, Thread.currentThread().getContextClassLoader()));
WiseMapper mapper = new SmooksMapper("./smooks/smooks-config.xml", "target/smooks-report/report.html", client);
Map<String, Object> originalObjects = new HashMap<String, Object>();
ExternalObject external = new ExternalObject();
InternalObject internal = new InternalObject();
internal.setNumber(Integer.valueOf(1));
internal.setText("fooText");
external.setInternal(internal);
originalObjects.put("external", external);
Map<String, Object> results;
results = mapper.applyMapping(originalObjects);
Integer integerResult = (Integer) results.get("complexObject").getClass().getMethod("getNumberField")
.invoke(results.get("complexObject"));
String stringResult = (String) results.get("complexObject").getClass().getMethod("getTextField")
.invoke(results.get("complexObject"));
assertThat(integerResult, equalTo(internal.getNumber()));
assertThat(stringResult, equalTo("fooText"));
}
@Test
public void shouldMapObjectContainingXMLGregorianCalendarField() throws Exception {
WSDynamicClient client = mock(WSDynamicClient.class);
when(client.getSmooksInstance()).thenReturn(new Smooks());
when(client.getClassLoader()).thenReturn(
new URLClassLoader(new URL[] {}, Thread.currentThread().getContextClassLoader()));
WiseMapper mapper = new SmooksMapper("./smooks/smooks-config-XMLGregorianCalendar.xml", client);
Map<String, Object> originalObjects = new HashMap<String, Object>();
ExternalObject external = new ExternalObject();
String dateString = "2007-03-07T04:27:00";
Date date = (new SimpleDateFormat(DEFAULT_DATE_FORMAT)).parse(dateString);
external.setDate(date);
originalObjects.put("external", external);
Map<String, Object> results;
results = mapper.applyMapping(originalObjects);
long returnedTime = ((XMLGregorianCalendar) results.get("complexObject").getClass().getMethod("getDateField")
.invoke(results.get("complexObject"))).toGregorianCalendar().getTimeInMillis();
assertThat(returnedTime, is(date.getTime()));
}
@Test
public void shouldMapToPrimitiveInput() throws Exception {
WSDynamicClient client = mock(WSDynamicClient.class);
when(client.getSmooksInstance()).thenReturn(new Smooks());
when(client.getClassLoader()).thenReturn(
new URLClassLoader(new URL[] {}, Thread.currentThread().getContextClassLoader()));
WiseMapper mapper = new SmooksMapper("./smooks/smooks-primitive-input.xml", client);
Map<String, Object> originalObjects = new HashMap<String, Object>();
InternalObject internal = new InternalObject();
internal.setNumber(Integer.valueOf(1));
internal.setText("fooText");
originalObjects.put("internal", internal);
Map<String, Object> results;
results = mapper.applyMapping(originalObjects);
assertThat(results, hasEntry("textInput", (Object) "fooText"));
assertThat(results, hasEntry("intInput", (Object) Integer.valueOf(1)));
}
@Test
public void shouldHaveDifferentContentDeliveryConfigurationPerxecutionContext() {
WSDynamicClient client = mock(WSDynamicClient.class);
when(client.getSmooksInstance()).thenReturn(new Smooks());
when(client.getClassLoader()).thenReturn(
new URLClassLoader(new URL[] {}, Thread.currentThread().getContextClassLoader()));
SmooksMapper mapper = new SmooksMapper("./smooks/smooks-primitive-input.xml", client);
SmooksMapper mapper2 = new SmooksMapper("./smooks/smooks-config-XMLGregorianCalendar.xml", client);
SmooksMapper mapper3 = new SmooksMapper("./smooks/smooks-config.xml", client);
ExecutionContext context = mapper.initExecutionContext(null);
ExecutionContext context2 = mapper2.initExecutionContext(null);
ExecutionContext context3 = mapper3.initExecutionContext(null);
assertThat(context.getContentDeliveryRuntime(), not(is(context2.getContentDeliveryRuntime())));
assertThat(context.getContentDeliveryRuntime(), not(is(context3.getContentDeliveryRuntime())));
assertThat(context2.getContentDeliveryRuntime(), not(is(context3.getContentDeliveryRuntime())));
}
}