1 // Licensed to the .NET Foundation under one or more agreements. 2 // See the LICENSE file in the project root for more information. 3 // 4 // System.Configuration.ConfigurationSectionTest.cs - Unit tests 5 // 6 // Author: 7 // Greg Smolyn 8 // Gonzalo Paniagua Javier <gonzalo@novell.com 9 // 10 // Copyright (C) 2005 Novell, Inc (http://www.novell.com) 11 // 12 // Permission is hereby granted, free of charge, to any person obtaining 13 // a copy of this software and associated documentation files (the 14 // "Software"), to deal in the Software without restriction, including 15 // without limitation the rights to use, copy, modify, merge, publish, 16 // distribute, sublicense, and/or sell copies of the Software, and to 17 // permit persons to whom the Software is furnished to do so, subject to 18 // the following conditions: 19 // 20 // The above copyright notice and this permission notice shall be 21 // included in all copies or substantial portions of the Software. 22 // 23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 30 31 using System; 32 using System.Configuration; 33 using System.IO; 34 using System.Xml; 35 using Xunit; 36 37 namespace MonoTests.System.Configuration 38 { 39 public class ConfigurationSectionTest 40 { 41 [Fact] TwoConfigElementsInARow()42 public void TwoConfigElementsInARow() // Bug #521231 43 { 44 string config = @"<fooconfig><foos><foo id=""1"" /></foos><bars><bar id=""1"" /></bars></fooconfig>"; 45 var fooSection = new FooConfigSection(); 46 fooSection.Load(config); 47 } 48 49 class FooConfigSection : ConfigurationSection 50 { Load(string xml)51 public void Load(string xml) 52 { 53 Init(); 54 using (StringReader sr = new StringReader(xml)) 55 using (XmlReader reader = new XmlTextReader(sr)) 56 { 57 DeserializeSection(reader); 58 } 59 } 60 61 [ConfigurationProperty("foos")] 62 [ConfigurationCollection(typeof(FooConfigElementCollection), AddItemName = "foo")] 63 public FooConfigElementCollection Foos 64 { 65 get { return (FooConfigElementCollection)base["foos"]; } 66 set { base["foos"] = value; } 67 } 68 69 [ConfigurationProperty("bars")] 70 [ConfigurationCollection(typeof(BarConfigElementCollection), AddItemName = "bar")] 71 public BarConfigElementCollection Bars 72 { 73 get { return (BarConfigElementCollection)base["bars"]; } 74 set { base["bars"] = value; } 75 } 76 } 77 78 class FooConfigElementCollection : ConfigurationElementCollection 79 { CreateNewElement()80 protected override ConfigurationElement CreateNewElement() 81 { 82 return new FooConfigElement(); 83 } 84 GetElementKey(ConfigurationElement element)85 protected override object GetElementKey(ConfigurationElement element) 86 { 87 return ((FooConfigElement)element).Id; 88 } 89 } 90 91 class FooConfigElement : ConfigurationElement 92 { 93 [ConfigurationProperty("id")] 94 public int Id 95 { 96 get { return (int)base["id"]; } 97 set { base["id"] = value; } 98 } 99 100 } 101 102 class BarConfigElementCollection : ConfigurationElementCollection 103 { CreateNewElement()104 protected override ConfigurationElement CreateNewElement() 105 { 106 return new BarConfigElement(); 107 } 108 GetElementKey(ConfigurationElement element)109 protected override object GetElementKey(ConfigurationElement element) 110 { 111 return ((BarConfigElement)element).Id; 112 } 113 } 114 115 class BarConfigElement : ConfigurationElement 116 { 117 [ConfigurationProperty("id")] 118 public int Id 119 { 120 get { return (int)base["id"]; } 121 set { base["id"] = value; } 122 } 123 } 124 } 125 } 126 127