1 package org
.argeo
.slc
.core
.process
;
3 import java
.io
.StringReader
;
4 import java
.text
.SimpleDateFormat
;
7 import javax
.xml
.parsers
.DocumentBuilder
;
8 import javax
.xml
.parsers
.DocumentBuilderFactory
;
9 import javax
.xml
.transform
.dom
.DOMResult
;
10 import javax
.xml
.transform
.dom
.DOMSource
;
12 import org
.springframework
.oxm
.Marshaller
;
13 import org
.springframework
.oxm
.Unmarshaller
;
14 import org
.w3c
.dom
.Document
;
15 import org
.xml
.sax
.InputSource
;
17 import org
.apache
.commons
.logging
.Log
;
18 import org
.apache
.commons
.logging
.LogFactory
;
20 import org
.argeo
.slc
.core
.xml
.XmlUtils
;
21 import org
.argeo
.slc
.unit
.AbstractSpringTestCase
;
23 public class SlcExecutionCastorTest
extends AbstractSpringTestCase
{
24 private Log log
= LogFactory
.getLog(getClass());
26 public void testMarshalling() throws Exception
{
27 Marshaller marshaller
= getBean("castorMarshaller");
29 SlcExecution exec1
= new SlcExecution();
30 exec1
.setUuid(UUID
.randomUUID().toString());
31 exec1
.setHost("localhost");
32 exec1
.setPath("/test");
33 exec1
.setType("slcAnt");
34 exec1
.setStatus("STARTED");
36 SimpleDateFormat sdf
= new SimpleDateFormat("yyyy-MM-dd HH:mm");
37 SlcExecutionStep step1
= new SlcExecutionStep();
38 step1
.setBegin(sdf
.parse("2008-04-17 18:21"));
40 step1
.setLog("A log message\nand another line");
41 step1
.setSlcExecution(exec1
);
43 Message msg1
= new Message();
47 DocumentBuilderFactory factory
= DocumentBuilderFactory
.newInstance();
48 DocumentBuilder parser
= factory
.newDocumentBuilder();
49 Document doc
= parser
.newDocument();
50 // Element rootElem = doc.createElement("msg");
51 // doc.appendChild(rootElem);
52 DOMResult domResult
= new DOMResult(doc
);
54 marshaller
.marshal(msg1
, domResult
);
55 // marshaller.marshal(step1, domResult);
56 // marshaller.marshal(exec1, domResult);
58 String xml
= XmlUtils
.getDomAsString(doc
, true);
61 Unmarshaller unmarshaller
= getBean("castorMarshaller");
63 StringReader reader
= new StringReader(xml
);
64 Document parsedDoc
= parser
.parse(new InputSource(reader
));
66 Message message
= (Message
) unmarshaller
.unmarshal(new DOMSource(
69 assertNotNull(message
);
71 for (Object obj
: message
.getParts()) {
72 if (obj
instanceof SlcExecutionStep
) {
73 assertSlcExecution(exec1
, ((SlcExecutionStep
) obj
)
75 log
.debug("Execution step ok");
79 log
.info(message
.getParts());
81 // NodeList lstSteps = parsedDoc
82 // .getElementsByTagName("slc-execution-step");
83 // SlcExecutionStep slcExecutionStep = (SlcExecutionStep) unmarshaller
84 // .unmarshal(new DOMSource(lstSteps.item(0)));
86 // assertNotNull(slcExecutionStep);
88 // SlcExecution slcExecution = slcExecutionStep.getSlcExecution();
90 // assertNotNull(slcExecution);
91 // assertEquals(exec1.getHost(), slcExecution.getHost());
92 // assertEquals(exec1.getPath(), slcExecution.getPath());
93 // assertEquals(exec1.getType(), slcExecution.getType());
94 // assertEquals(exec1.getStatus(), slcExecution.getStatus());
98 private void assertSlcExecution(SlcExecution expected
, SlcExecution reached
) {
99 assertNotNull(reached
);
100 assertEquals(expected
.getHost(), reached
.getHost());
101 assertEquals(expected
.getPath(), reached
.getPath());
102 assertEquals(expected
.getType(), reached
.getType());
103 assertEquals(expected
.getStatus(), reached
.getStatus());