blob: 28bfe8e9d3030f5145c9c424648e9155a2640396 [file] [log] [blame]
/***************************************************************************
Copyright (c) Microsoft Corporation 2012-2015.
This code is licensed using the Microsoft Public License (Ms-PL). The text of the license can be found here:
http://www.microsoft.com/resources/sharedsource/licensingbasics/publiclicense.mspx
Published at http://OpenXmlDeveloper.org
Resource Center and Documentation: http://openxmldeveloper.org/wiki/w/wiki/powertools-for-open-xml.aspx
Developer: Eric White
Blog: http://www.ericwhite.com
Twitter: @EricWhiteDev
Email: eric@ericwhite.com
***************************************************************************/
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Linq;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Validation;
using OpenXmlPowerTools;
using Xunit;
#if !ELIDE_XUNIT_TESTS
namespace OxPt
{
public class DaTests
{
[Theory]
[InlineData("DA001-TemplateDocument.docx", "DA-Data.xml", false)]
[InlineData("DA002-TemplateDocument.docx", "DA-DataNotHighValueCust.xml", false)]
[InlineData("DA003-Select-XPathFindsNoData.docx", "DA-Data.xml", true)]
[InlineData("DA004-Select-XPathFindsNoDataOptional.docx", "DA-Data.xml", false)]
[InlineData("DA005-SelectRowData-NoData.docx", "DA-Data.xml", true)]
[InlineData("DA006-SelectTestValue-NoData.docx", "DA-Data.xml", true)]
[InlineData("DA007-SelectRepeatingData-NoData.docx", "DA-Data.xml", true)]
[InlineData("DA008-TableElementWithNoTable.docx", "DA-Data.xml", true)]
[InlineData("DA009-InvalidXPath.docx", "DA-Data.xml", true)]
[InlineData("DA010-InvalidXml.docx", "DA-Data.xml", true)]
[InlineData("DA011-SchemaError.docx", "DA-Data.xml", true)]
[InlineData("DA012-OtherMarkupTypes.docx", "DA-Data.xml", true)]
[InlineData("DA013-Runs.docx", "DA-Data.xml", false)]
[InlineData("DA014-TwoRuns-NoValuesSelected.docx", "DA-Data.xml", true)]
[InlineData("DA015-TwoRunsXmlExceptionInFirst.docx", "DA-Data.xml", true)]
[InlineData("DA016-TwoRunsSchemaErrorInSecond.docx", "DA-Data.xml", true)]
[InlineData("DA017-FiveRuns.docx", "DA-Data.xml", true)]
[InlineData("DA018-SmartQuotes.docx", "DA-Data.xml", false)]
[InlineData("DA019-RunIsEntireParagraph.docx", "DA-Data.xml", false)]
[InlineData("DA020-TwoRunsAndNoOtherContent.docx", "DA-Data.xml", true)]
[InlineData("DA021-NestedRepeat.docx", "DA-DataNestedRepeat.xml", false)]
[InlineData("DA022-InvalidXPath.docx", "DA-Data.xml", true)]
[InlineData("DA023-RepeatWOEndRepeat.docx", "DA-Data.xml", true)]
[InlineData("DA026-InvalidRootXmlElement.docx", "DA-Data.xml", true)]
[InlineData("DA027-XPathErrorInPara.docx", "DA-Data.xml", true)]
[InlineData("DA028-NoPrototypeRow.docx", "DA-Data.xml", true)]
[InlineData("DA029-NoDataForCell.docx", "DA-Data.xml", true)]
[InlineData("DA030-TooMuchDataForCell.docx", "DA-TooMuchDataForCell.xml", true)]
[InlineData("DA031-CellDataInAttributes.docx", "DA-CellDataInAttributes.xml", true)]
[InlineData("DA032-TooMuchDataForConditional.docx", "DA-TooMuchDataForConditional.xml", true)]
[InlineData("DA033-ConditionalOnAttribute.docx", "DA-ConditionalOnAttribute.xml", false)]
[InlineData("DA034-HeaderFooter.docx", "DA-Data.xml", false)]
[InlineData("DA035-SchemaErrorInRepeat.docx", "DA-Data.xml", true)]
[InlineData("DA036-SchemaErrorInConditional.docx", "DA-Data.xml", true)]
[InlineData("DA100-TemplateDocument.docx", "DA-Data.xml", false)]
[InlineData("DA101-TemplateDocument.docx", "DA-Data.xml", true)]
[InlineData("DA102-TemplateDocument.docx", "DA-Data.xml", true)]
[InlineData("DA201-TemplateDocument.docx", "DA-Data.xml", false)]
[InlineData("DA202-TemplateDocument.docx", "DA-DataNotHighValueCust.xml", false)]
[InlineData("DA203-Select-XPathFindsNoData.docx", "DA-Data.xml", true)]
[InlineData("DA204-Select-XPathFindsNoDataOptional.docx", "DA-Data.xml", false)]
[InlineData("DA205-SelectRowData-NoData.docx", "DA-Data.xml", true)]
[InlineData("DA206-SelectTestValue-NoData.docx", "DA-Data.xml", true)]
[InlineData("DA207-SelectRepeatingData-NoData.docx", "DA-Data.xml", true)]
[InlineData("DA209-InvalidXPath.docx", "DA-Data.xml", true)]
[InlineData("DA210-InvalidXml.docx", "DA-Data.xml", true)]
[InlineData("DA211-SchemaError.docx", "DA-Data.xml", true)]
[InlineData("DA212-OtherMarkupTypes.docx", "DA-Data.xml", true)]
[InlineData("DA213-Runs.docx", "DA-Data.xml", false)]
[InlineData("DA214-TwoRuns-NoValuesSelected.docx", "DA-Data.xml", true)]
[InlineData("DA215-TwoRunsXmlExceptionInFirst.docx", "DA-Data.xml", true)]
[InlineData("DA216-TwoRunsSchemaErrorInSecond.docx", "DA-Data.xml", true)]
[InlineData("DA217-FiveRuns.docx", "DA-Data.xml", true)]
[InlineData("DA218-SmartQuotes.docx", "DA-Data.xml", false)]
[InlineData("DA219-RunIsEntireParagraph.docx", "DA-Data.xml", false)]
[InlineData("DA220-TwoRunsAndNoOtherContent.docx", "DA-Data.xml", true)]
[InlineData("DA221-NestedRepeat.docx", "DA-DataNestedRepeat.xml", false)]
[InlineData("DA222-InvalidXPath.docx", "DA-Data.xml", true)]
[InlineData("DA223-RepeatWOEndRepeat.docx", "DA-Data.xml", true)]
[InlineData("DA226-InvalidRootXmlElement.docx", "DA-Data.xml", true)]
[InlineData("DA227-XPathErrorInPara.docx", "DA-Data.xml", true)]
[InlineData("DA228-NoPrototypeRow.docx", "DA-Data.xml", true)]
[InlineData("DA229-NoDataForCell.docx", "DA-Data.xml", true)]
[InlineData("DA230-TooMuchDataForCell.docx", "DA-TooMuchDataForCell.xml", true)]
[InlineData("DA231-CellDataInAttributes.docx", "DA-CellDataInAttributes.xml", true)]
[InlineData("DA232-TooMuchDataForConditional.docx", "DA-TooMuchDataForConditional.xml", true)]
[InlineData("DA233-ConditionalOnAttribute.docx", "DA-ConditionalOnAttribute.xml", false)]
[InlineData("DA234-HeaderFooter.docx", "DA-Data.xml", false)]
[InlineData("DA235-Crashes.docx", "DA-Content-List.xml", false)]
[InlineData("DA236-Page-Num-in-Footer.docx", "DA-Content-List.xml", false)]
[InlineData("DA237-SchemaErrorInRepeat.docx", "DA-Data.xml", true)]
[InlineData("DA238-SchemaErrorInConditional.docx", "DA-Data.xml", true)]
[InlineData("DA239-RunLevelCC-Repeat.docx", "DA-Data.xml", false)]
[InlineData("DA250-ConditionalWithRichXPath.docx", "DA250-Address.xml", false)]
[InlineData("DA251-EnhancedTables.docx", "DA-Data.xml", false)]
[InlineData("DA252-Table-With-Sum.docx", "DA-Data.xml", false)]
[InlineData("DA253-Table-With-Sum-Run-Level-CC.docx", "DA-Data.xml", false)]
[InlineData("DA254-Table-With-XPath-Sum.docx", "DA-Data.xml", false)]
[InlineData("DA255-Table-With-XPath-Sum-Run-Level-CC.docx", "DA-Data.xml", false)]
[InlineData("DA256-NoInvalidDocOnErrorInRun.docx", "DA-Data.xml", true)]
[InlineData("DA257-OptionalRepeat.docx", "DA-Data.xml", false)]
[InlineData("DA258-ContentAcceptsCharsAsXPathResult.docx", "DA-Data.xml", false)]
[InlineData("DA259-MultiLineContents.docx", "DA-Data.xml", false)]
[InlineData("DA260-RunLevelRepeat.docx", "DA-Data.xml", false)]
[InlineData("DA261-RunLevelConditional.docx", "DA-Data.xml", false)]
[InlineData("DA262-ConditionalNotMatch.docx", "DA-Data.xml", false)]
[InlineData("DA263-ConditionalNotMatch.docx", "DA-DataSmallCustomer.xml", false)]
[InlineData("DA264-InvalidRunLevelRepeat.docx", "DA-Data.xml", true)]
[InlineData("DA265-RunLevelRepeatWithWhiteSpaceBefore.docx", "DA-Data.xml", false)]
[InlineData("DA266-RunLevelRepeat-NoData.docx", "DA-Data.xml", true)]
public void DA101(string name, string data, bool err)
{
FileInfo templateDocx = new FileInfo(Path.Combine(TestUtil.SourceDir.FullName, name));
FileInfo dataFile = new FileInfo(Path.Combine(TestUtil.SourceDir.FullName, data));
WmlDocument wmlTemplate = new WmlDocument(templateDocx.FullName);
XElement xmldata = XElement.Load(dataFile.FullName);
bool returnedTemplateError;
WmlDocument afterAssembling = DocumentAssembler.AssembleDocument(wmlTemplate, xmldata, out returnedTemplateError);
var assembledDocx = new FileInfo(Path.Combine(TestUtil.TempDir.FullName, templateDocx.Name.Replace(".docx", "-processed-by-DocumentAssembler.docx")));
afterAssembling.SaveAs(assembledDocx.FullName);
using (MemoryStream ms = new MemoryStream())
{
ms.Write(afterAssembling.DocumentByteArray, 0, afterAssembling.DocumentByteArray.Length);
using (WordprocessingDocument wDoc = WordprocessingDocument.Open(ms, true))
{
OpenXmlValidator v = new OpenXmlValidator();
var valErrors = v.Validate(wDoc).Where(ve => !s_ExpectedErrors.Contains(ve.Description));
#if false
StringBuilder sb = new StringBuilder();
foreach (var item in valErrors.Select(r => r.Description).OrderBy(t => t).Distinct())
{
sb.Append(item).Append(Environment.NewLine);
}
string z = sb.ToString();
Console.WriteLine(z);
#endif
Assert.Empty(valErrors);
}
}
Assert.Equal(err, returnedTemplateError);
}
[Theory]
[InlineData("DA259-MultiLineContents.docx", "DA-Data.xml", false)]
public void DA259(string name, string data, bool err)
{
DA101(name, data, err);
var assembledDocx = new FileInfo(Path.Combine(TestUtil.TempDir.FullName, name.Replace(".docx", "-processed-by-DocumentAssembler.docx")));
WmlDocument afterAssembling = new WmlDocument(assembledDocx.FullName);
int brCount = afterAssembling.MainDocumentPart
.Element(W.body)
.Elements(W.p).ElementAt(1)
.Elements(W.r)
.Elements(W.br).Count();
Assert.Equal(4, brCount);
}
[Theory]
[InlineData("DA024-TrackedRevisions.docx", "DA-Data.xml")]
public void DA102_Throws(string name, string data)
{
FileInfo templateDocx = new FileInfo(Path.Combine(TestUtil.SourceDir.FullName, name));
FileInfo dataFile = new FileInfo(Path.Combine(TestUtil.SourceDir.FullName, data));
WmlDocument wmlTemplate = new WmlDocument(templateDocx.FullName);
XElement xmldata = XElement.Load(dataFile.FullName);
bool returnedTemplateError;
WmlDocument afterAssembling;
Assert.Throws<OpenXmlPowerToolsException>(() =>
{
afterAssembling = DocumentAssembler.AssembleDocument(wmlTemplate, xmldata, out returnedTemplateError);
});
}
[Theory]
[InlineData("DA025-TemplateDocument.docx", "DA-Data.xml", false)]
public void DA103_UseXmlDocument(string name, string data, bool err)
{
FileInfo templateDocx = new FileInfo(Path.Combine(TestUtil.SourceDir.FullName, name));
FileInfo dataFile = new FileInfo(Path.Combine(TestUtil.SourceDir.FullName, data));
WmlDocument wmlTemplate = new WmlDocument(templateDocx.FullName);
XmlDocument xmldata = new XmlDocument();
xmldata.Load(dataFile.FullName);
bool returnedTemplateError;
WmlDocument afterAssembling = DocumentAssembler.AssembleDocument(wmlTemplate, xmldata, out returnedTemplateError);
var assembledDocx = new FileInfo(Path.Combine(TestUtil.TempDir.FullName, templateDocx.Name.Replace(".docx", "-processed-by-DocumentAssembler.docx")));
afterAssembling.SaveAs(assembledDocx.FullName);
using (MemoryStream ms = new MemoryStream())
{
ms.Write(afterAssembling.DocumentByteArray, 0, afterAssembling.DocumentByteArray.Length);
using (WordprocessingDocument wDoc = WordprocessingDocument.Open(ms, true))
{
OpenXmlValidator v = new OpenXmlValidator();
var valErrors = v.Validate(wDoc).Where(ve => !s_ExpectedErrors.Contains(ve.Description));
Assert.Empty(valErrors);
}
}
Assert.Equal(err, returnedTemplateError);
}
private static List<string> s_ExpectedErrors = new List<string>()
{
"The 'http://schemas.openxmlformats.org/wordprocessingml/2006/main:evenHBand' attribute is not declared.",
"The 'http://schemas.openxmlformats.org/wordprocessingml/2006/main:evenVBand' attribute is not declared.",
"The 'http://schemas.openxmlformats.org/wordprocessingml/2006/main:firstColumn' attribute is not declared.",
"The 'http://schemas.openxmlformats.org/wordprocessingml/2006/main:firstRow' attribute is not declared.",
"The 'http://schemas.openxmlformats.org/wordprocessingml/2006/main:firstRowFirstColumn' attribute is not declared.",
"The 'http://schemas.openxmlformats.org/wordprocessingml/2006/main:firstRowLastColumn' attribute is not declared.",
"The 'http://schemas.openxmlformats.org/wordprocessingml/2006/main:lastColumn' attribute is not declared.",
"The 'http://schemas.openxmlformats.org/wordprocessingml/2006/main:lastRow' attribute is not declared.",
"The 'http://schemas.openxmlformats.org/wordprocessingml/2006/main:lastRowFirstColumn' attribute is not declared.",
"The 'http://schemas.openxmlformats.org/wordprocessingml/2006/main:lastRowLastColumn' attribute is not declared.",
"The 'http://schemas.openxmlformats.org/wordprocessingml/2006/main:noHBand' attribute is not declared.",
"The 'http://schemas.openxmlformats.org/wordprocessingml/2006/main:noVBand' attribute is not declared.",
"The 'http://schemas.openxmlformats.org/wordprocessingml/2006/main:oddHBand' attribute is not declared.",
"The 'http://schemas.openxmlformats.org/wordprocessingml/2006/main:oddVBand' attribute is not declared.",
};
}
}
#endif