View Javadoc
1   /*
2    * SPDX-License-Identifier: MIT
3    * See LICENSE file for details.
4    *
5    * Copyright 2010-2026 The Waffle Project Contributors: https://github.com/Waffle/waffle/graphs/contributors
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   * Build an info document and check that it has the right values.
30   */
31  class WaffleInfoTest {
32  
33      /**
34       * Test waffle info.
35       *
36       * @throws ParserConfigurationException
37       *             the parser configuration exception
38       */
39      @Test
40      void testWaffleInfo() throws ParserConfigurationException {
41          final WaffleInfo helper = new WaffleInfo();
42          final Document info = helper.getWaffleInfo();
43  
44          // Make sure JNA Version is properly noted
45          Assertions.assertEquals(Platform.class.getPackage().getImplementationVersion(),
46                  info.getDocumentElement().getAttribute("jna"));
47  
48          // waffle auth currentUser computer
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          // Add Lookup Info for Various accounts
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          // Report an error when unknown name
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       * Test get exception element.
77       *
78       * @throws ParserConfigurationException
79       *             the parser configuration exception
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          // message child
92          final Node messageNode = exElem.getFirstChild();
93          Assertions.assertEquals("message", messageNode.getNodeName());
94          Assertions.assertEquals("test error message", messageNode.getTextContent());
95      }
96  
97      /**
98       * Test get exception element with null message.
99       *
100      * @throws ParserConfigurationException
101      *             the parser configuration exception
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      * Test to pretty xml.
115      *
116      * @throws ParserConfigurationException
117      *             the parser configuration exception
118      * @throws TransformerException
119      *             the transformer exception
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 }