Пример #1
0
        public void Test_SaveCommands()
        {
            RenamePropertyCommand command1 = new RenamePropertyCommand();

            command1.TypeName        = "TestUser";
            command1.PropertyName    = "LastName";
            command1.NewPropertyName = "Surname";

            RenameTypeCommand command2 = new RenameTypeCommand();

            command2.TypeName    = "TestUser";
            command2.NewTypeName = "TestAccount";

            DataSchemaCommandCollection commands = new DataSchemaCommandCollection();

            commands.Add(command1);
            commands.Add(command2);

            Version legacyVersion  = new Version(1, 0, 0, 0);
            Version commandVersion = new Version(1, 1, 0, 0);
            Version currentVersion = new Version(1, 1, 0, 0);

            string groupName = "TestGroup";

            string commandsPath = TestUtilities.GetTestingPath(this) + Path.DirectorySeparatorChar + DataAccess.Data.Schema.SchemaDirectory;

            DataAccess.Data.Schema.SaveCommands(commandsPath, commands, groupName, commandVersion);

            int fileCount = Directory.GetFiles(commandsPath).Length;

            Assert.AreEqual(1, fileCount, "Incorrect number of files created.");
        }
Пример #2
0
        public void Test_CheckIsUpToDate_False()
        {
            RenamePropertyCommand command1 = new RenamePropertyCommand();

            command1.TypeName        = "TestUser";
            command1.PropertyName    = "LastName";
            command1.NewPropertyName = "Surname";

            RenameTypeCommand command2 = new RenameTypeCommand();

            command2.TypeName    = "TestUser";
            command2.NewTypeName = "TestAccount";

            DataSchemaCommandCollection commands = new DataSchemaCommandCollection();

            commands.Add(command1);
            commands.Add(command2);

            Version legacyVersion  = new Version(1, 0, 0, 0);
            Version commandVersion = new Version(1, 1, 0, 0);
            Version currentVersion = new Version(1, 1, 0, 0);

            string groupName = "TestGroup";

            string commandsPath = TestUtilities.GetTestingPath(this) + Path.DirectorySeparatorChar + DataAccess.Data.Schema.SchemaDirectory;


            DataSchema schema = (DataSchema)DataAccess.Data.InitializeDataSchema();

            schema.SchemaDirectory = "Testing" + Path.DirectorySeparatorChar + "Schema";

            schema.LegacyVersion      = legacyVersion;
            schema.ApplicationVersion = currentVersion;

            schema.SaveCommands(commandsPath, commands, groupName, commandVersion);

            bool isUpToDate = schema.CheckIsUpToDate();
        }
Пример #3
0
        public void Test_GetCommands()
        {
            RenamePropertyCommand command1 = new RenamePropertyCommand();

            command1.TypeName        = "TestUser";
            command1.PropertyName    = "LastName";
            command1.NewPropertyName = "Surname";

            RenameTypeCommand command2 = new RenameTypeCommand();

            command2.TypeName    = "TestUser";
            command2.NewTypeName = "TestAccount";

            DataSchemaCommandCollection commands = new DataSchemaCommandCollection();

            commands.Add(command1);
            commands.Add(command2);

            Version legacyVersion  = new Version(1, 0, 0, 0);
            Version commandVersion = new Version(1, 1, 0, 0);
            Version currentVersion = new Version(1, 1, 0, 0);

            string groupName = "TestGroup";

            string commandsPath = TestUtilities.GetTestingPath(this) + Path.DirectorySeparatorChar + DataAccess.Data.Schema.SchemaDirectory;

            DataSchema schema = (DataSchema)DataAccess.Data.InitializeDataSchema();

            schema.SchemaCommandDirectoryPath = commandsPath;
            schema.LegacyVersion      = legacyVersion;
            schema.ApplicationVersion = currentVersion;

            schema.SaveCommands(commandsPath, commands, groupName, commandVersion);

            DataSchemaCommandCollection foundCommands = schema.GetCommands();

            Assert.AreEqual(commands.Count, foundCommands.Count, "Incorrect number of commands found.");
        }
