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
;
21 import org
.springframework
.oxm
.Marshaller
;
22 import org
.springframework
.oxm
.Unmarshaller
;
23 import org
.springframework
.xml
.transform
.StringResult
;
24 import org
.springframework
.xml
.transform
.StringSource
;
25 import org
.springframework
.xml
.validation
.XmlValidator
;
26 import org
.springframework
.xml
.xsd
.XsdSchema
;
28 public class SlcExecutionCastorTest
extends AbstractSpringTestCase
{
29 private Log log
= LogFactory
.getLog(getClass());
31 private Marshaller marshaller
;
32 private Unmarshaller unmarshaller
;
36 marshaller
= getBean(Marshaller
.class);
37 unmarshaller
= getBean(Unmarshaller
.class);
40 public void testMarshalling() throws Exception
{
41 SlcExecution slcExec
= SlcExecutionTestUtils
.createSimpleSlcExecution();
43 SlcExecutionRequest msgSave
= new SlcExecutionRequest();
44 msgSave
.setSlcExecution(slcExec
);
46 String msgSaveXml
= marshallAndLog(marshaller
, msgSave
);
48 SimpleDateFormat sdf
= new SimpleDateFormat("yyyy-MM-dd HH:mm");
49 SlcExecutionStep step0
= new SlcExecutionStep();
50 step0
.setUuid(UUID
.randomUUID().toString());
51 step0
.setBegin(sdf
.parse("2008-04-17 18:21"));
53 step0
.addLog("A log message\nand another line");
55 SlcExecutionStep step1
= new SlcExecutionStep();
56 step1
.setUuid(UUID
.randomUUID().toString());
57 step1
.setBegin(sdf
.parse("2008-04-17 18:25"));
59 step1
.addLog("A nother log message");
61 SlcExecutionStepsRequest msgNotif
= new SlcExecutionStepsRequest();
62 msgNotif
.addStep(step0
);
63 msgNotif
.addStep(step1
);
64 msgNotif
.setSlcExecutionUuid(slcExec
.getUuid());
66 String msgNotifXml
= marshallAndLog(marshaller
, msgNotif
);
68 SlcExecutionRequest msgSaveUnm
= unmarshall(unmarshaller
, msgSaveXml
);
69 assertNotNull(msgSaveUnm
);
70 SlcExecutionTestUtils
.assertSlcExecution(slcExec
, msgSaveUnm
73 SlcExecutionStepsRequest msgNotifUnm
= unmarshall(unmarshaller
,
75 assertNotNull(msgNotifUnm
);
76 assertEquals(slcExec
.getUuid(), msgNotifUnm
.getSlcExecutionUuid());
77 assertEquals(2, msgNotifUnm
.getSteps().size());
78 SlcExecutionTestUtils
.assertSlcExecutionStep(step0
, msgNotifUnm
80 SlcExecutionTestUtils
.assertSlcExecutionStep(step1
, msgNotifUnm
83 SlcExecution slcExecUnm
= msgSaveUnm
.getSlcExecution();
84 slcExecUnm
.getSteps().addAll(msgNotifUnm
.getSteps());
86 SlcExecutionRequest msgUpdate
= new SlcExecutionRequest();
87 msgUpdate
.setSlcExecution(slcExecUnm
);
88 String msgUpdateXml
= marshallAndLog(marshaller
, msgUpdate
);
90 SlcExecutionRequest msgUpdateUnm
= unmarshall(unmarshaller
,
92 assertNotNull(msgUpdateUnm
);
95 private String
marshallAndLog(Marshaller marshaller
, Object obj
)
97 StringResult xml
= new StringResult();
98 marshaller
.marshal(obj
, xml
);
99 log
.info("Marshalled object: " + xml
);
101 UnitXmlUtils
.assertXmlValidation(getBean(XmlValidator
.class),
102 new StringSource(xml
.toString()));
104 return xml
.toString();
107 private <T
> T
unmarshall(Unmarshaller unmarshaller
, String xml
)
109 StringReader reader
= new StringReader(xml
);
110 Object obj
= unmarshaller
.unmarshal(new StreamSource(reader
));
111 IOUtils
.closeQuietly(reader
);