2 * Copyright (C) 2007-2012 Mathieu Baudier
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org
.argeo
.slc
.castor
;
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
;
28 public abstract class AbstractCastorTestCase
extends AbstractSpringTestCase
{
29 protected Log log
= LogFactory
.getLog(getClass());
31 private Marshaller marshaller
;
32 private Unmarshaller unmarshaller
;
36 marshaller
= getBean(Marshaller
.class);
37 unmarshaller
= getBean(Unmarshaller
.class);
40 protected StringResult
marshal(Object obj
) throws Exception
{
41 return marshal(obj
, false);
44 protected StringResult
marshalAndValidate(Object obj
) throws Exception
{
45 return marshal(obj
, true);
48 protected StringResult
marshal(Object obj
, boolean validate
)
50 StringResult xml
= new StringResult();
51 marshaller
.marshal(obj
, xml
);
53 log
.info("Marshalled " + obj
.getClass() + ": " + xml
+ "\n");
56 UnitXmlUtils
.assertXmlValidation(getBean(XmlValidator
.class),
57 new StringSource(xml
.toString()));
61 @SuppressWarnings("unchecked")
62 protected <T
> T
unmarshal(StringResult xml
) throws Exception
{
63 return (T
) unmarshaller
.unmarshal(new StringSource(xml
.toString()));
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
);
72 @SuppressWarnings("unchecked")
73 protected <T
> T
marshUnmarsh(Object obj
) throws Exception
{
74 return (T
) marshUnmarsh(obj
, true);