Пример #4
0
        public void Test_RenewSchema()
        {
            TestUser user = new TestUser();

            user.ID        = Guid.NewGuid();
            user.FirstName = "--FirstName--";
            user.LastName  = "--LastName--";
            user.Email     = "--Email--";
            user.Username  = "******";

            XmlDocument document = XmlUtilities.SerializeToDocument(user);

            RenamePropertyCommand command1 = new RenamePropertyCommand();

            command1.PropertyName    = "LastName";
            command1.NewPropertyName = "Surname";
            command1.TypeName        = "TestUser";

            DataSchemaCommandCollection commands = new DataSchemaCommandCollection();

            commands.Add(command1);

            DataSchema schema = (DataSchema)DataAccess.Data.InitializeDataSchema();

            schema.SchemaCommands = commands;

            schema.RenewSchema(document);

            TestUser user2 = (TestUser)XmlUtilities.DeserializeFromDocument(document, user.GetType());

            Assert.IsNotNull(user2, "user2 == null");

            Assert.AreEqual(user.ID.ToString(), user2.ID.ToString(), "The IDs don't match.");

            Assert.AreEqual(user.LastName, user2.Surname, "The value of the LastName wasn't moved to the Surname property like it should have.");
        }
Пример #5
0
        public void Test_ImportFromXml_RenewSchema_TypeAndProperty()
        {
            Version currentVersion = new Version(1, 0, 0, 0);
            Version legacyVersion  = new Version(0, 9, 0, 0);

            TestUser user = new TestUser();

            user.ID        = Guid.NewGuid();
            user.FirstName = "Test";
            user.LastName  = "Test";

            TestRole role = new TestRole();

            role.ID   = Guid.NewGuid();
            role.Name = "Test Role";


            user.Roles = new TestRole[] { role };


            DataExporter exporter = (DataExporter)DataAccess.Data.InitializeDataExporter();

            exporter.ExportDirectoryPath = TestUtilities.GetTestingPath(this) + Path.DirectorySeparatorChar + "Exported";

            // IMPORTANT: Export the references before the entities, because the references are stripped from the entities upon export
            // Alternative is to reactivate the entities and their references using DataAccess.Data.Activator.
            EntityReferenceCollection references = DataAccess.Data.Referencer.GetActiveReferences(user);

            exporter.ExportEntity(references[0]);

            exporter.ExportEntity(user);
            exporter.ExportEntity(role);


            DataSchema schema = (DataSchema)DataAccess.Data.Schema;

            schema.LegacyVersion      = legacyVersion;
            schema.ApplicationVersion = currentVersion;

            schema.SchemaCommandDirectoryPath = TestUtilities.GetTestingPath(this) + Path.DirectorySeparatorChar + "Schema";

            RenamePropertyCommand command1 = new RenamePropertyCommand();

            command1.TypeName        = user.ShortTypeName;
            command1.PropertyName    = "Roles";
            command1.NewPropertyName = "GrantedRoles";

            RenameTypeCommand command2 = new RenameTypeCommand();

            command2.TypeName    = user.ShortTypeName;
            command2.NewTypeName = typeof(TestAccount).Name;

            RenamePropertyCommand command3 = new RenamePropertyCommand();

            command3.TypeName        = role.ShortTypeName;
            command3.PropertyName    = "Users";
            command3.NewPropertyName = "Accounts";

            DataSchemaCommandCollection commands = new DataSchemaCommandCollection();

            commands.Add(command1);
            commands.Add(command2);
            commands.Add(command3);

            schema.SchemaCommands = commands;

            DataImporter importer = (DataImporter)DataAccess.Data.Importer;

            importer.ImportableDirectoryPath = exporter.ExportDirectoryPath;
            importer.ImportedDirectoryPath   = TestUtilities.GetTestingPath(this) + Path.DirectorySeparatorChar + "Imported";

            importer.ImportFromXml();



            TestAccount foundAccount = DataAccess.Data.Reader.GetEntity <TestAccount>("ID", user.ID);

            Assert.IsNotNull(foundAccount, "foundAccount == null");

            Assert.AreEqual(user.ID.ToString(), foundAccount.ID.ToString(), "The ID of the found account doesn't match the ID of the original user.");

            DataAccess.Data.Activator.Activate(foundAccount, "GrantedRoles");

            Assert.IsNotNull(foundAccount.GrantedRoles, "user.GrantedRoles == null");

            Assert.AreEqual(1, foundAccount.GrantedRoles.Length, "Invalid number of roles found.");



            TestRole foundRole = DataAccess.Data.Reader.GetEntity <TestRole>("ID", role.ID);

            Assert.IsNotNull(foundRole, "foundRole == null");

            Assert.AreEqual(role.ID.ToString(), foundRole.ID.ToString(), "The ID of the found role doesn't match the ID of the original role.");
        }
