Example #1
0
        public void XsltNoInput()
        {
            Xslt task = new Xslt();
            task.BuildEngine = new MockBuild();

            // assert default values
            Assert.IsNull(task.Inputs, @"Wrong default inputs");
            Assert.IsNull(task.RootTag, @"Wrong default root tag");
            Assert.IsNull(task.RootAttributes, @"Wrong default root attributes");
            Assert.IsNull(task.Xsl, @"Wrong default xsl");
            Assert.IsNull(task.Output, @"Wrong default output");

            // executing without input files must fail
            Assert.IsFalse(task.Execute(), @"Task without input files did not fail");
        }
Example #2
0
        public void XsltOneInput()
        {
            Xslt task = new Xslt();
            task.BuildEngine = new MockBuild();

            string testDir;
            string assemblyDir = TaskUtility.AssemblyDirectory;
            TaskUtility.logEnvironmentInfo(task.Log);
            if (TaskUtility.CalledInBuildDirectory)
            {
                task.Log.LogMessage("Called in build directory");

                // get the files from where msbuild put them
                testDir = TaskUtility.TestDirectory;
            }
            else
            {
                task.Log.LogMessage("Not called in build directory");

                // get the files from where Visual Studio put them
                testDir = Path.Combine(TaskUtility.AssemblyDirectory, @"Xslt");
            }

            task.Inputs = TaskUtility.StringArrayToItemArray(Path.Combine(testDir, @"XsltTestInput.xml"));
            task.Inputs[0].SetMetadata(@"configuration", "test");
            task.Xsl = new TaskItem(Path.Combine(assemblyDir, @"NUnitReport.xsl"));
            task.Xsl.SetMetadata(@"project", "XsltTest");

            task.RootTag = "mergedroot";
            task.Output = Path.Combine(testDir, @"XsltTestOutput.html");

            Assert.IsNotNull(task.Inputs, @"No inputs");
            Assert.IsNull(task.RootAttributes, @"Wrong default root attributes");
            Assert.IsNotNull(task.Xsl, @"No xsl");
            Assert.IsNotNull(task.Output, @"No output");

            // executing with one input files
            Assert.IsTrue(task.Execute(), @"Task with one input files failed");
            Assert.IsTrue(File.Exists(task.Output), @"Missing output file");

            string taskOutput = File.ReadAllText(task.Output);
            Assert.IsTrue(taskOutput.Contains(
                @"<h1>Project ""XsltTest"" - Configuration ""test"" - NUnit Test Report</h1>"),
                @"Input or Xsl metadata not correctly added.");
        }
Example #3
0
            public void TestTransform()
            {
                // Given
                string xsltInput = string.Empty
                                   + "<xsl:stylesheet xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\" version=\"1.0\">"
                                   + "<xsl:template match=\"bookstore\">"
                                   + "  <HTML>"
                                   + "    <BODY>"
                                   + "      <TABLE BORDER=\"2\">"
                                   + "        <TR>"
                                   + "          <TD>ISBN</TD>"
                                   + "          <TD>Title</TD>"
                                   + "          <TD>Price</TD>"
                                   + "        </TR>"
                                   + "        <xsl:apply-templates select=\"book\"/>"
                                   + "      </TABLE>"
                                   + "    </BODY>"
                                   + "  </HTML>"
                                   + "</xsl:template>"
                                   + "<xsl:template match=\"book\">"
                                   + "  <TR>"
                                   + "    <TD><xsl:value-of select=\"@ISBN\"/></TD>"
                                   + "    <TD><xsl:value-of select=\"title\"/></TD>"
                                   + "    <TD><xsl:value-of select=\"price\"/></TD>"
                                   + "  </TR>"
                                   + "</xsl:template>"
                                   + "</xsl:stylesheet>";

                string input = string.Empty
                               + "<?xml version='1.0'?>"
                               + "<!-- This file represents a fragment of a book store inventory database -->"
                               + "<bookstore>"
                               + "  <book genre=\"autobiography\" publicationdate=\"1981\" ISBN=\"1-861003-11-0\">"
                               + "    <title>The Autobiography of Benjamin Franklin</title>"
                               + "    <author>"
                               + "      <first-name>Benjamin</first-name>"
                               + "      <last-name>Franklin</last-name>"
                               + "    </author>"
                               + "    <price>8.99</price>"
                               + "  </book>"
                               + "  <book genre=\"novel\" publicationdate=\"1967\" ISBN=\"0-201-63361-2\">"
                               + "    <title>The Confidence Man</title>"
                               + "    <author>"
                               + "      <first-name>Herman</first-name>"
                               + "      <last-name>Melville</last-name>"
                               + "    </author>"
                               + "    <price>11.99</price>"
                               + "  </book>"
                               + "  <book genre=\"philosophy\" publicationdate=\"1991\" ISBN=\"1-861001-57-6\">"
                               + "    <title>The Gorgias</title>"
                               + "    <author>"
                               + "      <name>Plato</name>"
                               + "    </author>"
                               + "    <price>9.99</price>"
                               + "  </book>"
                               + "</bookstore>";

                string output = "<HTML><BODY><TABLE BORDER=\"2\"><TR><TD>ISBN</TD><TD>Title</TD><TD>Price</TD></TR><TR><TD>1-861003-11-0</TD><TD>The Autobiography of Benjamin Franklin</TD><TD>8.99</TD></TR><TR><TD>0-201-63361-2</TD><TD>The Confidence Man</TD><TD>11.99</TD></TR><TR><TD>1-861001-57-6</TD><TD>The Gorgias</TD><TD>9.99</TD></TR></TABLE></BODY></HTML>";

                TestExecutionContext context      = new TestExecutionContext();
                IDocument            document     = new TestDocument(input);
                IDocument            xsltDocument = new TestDocument(xsltInput);
                IModule module = new Execute(_ => new[] { xsltDocument });
                Xslt    xslt   = new Xslt(module);

                // When
                IList <IDocument> results = xslt.Execute(new[] { document }, context).ToList();  // Make sure to materialize the result list

                // Then
                Assert.That(results.Select(x => x.Content), Is.EquivalentTo(new[] { output }));
            }
