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.servlet.ServletException;
32 import javax.xml.parsers.DocumentBuilder;
33 import javax.xml.parsers.DocumentBuilderFactory;
34 import javax.xml.parsers.ParserConfigurationException;
35
36 import org.junit.jupiter.api.Assertions;
37 import org.junit.jupiter.api.Test;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40 import org.w3c.dom.Document;
41 import org.w3c.dom.Node;
42 import org.xml.sax.InputSource;
43 import org.xml.sax.SAXException;
44
45 import waffle.mock.http.SimpleHttpRequest;
46 import waffle.mock.http.SimpleHttpResponse;
47
48
49
50
51 class WaffleInfoServletTest {
52
53
54 private static final Logger LOGGER = LoggerFactory.getLogger(WaffleInfoServletTest.class);
55
56
57
58
59
60
61
62
63
64
65
66
67
68 @Test
69 void testGetInfo() throws ParserConfigurationException, SAXException, IOException, ServletException {
70 final SimpleHttpRequest request = new SimpleHttpRequest();
71 request.addHeader("hello", "waffle");
72
73 final SimpleHttpResponse response = new SimpleHttpResponse();
74
75 final WaffleInfoServlet servlet = new WaffleInfoServlet();
76 servlet.doGet(request, response);
77
78 final String xml = response.getOutputText();
79 final Document doc = WaffleInfoServletTest.loadXMLFromString(xml);
80
81 WaffleInfoServletTest.LOGGER.info("GOT: {}", xml);
82
83
84 Assertions.assertEquals(Platform.class.getPackage().getImplementationVersion(),
85 doc.getDocumentElement().getAttribute("jna"));
86
87 final Node node = doc.getDocumentElement().getFirstChild().getNextSibling()
88 .getFirstChild().getNextSibling()
89 .getNextSibling().getNextSibling();
90
91
92 Assertions.assertEquals("headers", node.getNodeName());
93 final Node child = node.getFirstChild().getNextSibling();
94 Assertions.assertEquals("hello", child.getNodeName());
95 }
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112 private static Document loadXMLFromString(final String xml)
113 throws ParserConfigurationException, SAXException, IOException {
114 final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
115 final DocumentBuilder builder = factory.newDocumentBuilder();
116 final InputSource is = new InputSource(new StringReader(xml));
117 return builder.parse(is);
118 }
119 }