-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
Jaxb2NamespaceMarshallingTests.java
144 lines (118 loc) · 4.58 KB
/
Jaxb2NamespaceMarshallingTests.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
/*
* Copyright 2010-2020 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
*
* https://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.springframework.batch.item.xml;
import java.io.File;
import java.io.StringWriter;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import javax.xml.transform.stream.StreamResult;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.xmlunit.builder.Input;
import org.xmlunit.matchers.CompareMatcher;
import org.springframework.batch.item.ExecutionContext;
import org.springframework.batch.item.xml.domain.QualifiedTrade;
import org.springframework.batch.support.transaction.ResourcelessTransactionManager;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.oxm.Marshaller;
import org.springframework.oxm.jaxb.Jaxb2Marshaller;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallback;
import org.springframework.transaction.support.TransactionTemplate;
import org.springframework.util.ClassUtils;
import org.springframework.util.StopWatch;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertTrue;
public class Jaxb2NamespaceMarshallingTests {
private Log logger = LogFactory.getLog(getClass());
private static final int MAX_WRITE = 100;
private StaxEventItemWriter<QualifiedTrade> writer = new StaxEventItemWriter<>();
private Resource resource;
private File outputFile;
private Resource expected = new ClassPathResource("expected-qualified-output.xml", getClass());
@SuppressWarnings("serial")
private List<QualifiedTrade> objects = new ArrayList<QualifiedTrade>() {
{
add(new QualifiedTrade("isin1", 1, new BigDecimal(1.0), "customer1"));
add(new QualifiedTrade("isin2", 2, new BigDecimal(2.0), "customer2"));
add(new QualifiedTrade("isin3", 3, new BigDecimal(3.0), "customer3"));
}
};
/**
* Write list of domain objects and check the output file.
*/
@SuppressWarnings("resource")
@Test
public void testWrite() throws Exception {
StopWatch stopWatch = new StopWatch(getClass().getSimpleName());
stopWatch.start();
for (int i = 0; i < MAX_WRITE; i++) {
new TransactionTemplate(new ResourcelessTransactionManager()).execute(new TransactionCallback<Void>() {
@Override
public Void doInTransaction(TransactionStatus status) {
try {
writer.write(objects);
}
catch (RuntimeException e) {
throw e;
}
catch (Exception e) {
throw new IllegalStateException("Exception encountered on write", e);
}
return null;
}
});
}
writer.close();
stopWatch.stop();
logger.info("Timing for XML writer: " + stopWatch);
assertThat(
Input.from(expected.getFile()),
CompareMatcher.isSimilarTo(Input.from(resource.getFile())).normalizeWhitespace());
}
@Before
public void setUp() throws Exception {
File directory = new File("target/data");
directory.mkdirs();
outputFile = File.createTempFile(ClassUtils.getShortName(this.getClass()), ".xml", directory);
resource = new FileSystemResource(outputFile);
writer.setResource(resource);
writer.setMarshaller(getMarshaller());
writer.setRootTagName("{urn:org.springframework.batch.io.oxm.domain}trades");
writer.afterPropertiesSet();
writer.open(new ExecutionContext());
}
@After
public void tearDown() throws Exception {
outputFile.delete();
}
protected Marshaller getMarshaller() throws Exception {
Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
marshaller.setClassesToBeBound(new Class<?>[] { QualifiedTrade.class });
marshaller.afterPropertiesSet();
StringWriter string = new StringWriter();
marshaller.marshal(new QualifiedTrade("FOO", 100, BigDecimal.valueOf(10.), "bar"), new StreamResult(string));
String content = string.toString();
assertTrue("Wrong content: "+content, content.contains("<customer>bar</customer>"));
return marshaller;
}
}