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
.unit
.AbstractSpringTestCase
;
16 import org
.argeo
.slc
.unit
.UnitXmlUtils
;
17 import org
.argeo
.slc
.unit
.process
.SlcExecutionTestUtils
;
18 import org
.springframework
.oxm
.Marshaller
;
19 import org
.springframework
.oxm
.Unmarshaller
;
20 import org
.springframework
.xml
.transform
.StringResult
;
21 import org
.springframework
.xml
.transform
.StringSource
;
22 import org
.springframework
.xml
.validation
.XmlValidator
;
24 public class SlcExecutionCastorTest
extends AbstractSpringTestCase
{
25 private Log log
= LogFactory
.getLog(getClass());
27 private Marshaller marshaller
;
28 private Unmarshaller unmarshaller
;
32 marshaller
= getBean(Marshaller
.class);
33 unmarshaller
= getBean(Unmarshaller
.class);
36 public void testMarshalling() throws Exception
{
37 SlcExecution slcExec
= SlcExecutionTestUtils
.createSimpleSlcExecution();
39 SlcExecutionRequest msgSave
= new SlcExecutionRequest();
40 msgSave
.setSlcExecution(slcExec
);
42 String msgSaveXml
= marshallAndLog(marshaller
, msgSave
);
44 SimpleDateFormat sdf
= new SimpleDateFormat("yyyy-MM-dd HH:mm");
45 SlcExecutionStep step0
= new SlcExecutionStep();
46 step0
.setUuid(UUID
.randomUUID().toString());
47 step0
.setBegin(sdf
.parse("2008-04-17 18:21"));
49 step0
.addLog("A log message\nand another line");
51 SlcExecutionStep step1
= new SlcExecutionStep();
52 step1
.setUuid(UUID
.randomUUID().toString());
53 step1
.setBegin(sdf
.parse("2008-04-17 18:25"));
55 step1
.addLog("A nother log message");
57 SlcExecutionStepsRequest msgNotif
= new SlcExecutionStepsRequest();
58 msgNotif
.addStep(step0
);
59 msgNotif
.addStep(step1
);
60 msgNotif
.setSlcExecutionUuid(slcExec
.getUuid());
62 String msgNotifXml
= marshallAndLog(marshaller
, msgNotif
);
64 SlcExecutionRequest msgSaveUnm
= unmarshall(unmarshaller
, msgSaveXml
);
65 assertNotNull(msgSaveUnm
);
66 SlcExecutionTestUtils
.assertSlcExecution(slcExec
, msgSaveUnm
69 SlcExecutionStepsRequest msgNotifUnm
= unmarshall(unmarshaller
,
71 assertNotNull(msgNotifUnm
);
72 assertEquals(slcExec
.getUuid(), msgNotifUnm
.getSlcExecutionUuid());
73 assertEquals(2, msgNotifUnm
.getSteps().size());
74 SlcExecutionTestUtils
.assertSlcExecutionStep(step0
, msgNotifUnm
76 SlcExecutionTestUtils
.assertSlcExecutionStep(step1
, msgNotifUnm
79 SlcExecution slcExecUnm
= msgSaveUnm
.getSlcExecution();
80 slcExecUnm
.getSteps().addAll(msgNotifUnm
.getSteps());
82 SlcExecutionRequest msgUpdate
= new SlcExecutionRequest();
83 msgUpdate
.setSlcExecution(slcExecUnm
);
84 String msgUpdateXml
= marshallAndLog(marshaller
, msgUpdate
);
86 SlcExecutionRequest msgUpdateUnm
= unmarshall(unmarshaller
,
88 assertNotNull(msgUpdateUnm
);
91 private String
marshallAndLog(Marshaller marshaller
, Object obj
)
93 StringResult xml
= new StringResult();
94 marshaller
.marshal(obj
, xml
);
95 log
.info("Marshalled object: " + xml
);
97 UnitXmlUtils
.assertXmlValidation(getBean(XmlValidator
.class),
98 new StringSource(xml
.toString()));
100 return xml
.toString();
103 private <T
> T
unmarshall(Unmarshaller unmarshaller
, String xml
)
105 StringReader reader
= new StringReader(xml
);
106 Object obj
= unmarshaller
.unmarshal(new StreamSource(reader
));
107 IOUtils
.closeQuietly(reader
);