public static void SetContainer(IContainer container) { if (HasContainer) { throw new ArgumentException("上下文中已经存在容器", nameof(container)); } var policy = new CacheItemPolicy() { SlidingExpiration = SlidingExpiration, RemovedCallback = CacheRemoved, }; if (Debugger.IsAttached) //调试中过期时间变为1小时 { policy.SlidingExpiration = new TimeSpan(1, 0, 0); } //尝试3次添加到容器缓存 for (var i = 0; i < 3; i++) { var uid = Guid.NewGuid(); if (MemoryCache.Default.AddOrGetExisting(Key + uid, container, policy) == null) { //加入默认组件 var defcomp = new DefaultComponent(uid); container.Add(defcomp); //添加到上下文 CallContext.LogicalSetData(Key, uid); return; } Thread.Sleep(20); } throw new NotImplementedException("容器添加到上下文失败"); }
public void SetAveragePrice_Called_ShouldSetAveragePrice() { //Act DefaultComponent.WithAveragePrice(ComponentPrice); Assert.That(DefaultComponent.AveragePrice, Is.EqualTo(ComponentPrice)); }
public void WithType_InvalidComponentType_ShouldThrowArgumentException() { //Arrange const ComponentType newType = (ComponentType)2345678; Assert.That(() => DefaultComponent.WithType(newType), Throws.ArgumentException); }
public Image GetImage(Type type, string imgName, bool large) { if ((large && largeImage == null) || (!large && smallImage == null)) { Point largeDim = new Point(32, 32); Image img = null; if (large) { img = largeImage; } else { img = smallImage; } if (img == null) { img = GetImageFromResource(type, imgName, large); } //last resort for large images. if (large && largeImage == null && smallImage != null) { img = new Bitmap((Bitmap)smallImage, largeDim.X, largeDim.Y); } Bitmap b = img as Bitmap; if (b != null) { MakeBackgroundAlphaZero(b); } if (img == null) { img = DefaultComponent.GetImage(type, large); } if (large) { largeImage = img; } else { smallImage = img; } } Image toReturn = (large) ? largeImage : smallImage; if (this.Equals(Default)) { largeImage = null; smallImage = null; } return(toReturn); }
public void WithCharacteristicValue_ValueForThisCharacteristicNotSpecifiedYet_ShouldAddValueToCharacteristicValues() { //Arrange var characteristicValue = CreateCharacteristicValue(); DefaultComponent.WithCharacteristicValue(characteristicValue); Assert.That(DefaultComponent.CharacteristicValues.Contains(characteristicValue)); }
public void WithCharacteristicValue_ValueForCharacteristicNotSpecifiedYet_ShouldAttachCharacteristicToComponent() { //Arrange var characteristicValue = CreateCharacteristicValue(); DefaultComponent.WithCharacteristicValue(characteristicValue); Assert.That(characteristicValue.Component.SameIdentityAs(DefaultComponent)); }
public void WithContainedSlot_NotNullSlot_ShouldAddSlotToContainedSLots() { //Arrange var slotToAdd = CreateInterface(1); DefaultComponent.WithContainedSlot(slotToAdd.Object); Assert.That(DefaultComponent.ContainedSlots.Count, Is.EqualTo(1)); Assert.That(DefaultComponent.ContainedSlots.Contains(slotToAdd.Object)); }
public void SetName_Called_ShouldSetNewName() { //Arrange var newComponentName = NamesGenerator.ComponentName(2); //Act DefaultComponent.WithName(newComponentName); //Assert Assert.That(DefaultComponent.Name, Is.EqualTo(newComponentName)); }
public void WithPlugSlot_NotNullArgument_ShouldAddNewSlot() { //Arrange var componentInterface = CreateInterface(1).Object; //Act DefaultComponent.WithPlugSlot(componentInterface); //Assert Assert.That(DefaultComponent.PlugSlots.Contains(componentInterface)); }
public void WithType_ValidComponentType_ShouldSetTypeProperty() { //Arrange const ComponentType newType = ComponentType.Motherboard; Assert.That(DefaultComponent.Type, Is.Not.EqualTo(newType)); DefaultComponent.WithType(newType); //Assert Assert.That(DefaultComponent.Type, Is.EqualTo(newType)); }
public void WithContainedComponent_ComponentWithSameIdentityAdded_ShouldThrowArgumentException() { //Arrange var commonId = Guid.NewGuid(); var firstComponent = CreateComponent(1).WithId(commonId); var secondComponent = CreateComponent(2).WithId(commonId); DefaultComponent.WithContainedComponent(firstComponent); Assert.That(() => DefaultComponent.WithContainedComponent(secondComponent), Throws.InstanceOf <DuplicateElementException>()); }
public void WithPlugSlot_SlotAlreadyAdded_ShouldAddDuplicateSlot() { //Arrange var componentInterface = CreateInterface(1).Object; DefaultComponent.WithPlugSlot(componentInterface); //Act DefaultComponent.WithPlugSlot(componentInterface); //Assert Assert.That(DefaultComponent.PlugSlots.Count(x => x.SameIdentityAs(componentInterface)) == 2); }
public void WithContainedSlot_DifferentSlotWithSameIdentityAdded_ShouldThrowDuplicateElementException() { //Arrange var commonId = Guid.NewGuid(); var firstSlot = CreateInterface(1).WithId(commonId).Object; DefaultComponent.WithContainedSlot(firstSlot); //Act var secondSlot = CreateInterface(2).WithId(commonId).Object; DefaultComponent.WithContainedSlot(secondSlot); //Assert Assert.That(DefaultComponent.ContainedSlots.Count(x => x.SameIdentityAs(firstSlot)) == 2); }
public void Characteristics_CharacteristicValuesArePredefined_ShouldContainAllCharacteristics() { //Arrange var valuesList = new List <CharacteristicValue>(); for (var i = 0; i < 5; i++) { valuesList.Add(CreateCharacteristicValue()); } DefaultComponent.GetType().GetProperty("CharacteristicVals", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.SetProperty | BindingFlags.SetProperty) .SetValue(DefaultComponent, valuesList); //Assert Assert.That(DefaultComponent.Characteristics.Count == valuesList.Count); foreach (var value in valuesList) { Assert.That(DefaultComponent.Characteristics.ContainsKey(value.Characteristic)); Assert.That(DefaultComponent.Characteristics[value.Characteristic] == value); } }
public void SetAveragePrice_NegativeValue_ShouldThrowArgumentOutOfRangeException() { Assert.That(() => DefaultComponent.WithAveragePrice(-15m), Throws.InstanceOf <ArgumentOutOfRangeException>()); }
public void WithContainedComponent_SameReferenceAsParent_ShouldThrowArgumentException() { Assert.That(() => DefaultComponent.WithContainedComponent(DefaultComponent), Throws.InstanceOf <ArgumentException>()); }
protected override void Init(MyObjectBuilder_DefinitionBase builder) { base.Init(builder); var definitionBuilder = (builder as MyObjectBuilder_ContainerDefinition); Flags = definitionBuilder.Flags; if (definitionBuilder.DefaultComponents != null && definitionBuilder.DefaultComponents.Length > 0) { if (DefaultComponents == null) { DefaultComponents = new List <DefaultComponent>(); } foreach (var component in definitionBuilder.DefaultComponents) { DefaultComponent defComponent = new DefaultComponent(); try { if (component.BuilderType != null) { MyObjectBuilderType obType; obType = MyObjectBuilderType.Parse(component.BuilderType); defComponent.BuilderType = obType; } } catch (Exception) { System.Diagnostics.Debug.Fail(String.Format("Can not parse defined component builder type {0} for container {1}", component.BuilderType.ToString(), Id.ToString())); MyLog.Default.WriteLine(String.Format("Container definition error: can not parse defined component type {0} for container {1}", component, Id.ToString())); } try { if (component.InstanceType != null) { Type runtimeType = Type.GetType(component.InstanceType, true); defComponent.InstanceType = runtimeType; } } catch (Exception) { System.Diagnostics.Debug.Fail(String.Format("Can not parse defined component instance type {0} for container {1}", component.InstanceType.ToString(), Id.ToString())); MyLog.Default.WriteLine(String.Format("Container definition error: can not parse defined component type {0} for container {1}", component, Id.ToString())); } defComponent.ForceCreate = component.ForceCreate; if (component.SubtypeId != null) { defComponent.SubtypeId = MyStringHash.GetOrCompute(component.SubtypeId); } if (defComponent.IsValid()) { DefaultComponents.Add(defComponent); } else { System.Diagnostics.Debug.Fail(String.Format("Defined component {0} for container {1} is invalid, none builder type or instance type is defined! Skipping it.", component, Id.ToString())); MyLog.Default.WriteLine(String.Format("Defined component {0} for container {1} is invalid, none builder type or instance type is defined! Skipping it.", component, Id.ToString())); } } } }
public void WithContainedSlot_NullSlot_ShouldThrowArgumentNullException() { Assert.That(() => DefaultComponent.WithContainedSlot(null), Throws.InstanceOf <ArgumentNullException>()); }
private static void Update(ref DefaultComponent component) { ++component.Value; }
public void WithCharacteristicValue_NullCharacteristic_ShouldThrowArgumentNullException() { Assert.That(() => DefaultComponent.WithCharacteristicValue(null), Throws.InstanceOf <ArgumentNullException>()); }
protected override void Init(MyObjectBuilder_DefinitionBase builder) { base.Init(builder); var definitionBuilder = (builder as MyObjectBuilder_ContainerDefinition); Flags = definitionBuilder.Flags; if (definitionBuilder.DefaultComponents != null && definitionBuilder.DefaultComponents.Length > 0) { if (DefaultComponents == null) { DefaultComponents = new List<DefaultComponent>(); } foreach (var component in definitionBuilder.DefaultComponents) { DefaultComponent defComponent = new DefaultComponent(); try { if (component.BuilderType != null) { MyObjectBuilderType obType; obType = MyObjectBuilderType.Parse(component.BuilderType); defComponent.BuilderType = obType; } } catch (Exception) { System.Diagnostics.Debug.Fail(String.Format("Can not parse defined component builder type {0} for container {1}", component.BuilderType.ToString(), Id.ToString())); MyLog.Default.WriteLine(String.Format("Container definition error: can not parse defined component type {0} for container {1}", component, Id.ToString())); } try { if (component.InstanceType != null) { Type runtimeType = Type.GetType(component.InstanceType, true); defComponent.InstanceType = runtimeType; } } catch (Exception) { System.Diagnostics.Debug.Fail(String.Format("Can not parse defined component instance type {0} for container {1}", component.InstanceType.ToString(), Id.ToString())); MyLog.Default.WriteLine(String.Format("Container definition error: can not parse defined component type {0} for container {1}", component, Id.ToString())); } defComponent.ForceCreate = component.ForceCreate; if (component.SubtypeId != null) { defComponent.SubtypeId = MyStringHash.GetOrCompute(component.SubtypeId); } if (defComponent.IsValid()) { DefaultComponents.Add(defComponent); } else { System.Diagnostics.Debug.Fail(String.Format("Defined component {0} for container {1} is invalid, none builder type or instance type is defined! Skipping it.", component, Id.ToString())); MyLog.Default.WriteLine(String.Format("Defined component {0} for container {1} is invalid, none builder type or instance type is defined! Skipping it.", component, Id.ToString())); } } } }
public void SetName_EmptyString_ShouldThrowArgumentNullException(string name) { Assert.That(() => DefaultComponent.WithName(name), Throws.InstanceOf <ArgumentNullException>()); }