Пример #6
0
        public void Test_SaveCommands()
        {
            RenamePropertyCommand command1 = new RenamePropertyCommand();
            command1.TypeName = "TestUser";
            command1.PropertyName = "LastName";
            command1.NewPropertyName = "Surname";

            RenameTypeCommand command2 = new RenameTypeCommand();
            command2.TypeName = "TestUser";
            command2.NewTypeName = "TestAccount";

            DataSchemaCommandCollection commands = new DataSchemaCommandCollection();
            commands.Add(command1);
            commands.Add(command2);

            Version legacyVersion = new Version(1, 0, 0, 0);
            Version commandVersion = new Version(1, 1, 0, 0);
            Version currentVersion = new Version(1, 1, 0, 0);

            string groupName = "TestGroup";

            string commandsPath = TestUtilities.GetTestingPath(this) + Path.DirectorySeparatorChar + DataAccess.Data.Schema.SchemaDirectory;

            DataAccess.Data.Schema.SaveCommands(commandsPath, commands, groupName, commandVersion);

            int fileCount = Directory.GetFiles(commandsPath).Length;

            Assert.AreEqual(1, fileCount, "Incorrect number of files created.");
        }
Пример #7
0
        public void Test_RenewSchema()
        {
            TestUser user = new TestUser();
            user.ID = Guid.NewGuid();
            user.FirstName = "--FirstName--";
            user.LastName = "--LastName--";
            user.Email = "--Email--";
            user.Username = "******";

            XmlDocument document = XmlUtilities.SerializeToDocument(user);

            RenamePropertyCommand command1 = new RenamePropertyCommand();
            command1.PropertyName = "LastName";
            command1.NewPropertyName = "Surname";
            command1.TypeName = "TestUser";

            DataSchemaCommandCollection commands = new DataSchemaCommandCollection();
            commands.Add(command1);

            DataSchema schema = (DataSchema)DataAccess.Data.InitializeDataSchema();

            schema.SchemaCommands = commands;

            schema.RenewSchema(document);

            TestUser user2 = (TestUser)XmlUtilities.DeserializeFromDocument(document, user.GetType());

            Assert.IsNotNull(user2, "user2 == null");

            Assert.AreEqual(user.ID.ToString(), user2.ID.ToString(), "The IDs don't match.");

            Assert.AreEqual(user.LastName, user2.Surname, "The value of the LastName wasn't moved to the Surname property like it should have.");
        }
Пример #8
0
        public void Test_GetCommands()
        {
            RenamePropertyCommand command1 = new RenamePropertyCommand();
            command1.TypeName = "TestUser";
            command1.PropertyName = "LastName";
            command1.NewPropertyName = "Surname";

            RenameTypeCommand command2 = new RenameTypeCommand();
            command2.TypeName = "TestUser";
            command2.NewTypeName = "TestAccount";

            DataSchemaCommandCollection commands = new DataSchemaCommandCollection();
            commands.Add(command1);
            commands.Add(command2);

            Version legacyVersion = new Version(1, 0, 0, 0);
            Version commandVersion = new Version(1, 1, 0, 0);
            Version currentVersion = new Version(1, 1, 0, 0);

            string groupName = "TestGroup";

            string commandsPath = TestUtilities.GetTestingPath(this) + Path.DirectorySeparatorChar + DataAccess.Data.Schema.SchemaDirectory;

            DataSchema schema = (DataSchema)DataAccess.Data.InitializeDataSchema();

            schema.SchemaCommandDirectoryPath = commandsPath;
            schema.LegacyVersion = legacyVersion;
            schema.ApplicationVersion = currentVersion;

            schema.SaveCommands(commandsPath, commands, groupName, commandVersion);

            DataSchemaCommandCollection foundCommands = schema.GetCommands();

            Assert.AreEqual(commands.Count, foundCommands.Count, "Incorrect number of commands found.");
        }
