Example #1
0
        private void Transform()
        {
            if (string.IsNullOrEmpty(tbOldDoc.Text) || string.IsNullOrEmpty(tbXslt.Text))
            {
                return;
            }

            string outDoc = XsltProcessing.Transform(tbOldDoc.Text, tbXslt.Text, BASE_DIR, SchemaVersion1, bSchemaAware.IsChecked == true);

            tbNewDoc.Text = outDoc;

            int    si = outDoc.IndexOf("xmlns:xsi=\"");
            int    ei;
            string text = outDoc;

            if (si >= 0)
            {
                ei   = outDoc.IndexOf("\"", si + "xmlns:xsi=\"".Length) + 1;
                text = outDoc.Remove(si, ei - si);
            }
            si = text.IndexOf("xmlns=\"");
            if (si >= 0)
            {
                ei = text.IndexOf("\"", si + "xmlns=\"".Length) + 1;
                string xmlns = text.Substring(si, ei - si);
                text = text.Remove(si, ei - si);
            }

            if (Environment.MachineName.Contains("TRUPIK"))
            {
                XmlDocumentHelper.SaveInUtf8(text.Replace("utf-16", "utf-8"), SAVE_DOCUMENT.Replace("LastInput", "LastOutput"));
            }
        }
Example #2
0
        private void Transform()
        {
            if (string.IsNullOrEmpty(tbOldDoc.Text) || string.IsNullOrEmpty(tbXslt.Text))
            {
                return;
            }

            string outDoc = XsltProcessing.Transform(tbOldDoc.Text, tbXslt.Text, BASE_DIR);

            tbNewDoc.Text = outDoc;

#if DEBUG
#if SAVE_DOC_FOR_TEST
            int    si   = outDoc.IndexOf("xmlns:xsi=\"");
            int    ei   = outDoc.IndexOf("\"", si + "xmlns:xsi=\"".Length) + 1;
            string text = outDoc.Remove(si, ei - si);
            si = text.IndexOf("xmlns=\"");
            ei = text.IndexOf("\"", si + "xmlns=\"".Length) + 1;
            string xmlns = text.Substring(si, ei - si);
            text = text.Remove(si, ei - si);
            File.WriteAllText(SAVE_DOCUMENT.Replace("LastInput", "LastOutput"), text.Replace("utf-16", "utf-8"), Encoding.UTF8);
#endif
#endif
        }
