1
2
3
4
5
6
7 package waffle.util;
8
9 import com.sun.jna.Platform;
10
11 import javax.xml.parsers.DocumentBuilderFactory;
12 import javax.xml.parsers.ParserConfigurationException;
13 import javax.xml.transform.TransformerException;
14
15 import org.junit.jupiter.api.Assertions;
16 import org.junit.jupiter.api.Test;
17 import org.w3c.dom.Document;
18 import org.w3c.dom.Element;
19 import org.w3c.dom.Node;
20 import org.w3c.dom.NodeList;
21
22 import waffle.windows.auth.IWindowsAccount;
23 import waffle.windows.auth.IWindowsAuthProvider;
24 import waffle.windows.auth.IWindowsComputer;
25 import waffle.windows.auth.impl.WindowsAccountImpl;
26 import waffle.windows.auth.impl.WindowsAuthProviderImpl;
27
28
29
30
31 class WaffleInfoTest {
32
33
34
35
36
37
38
39 @Test
40 void testWaffleInfo() throws ParserConfigurationException {
41 final WaffleInfo helper = new WaffleInfo();
42 final Document info = helper.getWaffleInfo();
43
44
45 Assertions.assertEquals(Platform.class.getPackage().getImplementationVersion(),
46 info.getDocumentElement().getAttribute("jna"));
47
48
49 final Node node = info.getDocumentElement().getFirstChild().getFirstChild().getNextSibling();
50
51 Assertions.assertEquals("computer", node.getNodeName());
52
53 final IWindowsAuthProvider auth = new WindowsAuthProviderImpl();
54 final IWindowsComputer computer = auth.getCurrentComputer();
55
56 final NodeList nodes = node.getChildNodes();
57 Assertions.assertEquals(computer.getComputerName(), nodes.item(0).getTextContent());
58 Assertions.assertEquals(computer.getMemberOf(), nodes.item(1).getTextContent());
59 Assertions.assertEquals(computer.getJoinStatus(), nodes.item(2).getTextContent());
60
61
62 String lookup = WindowsAccountImpl.getCurrentUsername();
63 final IWindowsAccount account = new WindowsAccountImpl(lookup);
64 Element elem = helper.getLookupInfo(info, lookup);
65 Assertions.assertEquals(lookup, elem.getAttribute("name"));
66 Assertions.assertEquals(account.getName(), elem.getFirstChild().getTextContent());
67
68
69 lookup = "__UNKNOWN_ACCOUNT_NAME___";
70 elem = helper.getLookupInfo(info, lookup);
71 Assertions.assertEquals(lookup, elem.getAttribute("name"));
72 Assertions.assertEquals("exception", elem.getFirstChild().getNodeName());
73 }
74
75
76
77
78
79
80
81 @Test
82 void testGetException() throws ParserConfigurationException {
83 final Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
84 final RuntimeException ex = new RuntimeException("test error message");
85 final Element exElem = WaffleInfo.getException(doc, ex);
86
87 Assertions.assertNotNull(exElem);
88 Assertions.assertEquals("exception", exElem.getNodeName());
89 Assertions.assertEquals(ex.getClass().getName(), exElem.getAttribute("class"));
90
91
92 final Node messageNode = exElem.getFirstChild();
93 Assertions.assertEquals("message", messageNode.getNodeName());
94 Assertions.assertEquals("test error message", messageNode.getTextContent());
95 }
96
97
98
99
100
101
102
103 @Test
104 void testGetExceptionWithNullMessage() throws ParserConfigurationException {
105 final Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
106 final RuntimeException ex = new RuntimeException((String) null);
107 final Element exElem = WaffleInfo.getException(doc, ex);
108
109 Assertions.assertNotNull(exElem);
110 Assertions.assertEquals("exception", exElem.getNodeName());
111 }
112
113
114
115
116
117
118
119
120
121 @Test
122 void testToPrettyXml() throws ParserConfigurationException, TransformerException {
123 final WaffleInfo helper = new WaffleInfo();
124 final Document info = helper.getWaffleInfo();
125 final String xml = WaffleInfo.toPrettyXML(info);
126 Assertions.assertNotNull(xml);
127 Assertions.assertFalse(xml.isEmpty());
128 Assertions.assertTrue(xml.contains("waffle"));
129 }
130 }