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 import android.support.test.filters.SmallTest; 24 25 import org.junit.Before; 26 import org.junit.Test; 27 import org.xml.sax.SAXException; 28 29 import java.io.IOException; 30 31 /** 32 * Unit tests for {@link android.net.wifi.hotspot2.omadm.XMLParser}. 33 */ 34 @SmallTest 35 public class XMLParserTest { 36 XMLParser mParser; 37 createNode(XMLNode parent, String tag, String text)38 private static XMLNode createNode(XMLNode parent, String tag, String text) { 39 XMLNode node = new XMLNode(parent, tag); 40 node.addText(text); 41 if (parent != null) 42 parent.addChild(node); 43 node.close(); 44 return node; 45 } 46 47 /** 48 * Setup before tests. 49 */ 50 @Before setUp()51 public void setUp() throws Exception { 52 mParser = new XMLParser(); 53 } 54 55 @Test(expected = IOException.class) parseNullXML()56 public void parseNullXML() throws Exception { 57 mParser.parse(null); 58 } 59 60 @Test(expected = IOException.class) parseEmptyXML()61 public void parseEmptyXML() throws Exception { 62 mParser.parse(new String()); 63 } 64 65 @Test(expected = SAXException.class) parseMalformedXML()66 public void parseMalformedXML() throws Exception { 67 String malformedXmlTree = "<root><child1>test1</child2></root>"; 68 mParser.parse(malformedXmlTree); 69 } 70 71 @Test parseValidXMLTree()72 public void parseValidXMLTree() throws Exception { 73 String xmlTree = "<root><child1>test1</child1><child2>test2</child2></root>"; 74 75 // Construct the expected XML tree. 76 XMLNode expectedRoot = createNode(null, "root", ""); 77 createNode(expectedRoot, "child1", "test1"); 78 createNode(expectedRoot, "child2", "test2"); 79 80 XMLNode actualRoot = mParser.parse(xmlTree); 81 assertTrue(actualRoot.equals(expectedRoot)); 82 } 83 } 84