]> git.argeo.org Git - gpl/argeo-slc.git/commitdiff
Introduce XML un/marshallign for SLC Detached
authorMathieu Baudier <mbaudier@argeo.org>
Sun, 5 Oct 2008 12:33:00 +0000 (12:33 +0000)
committerMathieu Baudier <mbaudier@argeo.org>
Sun, 5 Oct 2008 12:33:00 +0000 (12:33 +0000)
git-svn-id: https://svn.argeo.org/slc/trunk@1696 4cfe0d0a-d680-48aa-b62c-e0a02a3f76cc

org.argeo.slc.agent/.classpath
org.argeo.slc.agent/src/main/java/org/argeo/slc/lib/detached/DetachedXmlConverterSpring.java [new file with mode: 0644]
org.argeo.slc.agent/src/main/resources/org/argeo/slc/lib/detached/castor.xml [new file with mode: 0644]
org.argeo.slc.agent/src/main/resources/org/argeo/slc/lib/detached/spring.xml [new file with mode: 0644]
org.argeo.slc.agent/src/test/java/org/argeo/slc/lib/detached/DetachedCastorTest.java [new file with mode: 0644]
org.argeo.slc.agent/src/test/resources/org/argeo/slc/lib/detached/applicationContext.xml [new file with mode: 0644]

index 372e5299edbec0d603ca69cccc6ae0c8894b433e..7dc80e91b8c60e97306a2b3b1a48762a364cecfb 100644 (file)
@@ -6,5 +6,5 @@
        <classpathentry kind="src" path="src/test/resources"/>
        <classpathentry kind="con" path="org.maven.ide.eclipse.MAVEN2_CLASSPATH_CONTAINER"/>
        <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/J2SE-1.5"/>
-       <classpathentry kind="output" path="bin"/>
+       <classpathentry kind="output" path="target/classes"/>
 </classpath>
diff --git a/org.argeo.slc.agent/src/main/java/org/argeo/slc/lib/detached/DetachedXmlConverterSpring.java b/org.argeo.slc.agent/src/main/java/org/argeo/slc/lib/detached/DetachedXmlConverterSpring.java
new file mode 100644 (file)
index 0000000..c3e9e30
--- /dev/null
@@ -0,0 +1,62 @@
+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;
+       }
+
+}
diff --git a/org.argeo.slc.agent/src/main/resources/org/argeo/slc/lib/detached/castor.xml b/org.argeo.slc.agent/src/main/resources/org/argeo/slc/lib/detached/castor.xml
new file mode 100644 (file)
index 0000000..ce8eb43
--- /dev/null
@@ -0,0 +1,53 @@
+<?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
diff --git a/org.argeo.slc.agent/src/main/resources/org/argeo/slc/lib/detached/spring.xml b/org.argeo.slc.agent/src/main/resources/org/argeo/slc/lib/detached/spring.xml
new file mode 100644 (file)
index 0000000..2e259da
--- /dev/null
@@ -0,0 +1,34 @@
+<beans xmlns="http://www.springframework.org/schema/beans"
+    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"
+    default-lazy-init="true">
+
+    <bean id="slcDefault.detached.xmlConverterCastor"
+        class="org.argeo.slc.lib.detached.DetachedXmlConverterSpring">
+        <property name="marshaller" ref="slcDefault.detached.castor.marshaller"/>
+        <property name="unmarshaller" ref="slcDefault.detached.castor.marshaller"/>
+    </bean>
+
+    <bean id="slcDefault.detached.castor.marshaller"
+        class="org.springframework.oxm.castor.CastorMarshaller">
+        <property name="mappingLocations">
+            <list>
+                <value>
+                    classpath:org/argeo/slc/lib/detached/castor.xml
+                </value>
+            </list>
+        </property>
+        <property name="whitespacePreserve" value="true" />
+    </bean>
+
+    <bean id="slcDefault.detached.xml.xsdValidator"
+        factory-bean="slcDefault.detached.xml.xsdSchema"
+        factory-method="createValidator" />
+
+    <bean id="slcDefault.detached.xml.xsdSchema"
+        class="org.springframework.xml.xsd.SimpleXsdSchema">
+        <property name="xsd"
+            value="classpath:/org/argeo/slc/detached/slc-detached.xsd" />
+    </bean>
+
+</beans>
diff --git a/org.argeo.slc.agent/src/test/java/org/argeo/slc/lib/detached/DetachedCastorTest.java b/org.argeo.slc.agent/src/test/java/org/argeo/slc/lib/detached/DetachedCastorTest.java
new file mode 100644 (file)
index 0000000..b178c82
--- /dev/null
@@ -0,0 +1,156 @@
+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();
+               }
+       }
+
+}
diff --git a/org.argeo.slc.agent/src/test/resources/org/argeo/slc/lib/detached/applicationContext.xml b/org.argeo.slc.agent/src/test/resources/org/argeo/slc/lib/detached/applicationContext.xml
new file mode 100644 (file)
index 0000000..88d6483
--- /dev/null
@@ -0,0 +1,7 @@
+<beans xmlns="http://www.springframework.org/schema/beans"
+    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
+
+    <import resource="classpath:/org/argeo/slc/lib/detached/spring.xml" />
+
+</beans>