Example #3
0
        public void TestOCLAdaptation(object directory)
        {
            DirectoryInfo testDir = new DirectoryInfo(directory.ToString());

            try
            {
                FileInfo[] inputFiles  = testDir.GetFiles("*in.xml");
                FileInfo   projectFile = testDir.GetFiles("*.eXo").FirstOrDefault();

                if (projectFile == null)
                {
                    Assert.Fail("No project file found for test {0}", testDir.Name);
                }

                FileInfo[] stylesheetFiles = testDir.GetFiles("*.xslt");

                FileInfo referenceStylesheetFileInfo = null;

                foreach (FileInfo stylesheetFile in stylesheetFiles)
                {
                    if (stylesheetFile.Name.Contains("DesiredStylesheet") ||
                        stylesheetFile.Name.Contains("LastStylesheet") ||
                        stylesheetFile.Name.Contains("generated") ||
                        stylesheetFile.Name.Contains("last-working"))
                    {
                        continue;
                    }

                    referenceStylesheetFileInfo = stylesheetFile;
                }

                Project project = serializationManager.LoadProject(projectFile);

                XsltAdaptationScriptGenerator generator               = new XsltAdaptationScriptGenerator();
                ChangeDetector             detector                   = new ChangeDetector();
                PSMSchema                  psmSchemaOldVersion        = project.ProjectVersions[0].PSMSchemas.First();
                PSMSchema                  psmSchemaNewVersion        = project.ProjectVersions[1].PSMSchemas.First();
                DetectedChangeInstancesSet detectedChangeInstancesSet = detector.DetectChanges(psmSchemaOldVersion, psmSchemaNewVersion);
                generator.Initialize(psmSchemaOldVersion, psmSchemaNewVersion, detectedChangeInstancesSet);
                generator.SchemaAware = true;
                generator.GenerateTransformationStructure();
                XDocument testGeneratedStylesheet         = generator.GetAdaptationTransformation();
                string    testGeneratedStylesheetFileName = testDir.FullName + "/" + testDir.Name + "-generated.xslt";
                testGeneratedStylesheet.SaveInUtf8(testGeneratedStylesheetFileName);
                Console.WriteLine("Revalidation stylesheet generated.");
                Console.WriteLine();

                string generatedStylesheetText = File.ReadAllText(testGeneratedStylesheetFileName);

                StringBuilder failMessage         = new StringBuilder();
                int           failCounts          = 0;
                StringBuilder inconclusiveMessage = new StringBuilder();


                if (referenceStylesheetFileInfo != null)
                {
                    //XDocument xdRefStylesheet = XDocument.Load(referenceStylesheet.FullName);
                    //xdRefStylesheet.RemoveComments();
                    //string  referenceStylesheetText = xdRefStylesheet.ToString();
                    //XDocument xdGenStylesheet = XDocument.Load(testGeneratedStylesheetFile);
                    //xdGenStylesheet.RemoveComments();
                    //generatedStylesheetText = xdGenStylesheet.ToString();
                    string diff;
                    if (XmlDocumentHelper.DocumentCompare(testGeneratedStylesheetFileName, referenceStylesheetFileInfo.FullName, out diff,
                                                          XmlDiffOptions.IgnoreComments | XmlDiffOptions.IgnoreWhitespace))
                    {
                        string message = string.Format("Generated stylesheet for {1} is identical to reference stylesheet {0}.", referenceStylesheetFileInfo.Name, testDir.Name);
                        Console.WriteLine(message);
                    }
                    else
                    {
                        string message = string.Format("Generated stylesheet for {1} differs from reference stylesheet {0}.", referenceStylesheetFileInfo.Name, testDir.Name);
                        Console.WriteLine(message);
                        inconclusiveMessage.AppendLine(message);
                    }
                }
                else
                {
                    string message = string.Format("No reference stylesheet available. ");
                    Console.WriteLine(message);
                }
                Console.WriteLine();

                string tmpDir = Path.GetTempPath();

                foreach (FileInfo inputFile in inputFiles)
                {
                    Console.WriteLine("Testing revalidation of file {0}.", inputFile.Name);

                    string input  = File.ReadAllText(inputFile.FullName);
                    string output = XsltProcessing.TransformSAXON(inputFile.FullName, testGeneratedStylesheetFileName, true);

                    string genOutputFile = inputFile.FullName.Replace("in.xml", "out-generated.xml");
                    File.WriteAllText(genOutputFile, output);
                    Console.WriteLine("XSLT transformation succeeded, results written to {0}. ", genOutputFile);

                    string refOutputFile = inputFile.FullName.Replace("in.xml", "out.xml");
                    if (File.Exists(refOutputFile))
                    {
                        bool bIdentical = XmlDocumentHelper.DocumentCompare(refOutputFile, genOutputFile, XmlDiffOptions.IgnoreXmlDecl | XmlDiffOptions.IgnoreComments);
                        if (bIdentical)
                        {
                            string message = string.Format("Output for file {0} is identical to the reference output. ", inputFile.Name);
                            Console.WriteLine(message);
                        }
                        else
                        {
                            string message = string.Format("Result differs from reference output for file {0}", inputFile.Name);
                            Console.WriteLine(message);
                            failMessage.AppendLine(message);
                            failCounts++;
                        }
                    }
                    else
                    {
                        string message = string.Format("Reference output not found for file {0}", inputFile.Name);
                        Console.WriteLine(message);
                        inconclusiveMessage.AppendLine(message);
                    }
                }

                if (inputFiles.Length == 0)
                {
                    string message = string.Format("No input files for test {0}", testDir.Name);
                    Console.WriteLine(message);
                    inconclusiveMessage.AppendLine(message);
                }

                if (failCounts > 0)
                {
                    string finalMessage = string.Format("Failed for {0}/{1} test inputs. \r\n", failCounts, inputFiles.Length);
                    Console.WriteLine(finalMessage);
                    failMessage.Insert(0, finalMessage);
                    Assert.Fail(failMessage.ToString());
                }

                if (inconclusiveMessage.Length > 0)
                {
                    Assert.Inconclusive(inconclusiveMessage.ToString());
                }

                if (failCounts == 0)
                {
                    string lastWorkingStylesheetFile = testDir.FullName + "/" + testDir.Name + "-last-working.xslt";
                    testGeneratedStylesheet.SaveInUtf8(lastWorkingStylesheetFile);
                }

                Console.WriteLine("Test succeeded. ");
            }
            catch (Exception exception)
            {
            }
        }
