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
.springframework
.oxm
.Marshaller
;
20 import org
.springframework
.oxm
.Unmarshaller
;
22 public class SlcExecutionCastorTest
extends AbstractSpringTestCase
{
23 private Log log
= LogFactory
.getLog(getClass());
25 private Marshaller marshaller
;
26 private Unmarshaller unmarshaller
;
29 marshaller
= getBean("marshaller");
30 unmarshaller
= getBean("marshaller");
33 public void testMarshalling() throws Exception
{
34 SlcExecution slcExec
= SlcExecutionTestUtils
.createSimpleSlcExecution();
36 SlcExecutionRequest msgSave
= new SlcExecutionRequest();
37 msgSave
.setSlcExecution(slcExec
);
39 String msgSaveXml
= marshallAndLog(marshaller
, msgSave
);
41 SimpleDateFormat sdf
= new SimpleDateFormat("yyyy-MM-dd HH:mm");
42 SlcExecutionStep step0
= new SlcExecutionStep();
43 step0
.setUuid(UUID
.randomUUID().toString());
44 step0
.setBegin(sdf
.parse("2008-04-17 18:21"));
46 step0
.addLog("A log message\nand another line");
48 SlcExecutionStep step1
= new SlcExecutionStep();
49 step1
.setUuid(UUID
.randomUUID().toString());
50 step1
.setBegin(sdf
.parse("2008-04-17 18:25"));
52 step1
.addLog("A nother log message");
54 SlcExecutionStepsRequest msgNotif
= new SlcExecutionStepsRequest();
55 msgNotif
.addStep(step0
);
56 msgNotif
.addStep(step1
);
57 msgNotif
.setSlcExecutionUuid(slcExec
.getUuid());
59 String msgNotifXml
= marshallAndLog(marshaller
, msgNotif
);
61 SlcExecutionRequest msgSaveUnm
= unmarshall(unmarshaller
, msgSaveXml
);
62 assertNotNull(msgSaveUnm
);
63 SlcExecutionTestUtils
.assertSlcExecution(slcExec
, msgSaveUnm
66 SlcExecutionStepsRequest msgNotifUnm
= unmarshall(unmarshaller
,
68 assertNotNull(msgNotifUnm
);
69 assertEquals(slcExec
.getUuid(), msgNotifUnm
.getSlcExecutionUuid());
70 assertEquals(2, msgNotifUnm
.getSteps().size());
71 SlcExecutionTestUtils
.assertSlcExecutionStep(step0
, msgNotifUnm
73 SlcExecutionTestUtils
.assertSlcExecutionStep(step1
, msgNotifUnm
76 SlcExecution slcExecUnm
= msgSaveUnm
.getSlcExecution();
77 slcExecUnm
.getSteps().addAll(msgNotifUnm
.getSteps());
79 SlcExecutionRequest msgUpdate
= new SlcExecutionRequest();
80 msgUpdate
.setSlcExecution(slcExecUnm
);
81 String msgUpdateXml
= marshallAndLog(marshaller
, msgUpdate
);
83 SlcExecutionRequest msgUpdateUnm
= unmarshall(unmarshaller
,
85 assertNotNull(msgUpdateUnm
);
88 private String
marshallAndLog(Marshaller marshaller
, Object obj
)
90 StringWriter writer
= new StringWriter();
91 marshaller
.marshal(obj
, new StreamResult(writer
));
92 String xml
= writer
.toString();
94 IOUtils
.closeQuietly(writer
);
98 private <T
> T
unmarshall(Unmarshaller unmarshaller
, String xml
)
100 StringReader reader
= new StringReader(xml
);
101 Object obj
= unmarshaller
.unmarshal(new StreamSource(reader
));
102 IOUtils
.closeQuietly(reader
);