Пример #9
0
        public void Test_CheckIsUpToDate_True()
        {
            RenamePropertyCommand command1 = new RenamePropertyCommand();
            command1.TypeName = "TestUser";
            command1.PropertyName = "LastName";
            command1.NewPropertyName = "Surname";

            RenameTypeCommand command2 = new RenameTypeCommand();
            command2.TypeName = "TestUser";
            command2.NewTypeName = "TestAccount";

            DataSchemaCommandCollection commands = new DataSchemaCommandCollection();
            commands.Add(command1);
            commands.Add(command2);

            Version legacyVersion = new Version(1, 0, 0, 0);
            Version commandVersion = new Version(0, 1, 0, 0);
            Version currentVersion = new Version(1, 1, 0, 0);

            string groupName = "TestGroup";

            string commandsPath = TestUtilities.GetTestingPath(this) + Path.DirectorySeparatorChar + DataAccess.Data.Schema.SchemaDirectory;

            DataSchema schema = (DataSchema)DataAccess.Data.InitializeDataSchema();

            schema.SchemaDirectory = "Testing" + Path.DirectorySeparatorChar + "Schema";

            schema.LegacyVersion = legacyVersion;
            schema.ApplicationVersion = currentVersion;

            schema.SaveCommands(commandsPath, commands, groupName, commandVersion);

            bool isUpToDate = schema.CheckIsUpToDate();
        }
Пример #10
0
        /// <summary>
        /// Serializes and saves the provided data commands to file ready for execution.
        /// </summary>
        /// <param name="commandsDirectoryPath">The physical path to the schema commands directory.</param>
        /// <param name="commands">The data schema command to serialize and save.</param>
        /// <param name="groupName">The name of the group/module that the commands apply to. (This is used in the file name.)</param>
        /// <param name="version">The version of data schema that the command corresponds with. (This is used in the file name.)</param>
        public void SaveCommands(string commandsDirectoryPath, DataSchemaCommandCollection commands, string groupName, Version version)
        {
            string commandFile = CreateCommandFilePath(commandsDirectoryPath, groupName, version);

            if (!Directory.Exists(Path.GetDirectoryName(commandFile)))
                Directory.CreateDirectory(Path.GetDirectoryName(commandFile));

            // Define the extra types that might be required during serialization
            Type[] extraTypes = new Type[]
            {
                typeof(RenameTypeCommand),
                typeof(RenamePropertyCommand)
            };

            XmlSerializer serializer = new XmlSerializer(commands.GetType(), extraTypes);
            using (StreamWriter writer = File.CreateText(commandFile))
            {
                serializer.Serialize(writer, commands);

                writer.Close();
            }
        }
Пример #11
0
        /// <summary>
        /// Serializes and saves the provided data commands to file ready for execution.
        /// </summary>
        /// <param name="commands">The data schema command to serialize and save.</param>
        /// <param name="groupName">The name of the group/module that the commands apply to. (This is used in the file name.)</param>
        /// <param name="version">The version of data schema that the command corresponds with. (This is used in the file name.)</param>
        public void SaveCommands(DataSchemaCommandCollection commands, string groupName, Version version)
        {
            string path = Configuration.Config.Application.PhysicalApplicationPath + Path.DirectorySeparatorChar + SchemaDirectory;

            SaveCommands(path, commands, groupName, version);
        }
Пример #12
0
        /// <summary>
        /// Retrieves the schema commands that are applicable to legacy data (between legacy version and current application version).
        /// </summary>
        /// <returns>A collection of the commands that need to be applied the legacy data.</returns>
        public DataSchemaCommandCollection GetCommands()
        {
            DataSchemaCommandCollection collection = new DataSchemaCommandCollection();

            string schemaCommandDirectoryPath = SchemaCommandDirectoryPath;
            Version legacyVersion = LegacyVersion;
            Version currentVersion = ApplicationVersion;

            if (Directory.Exists(schemaCommandDirectoryPath))
            {
                foreach (string file in Directory.GetFiles(schemaCommandDirectoryPath))
                {
                    Version commandVersion = ExtractVersionFromFileName(file);

                    // If the version specified in the command file is greater than the legacy version and less than or equal to the current version (inclusive)
                    if (commandVersion > legacyVersion &&
                        commandVersion <= currentVersion)
                    {
                        // Define the extra types that might be required during serialization
                        Type[] extraTypes = new Type[]
                        {
                            typeof(RenameTypeCommand),
                            typeof(RenamePropertyCommand)
                        };

                        XmlSerializer serializer = new XmlSerializer(typeof(DataSchemaCommandCollection), extraTypes);
                        using (StreamReader reader = new StreamReader(File.OpenRead(file)))
                        {
                            DataSchemaCommandCollection c = (DataSchemaCommandCollection)serializer.Deserialize(reader);

                            collection.AddRange(c);

                            reader.Close();
                        }
                    }
                }
            }

            return collection;
        }
