Beispiel #1
0
        public void SerializationIntegration()
        {
            //Load the model
            var repository = new ModelRepository();
            var model = LoadRailwayModel(repository);

            //Create the recorder
            var recorder = new ModelChangeRecorder();
            recorder.Start(model);

            //Change the model
            var route = new Route() { Id = 42 };
            model.Routes.Add(route);
            model.Routes[0].DefinedBy.RemoveAt(0);
            model.Routes[0].DefinedBy[0].Elements.RemoveAt(0);
            model.Semaphores[0].Signal = Signal.FAILURE;

            //Parse the changes
            var changes = recorder.GetModelChanges();

            //Serialize the changes
            var types = changes.TraverseFlat().Select(t => t.GetType()).Distinct();
            var serializer = new XmiSerializer(types);
            string xmi;
            using (var writer = new StringWriter())
            {
                serializer.Serialize(changes, writer);
                xmi = writer.ToString();
            }

            //Deserialize the XMI
            ModelChangeCollection newChanges;
            using (var reader = new StringReader(xmi))
            {
                newChanges = serializer.Deserialize(reader) as ModelChangeCollection;
            }

            Assert.IsNotNull(newChanges);
            //Since model elements don't implement Equals() we can only test for property equality
            var newRoute = ((ListInsertionComposition<IRoute>)((ChangeTransaction)newChanges.Changes[0]).SourceChange).NewElements[0];
            Assert.AreEqual(route.Id, newRoute.Id);
            //We have to leave out the changes with model elements in them when tesing for equality
            CollectionAssert.AreEqual(changes.Changes.Skip(1).ToArray(), newChanges.Changes.Skip(1).ToArray());

            //Load second instance of the model
            var newRepository = new ModelRepository();
            var newModel = LoadRailwayModel(newRepository);

            //Apply changes to the new model
            newChanges.Apply(newRepository);

            Assert.AreEqual(model.Routes.Count, newModel.Routes.Count);
            Assert.AreEqual(model.Routes[0].DefinedBy.Count, newModel.Routes[0].DefinedBy.Count);
            Assert.AreEqual(model.Routes[0].DefinedBy[0].Elements.Count, newModel.Routes[0].DefinedBy[0].Elements.Count);
            Assert.AreEqual(model.Semaphores[0].Signal, newModel.Semaphores[0].Signal);
        }
        public void RecordListDeletion()
        {
            var rec = new ModelChangeRecorder();
            rec.Start(railway);

            railway.Semaphores.RemoveAt(0);

            var expected = new ListDeletion(railway.AbsoluteUri, "Semaphores", 0, 1);
            var actual = ((ChangeTransaction)rec.GetModelChanges().Changes[0]).SourceChange;
            Assert.AreEqual(expected, actual);
        }
        public void RecordListClear()
        {
            var parent = railway.Routes[0].DefinedBy[0].Elements[0];
            var rec = new ModelChangeRecorder();
            rec.Start(railway);

            parent.ConnectsTo.Clear();

            var expected = new ListDeletion(parent.AbsoluteUri, "ConnectsTo", 0, int.MaxValue);
            var actual = rec.GetModelChanges().Changes[0];
            Assert.AreEqual(expected, actual);
        }
        public void RecordElementDeletion()
        {
            var toDelete = railway.Routes[0];
            var expected = new ElementDeletion(toDelete.AbsoluteUri);
            var rec = new ModelChangeRecorder();
            rec.Start(railway);

            toDelete.Delete();

            var actual = ((ChangeTransaction)rec.GetModelChanges().Changes[0]).SourceChange;
            Assert.AreEqual(expected, actual);
        }
        public void RecordListInsertionComposition()
        {
            var semaphore = new Semaphore { Signal = Signal.STOP };
            var rec = new ModelChangeRecorder();
            rec.Start(railway);

            railway.Semaphores.Insert(0, semaphore);

            var expected = new ChangeTransaction()
            {
                SourceChange = new ListInsertionComposition<ISemaphore>(railway.AbsoluteUri, "Semaphores", 0, new List<ISemaphore>() { semaphore }),
                NestedChanges = new List<IModelChange>() { new ElementCreation(semaphore) }
            };
            var actual = rec.GetModelChanges().Changes[0];
            Assert.AreEqual(expected, actual);
        }
        public void RecordListInsertionAssociation()
        {
            var parent = railway.Routes[0].DefinedBy[0].Elements[0];
            var newItem = railway.Routes[0].DefinedBy[1].Elements[0];
            var rec = new ModelChangeRecorder();
            rec.Start(railway);

            parent.ConnectsTo.Insert(0, newItem);

            var expected = new List<IModelChange>()
            {
                new ListInsertionAssociation<ITrackElement>(parent.AbsoluteUri, "ConnectsTo", 0, new List<Uri>() { newItem.AbsoluteUri })
            };
            var actual = rec.GetModelChanges().Changes;
            CollectionAssert.AreEqual(expected, actual);
        }
        public void RecordPropertyChangeReference()
        {
            var parent = railway.Routes[0];
            var rec = new ModelChangeRecorder();
            rec.Start(railway);

            parent.Entry = railway.Semaphores[0];

            var expected = new PropertyChangeReference<ISemaphore>(parent.AbsoluteUri, "Entry", railway.Semaphores[0].AbsoluteUri);
            var actual = rec.GetModelChanges().Changes[0];
            Assert.AreEqual(expected, actual);
        }
        public void RecordPropertyChangeAttribute()
        {
            var semaphore = railway.Semaphores[0];
            var rec = new ModelChangeRecorder();
            rec.Start(railway);

            semaphore.Signal = Signal.FAILURE;

            var expected = new PropertyChangeAttribute<Signal>(semaphore.AbsoluteUri, "Signal", Signal.FAILURE);
            var actual = rec.GetModelChanges().Changes[0];
            Assert.AreEqual(expected, actual);
        }
