1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 package waffle.servlet;
25
26 import com.sun.jna.Platform;
27
28 import java.io.IOException;
29 import java.io.StringReader;
30
31 import javax.xml.parsers.DocumentBuilder;
32 import javax.xml.parsers.DocumentBuilderFactory;
33 import javax.xml.parsers.ParserConfigurationException;
34
35 import org.junit.jupiter.api.Assertions;
36 import org.junit.jupiter.api.Test;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39 import org.w3c.dom.Document;
40 import org.w3c.dom.Node;
41 import org.xml.sax.InputSource;
42 import org.xml.sax.SAXException;
43
44 import waffle.mock.http.SimpleHttpRequest;
45 import waffle.mock.http.SimpleHttpResponse;
46
47
48
49
50 class WaffleInfoServletTest {
51
52
53 private static final Logger LOGGER = LoggerFactory.getLogger(WaffleInfoServletTest.class);
54
55
56
57
58
59
60
61
62
63
64
65 @Test
66 void testGetInfo() throws ParserConfigurationException, SAXException, IOException {
67 final SimpleHttpRequest request = new SimpleHttpRequest();
68 request.addHeader("hello", "waffle");
69
70 final SimpleHttpResponse response = new SimpleHttpResponse();
71
72 final WaffleInfoServlet servlet = new WaffleInfoServlet();
73 servlet.doGet(request, response);
74
75 final String xml = response.getOutputText();
76 final Document doc = WaffleInfoServletTest.loadXMLFromString(xml);
77
78 WaffleInfoServletTest.LOGGER.info("GOT: {}", xml);
79
80
81 Assertions.assertEquals(Platform.class.getPackage().getImplementationVersion(),
82 doc.getDocumentElement().getAttribute("jna"));
83
84 final Node node = doc.getDocumentElement().getFirstChild().getNextSibling()
85 .getFirstChild().getNextSibling()
86 .getNextSibling().getNextSibling();
87
88
89 Assertions.assertEquals("headers", node.getNodeName());
90 final Node child = node.getFirstChild().getNextSibling();
91 Assertions.assertEquals("hello", child.getNodeName());
92 }
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109 private static Document loadXMLFromString(final String xml)
110 throws ParserConfigurationException, SAXException, IOException {
111 final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
112 final DocumentBuilder builder = factory.newDocumentBuilder();
113 final InputSource is = new InputSource(new StringReader(xml));
114 return builder.parse(is);
115 }
116 }