Brett Chabot | e278a5b | 2010-06-01 16:20:14 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2010 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 | package com.android.tradefed.config; |
| 17 | |
| 18 | import java.io.ByteArrayInputStream; |
| 19 | import java.io.InputStream; |
| 20 | |
| 21 | import junit.framework.TestCase; |
| 22 | |
| 23 | /** |
| 24 | * Unit tests for {@link ConfigurationXmlParser}. |
| 25 | */ |
| 26 | public class ConfigurationXmlParserTest extends TestCase { |
| 27 | |
| 28 | /** |
| 29 | * Normal case test for {@link ConfigurationXmlParser#parse(String, InputStream)}. |
| 30 | */ |
| 31 | public void testParse() throws ConfigurationException { |
| 32 | final String normalConfig = |
| 33 | "<configuration description=\"desc\" >\n" + |
| 34 | " <test class=\"junit.framework.TestCase\">\n" + |
| 35 | " <option name=\"opName\" value=\"val\" />\n" + |
| 36 | " </test>\n" + |
| 37 | "</configuration>"; |
| 38 | ConfigurationXmlParser xmlParser = new ConfigurationXmlParser(); |
| 39 | final String configName = "config"; |
| 40 | ConfigurationDef configDef = xmlParser.parse(configName, getStringAsStream(normalConfig)); |
| 41 | assertEquals(configName, configDef.getName()); |
| 42 | assertEquals("desc", configDef.getDescription()); |
| 43 | assertEquals("junit.framework.TestCase", configDef.getObjectClassMap().get("test")); |
| 44 | assertEquals("val", configDef.getOptionMap().get("opName")); |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * Test parsing a object tag missing a attribute. |
| 49 | */ |
| 50 | public void testParse_objectMissingAttr() { |
| 51 | final String config = |
| 52 | "<object name=\"foo\" />"; |
| 53 | ConfigurationXmlParser xmlParser = new ConfigurationXmlParser(); |
| 54 | try { |
| 55 | xmlParser.parse("name", getStringAsStream(config)); |
| 56 | fail("ConfigurationException not thrown"); |
| 57 | } catch (ConfigurationException e) { |
| 58 | // expected |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Test parsing a option tag missing a attribute. |
| 64 | */ |
| 65 | public void testParse_optionMissingAttr() { |
| 66 | final String config = |
| 67 | "<option name=\"foo\" />"; |
| 68 | ConfigurationXmlParser xmlParser = new ConfigurationXmlParser(); |
| 69 | try { |
| 70 | xmlParser.parse("name", getStringAsStream(config)); |
| 71 | fail("ConfigurationException not thrown"); |
| 72 | } catch (ConfigurationException e) { |
| 73 | // expected |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Test parsing a object tag. |
| 79 | */ |
| 80 | public void testParse_object() throws ConfigurationException { |
| 81 | final String config = |
| 82 | "<object name=\"foo\" class=\"junit.framework.TestCase\" />"; |
| 83 | ConfigurationXmlParser xmlParser = new ConfigurationXmlParser(); |
| 84 | ConfigurationDef configDef = xmlParser.parse("name", getStringAsStream(config)); |
| 85 | assertEquals("junit.framework.TestCase", configDef.getObjectClassMap().get("foo")); |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Test parsing invalid xml. |
| 90 | */ |
| 91 | public void testParse_xml() throws ConfigurationException { |
| 92 | final String config = "blah"; |
| 93 | ConfigurationXmlParser xmlParser = new ConfigurationXmlParser(); |
| 94 | try { |
| 95 | xmlParser.parse("name", getStringAsStream(config)); |
| 96 | fail("ConfigurationException not thrown"); |
| 97 | } catch (ConfigurationException e) { |
| 98 | // expected |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | private InputStream getStringAsStream(String input) { |
| 103 | return new ByteArrayInputStream(input.getBytes()); |
| 104 | } |
| 105 | } |