Beispiel #9
0
        public void SerializationIntegration()
        {
            //Load the model
            var repository = new ModelRepository();
            var model      = LoadRailwayModel(repository);

            //Create the recorder
            var recorder = new ModelChangeRecorder();

            recorder.Start(model);

            //Change the model
            var route = new Route()
            {
                Id = 42
            };

            model.Routes.Add(route);
            model.Routes[0].DefinedBy.RemoveAt(0);
            model.Routes[0].DefinedBy[0].Elements.RemoveAt(0);
            model.Semaphores[0].Signal = Signal.FAILURE;

            //Parse the changes
            var changes = recorder.GetModelChanges();

            //Serialize the changes
            var    types      = changes.TraverseFlat().Select(t => t.GetType()).Distinct();
            var    serializer = new XmiSerializer(types);
            string xmi;

            using (var writer = new StringWriter())
            {
                serializer.Serialize(changes, writer);
                xmi = writer.ToString();
            }

            //Deserialize the XMI
            ModelChangeCollection newChanges;

            using (var reader = new StringReader(xmi))
            {
                newChanges = serializer.Deserialize(reader) as ModelChangeCollection;
            }

            Assert.IsNotNull(newChanges);
            //Since model elements don't implement Equals() we can only test for property equality
            var newRoute = ((ListInsertionComposition <IRoute>)((ChangeTransaction)newChanges.Changes[0]).SourceChange).NewElements[0];

            Assert.AreEqual(route.Id, newRoute.Id);
            //We have to leave out the changes with model elements in them when tesing for equality
            CollectionAssert.AreEqual(changes.Changes.Skip(1).ToArray(), newChanges.Changes.Skip(1).ToArray());

            //Load second instance of the model
            var newRepository = new ModelRepository();
            var newModel      = LoadRailwayModel(newRepository);

            //Apply changes to the new model
            newChanges.Apply(newRepository);

            Assert.AreEqual(model.Routes.Count, newModel.Routes.Count);
            Assert.AreEqual(model.Routes[0].DefinedBy.Count, newModel.Routes[0].DefinedBy.Count);
            Assert.AreEqual(model.Routes[0].DefinedBy[0].Elements.Count, newModel.Routes[0].DefinedBy[0].Elements.Count);
            Assert.AreEqual(model.Semaphores[0].Signal, newModel.Semaphores[0].Signal);
        }