Example #4
0
        public void TestByCompare(string filename)
        {
            //string filename = "Attributes\\Attributes5.XCase";
            XmlDeserializatorVersions deserializator = new XmlDeserializatorVersions();
            VersionManager            versionManager = deserializator.RestoreVersionedProject(TEST_BASE_DIR + filename);

            Assert.IsTrue(versionManager != null && versionManager.VersionedProjects.Count == 2 &&
                          versionManager.Versions.Count == 2);

            Version    oldVersion        = versionManager.Versions[0];
            Version    newVersion        = versionManager.Versions[1];
            PSMDiagram diagramNewVersion = versionManager.VersionedProjects[newVersion].PSMDiagrams[0];
            PSMDiagram diagramOldVersion = versionManager.VersionedProjects[oldVersion].PSMDiagrams[0];

            ChangesDetector        detector         = new ChangesDetector();
            List <EvolutionChange> evolutionChanges = detector.Translate(diagramOldVersion, diagramNewVersion);

            XsltTemplateGenerator xsltGenerator = new XsltTemplateGenerator(diagramNewVersion);
            string xsltTransformation           = xsltGenerator.Generate(evolutionChanges, oldVersion, newVersion);

            string inputFileDir  = TEST_BASE_DIR + Path.GetDirectoryName(filename) + "\\" + Path.GetFileNameWithoutExtension(filename) + "-in";
            string outputFileDir = TEST_BASE_DIR + Path.GetDirectoryName(filename) + "\\" + Path.GetFileNameWithoutExtension(filename) + "-out";

            if (!Directory.Exists(inputFileDir))
            {
                Console.WriteLine("No input files. ");
                Assert.Inconclusive(string.Format("Input file directory does not exist : {0}", inputFileDir));
            }
            if (!Directory.Exists(outputFileDir))
            {
                Console.WriteLine("No reference files. ");
                Assert.Inconclusive(string.Format("Output file directory does not exist: {0}", outputFileDir));
            }

            DirectoryInfo dir = new DirectoryInfo(inputFileDir);

            FileInfo[] files       = dir.GetFiles("*.xml");
            bool       fail        = false;
            int        failedFiles = 0;

            foreach (FileInfo fileInfo in files)
            {
                Console.WriteLine(string.Format("File: {0}", fileInfo.Name));
                string input = File.ReadAllText(fileInfo.FullName);

                string result = XsltProcessing.Transform(input, xsltTransformation, TEST_BASE_DIR);
                File.WriteAllText(outputFileDir + "//" + fileInfo, result);

                DocumentValidator validator = new DocumentValidator {
                    SilentMode = true
                };

                bool inputInvalid  = false;
                bool resultInvalid = false;
                bool compareFail   = false;
                if (!validator.ValidateDocument(diagramOldVersion, input))
                {
                    inputInvalid = true;
                }

                if (!validator.ValidateDocument(diagramNewVersion, result))
                {
                    Console.WriteLine("Result document INVALID: " + validator.ErrorMessage);
                    resultInvalid = true;
                }

                string referenceFile = outputFileDir + "//" + Path.GetFileNameWithoutExtension(fileInfo.Name) + "-reference.xml";
                Assert.IsTrue(File.Exists(referenceFile), "Reference output file does not exist for: " + fileInfo.Name);
                string referenceOutput = File.ReadAllText(referenceFile);

                File.WriteAllText(outputFileDir + "//" + "last-stylesheet.xslt", xsltTransformation);

                if ((!inputInvalid && !resultInvalid) && result != referenceOutput)
                {
                    Console.WriteLine("Output and REFERENCE output DIFFERS for: " + fileInfo.Name);
                    compareFail = true;
                    Console.WriteLine("Input file: ");
                    Console.WriteLine(input);
                    Console.WriteLine("Result file: ");
                    Console.WriteLine(result);
                    Console.WriteLine("Reference file: ");
                    Console.WriteLine(referenceOutput);
                }
                else if (!inputInvalid && !resultInvalid && result == referenceOutput)
                {
                    Console.WriteLine("Comparison succeeded.");
                    File.WriteAllText(outputFileDir + "//" + "last-working-stylesheet.xslt", xsltTransformation);
                }

                if (inputInvalid)
                {
                    Console.WriteLine("Input document INVALID: " + validator.ErrorMessage);
                    Console.WriteLine("Input file: ");
                    Console.WriteLine(input);
                    fail = true;
                    failedFiles++;
                }

                if (resultInvalid)
                {
                    Console.WriteLine("Result document INVALID: " + validator.ErrorMessage);
                    Console.WriteLine("Result file: ");
                    Console.WriteLine(result);
                    fail = true;
                    failedFiles++;
                }

                if (compareFail)
                {
                    Console.WriteLine("Result of {0} DIFFERS from expected result.", fileInfo.Name);
                    fail = true;
                    failedFiles++;
                }
            }
            if (files.Length == 0)
            {
                Console.WriteLine("No input files. ");
                Assert.Inconclusive("No input files. ");
            }
            if (fail)
            {
                Console.WriteLine("{0} out of {1} files FAILED. ", failedFiles, files.Length);
                Assert.Fail("{0} out of {1} files FAILED. ", failedFiles, files.Length);
            }
            Console.WriteLine("Altogether tested on {0} files.", files.Length);
        }
