Пример #1
0
        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("容器添加到上下文失败");
        }
Пример #2
0
        public void SetAveragePrice_Called_ShouldSetAveragePrice()
        {
            //Act
            DefaultComponent.WithAveragePrice(ComponentPrice);

            Assert.That(DefaultComponent.AveragePrice, Is.EqualTo(ComponentPrice));
        }
Пример #3
0
        public void WithType_InvalidComponentType_ShouldThrowArgumentException()
        {
            //Arrange
            const ComponentType newType = (ComponentType)2345678;

            Assert.That(() => DefaultComponent.WithType(newType), Throws.ArgumentException);
        }
Пример #4
0
        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);
        }
Пример #5
0
        public void WithCharacteristicValue_ValueForThisCharacteristicNotSpecifiedYet_ShouldAddValueToCharacteristicValues()
        {
            //Arrange
            var characteristicValue = CreateCharacteristicValue();

            DefaultComponent.WithCharacteristicValue(characteristicValue);

            Assert.That(DefaultComponent.CharacteristicValues.Contains(characteristicValue));
        }
Пример #6
0
        public void WithCharacteristicValue_ValueForCharacteristicNotSpecifiedYet_ShouldAttachCharacteristicToComponent()
        {
            //Arrange
            var characteristicValue = CreateCharacteristicValue();

            DefaultComponent.WithCharacteristicValue(characteristicValue);

            Assert.That(characteristicValue.Component.SameIdentityAs(DefaultComponent));
        }
Пример #7
0
        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));
        }
Пример #8
0
        public void SetName_Called_ShouldSetNewName()
        {
            //Arrange
            var newComponentName = NamesGenerator.ComponentName(2);

            //Act
            DefaultComponent.WithName(newComponentName);

            //Assert
            Assert.That(DefaultComponent.Name, Is.EqualTo(newComponentName));
        }
Пример #9
0
        public void WithPlugSlot_NotNullArgument_ShouldAddNewSlot()
        {
            //Arrange
            var componentInterface = CreateInterface(1).Object;

            //Act
            DefaultComponent.WithPlugSlot(componentInterface);

            //Assert
            Assert.That(DefaultComponent.PlugSlots.Contains(componentInterface));
        }
Пример #10
0
        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));
        }
Пример #11
0
        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>());
        }
Пример #12
0
        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);
        }
Пример #13
0
        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);
        }
Пример #14
0
        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);
            }
        }
Пример #15
0
 public void SetAveragePrice_NegativeValue_ShouldThrowArgumentOutOfRangeException()
 {
     Assert.That(() => DefaultComponent.WithAveragePrice(-15m), Throws.InstanceOf <ArgumentOutOfRangeException>());
 }
Пример #16
0
 public void WithContainedComponent_SameReferenceAsParent_ShouldThrowArgumentException()
 {
     Assert.That(() => DefaultComponent.WithContainedComponent(DefaultComponent),
                 Throws.InstanceOf <ArgumentException>());
 }
Пример #17
0
        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()));
                    }
                }
            }
        }
Пример #18
0
 public void WithContainedSlot_NullSlot_ShouldThrowArgumentNullException()
 {
     Assert.That(() => DefaultComponent.WithContainedSlot(null), Throws.InstanceOf <ArgumentNullException>());
 }
 private static void Update(ref DefaultComponent component)
 {
     ++component.Value;
 }
Пример #20
0
 public void WithCharacteristicValue_NullCharacteristic_ShouldThrowArgumentNullException()
 {
     Assert.That(() => DefaultComponent.WithCharacteristicValue(null),
                 Throws.InstanceOf <ArgumentNullException>());
 }
Пример #21
0
        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()));
                    }
                }
            }
        }
Пример #22
0
 public void SetName_EmptyString_ShouldThrowArgumentNullException(string name)
 {
     Assert.That(() => DefaultComponent.WithName(name), Throws.InstanceOf <ArgumentNullException>());
 }