]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.support.castor/src/test/java/org/argeo/slc/castor/AbstractCastorTestCase.java
Fix issue with object lists in Castor
[gpl/argeo-slc.git] / runtime / org.argeo.slc.support.castor / src / test / java / org / argeo / slc / castor / AbstractCastorTestCase.java
1 package org.argeo.slc.castor;
2
3 import org.apache.commons.logging.Log;
4 import org.apache.commons.logging.LogFactory;
5 import org.argeo.slc.unit.AbstractSpringTestCase;
6 import org.argeo.slc.unit.UnitXmlUtils;
7 import org.springframework.oxm.Marshaller;
8 import org.springframework.oxm.Unmarshaller;
9 import org.springframework.xml.transform.StringResult;
10 import org.springframework.xml.transform.StringSource;
11 import org.springframework.xml.validation.XmlValidator;
12
13 public abstract class AbstractCastorTestCase extends AbstractSpringTestCase {
14 protected Log log = LogFactory.getLog(getClass());
15
16 private Marshaller marshaller;
17 private Unmarshaller unmarshaller;
18
19 @Override
20 public void setUp() {
21 marshaller = getBean(Marshaller.class);
22 unmarshaller = getBean(Unmarshaller.class);
23 }
24
25 protected StringResult marshal(Object obj) throws Exception {
26 return marshal(obj, false);
27 }
28
29 protected StringResult marshalAndValidate(Object obj) throws Exception {
30 return marshal(obj, true);
31 }
32
33 protected StringResult marshal(Object obj, boolean validate)
34 throws Exception {
35 StringResult xml = new StringResult();
36 marshaller.marshal(obj, xml);
37
38 log.info("Marshalled ResultPart Request: " + xml);
39
40 if (validate)
41 UnitXmlUtils.assertXmlValidation(getBean(XmlValidator.class),
42 new StringSource(xml.toString()));
43 return xml;
44 }
45
46 @SuppressWarnings("unchecked")
47 protected <T> T unmarshal(StringResult xml) throws Exception {
48 return (T) unmarshaller.unmarshal(new StringSource(xml.toString()));
49 }
50
51 @SuppressWarnings("unchecked")
52 protected <T> T marshUnmarsh(Object obj, boolean validate) throws Exception {
53 StringResult xml = marshal(obj, validate);
54 return (T) unmarshal(xml);
55 }
56
57 @SuppressWarnings("unchecked")
58 protected <T> T marshUnmarsh(Object obj) throws Exception {
59 return (T) marshUnmarsh(obj, true);
60 }
61 }