public void SetAttribute_MultipleEntities()
        {
            var selectedEntities = new List <IEntity>
            {
                new FileEntity("test1"),
                new FileEntity("test2"),
            };

            _selection
            .As <IEnumerable>()
            .Setup(mock => mock.GetEnumerator())
            .Returns(selectedEntities.GetEnumerator());

            var newAttr = new Attribute("attr", new StringValue("value"), AttributeSource.Custom);

            _attributes.SetAttribute("attr", newAttr);

            // we have added the attribute to both entities
            _entityManager.Verify(mock => mock.SetEntity(
                                      It.Is <IEntity>(item =>
                                                      item.Path == selectedEntities[0].Path &&
                                                      item.GetAttribute(newAttr.Name).Equals(newAttr)
                                                      ), true
                                      ), Times.Once);

            _entityManager.Verify(mock => mock.SetEntity(
                                      It.Is <IEntity>(item =>
                                                      item.Path == selectedEntities[1].Path &&
                                                      item.GetAttribute(newAttr.Name).Equals(newAttr)
                                                      ), true
                                      ), Times.Once);
        }
        public void SetAttribute_OneEntity()
        {
            var oldAttr = new Attribute("oldAttr", new IntValue(42), AttributeSource.Custom);
            var newAttr = new Attribute("attr", new StringValue("value"), AttributeSource.Custom);

            var entity           = new FileEntity("test").SetAttribute(oldAttr);
            var selectedEntities = new List <IEntity>
            {
                entity
            };

            _selection
            .As <IEnumerable>()
            .Setup(mock => mock.GetEnumerator())
            .Returns(selectedEntities.GetEnumerator());

            _attributes.SetAttribute(oldAttr.Name, newAttr);

            // we have marked the entity as changed
            _entityManager.Verify(mock => mock.SetEntity(
                                      It.Is <IEntity>(item =>
                                                      item.GetAttribute(newAttr.Name).Equals(newAttr) &&
                                                      item.GetAttribute(oldAttr.Name) == null
                                                      ), true
                                      ), Times.Once);
        }
Exemple #3
0
        public void SetAttribute(string oldName, Attribute attr)
        {
            foreach (var entity in GetFilesInSelection())
            {
                // save initial state of the entity if necessary
                _entityManager.SetEntity(entity, false);

                // modify the entity
                var newEntity = entity.RemoveAttribute(oldName).SetAttribute(attr);
                _entityManager.SetEntity(newEntity, true);
            }
        }
Exemple #4
0
        public void Store_NonEmptyEntityAndOneAttributeSegment()
        {
            var segments = new[]
            {
                new JpegSegment(JpegSegmentType.App1, new byte[] { 0x41, 0x74, 0x74, 0x72, 0x0, 0x1, 0x2, 0x3 }, 0),
                new JpegSegment(JpegSegmentType.App1, new byte[] { 0x1 }, 8),
                new JpegSegment(JpegSegmentType.Sos, new byte[] { }, 9),
            };

            _segmentReader
            .SetupSequence(mock => mock.ReadSegment())
            .Returns(segments[0])
            .Returns(segments[1])
            .Returns(segments[2])
            .Returns((JpegSegment)null);

            _attrWriterFactory
            .Setup(mock => mock.Create(It.IsAny <Stream>()))
            .Callback <Stream>(stream =>
            {
                // write data to the stream
                stream.WriteByte(0x24);
                stream.WriteByte(0x42);
            })
            .Returns(_attrWriter.Object);

            var attr = new Attribute("attr", new IntValue(1), AttributeSource.Custom);

            _storage.Store(new FileEntity("test").SetAttribute(attr));

            _attrWriter.Verify(mock => mock.Write(attr), Times.Once);
            _attrWriter.VerifyNoOtherCalls();

            _segmentReader.Verify(mock => mock.ReadSegment(), Times.Exactly(4));
            _segmentReader.Verify(mock => mock.Dispose(), Times.Once);
            _segmentReader.Verify(mock => mock.BaseStream, Times.AtLeastOnce);
            _segmentReader.VerifyNoOtherCalls();

            _segmentWriter.Verify(mock => mock.WriteSegment(segments[0]), Times.Never);
            _segmentWriter.Verify(mock => mock.WriteSegment(segments[1]), Times.Once);
            _segmentWriter.Verify(mock => mock.WriteSegment(segments[2]), Times.Never);
            _segmentWriter.Verify(mock => mock.WriteSegment(segments[2]), Times.Never);
            _segmentWriter.Verify(mock => mock.WriteSegment(It.Is <JpegSegment>(segment =>
                                                                                segment.Type == JpegSegmentType.App1 &&
                                                                                segment.Bytes.SequenceEqual(new byte[] { 0x41, 0x74, 0x74, 0x72, 0x0, 0x24, 0x42 })
                                                                                )), Times.Once);
            _segmentWriter.Verify(mock => mock.Finish(It.IsAny <Stream>()), Times.Once);
            _segmentWriter.Verify(mock => mock.Dispose(), Times.Once);
            _segmentWriter.VerifyNoOtherCalls();

            _fileSystem.Verify(mock => mock.ReplaceFile(_tmpFileName, PathUtils.NormalizePath("test"), null), Times.Once);
        }
Exemple #5
0
 public AttributeParserMock(Attribute attr)
 {
     _attr = attr;
 }
        public void Store_MultipleConcurrentReadersAndWriters()
        {
            // generate tags to write
            var tags = new Dictionary <string, Attribute>();

            for (var i = 0; i < 1000; ++i)
            {
                string name  = GenerateRandomString(1, 12);
                string value = GenerateRandomString(10, 40);
                tags[name] = new Attribute(name, new StringValue(value), AttributeSource.Custom);
            }

            // stress test reading/writing file with high contention
            var random = new Random();

            Parallel.For(0, 20, i =>
            {
                // add random delay
                int delayTime;
                lock (random)
                {
                    delayTime = random.Next(0, 500);
                }
                Thread.Sleep(delayTime);

                try
                {
                    var entity = _storage.Load(TestDataDir + "/empty.jpg");

                    // writer
                    if (i % 2 == 0)
                    {
                        foreach (var attr in tags)
                        {
                            entity.SetAttribute(attr.Value);
                        }

                        try
                        {
                            _storage.Store(entity);
                        }
                        catch (IOException)
                        {
                            // file is busy
                        }
                    }
                    else // reader
                    {
                        // it either sees all attributes written to the file, or none of them
                        var attributes = entity
                                         .Where(attr => attr.Source == AttributeSource.Custom)
                                         .ToList();
                        var hasAllTags = attributes.All(attr => tags[attr.Name].Equals(attr)) &&
                                         attributes.Count == tags.Count;
                        var doesNotHaveAnyTag = !attributes.Any(attr => tags[attr.Name].Equals(attr));
                        Assert.IsTrue(hasAllTags || doesNotHaveAnyTag);
                    }
                }
                catch (IOException e) when(e.GetType() == typeof(IOException))
                {
                    // a thread tried to read the file while it is being replaced, this is ok
                }
            });

            var userAttributes = _storage.Load(TestDataDir + "/empty.jpg")
                                 .Where(attr => attr.Source == AttributeSource.Custom)
                                 .ToList();
            var containsExactlyTags = userAttributes
                                      .All(attr => tags[attr.Name].Equals(attr)) && userAttributes.Count == tags.Count;

            Assert.IsTrue(containsExactlyTags);

            VerifyImage(TestDataDir + "/empty.jpg");
        }
Exemple #7
0
 public IEntity SetAttribute(Attribute attr)
 {
     return(_currentState.SetAttribute(attr));
 }