Example #5
0
        public void TestValidationOnRandomFiles(string filename)
        {
            Console.WriteLine("Testing file {0}.", filename);
            //string filename = "Attributes\\Attributes5.XCase";
            XmlDeserializatorVersions deserializator = new XmlDeserializatorVersions();
            VersionManager            versionManager = deserializator.RestoreVersionedProject(TEST_BASE_DIR + filename);

            Assert.IsTrue(versionManager != null && versionManager.VersionedProjects.Count == 2 &&
                          versionManager.Versions.Count == 2);

            Version    oldVersion        = versionManager.Versions[0];
            Version    newVersion        = versionManager.Versions[1];
            PSMDiagram diagramNewVersion = versionManager.VersionedProjects[newVersion].PSMDiagrams[0];
            PSMDiagram diagramOldVersion = versionManager.VersionedProjects[oldVersion].PSMDiagrams[0];

            ChangesDetector        detector         = new ChangesDetector();
            List <EvolutionChange> evolutionChanges = detector.Translate(diagramOldVersion, diagramNewVersion);

            XsltTemplateGenerator xsltGenerator = new XsltTemplateGenerator(diagramNewVersion);
            string xsltTransformation           = xsltGenerator.Generate(evolutionChanges, oldVersion, newVersion);

            SampleDataGenerator g         = new SampleDataGenerator();
            DocumentValidator   validator = new DocumentValidator {
                SilentMode = true
            };

            string outputFileDir = TEST_BASE_DIR + Path.GetDirectoryName(filename) + "\\" + Path.GetFileNameWithoutExtension(filename) + "-error";

            int fails = 0;

            for (int i = 0; i < RANDOM_FILE_COUNT; i++)
            {
                string input = g.Translate(diagramOldVersion);
                if (!validator.ValidateDocument(diagramOldVersion, input))
                {
                    if (!Directory.Exists(outputFileDir))
                    {
                        Directory.CreateDirectory(outputFileDir);
                    }
                    File.WriteAllText(string.Format("{0}\\i{1}.xml", outputFileDir, i), input);
                    Console.WriteLine("Input document INVALID: " + validator.ErrorMessage);
                    fails++;
                    continue;
                }
                string result = XsltProcessing.Transform(input, xsltTransformation, TEST_BASE_DIR);
                if (!validator.ValidateDocument(diagramNewVersion, result))
                {
                    if (!Directory.Exists(outputFileDir))
                    {
                        Directory.CreateDirectory(outputFileDir);
                    }
                    File.WriteAllText(string.Format("{0}\\i{1}.xml", outputFileDir, i), input);
                    File.WriteAllText(string.Format("{0}\\o{1}.xml", outputFileDir, i), result);
                    Console.WriteLine("Result document INVALID: " + validator.ErrorMessage);
                    fails++;
                    continue;
                }
                Console.WriteLine("Test passed on file {0}/{1}", i, RANDOM_FILE_COUNT);
            }
            Console.WriteLine("Altogether tested on {0} random files. ", RANDOM_FILE_COUNT);
            if (fails != 0)
            {
                Console.WriteLine("{0} out of {1} files failed to pass. ", fails, RANDOM_FILE_COUNT);
                Assert.Fail("{0} out of {1} files failed to pass. ", fails, RANDOM_FILE_COUNT);
            }
            else
            {
                Console.WriteLine("All {0} files passed. ", RANDOM_FILE_COUNT);
            }
        }