]> git.argeo.org Git - gpl/argeo-slc.git/blob - sandbox/argeo.slc.ws/src/test/java/org/argeo/slc/core/process/SlcExecutionCastorTest.java
Sandbox argeo.slc.ws
[gpl/argeo-slc.git] / sandbox / argeo.slc.ws / src / test / java / org / argeo / slc / core / process / SlcExecutionCastorTest.java
1 package org.argeo.slc.core.process;
2
3 import java.io.StringReader;
4 import java.text.SimpleDateFormat;
5 import java.util.UUID;
6
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;
11
12 import org.springframework.oxm.Marshaller;
13 import org.springframework.oxm.Unmarshaller;
14 import org.w3c.dom.Document;
15 import org.xml.sax.InputSource;
16
17 import org.apache.commons.logging.Log;
18 import org.apache.commons.logging.LogFactory;
19
20 import org.argeo.slc.core.xml.XmlUtils;
21 import org.argeo.slc.unit.AbstractSpringTestCase;
22
23 public class SlcExecutionCastorTest extends AbstractSpringTestCase {
24 private Log log = LogFactory.getLog(getClass());
25
26 public void testMarshalling() throws Exception {
27 Marshaller marshaller = getBean("castorMarshaller");
28
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");
35
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"));
39 step1.setType("LOG");
40 step1.setLog("A log message\nand another line");
41 step1.setSlcExecution(exec1);
42
43 Message msg1 = new Message();
44 msg1.addPart(exec1);
45 msg1.addPart(step1);
46
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);
53
54 marshaller.marshal(msg1, domResult);
55 // marshaller.marshal(step1, domResult);
56 // marshaller.marshal(exec1, domResult);
57
58 String xml = XmlUtils.getDomAsString(doc, true);
59 log.info(xml);
60
61 Unmarshaller unmarshaller = getBean("castorMarshaller");
62
63 StringReader reader = new StringReader(xml);
64 Document parsedDoc = parser.parse(new InputSource(reader));
65
66 Message message = (Message) unmarshaller.unmarshal(new DOMSource(
67 parsedDoc));
68
69 assertNotNull(message);
70
71 for (Object obj : message.getParts()) {
72 if (obj instanceof SlcExecutionStep) {
73 assertSlcExecution(exec1, ((SlcExecutionStep) obj)
74 .getSlcExecution());
75 log.debug("Execution step ok");
76 }
77 }
78
79 log.info(message.getParts());
80
81 // NodeList lstSteps = parsedDoc
82 // .getElementsByTagName("slc-execution-step");
83 // SlcExecutionStep slcExecutionStep = (SlcExecutionStep) unmarshaller
84 // .unmarshal(new DOMSource(lstSteps.item(0)));
85
86 // assertNotNull(slcExecutionStep);
87 //
88 // SlcExecution slcExecution = slcExecutionStep.getSlcExecution();
89 //
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());
95
96 }
97
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());
104 }
105 }