--- /dev/null
+package org.argeo.slc.lib.detached;
+
+import java.io.IOException;
+
+import javax.xml.transform.Result;
+import javax.xml.transform.Source;
+
+import org.argeo.slc.core.SlcException;
+import org.argeo.slc.detached.DetachedAnswer;
+import org.argeo.slc.detached.DetachedRequest;
+import org.argeo.slc.detached.DetachedXmlConverter;
+import org.springframework.oxm.Marshaller;
+import org.springframework.oxm.Unmarshaller;
+import org.springframework.oxm.XmlMappingException;
+import org.springframework.xml.validation.XmlValidator;
+import org.xml.sax.InputSource;
+
+public class DetachedXmlConverterSpring implements DetachedXmlConverter {
+ private Marshaller marshaller;
+ private Unmarshaller unmarshaller;
+
+ public void marshallRequest(DetachedRequest request, Result result) {
+ try {
+ marshaller.marshal(request, result);
+ } catch (Exception e) {
+ throw new SlcException("Could not marshall", e);
+ }
+ }
+
+ public void marshallAnswer(DetachedAnswer answer, Result result) {
+ try {
+ marshaller.marshal(answer, result);
+ } catch (Exception e) {
+ throw new SlcException("Could not marshall", e);
+ }
+ }
+
+ public DetachedAnswer unmarshallAnswer(Source source) {
+ try {
+ return (DetachedAnswer) unmarshaller.unmarshal(source);
+ } catch (Exception e) {
+ throw new SlcException("Could not unmarshall", e);
+ }
+ }
+
+ public DetachedRequest unmarshallRequest(Source source) {
+ try {
+ return (DetachedRequest) unmarshaller.unmarshal(source);
+ } catch (Exception e) {
+ throw new SlcException("Could not unmarshall", e);
+ }
+ }
+
+ public void setMarshaller(Marshaller marshaller) {
+ this.marshaller = marshaller;
+ }
+
+ public void setUnmarshaller(Unmarshaller unmarshaller) {
+ this.unmarshaller = unmarshaller;
+ }
+
+}
--- /dev/null
+<?xml version="1.0"?>
+<!DOCTYPE doc [
+<!ENTITY slc-detached-namespace-uri "http://argeo.org/projects/slc/schemas/detached">
+<!ENTITY slc-detached-namespace-prefix "slc-det">
+]>
+
+<mapping>
+ <description>Mapping of SLC detached types</description>
+
+ <class name="org.argeo.slc.detached.DetachedRequest">
+ <map-to ns-uri="&slc-detached-namespace-uri;"
+ ns-prefix="&slc-detached-namespace-prefix;" />
+ <field name="uuid" />
+ <field name="ref" />
+ <field name="path" />
+ <field name="properties" collection="map">
+ <bind-xml name="prop" location="properties">
+ <class name="org.exolab.castor.mapping.MapItem">
+ <map-to ns-uri="&slc-detached-namespace-uri;"
+ ns-prefix="&slc-detached-namespace-prefix;" />
+ <field name="key" type="string">
+ <bind-xml name="key" node="attribute" />
+ </field>
+ <field name="value" type="string">
+ <bind-xml node="text" />
+ </field>
+ </class>
+ </bind-xml>
+ </field>
+ </class>
+
+ <class name="org.argeo.slc.detached.DetachedAnswer">
+ <map-to ns-uri="&slc-detached-namespace-uri;"
+ ns-prefix="&slc-detached-namespace-prefix;" />
+ <field name="uuid" />
+ <field name="status" />
+ <field name="log" />
+ <field name="properties" collection="map">
+ <bind-xml name="prop" location="properties">
+ <class name="org.exolab.castor.mapping.MapItem">
+ <map-to ns-uri="&slc-detached-namespace-uri;"
+ ns-prefix="&slc-detached-namespace-prefix;" />
+ <field name="key" type="string">
+ <bind-xml name="key" node="attribute" />
+ </field>
+ <field name="value" type="string">
+ <bind-xml node="text" />
+ </field>
+ </class>
+ </bind-xml>
+ </field>
+ </class>
+</mapping>
\ No newline at end of file
--- /dev/null
+package org.argeo.slc.lib.detached;
+
+import java.util.Enumeration;
+import java.util.Properties;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.argeo.slc.detached.DetachedAnswer;
+import org.argeo.slc.detached.DetachedRequest;
+import org.argeo.slc.detached.DetachedXmlConverter;
+import org.argeo.slc.detached.DetachedXmlConverterCompat;
+import org.argeo.slc.unit.AbstractSpringTestCase;
+import org.argeo.slc.unit.UnitXmlUtils;
+import org.springframework.xml.transform.StringResult;
+import org.springframework.xml.transform.StringSource;
+import org.springframework.xml.validation.XmlValidator;
+
+public class DetachedCastorTest extends AbstractSpringTestCase {
+ private final Log log = LogFactory.getLog(getClass());
+
+ private DetachedXmlConverter converter = null;
+ private XmlValidator validator = null;
+ private DetachedXmlConverter converterCompat = null;
+
+ public void setUp() {
+ converter = getBean("slcDefault.detached.xmlConverterCastor");
+ validator = getBean("slcDefault.detached.xml.xsdValidator");
+ converterCompat = new DetachedXmlConverterCompat();
+ }
+
+ public void testRequest() throws Exception {
+ DetachedRequest request = createTestRequest();
+
+ StringResult result = new StringResult();
+ converter.marshallRequest(request, result);
+ String xml = result.toString();
+ log.debug(xml + "\n");
+
+ UnitXmlUtils.assertXmlValidation(validator, new StringSource(xml));
+ DetachedRequest requestUnm = converter
+ .unmarshallRequest(new StringSource(xml));
+ assertDetachedRequest(request, requestUnm);
+ }
+
+ public void testRequestCompat() throws Exception {
+ DetachedRequest request = createTestRequest();
+
+ StringResult result = new StringResult();
+ converter.marshallRequest(request, result);
+ String xml = result.toString();
+ log.debug(xml + "\n");
+
+ UnitXmlUtils.assertXmlValidation(validator, new StringSource(xml));
+ DetachedRequest requestUnm = converterCompat
+ .unmarshallRequest(new StringSource(xml));
+ assertDetachedRequest(request, requestUnm);
+ }
+
+ public void testAnswer() throws Exception {
+ DetachedAnswer answer = createTestAnswer();
+ StringResult result = new StringResult();
+ converter.marshallAnswer(answer, result);
+ String xml = result.toString();
+ log.debug(xml + "\n");
+
+ UnitXmlUtils.assertXmlValidation(validator, new StringSource(xml));
+ DetachedAnswer answerUnm = converter.unmarshallAnswer(new StringSource(
+ xml));
+ assertDetachedAnswer(answer, answerUnm);
+ }
+
+ public void testAnswerCompat() throws Exception {
+ DetachedAnswer answer = createTestAnswer();
+ StringResult result = new StringResult();
+ converterCompat.marshallAnswer(answer, result);
+ String xml = result.toString();
+ log.debug(xml + "\n");
+
+ UnitXmlUtils.assertXmlValidation(validator, new StringSource(xml));
+ DetachedAnswer answerUnm = converter.unmarshallAnswer(new StringSource(
+ xml));
+ assertDetachedAnswer(answer, answerUnm);
+ }
+
+ public static DetachedRequest createTestRequest() {
+ DetachedRequest request = new DetachedRequest();
+ request.setUuid("12345");
+ request.setPath("/root/test");
+ request.setRef("testRef");
+ Properties properties = new Properties();
+ properties.setProperty("key1", "value1");
+ properties.setProperty("key2", "value2");
+ request.setProperties(properties);
+ return request;
+ }
+
+ public static DetachedAnswer createTestAnswer() {
+ DetachedAnswer answer = new DetachedAnswer();
+ answer.setUuid("12345");
+ answer.setStatus(DetachedAnswer.PROCESSED);
+ answer.setLog("my log\nline break.");
+ Properties properties = new Properties();
+ properties.setProperty("key1", "value1");
+ properties.setProperty("key2", "value2");
+ answer.setProperties(properties);
+ return answer;
+ }
+
+ public static void assertDetachedRequest(DetachedRequest expected,
+ DetachedRequest reached) {
+ assertEquals(expected.getUuid(), reached.getUuid());
+ assertEquals(expected.getPath(), reached.getPath());
+ assertEquals(expected.getRef(), reached.getRef());
+ Properties expectedProps = expected.getProperties();
+ Properties reachedProps = reached.getProperties();
+ assertEquals(expectedProps.size(), reachedProps.size());
+ Enumeration keys = expectedProps.keys();
+ while (keys.hasMoreElements()) {
+ String key = (String) keys.nextElement();
+ assertTrue(reachedProps.containsKey(key));
+ assertEquals(expectedProps.getProperty(key), reachedProps
+ .getProperty(key));
+ }
+ }
+
+ public static void assertDetachedAnswer(DetachedAnswer expected,
+ DetachedAnswer reached) {
+ assertEquals(expected.getUuid(), reached.getUuid());
+ assertEquals(expected.getStatus(), reached.getStatus());
+ assertEquals(expected.getLog(), reached.getLog());
+ Properties expectedProps = expected.getProperties();
+ Properties reachedProps = reached.getProperties();
+ assertEquals(expectedProps.size(), reachedProps.size());
+ Enumeration keys = expectedProps.keys();
+ while (keys.hasMoreElements()) {
+ String key = (String) keys.nextElement();
+ assertTrue(reachedProps.containsKey(key));
+ assertEquals(expectedProps.getProperty(key), reachedProps
+ .getProperty(key));
+ }
+ }
+
+ private void validate() {
+
+ }
+
+ public static void main(String[] args) {
+ try {
+ new DetachedCastorTest().testRequest();
+ } catch (Exception e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ }
+
+}