1 package org
.argeo
.slc
.core
.process
;
3 import java
.io
.IOException
;
4 import java
.io
.InputStream
;
5 import java
.io
.StringReader
;
6 import java
.io
.StringWriter
;
7 import java
.text
.SimpleDateFormat
;
10 import javax
.xml
.transform
.stream
.StreamResult
;
11 import javax
.xml
.transform
.stream
.StreamSource
;
13 import org
.apache
.commons
.io
.IOUtils
;
14 import org
.apache
.commons
.logging
.Log
;
15 import org
.apache
.commons
.logging
.LogFactory
;
16 import org
.argeo
.slc
.msg
.process
.SlcExecutionRequest
;
17 import org
.argeo
.slc
.msg
.process
.SlcExecutionStepsRequest
;
18 import org
.argeo
.slc
.unit
.AbstractSpringTestCase
;
19 import org
.argeo
.slc
.unit
.UnitXmlUtils
;
20 import org
.argeo
.slc
.unit
.process
.SlcExecutionTestUtils
;
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 import org
.springframework
.xml
.xsd
.XsdSchema
;
29 public class SlcExecutionCastorTest
extends AbstractSpringTestCase
{
30 private Log log
= LogFactory
.getLog(getClass());
32 private Marshaller marshaller
;
33 private Unmarshaller unmarshaller
;
37 marshaller
= getBean(Marshaller
.class);
38 unmarshaller
= getBean(Unmarshaller
.class);
41 public void testMarshalling() throws Exception
{
42 SlcExecution slcExec
= SlcExecutionTestUtils
.createSimpleSlcExecution();
44 SlcExecutionRequest msgSave
= new SlcExecutionRequest();
45 msgSave
.setSlcExecution(slcExec
);
47 String msgSaveXml
= marshallAndLog(marshaller
, msgSave
);
49 SimpleDateFormat sdf
= new SimpleDateFormat("yyyy-MM-dd HH:mm");
50 SlcExecutionStep step0
= new SlcExecutionStep();
51 step0
.setUuid(UUID
.randomUUID().toString());
52 step0
.setBegin(sdf
.parse("2008-04-17 18:21"));
54 step0
.addLog("A log message\nand another line");
56 SlcExecutionStep step1
= new SlcExecutionStep();
57 step1
.setUuid(UUID
.randomUUID().toString());
58 step1
.setBegin(sdf
.parse("2008-04-17 18:25"));
60 step1
.addLog("A nother log message");
62 SlcExecutionStepsRequest msgNotif
= new SlcExecutionStepsRequest();
63 msgNotif
.addStep(step0
);
64 msgNotif
.addStep(step1
);
65 msgNotif
.setSlcExecutionUuid(slcExec
.getUuid());
67 String msgNotifXml
= marshallAndLog(marshaller
, msgNotif
);
69 SlcExecutionRequest msgSaveUnm
= unmarshall(unmarshaller
, msgSaveXml
);
70 assertNotNull(msgSaveUnm
);
71 SlcExecutionTestUtils
.assertSlcExecution(slcExec
, msgSaveUnm
74 SlcExecutionStepsRequest msgNotifUnm
= unmarshall(unmarshaller
,
76 assertNotNull(msgNotifUnm
);
77 assertEquals(slcExec
.getUuid(), msgNotifUnm
.getSlcExecutionUuid());
78 assertEquals(2, msgNotifUnm
.getSteps().size());
79 SlcExecutionTestUtils
.assertSlcExecutionStep(step0
, msgNotifUnm
81 SlcExecutionTestUtils
.assertSlcExecutionStep(step1
, msgNotifUnm
84 SlcExecution slcExecUnm
= msgSaveUnm
.getSlcExecution();
85 slcExecUnm
.getSteps().addAll(msgNotifUnm
.getSteps());
87 SlcExecutionRequest msgUpdate
= new SlcExecutionRequest();
88 msgUpdate
.setSlcExecution(slcExecUnm
);
89 String msgUpdateXml
= marshallAndLog(marshaller
, msgUpdate
);
91 SlcExecutionRequest msgUpdateUnm
= unmarshall(unmarshaller
,
93 assertNotNull(msgUpdateUnm
);
96 private String
marshallAndLog(Marshaller marshaller
, Object obj
)
98 StringResult xml
= new StringResult();
99 marshaller
.marshal(obj
, xml
);
100 log
.info("Marshalled object: " + xml
);
102 UnitXmlUtils
.assertXmlValidation(getBean(XmlValidator
.class),
103 new StringSource(xml
.toString()));
105 return xml
.toString();
108 private <T
> T
unmarshall(Unmarshaller unmarshaller
, String xml
)
110 StringReader reader
= new StringReader(xml
);
111 Object obj
= unmarshaller
.unmarshal(new StreamSource(reader
));
112 IOUtils
.closeQuietly(reader
);