public void ToStringTest() { ElementReferenceConfiguration elementReference = new ElementReferenceConfiguration(); elementReference.Id = "SomeId"; string str = elementReference.ToString(); Assert.AreEqual("Element Reference: SomeId", str, "Unexpected string representation."); }
public void CreateTest() { ElementReferenceConfiguration elementReferenceConfiguration = new ElementReferenceConfiguration(); // // Verify default state // Assert.IsNull(elementReferenceConfiguration.Id, "Unexpected default value for Id."); }
/// <summary> /// Creates a clone of this instance. /// </summary> /// <returns>Clone of this instance.</returns> protected override ConfigurationElement DoClone() { ElementReferenceConfiguration clone = new ElementReferenceConfiguration(); clone._id = _id; if (_referencedElement != null) { ElementConfiguration referenceClone = _referencedElement.Clone() as ElementConfiguration; clone._referencedElement = referenceClone; } return(clone); }
/// <summary> /// Resolves any reference elements in the configuration. /// </summary> public void ResolveReferences() { Dictionary <string, ElementConfiguration> elementMap = new Dictionary <string, ElementConfiguration>(); List <ElementReferenceConfiguration> elementReferences = new List <ElementReferenceConfiguration>(); Action <ConfigurationElement> populateElementMap = delegate(ConfigurationElement element) { ElementConfiguration elementConfiguration = element as ElementConfiguration; if (elementConfiguration != null && elementConfiguration.Id != null) { elementMap.Add(elementConfiguration.Id, elementConfiguration); } }; Action <ConfigurationElement> populateElementReferenceList = delegate(ConfigurationElement element) { ElementReferenceConfiguration elementReference = element as ElementReferenceConfiguration; if (elementReference != null && elementReference.Id != null) { elementReferences.Add(elementReference); } }; Action <ConfigurationElement>[] actions = new Action <ConfigurationElement>[] { populateElementMap, populateElementReferenceList }; TreeProcess(this, actions); // Resolve element references foreach (ElementReferenceConfiguration reference in elementReferences) { ElementConfiguration referencedElement = null; elementMap.TryGetValue(reference.Id, out referencedElement); if (referencedElement != null) { reference.ReferencedElement = referencedElement; } else { throw new InvalidOperationException( string.Format( "Unable to resolve element reference for Id={0}.", reference.Id)); } } }
/// <summary> /// Creates a clone of this instance. /// </summary> /// <returns>Clone of this instance.</returns> protected override ConfigurationElement DoClone() { ElementReferenceConfiguration clone = new ElementReferenceConfiguration(); clone._id = _id; if (_referencedElement != null) { ElementConfiguration referenceClone = _referencedElement.Clone() as ElementConfiguration; clone._referencedElement = referenceClone; } return clone; }
public void SerializeAndDeserializeTest() { CodeConfiguration origConfig = new CodeConfiguration(); ElementConfiguration elementConfiguration1 = new ElementConfiguration(); elementConfiguration1.ElementType = ElementType.Using; elementConfiguration1.Id = "TestId"; origConfig.Elements.Add(elementConfiguration1); ElementConfiguration elementConfiguration2 = new ElementConfiguration(); elementConfiguration2.ElementType = ElementType.Namespace; origConfig.Elements.Add(elementConfiguration2); ElementReferenceConfiguration elementReferenceConfiguration = new ElementReferenceConfiguration(); elementReferenceConfiguration.Id = "TestId"; origConfig.Elements.Add(elementReferenceConfiguration); RegionConfiguration regionConfiguration = new RegionConfiguration(); regionConfiguration.Name = "Test Region"; origConfig.Elements.Add(regionConfiguration); origConfig.ResolveReferences(); Assert.AreEqual( elementConfiguration1.Elements.Count, elementReferenceConfiguration.ReferencedElement.Elements.Count, "Element reference was not resolved."); string tempFile = Path.GetTempFileName(); try { // // Save the configuration to an XML file // origConfig.Save(tempFile); // // Load the configuration from the XML file // CodeConfiguration loadedConfig = CodeConfiguration.Load(tempFile); Assert.IsNotNull(loadedConfig, "Loaded configuration should not be null."); Assert.AreEqual(origConfig.Elements.Count, loadedConfig.Elements.Count, "An unexpected number of config elements were deserialized."); for (int index = 0; index < origConfig.Elements.Count; index++) { if (origConfig.Elements[index] is ElementConfiguration) { ElementConfiguration origElement = origConfig.Elements[index] as ElementConfiguration; ElementConfiguration loadedElement = loadedConfig.Elements[index] as ElementConfiguration; Assert.AreEqual(origElement.ElementType, loadedElement.ElementType, "Unexpected element type."); } else if (origConfig.Elements[index] is ElementReferenceConfiguration) { ElementReferenceConfiguration origElement = origConfig.Elements[index] as ElementReferenceConfiguration; ElementReferenceConfiguration loadedElement = loadedConfig.Elements[index] as ElementReferenceConfiguration; Assert.AreEqual(origElement.Id, loadedElement.Id, "Unexpected element type."); Assert.AreEqual( origElement.ReferencedElement.Id, loadedElement.ReferencedElement.Id, "Unexpected referenced element."); } else if (origConfig.Elements[index] is RegionConfiguration) { RegionConfiguration origRegion = origConfig.Elements[index] as RegionConfiguration; RegionConfiguration loadedRegion = loadedConfig.Elements[index] as RegionConfiguration; Assert.AreEqual(origRegion.Name, loadedRegion.Name, "Unexpected region name."); } } } finally { File.Delete(tempFile); } }