Example #4
0
            public void TestTransform()
            {
                // Given
                string xsltInput = ""
                                   + "<xsl:stylesheet xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\" version=\"1.0\">"
                                   + "<xsl:template match=\"bookstore\">"
                                   + "  <HTML>"
                                   + "    <BODY>"
                                   + "      <TABLE BORDER=\"2\">"
                                   + "        <TR>"
                                   + "          <TD>ISBN</TD>"
                                   + "          <TD>Title</TD>"
                                   + "          <TD>Price</TD>"
                                   + "        </TR>"
                                   + "        <xsl:apply-templates select=\"book\"/>"
                                   + "      </TABLE>"
                                   + "    </BODY>"
                                   + "  </HTML>"
                                   + "</xsl:template>"
                                   + "<xsl:template match=\"book\">"
                                   + "  <TR>"
                                   + "    <TD><xsl:value-of select=\"@ISBN\"/></TD>"
                                   + "    <TD><xsl:value-of select=\"title\"/></TD>"
                                   + "    <TD><xsl:value-of select=\"price\"/></TD>"
                                   + "  </TR>"
                                   + "</xsl:template>"
                                   + "</xsl:stylesheet>";

                string input = ""
                               + "<?xml version='1.0'?>"
                               + "<!-- This file represents a fragment of a book store inventory database -->"
                               + "<bookstore>"
                               + "  <book genre=\"autobiography\" publicationdate=\"1981\" ISBN=\"1-861003-11-0\">"
                               + "    <title>The Autobiography of Benjamin Franklin</title>"
                               + "    <author>"
                               + "      <first-name>Benjamin</first-name>"
                               + "      <last-name>Franklin</last-name>"
                               + "    </author>"
                               + "    <price>8.99</price>"
                               + "  </book>"
                               + "  <book genre=\"novel\" publicationdate=\"1967\" ISBN=\"0-201-63361-2\">"
                               + "    <title>The Confidence Man</title>"
                               + "    <author>"
                               + "      <first-name>Herman</first-name>"
                               + "      <last-name>Melville</last-name>"
                               + "    </author>"
                               + "    <price>11.99</price>"
                               + "  </book>"
                               + "  <book genre=\"philosophy\" publicationdate=\"1991\" ISBN=\"1-861001-57-6\">"
                               + "    <title>The Gorgias</title>"
                               + "    <author>"
                               + "      <name>Plato</name>"
                               + "    </author>"
                               + "    <price>9.99</price>"
                               + "  </book>"
                               + "</bookstore>";

                string       output   = "<HTML><BODY><TABLE BORDER=\"2\"><TR><TD>ISBN</TD><TD>Title</TD><TD>Price</TD></TR><TR><TD>1-861003-11-0</TD><TD>The Autobiography of Benjamin Franklin</TD><TD>8.99</TD></TR><TR><TD>0-201-63361-2</TD><TD>The Confidence Man</TD><TD>11.99</TD></TR><TR><TD>1-861001-57-6</TD><TD>The Gorgias</TD><TD>9.99</TD></TR></TABLE></BODY></HTML>";
                IDocument    document = Substitute.For <IDocument>();
                MemoryStream stream   = new MemoryStream(Encoding.UTF8.GetBytes(input));

                document.GetStream().Returns(stream);

                IExecutionContext           context = Substitute.For <IExecutionContext>();
                Dictionary <string, string> convertedLinks;

                context.TryConvert(new object(), out convertedLinks)
                .ReturnsForAnyArgs(x =>
                {
                    x[1] = x[0];
                    return(true);
                });

                IModule   module       = Substitute.For <IModule>();
                IDocument xsltDocument = Substitute.For <IDocument>();

                MemoryStream xsltStream = new MemoryStream(Encoding.UTF8.GetBytes(xsltInput));

                xsltDocument.GetStream().Returns(xsltStream);
                context.Execute(Arg.Any <IEnumerable <IModule> >(), Arg.Any <IEnumerable <IDocument> >()).Returns(new IDocument[] { xsltDocument });
                Xslt autoLink = new Xslt(module);

                // When
                autoLink.Execute(new[] { document }, context).ToList();  // Make sure to materialize the result list

                // Then
                context.Received().GetDocument(Arg.Is(document), output);
                stream.Dispose();
            }