1 /*
2  * Copyright (C) 2016 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package android.net.wifi.hotspot2.omadm;
18 
19 import static org.junit.Assert.assertTrue;
20 
21 import android.net.wifi.hotspot2.omadm.XMLNode;
22 import android.net.wifi.hotspot2.omadm.XMLParser;
23 
24 import androidx.test.filters.SmallTest;
25 
26 import org.junit.Before;
27 import org.junit.Test;
28 import org.xml.sax.SAXException;
29 
30 import java.io.IOException;
31 
32 /**
33  * Unit tests for {@link android.net.wifi.hotspot2.omadm.XMLParser}.
34  */
35 @SmallTest
36 public class XMLParserTest {
37     XMLParser mParser;
38 
createNode(XMLNode parent, String tag, String text)39     private static XMLNode createNode(XMLNode parent, String tag, String text) {
40         XMLNode node = new XMLNode(parent, tag);
41         node.addText(text);
42         if (parent != null)
43             parent.addChild(node);
44         node.close();
45         return node;
46     }
47 
48     /**
49      * Setup before tests.
50      */
51     @Before
setUp()52     public void setUp() throws Exception {
53         mParser = new XMLParser();
54     }
55 
56     @Test(expected = IOException.class)
parseNullXML()57     public void parseNullXML() throws Exception {
58         mParser.parse(null);
59     }
60 
61     @Test(expected = IOException.class)
parseEmptyXML()62     public void parseEmptyXML() throws Exception {
63         mParser.parse(new String());
64     }
65 
66     @Test(expected = SAXException.class)
parseMalformedXML()67     public void parseMalformedXML() throws Exception {
68         String malformedXmlTree = "<root><child1>test1</child2></root>";
69         mParser.parse(malformedXmlTree);
70     }
71 
72     @Test
parseValidXMLTree()73     public void parseValidXMLTree() throws Exception {
74         String xmlTree = "<root><child1>test1</child1><child2>test2</child2></root>";
75 
76         // Construct the expected XML tree.
77         XMLNode expectedRoot = createNode(null, "root", "");
78         createNode(expectedRoot, "child1", "test1");
79         createNode(expectedRoot, "child2", "test2");
80 
81         XMLNode actualRoot = mParser.parse(xmlTree);
82         assertTrue(actualRoot.equals(expectedRoot));
83     }
84 }
85