]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.support.castor/src/test/java/org/argeo/slc/castor/AbstractCastorTestCase.java
Update headers
[gpl/argeo-slc.git] / runtime / org.argeo.slc.support.castor / src / test / java / org / argeo / slc / castor / AbstractCastorTestCase.java
1 /*
2 * Copyright (C) 2007-2012 Mathieu Baudier
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package org.argeo.slc.castor;
17
18 import org.apache.commons.logging.Log;
19 import org.apache.commons.logging.LogFactory;
20 import org.argeo.slc.unit.AbstractSpringTestCase;
21 import org.argeo.slc.unit.UnitXmlUtils;
22 import org.springframework.oxm.Marshaller;
23 import org.springframework.oxm.Unmarshaller;
24 import org.springframework.xml.transform.StringResult;
25 import org.springframework.xml.transform.StringSource;
26 import org.springframework.xml.validation.XmlValidator;
27
28 public abstract class AbstractCastorTestCase extends AbstractSpringTestCase {
29 protected Log log = LogFactory.getLog(getClass());
30
31 private Marshaller marshaller;
32 private Unmarshaller unmarshaller;
33
34 @Override
35 public void setUp() {
36 marshaller = getBean(Marshaller.class);
37 unmarshaller = getBean(Unmarshaller.class);
38 }
39
40 protected StringResult marshal(Object obj) throws Exception {
41 return marshal(obj, false);
42 }
43
44 protected StringResult marshalAndValidate(Object obj) throws Exception {
45 return marshal(obj, true);
46 }
47
48 protected StringResult marshal(Object obj, boolean validate)
49 throws Exception {
50 StringResult xml = new StringResult();
51 marshaller.marshal(obj, xml);
52
53 log.info("Marshalled " + obj.getClass() + ": " + xml + "\n");
54
55 if (validate)
56 UnitXmlUtils.assertXmlValidation(getBean(XmlValidator.class),
57 new StringSource(xml.toString()));
58 return xml;
59 }
60
61 @SuppressWarnings("unchecked")
62 protected <T> T unmarshal(StringResult xml) throws Exception {
63 return (T) unmarshaller.unmarshal(new StringSource(xml.toString()));
64 }
65
66 @SuppressWarnings("unchecked")
67 protected <T> T marshUnmarsh(Object obj, boolean validate) throws Exception {
68 StringResult xml = marshal(obj, validate);
69 return (T) unmarshal(xml);
70 }
71
72 @SuppressWarnings("unchecked")
73 protected <T> T marshUnmarsh(Object obj) throws Exception {
74 return (T) marshUnmarsh(obj, true);
75 }
76 }