Пример #13
0
 /// <summary>
 /// Executes the requires commands from those provided. Changes already applied once are not
 /// re-applied.
 /// </summary>
 /// <param name="commands">The commands to apply.</param>
 public void ApplyCommands(DataSchemaCommandCollection commands)
 {
     throw new NotImplementedException();
 }
Пример #14
0
        public void Test_ImportFromXml_RenewSchema_TypeAndProperty()
        {
            Version currentVersion = new Version(1, 0, 0, 0);
            Version legacyVersion = new Version(0, 9, 0, 0);

            TestUser user = new TestUser();
            user.ID = Guid.NewGuid();
            user.FirstName = "Test";
            user.LastName = "Test";

            TestRole role = new TestRole();
            role.ID = Guid.NewGuid();
            role.Name = "Test Role";

            user.Roles = new TestRole[] {role};

            DataExporter exporter = (DataExporter)DataAccess.Data.InitializeDataExporter();

            exporter.ExportDirectoryPath = TestUtilities.GetTestingPath(this) + Path.DirectorySeparatorChar + "Exported";

            // IMPORTANT: Export the references before the entities, because the references are stripped from the entities upon export
            // Alternative is to reactivate the entities and their references using DataAccess.Data.Activator.
            EntityReferenceCollection references = DataAccess.Data.Referencer.GetActiveReferences(user);

            exporter.ExportEntity(references[0]);

            exporter.ExportEntity(user);
            exporter.ExportEntity(role);

            DataSchema schema = (DataSchema)DataAccess.Data.Schema;

            schema.LegacyVersion = legacyVersion;
            schema.ApplicationVersion = currentVersion;

            schema.SchemaCommandDirectoryPath = TestUtilities.GetTestingPath(this) + Path.DirectorySeparatorChar + "Schema";

            RenamePropertyCommand command1 = new RenamePropertyCommand();
            command1.TypeName = user.ShortTypeName;
            command1.PropertyName = "Roles";
            command1.NewPropertyName = "GrantedRoles";

            RenameTypeCommand command2 = new RenameTypeCommand();
            command2.TypeName = user.ShortTypeName;
            command2.NewTypeName = typeof(TestAccount).Name;

            RenamePropertyCommand command3 = new RenamePropertyCommand();
            command3.TypeName = role.ShortTypeName;
            command3.PropertyName = "Users";
            command3.NewPropertyName = "Accounts";

            DataSchemaCommandCollection commands = new DataSchemaCommandCollection();
            commands.Add(command1);
            commands.Add(command2);
            commands.Add(command3);

            schema.SchemaCommands = commands;

            DataImporter importer = (DataImporter)DataAccess.Data.Importer;

            importer.ImportableDirectoryPath = exporter.ExportDirectoryPath;
            importer.ImportedDirectoryPath = TestUtilities.GetTestingPath(this) + Path.DirectorySeparatorChar + "Imported";

            importer.ImportFromXml();

            TestAccount foundAccount = DataAccess.Data.Reader.GetEntity<TestAccount>("ID", user.ID);

            Assert.IsNotNull(foundAccount, "foundAccount == null");

            Assert.AreEqual(user.ID.ToString(), foundAccount.ID.ToString(), "The ID of the found account doesn't match the ID of the original user.");

            DataAccess.Data.Activator.Activate(foundAccount, "GrantedRoles");

            Assert.IsNotNull(foundAccount.GrantedRoles, "user.GrantedRoles == null");

            Assert.AreEqual(1, foundAccount.GrantedRoles.Length, "Invalid number of roles found.");

            TestRole foundRole = DataAccess.Data.Reader.GetEntity<TestRole>("ID", role.ID);

            Assert.IsNotNull(foundRole, "foundRole == null");

            Assert.AreEqual(role.ID.ToString(), foundRole.ID.ToString(), "The ID of the found role doesn't match the ID of the original role.");
        }