1 package org
.argeo
.slc
.core
.process
;
3 import java
.io
.IOException
;
4 import java
.io
.StringReader
;
5 import java
.io
.StringWriter
;
6 import java
.text
.SimpleDateFormat
;
9 import javax
.xml
.transform
.stream
.StreamResult
;
10 import javax
.xml
.transform
.stream
.StreamSource
;
12 import org
.apache
.commons
.io
.IOUtils
;
13 import org
.apache
.commons
.logging
.Log
;
14 import org
.apache
.commons
.logging
.LogFactory
;
15 import org
.argeo
.slc
.msg
.process
.SlcExecutionRequest
;
16 import org
.argeo
.slc
.msg
.process
.SlcExecutionStepsRequest
;
17 import org
.argeo
.slc
.unit
.AbstractSpringTestCase
;
18 import org
.springframework
.oxm
.Marshaller
;
19 import org
.springframework
.oxm
.Unmarshaller
;
21 public class SlcExecutionCastorTest
extends AbstractSpringTestCase
{
22 private Log log
= LogFactory
.getLog(getClass());
24 public void testMarshalling() throws Exception
{
25 Marshaller marshaller
= getBean("marshaller");
26 Unmarshaller unmarshaller
= getBean("marshaller");
28 SlcExecution slcExec
= SlcExecutionTestUtils
.createSimpleSlcExecution();
30 SlcExecutionRequest msgSave
= new SlcExecutionRequest();
31 msgSave
.setSlcExecution(slcExec
);
33 String msgSaveXml
= marshallAndLog(marshaller
, msgSave
);
35 SimpleDateFormat sdf
= new SimpleDateFormat("yyyy-MM-dd HH:mm");
36 SlcExecutionStep step0
= new SlcExecutionStep();
37 step0
.setUuid(UUID
.randomUUID().toString());
38 step0
.setBegin(sdf
.parse("2008-04-17 18:21"));
40 step0
.addLog("A log message\nand another line");
42 SlcExecutionStep step1
= new SlcExecutionStep();
43 step1
.setUuid(UUID
.randomUUID().toString());
44 step1
.setBegin(sdf
.parse("2008-04-17 18:25"));
46 step1
.addLog("A nother log message");
48 SlcExecutionStepsRequest msgNotif
= new SlcExecutionStepsRequest();
49 msgNotif
.addStep(step0
);
50 msgNotif
.addStep(step1
);
51 msgNotif
.setSlcExecutionUuid(slcExec
.getUuid());
53 String msgNotifXml
= marshallAndLog(marshaller
, msgNotif
);
55 SlcExecutionRequest msgSaveUnm
= unmarshall(unmarshaller
, msgSaveXml
);
56 assertNotNull(msgSaveUnm
);
57 SlcExecutionTestUtils
.assertSlcExecution(slcExec
, msgSaveUnm
.getSlcExecution());
59 SlcExecutionStepsRequest msgNotifUnm
= unmarshall(unmarshaller
,
61 assertNotNull(msgNotifUnm
);
62 assertEquals(slcExec
.getUuid(), msgNotifUnm
.getSlcExecutionUuid());
63 assertEquals(2, msgNotifUnm
.getSteps().size());
64 SlcExecutionTestUtils
.assertSlcExecutionStep(step0
, msgNotifUnm
.getSteps().get(0));
65 SlcExecutionTestUtils
.assertSlcExecutionStep(step1
, msgNotifUnm
.getSteps().get(1));
67 SlcExecution slcExecUnm
= msgSaveUnm
.getSlcExecution();
68 slcExecUnm
.getSteps().addAll(msgNotifUnm
.getSteps());
70 SlcExecutionRequest msgUpdate
= new SlcExecutionRequest();
71 msgUpdate
.setSlcExecution(slcExecUnm
);
72 String msgUpdateXml
= marshallAndLog(marshaller
, msgUpdate
);
75 private String
marshallAndLog(Marshaller marshaller
, Object obj
)
77 StringWriter writer
= new StringWriter();
78 marshaller
.marshal(obj
, new StreamResult(writer
));
79 String xml
= writer
.toString();
81 IOUtils
.closeQuietly(writer
);
85 private <T
> T
unmarshall(Unmarshaller unmarshaller
, String xml
)
87 StringReader reader
= new StringReader(xml
);
88 Object obj
= unmarshaller
.unmarshal(new StreamSource(reader
));
89 IOUtils
.closeQuietly(reader
);