1 package org
.argeo
.slc
.core
.process
;
3 import java
.io
.IOException
;
4 import java
.io
.StringReader
;
5 import java
.text
.SimpleDateFormat
;
8 import javax
.xml
.transform
.stream
.StreamSource
;
10 import org
.apache
.commons
.io
.IOUtils
;
11 import org
.apache
.commons
.logging
.Log
;
12 import org
.apache
.commons
.logging
.LogFactory
;
13 import org
.argeo
.slc
.msg
.process
.SlcExecutionRequest
;
14 import org
.argeo
.slc
.msg
.process
.SlcExecutionStepsRequest
;
15 import org
.argeo
.slc
.process
.SlcExecution
;
16 import org
.argeo
.slc
.process
.SlcExecutionStep
;
17 import org
.argeo
.slc
.unit
.AbstractSpringTestCase
;
18 import org
.argeo
.slc
.unit
.UnitXmlUtils
;
19 import org
.argeo
.slc
.unit
.process
.SlcExecutionTestUtils
;
20 import org
.springframework
.oxm
.Marshaller
;
21 import org
.springframework
.oxm
.Unmarshaller
;
22 import org
.springframework
.xml
.transform
.StringResult
;
23 import org
.springframework
.xml
.transform
.StringSource
;
24 import org
.springframework
.xml
.validation
.XmlValidator
;
26 public class SlcExecutionCastorTest
extends AbstractSpringTestCase
{
27 private Log log
= LogFactory
.getLog(getClass());
29 private Marshaller marshaller
;
30 private Unmarshaller unmarshaller
;
34 marshaller
= getBean(Marshaller
.class);
35 unmarshaller
= getBean(Unmarshaller
.class);
38 public void testMarshalling() throws Exception
{
39 SlcExecution slcExec
= SlcExecutionTestUtils
.createSimpleSlcExecution();
41 SlcExecutionRequest msgSave
= new SlcExecutionRequest();
42 msgSave
.setSlcExecution(slcExec
);
44 String msgSaveXml
= marshallAndLog(marshaller
, msgSave
);
46 SimpleDateFormat sdf
= new SimpleDateFormat("yyyy-MM-dd HH:mm");
47 SlcExecutionStep step0
= new SlcExecutionStep();
48 step0
.setUuid(UUID
.randomUUID().toString());
49 step0
.setBegin(sdf
.parse("2008-04-17 18:21"));
51 step0
.addLog("A log message\nand another line");
53 SlcExecutionStep step1
= new SlcExecutionStep();
54 step1
.setUuid(UUID
.randomUUID().toString());
55 step1
.setBegin(sdf
.parse("2008-04-17 18:25"));
57 step1
.addLog("A nother log message");
59 SlcExecutionStepsRequest msgNotif
= new SlcExecutionStepsRequest();
60 msgNotif
.addStep(step0
);
61 msgNotif
.addStep(step1
);
62 msgNotif
.setSlcExecutionUuid(slcExec
.getUuid());
64 String msgNotifXml
= marshallAndLog(marshaller
, msgNotif
);
66 SlcExecutionRequest msgSaveUnm
= unmarshall(unmarshaller
, msgSaveXml
);
67 assertNotNull(msgSaveUnm
);
68 SlcExecutionTestUtils
.assertSlcExecution(slcExec
, msgSaveUnm
71 SlcExecutionStepsRequest msgNotifUnm
= unmarshall(unmarshaller
,
73 assertNotNull(msgNotifUnm
);
74 assertEquals(slcExec
.getUuid(), msgNotifUnm
.getSlcExecutionUuid());
75 assertEquals(2, msgNotifUnm
.getSteps().size());
76 SlcExecutionTestUtils
.assertSlcExecutionStep(step0
, msgNotifUnm
78 SlcExecutionTestUtils
.assertSlcExecutionStep(step1
, msgNotifUnm
81 SlcExecution slcExecUnm
= msgSaveUnm
.getSlcExecution();
82 slcExecUnm
.getSteps().addAll(msgNotifUnm
.getSteps());
84 SlcExecutionRequest msgUpdate
= new SlcExecutionRequest();
85 msgUpdate
.setSlcExecution(slcExecUnm
);
86 String msgUpdateXml
= marshallAndLog(marshaller
, msgUpdate
);
88 SlcExecutionRequest msgUpdateUnm
= unmarshall(unmarshaller
,
90 assertNotNull(msgUpdateUnm
);
93 private String
marshallAndLog(Marshaller marshaller
, Object obj
)
95 StringResult xml
= new StringResult();
96 marshaller
.marshal(obj
, xml
);
97 log
.info("Marshalled object: " + xml
);
99 UnitXmlUtils
.assertXmlValidation(getBean(XmlValidator
.class),
100 new StringSource(xml
.toString()));
102 return xml
.toString();
105 private <T
> T
unmarshall(Unmarshaller unmarshaller
, String xml
)
107 StringReader reader
= new StringReader(xml
);
108 Object obj
= unmarshaller
.unmarshal(new StreamSource(reader
));
109 IOUtils
.closeQuietly(reader
);