public void XpathPredicateGroupNestedTest()
        {
            string          testValue1    = "test1";
            string          testValue2    = "test2";
            long            testValue3    = 55L;
            string          nonMatchValue = "test3";
            XPathQuery      predicate1    = new XPathQuery(UnitTestHelper.AttributeStringSV, ComparisonOperator.Equals, testValue1);
            XPathQuery      predicate2    = new XPathQuery(UnitTestHelper.AttributeStringMV, ComparisonOperator.Equals, testValue2);
            XPathQuery      predicate3    = new XPathQuery(UnitTestHelper.AttributeIntegerSV, ComparisonOperator.Equals, testValue3);
            XPathQueryGroup childGroup    = new XPathQueryGroup(GroupOperator.Or, predicate1, predicate2);
            XPathQueryGroup group         = new XPathQueryGroup(GroupOperator.And, predicate3, childGroup);

            string expected = string.Format("(({4} = {5}) and (({0} = '{1}') or ({2} = '{3}')))", UnitTestHelper.AttributeStringSV, testValue1, UnitTestHelper.AttributeStringMV, testValue2, UnitTestHelper.AttributeIntegerSV, testValue3);

            ResourceObject matchObject    = UnitTestHelper.CreateTestResource(UnitTestHelper.AttributeStringSV, testValue1);
            ResourceObject nonMatchObject = UnitTestHelper.CreateTestResource(UnitTestHelper.AttributeStringSV, nonMatchValue);

            matchObject.Attributes[UnitTestHelper.AttributeIntegerSV].SetValue(testValue3);
            matchObject.Save();

            try
            {
                this.SubmitXpath(group, expected, matchObject);
            }
            finally
            {
                UnitTestHelper.CleanupTestResources(matchObject, nonMatchObject);
            }
        }
        public void NamespaceQuery()
        {
            string xml =
                "<?xml version='1.0'?>\r\n" +
                "<Xml1></Xml1>";
            XPathQuery query = new XPathQuery(xml);

            XPathNodeMatch[] nodes = query.FindNodes("//namespace::*");

            ServiceContainer container = new ServiceContainer();

            container.AddService(typeof(MockTextMarkerService), new MockTextMarkerService());

            AvalonEditDocumentAdapter doc = new AvalonEditDocumentAdapter(new ICSharpCode.AvalonEdit.Document.TextDocument(), container);

            doc.Text = xml;
            XPathNodeTextMarker xpathNodeMarker = new XPathNodeTextMarker(doc);

            xpathNodeMarker.AddMarkers(nodes);

            ITextMarkerService service = doc.GetService(typeof(MockTextMarkerService)) as ITextMarkerService;
            List <ITextMarker> markers = new List <ITextMarker>(service.TextMarkers);

            Assert.AreEqual(0, markers.Count);
            Assert.AreEqual(1, nodes.Length);
        }
        public void EmptyCommentNode()
        {
            string     xml   = "<!----><root/>";
            XPathQuery query = new XPathQuery(xml);

            XPathNodeMatch[] nodes = query.FindNodes("//comment()");

            ServiceContainer container = new ServiceContainer();

            container.AddService(typeof(MockTextMarkerService), new MockTextMarkerService());

            IDocument doc = new ICSharpCode.AvalonEdit.Document.TextDocument()
            {
                ServiceProvider = container
            };

            doc.Text = xml;
            XPathNodeTextMarker xpathNodeMarker = new XPathNodeTextMarker(doc);

            xpathNodeMarker.AddMarkers(nodes);

            ITextMarkerService service = doc.GetService(typeof(MockTextMarkerService)) as ITextMarkerService;
            List <ITextMarker> markers = new List <ITextMarker>(service.TextMarkers);

            Assert.AreEqual(0, markers.Count);
            Assert.AreEqual(1, nodes.Length);
        }
Beispiel #4
0
        public void SingleElementWithNamespacePrefixFoundByXPath()
        {
            string xml =
                "<f:root xmlns:f='http://foo.com'>\r\n" +
                "\t<f:foo></f:foo>\r\n" +
                "</f:root>";
            XmlNamespaceCollection namespaces = new XmlNamespaceCollection();

            namespaces.Add(new XmlNamespace("fo", "http://foo.com"));
            XPathQuery query = new XPathQuery(xml, namespaces);

            XPathNodeMatch[] nodes = query.FindNodes("//fo:foo");
            XPathNodeMatch   node  = nodes[0];

            string                   nodeValue    = "f:foo";
            string                   displayValue = "<f:foo>";
            int                      lineNumber   = 1;
            int                      linePosition = 2;
            XPathNodeType            nodeType     = XPathNodeType.Element;
            XPathNodeMatch           expectedNode = new XPathNodeMatch(nodeValue, displayValue, lineNumber, linePosition, nodeType);
            XPathNodeMatchComparison comparison   = new XPathNodeMatchComparison();

            Assert.IsTrue(comparison.AreEqual(expectedNode, node), comparison.GetReasonForNotMatching());
            Assert.AreEqual(1, nodes.Length);
        }
        private void SubmitXpath(object value, string expected, string attributeName, ComparisonOperator xpathOp, GroupOperator queryOp, params ResourceObject[] matchResources)
        {
            XPathQuery predicate = new XPathQuery(attributeName, xpathOp, value);
            string     xpath     = XPathFilterBuilder.CreateFilter(UnitTestHelper.ObjectTypeUnitTestObjectName, queryOp, predicate);

            Assert.AreEqual(expected, xpath);

            ISearchResultCollection results = client.GetResources(xpath);

            if (results.Count == 0)
            {
                if (matchResources != null && matchResources.Length > 0)
                {
                    Assert.Fail("The query returned no results");
                }
            }

            if (matchResources == null || matchResources.Length == 0)
            {
                Assert.Fail("The query returned results where none were expectedXpath");
            }

            if (results.Count != matchResources.Length)
            {
                Assert.Fail("The query returned an unexpected number of results");
            }

            if (!results.All(t => matchResources.Any(u => u.ObjectID == t.ObjectID)))
            {
                Assert.Fail("The query did not return the correct results");
            }
        }
 public void TestMVReferenceEndsWith()
 {
     try
     {
         XPathQuery predicate = new XPathQuery(UnitTestHelper.AttributeReferenceMV, ComparisonOperator.EndsWith);
         Assert.Fail("The expectedXpath exception was not thrown");
     }
     catch { }
 }
 public void TestSVReferenceContains()
 {
     try
     {
         XPathQuery predicate = new XPathQuery(UnitTestHelper.AttributeReferenceSV, ComparisonOperator.Contains);
         Assert.Fail("The expectedXpath exception was not thrown");
     }
     catch { }
 }
        public void Init()
        {
            string     xml   = "<root xmlns='http://foo.com'/>";
            XPathQuery query = new XPathQuery(xml);

            nodes            = query.FindNodes("//namespace::*");
            node             = nodes[0];
            xmlNamespaceNode = nodes[1];
        }
Beispiel #9
0
 public void TestStringLessThanOrEquals()
 {
     try
     {
         XPathQuery predicate = new XPathQuery(UnitTestHelper.AttributeStringSV, ComparisonOperator.LessThanOrEquals);
         Assert.Fail("The expectedXpath exception was not thrown");
     }
     catch { }
 }
 public void TestSVBooleanStartsWith()
 {
     try
     {
         XPathQuery predicate = new XPathQuery(UnitTestHelper.AttributeBooleanSV, ComparisonOperator.StartsWith);
         Assert.Fail("The expectedXpath exception was not thrown");
     }
     catch { }
 }
 public void ThrowOnInvalidAttribute()
 {
     try
     {
         XPathQuery predicate1 = new XPathQuery("legalName", ComparisonOperator.Equals, "test");
         XPathQuery predicate2 = new XPathQuery("also:validName", ComparisonOperator.Equals, "test");
         XPathQuery predicate3 = new XPathQuery("%%%%%", ComparisonOperator.Equals, "test");
         Assert.Fail("The expected exception was not thrown");
     }
     catch { }
 }
Beispiel #12
0
        public void NoXPathNodeFoundForUnknownElementInXPathQuery()
        {
            string xml =
                "<root>\r\n" +
                "\t<foo/>\r\n" +
                "</root>";
            XPathQuery query = new XPathQuery(xml);

            XPathNodeMatch[] nodes = query.FindNodes("//bar");
            Assert.AreEqual(0, nodes.Length);
        }
        public void ThrowOnInvalidObjectTypeName()
        {
            try
            {
                string          testValue1      = "test1";
                XPathQuery      predicate1      = new XPathQuery(UnitTestHelper.AttributeStringSV, ComparisonOperator.Equals, testValue1);
                XPathExpression childExpression = new XPathExpression("legalName", predicate1);
                childExpression = new XPathExpression("also:legalName", predicate1);
                childExpression = new XPathExpression("%%%%%", predicate1);

                Assert.Fail("The expected exception was not thrown");
            }
            catch { }
        }
Beispiel #14
0
        private static void Extract(string filePath, string queryString)
        {
            if (!File.Exists(filePath))
            {
                Console.WriteLine("File not found! Path: {0}", filePath);
                return;
            }

            XmlReaderSettings settings = new XmlReaderSettings {
                IgnoreComments = true, IgnoreWhitespace = true
            };

            using (XmlReader reader = XmlReader.Create(filePath, settings))
            {
                XPathQuery query = new XPathQuery(queryString);
                query.Find(reader);
            }
        }
Beispiel #15
0
        public void ProcessingInstructionNodeFoundByXPath()
        {
            string     xml   = "<root><?test processinstruction='1.0'?></root>";
            XPathQuery query = new XPathQuery(xml);

            XPathNodeMatch[] nodes = query.FindNodes("//processing-instruction()");
            XPathNodeMatch   node  = nodes[0];

            string                   nodeValue    = "test";
            string                   displayValue = "<?test processinstruction='1.0'?>";
            int                      lineNumber   = 0;
            int                      linePosition = 8;
            XPathNodeType            nodeType     = XPathNodeType.ProcessingInstruction;
            XPathNodeMatch           expectedNode = new XPathNodeMatch(nodeValue, displayValue, lineNumber, linePosition, nodeType);
            XPathNodeMatchComparison comparison   = new XPathNodeMatchComparison();

            Assert.IsTrue(comparison.AreEqual(expectedNode, node), comparison.GetReasonForNotMatching());
        }
Beispiel #16
0
        public void CommentNodeFoundByXPath()
        {
            string     xml   = "<!-- Test --><root/>";
            XPathQuery query = new XPathQuery(xml);

            XPathNodeMatch[] nodes = query.FindNodes("//comment()");
            XPathNodeMatch   node  = nodes[0];

            string                   nodeValue    = " Test ";
            string                   displayValue = "<!-- Test -->";
            int                      lineNumber   = 0;
            int                      linePosition = 4;
            XPathNodeType            nodeType     = XPathNodeType.Comment;
            XPathNodeMatch           expectedNode = new XPathNodeMatch(nodeValue, displayValue, lineNumber, linePosition, nodeType);
            XPathNodeMatchComparison comparison   = new XPathNodeMatchComparison();

            Assert.IsTrue(comparison.AreEqual(expectedNode, node), comparison.GetReasonForNotMatching());
        }
Beispiel #17
0
        public void AttributeNodeFoundByXPath()
        {
            string xml = "<root>\r\n" +
                         "\t<foo Id='ab'></foo>\r\n" +
                         "</root>";
            XPathQuery query = new XPathQuery(xml);

            XPathNodeMatch[] nodes = query.FindNodes("//foo/@Id");
            XPathNodeMatch   node  = nodes[0];

            string                   nodeValue    = "Id";
            string                   displayValue = "@Id";
            int                      lineNumber   = 1;
            int                      linePosition = 6;
            XPathNodeType            nodeType     = XPathNodeType.Attribute;
            XPathNodeMatch           expectedNode = new XPathNodeMatch(nodeValue, displayValue, lineNumber, linePosition, nodeType);
            XPathNodeMatchComparison comparison   = new XPathNodeMatchComparison();

            Assert.IsTrue(comparison.AreEqual(expectedNode, node), comparison.GetReasonForNotMatching());
        }
        public void XpathExpressionDereferencedTest()
        {
            string     testValue1 = "test1";
            XPathQuery predicate1 = new XPathQuery(UnitTestHelper.AttributeStringSV, ComparisonOperator.Equals, testValue1);
            XPathDereferencedExpression expression = new XPathDereferencedExpression(UnitTestHelper.ObjectTypeUnitTestObjectName, UnitTestHelper.AttributeReferenceSV, predicate1);

            string expected = string.Format("/{0}[({1} = '{2}')]/{3}", UnitTestHelper.ObjectTypeUnitTestObjectName, UnitTestHelper.AttributeStringSV, testValue1, UnitTestHelper.AttributeReferenceSV);

            ResourceObject parentObject1      = UnitTestHelper.CreateTestResource();
            ResourceObject filterTargetObject = UnitTestHelper.CreateTestResource(UnitTestHelper.AttributeStringSV, testValue1, UnitTestHelper.AttributeReferenceSV, parentObject1);

            try
            {
                this.SubmitXpath(expression, expected, parentObject1);
            }
            finally
            {
                UnitTestHelper.CleanupTestResources(filterTargetObject, parentObject1);
            }
        }
        public void XpathPredicateGroupSingleValueTest()
        {
            string          testValue1    = "test1";
            string          nonMatchValue = "test3";
            XPathQuery      predicate1    = new XPathQuery(UnitTestHelper.AttributeStringSV, ComparisonOperator.Equals, testValue1);
            XPathQueryGroup group         = new XPathQueryGroup(GroupOperator.And, predicate1);

            string expected = string.Format("({0} = '{1}')", UnitTestHelper.AttributeStringSV, testValue1);

            ResourceObject matchObject    = UnitTestHelper.CreateTestResource(UnitTestHelper.AttributeStringSV, testValue1);
            ResourceObject nonMatchObject = UnitTestHelper.CreateTestResource(UnitTestHelper.AttributeStringSV, nonMatchValue);

            try
            {
                this.SubmitXpath(group, expected, matchObject);
            }
            finally
            {
                UnitTestHelper.CleanupTestResources(matchObject, nonMatchObject);
            }
        }
Beispiel #20
0
        public void SingleEmptyElementNodeFoundByXPath()
        {
            string xml =
                "<root>\r\n" +
                "\t<foo/>\r\n" +
                "</root>";
            XPathQuery query = new XPathQuery(xml);

            XPathNodeMatch[] nodes = query.FindNodes("//foo");
            XPathNodeMatch   node  = nodes[0];

            string                   nodeValue    = "foo";
            string                   displayValue = "<foo/>";
            int                      lineNumber   = 1;
            int                      linePosition = 2;
            XPathNodeType            nodeType     = XPathNodeType.Element;
            XPathNodeMatch           expectedNode = new XPathNodeMatch(nodeValue, displayValue, lineNumber, linePosition, nodeType);
            XPathNodeMatchComparison comparison   = new XPathNodeMatchComparison();

            Assert.IsTrue(comparison.AreEqual(expectedNode, node), comparison.GetReasonForNotMatching());
            Assert.AreEqual(1, nodes.Length);
        }
Beispiel #21
0
        public void TextNodeMatchedWithXPath()
        {
            string xml =
                "<root>\r\n" +
                "\t<foo>test</foo>\r\n" +
                "</root>";
            XPathQuery query = new XPathQuery(xml);

            XPathNodeMatch[] nodes = query.FindNodes("//foo/text()");
            XPathNodeMatch   node  = nodes[0];

            string                   nodeValue    = "test";
            string                   displayValue = "test";
            int                      lineNumber   = 1;
            int                      linePosition = 6;
            XPathNodeType            nodeType     = XPathNodeType.Text;
            XPathNodeMatch           expectedNode = new XPathNodeMatch(nodeValue, displayValue, lineNumber, linePosition, nodeType);
            XPathNodeMatchComparison comparison   = new XPathNodeMatchComparison();

            Assert.IsTrue(comparison.AreEqual(expectedNode, node), comparison.GetReasonForNotMatching());
            Assert.AreEqual(1, nodes.Length);
        }
        public void Init()
        {
            string     xml   = "<root><foo/></root>";
            XPathQuery query = new XPathQuery(xml);

            XPathNodeMatch[] nodes = query.FindNodes("//root");

            IDocument doc = MockTextMarkerService.CreateDocumentWithMockService();

            doc.Text = xml;
            XPathNodeTextMarker xpathNodeMarker = new XPathNodeTextMarker(doc);

            xpathNodeMarker.AddMarkers(nodes);

            ITextMarkerService service = doc.GetService(typeof(ITextMarkerService)) as ITextMarkerService;

            markers = new List <ITextMarker>(service.TextMarkers);

            // Remove markers.
            xpathNodeMarker.RemoveMarkers();
            markersAfterRemove = new List <ITextMarker>(service.TextMarkers);

            xpathNodeTextMarker = markers[0];
        }
        public void XpathExpressionNestedTest()
        {
            string          testValue1      = "test1";
            XPathQuery      predicate1      = new XPathQuery(UnitTestHelper.AttributeStringSV, ComparisonOperator.Equals, testValue1);
            XPathExpression childExpression = new XPathExpression(UnitTestHelper.ObjectTypeUnitTestObjectName, predicate1);
            XPathQuery      predicate2      = new XPathQuery(UnitTestHelper.AttributeReferenceSV, ComparisonOperator.Equals, childExpression);
            XPathExpression expression      = new XPathExpression(UnitTestHelper.ObjectTypeUnitTestObjectName, predicate2);

            string expected = string.Format("/{0}[({1} = /{0}[({2} = '{3}')])]", UnitTestHelper.ObjectTypeUnitTestObjectName, UnitTestHelper.AttributeReferenceSV, UnitTestHelper.AttributeStringSV, testValue1);

            ResourceObject filterTargetObject = UnitTestHelper.CreateTestResource(UnitTestHelper.AttributeStringSV, testValue1);
            ResourceObject childObject1       = UnitTestHelper.CreateTestResource(UnitTestHelper.AttributeReferenceSV, filterTargetObject.ObjectID);
            ResourceObject childObject2       = UnitTestHelper.CreateTestResource(UnitTestHelper.AttributeReferenceSV, filterTargetObject.ObjectID);
            ResourceObject childObject3       = UnitTestHelper.CreateTestResource(UnitTestHelper.AttributeReferenceSV, filterTargetObject.ObjectID);

            try
            {
                this.SubmitXpath(expression, expected, childObject1, childObject2, childObject3);
            }
            finally
            {
                UnitTestHelper.CleanupTestResources(filterTargetObject, childObject1, childObject2, childObject3);
            }
        }
Beispiel #24
0
        /// <summary>
        /// Writes out a test for the current node
        /// </summary>
        /// <param name="xWriter">XmlWriter to write test to</param>
        /// <param name="node">Node to write test for</param>
        private void WriteXPathListItemsRec(XmlTextWriter xWriter, ViewerNode node)
        {
            // Check if this node has a value
            if (!string.IsNullOrEmpty(node.Value))
            {
                // Check the type of node
                if (node.NodeType == NodeType.Element)
                {
                    // Add comment
                    xWriter.WriteComment(string.Format(CultureInfo.CurrentUICulture, " {0}: Element {1} must equal '{2}'", ++_xpathIndex, node.LocalName, node.Value));
                    // Open the XPathList element
                    xWriter.WriteStartElement("XPathValidation");
                    // Write attributes for XPathValidation
                    xWriter.WriteAttributeString("query", node.XPath);
                    // Write node value
                    xWriter.WriteValue(node.Value);
                    // Close the XPathList element
                    xWriter.WriteEndElement();
                }
                else if ((node.NodeType == NodeType.Attribute) && (node.AttributeType == AttributeType.None))
                {
                    // Add comment
                    xWriter.WriteComment(string.Format(CultureInfo.CurrentUICulture, " {0}: Attribute {1} must equal '{2}'", ++_xpathIndex, node.LocalName, node.Value));
                    // Open the XPathList element
                    xWriter.WriteStartElement("XPathValidation");
                    // Write attributes for XPathValidation
                    xWriter.WriteAttributeString("query", node.XPath);
                    // Write node value
                    xWriter.WriteValue(node.Value);
                    // Close the XPathList element
                    xWriter.WriteEndElement();
                }
            }

            // Process attributes for this node
            for (int attrIndex = 0; attrIndex < node.Attributes.Count; attrIndex++)
            {
                WriteXPathListItemsRec(xWriter, node.Attributes[attrIndex]);
            }

            List <string>     processedChildNodes = new List <string>();
            List <XPathQuery> nodeCountXPathQuery = new List <XPathQuery>();

            // Process child elements for this node
            for (int childIndex = 0; childIndex < node.ChildNodes.Count; childIndex++)
            {
                WriteXPathListItemsRec(xWriter, node.ChildNodes[childIndex]);
                // Check if we've processed this node as a repeatign node
                if (!processedChildNodes.Contains(node.ChildNodes[childIndex].Name))
                {
                    // Check if this node is a repeating node
                    if (node.ChildNodes[childIndex].OccurrenceIndex > -1)
                    {
                        // Get the count of the number of times this node repeats
                        int nodeCount = node.GetCountOfRepeatingChildNodes(node.ChildNodes[childIndex]);
                        if (nodeCount > 0)
                        {
                            // Create an xpath statement to verify the count of these nodes
                            string     nodeCountXPath = string.Format(CultureInfo.CurrentUICulture, "count({0}{1})", node.XPath, node.ChildNodes[childIndex].NonRecurringNodePath, nodeCount);
                            XPathQuery query          = new XPathQuery(node.ChildNodes[childIndex].LocalName, nodeCountXPath, nodeCount.ToString());
                            // Add the xpath query to a list
                            nodeCountXPathQuery.Add(query);
                        }
                    }
                    // Add the node to the list of processed nodes
                    processedChildNodes.Add(node.ChildNodes[childIndex].Name);
                }
            }

            // Process any Node Count statements
            if (nodeCountXPathQuery.Count > 0)
            {
                for (int queryIndex = 0; queryIndex < nodeCountXPathQuery.Count; queryIndex++)
                {
                    // Add comment
                    xWriter.WriteComment(string.Format(CultureInfo.CurrentUICulture, " {0}: Count of Element {1} must equal '{2}'", ++_xpathIndex, nodeCountXPathQuery[queryIndex].Name, nodeCountXPathQuery[queryIndex].Value));
                    // Open the XPathList element
                    xWriter.WriteStartElement("XPathValidation");
                    // Write attributes for XPathValidation
                    xWriter.WriteAttributeString("query", nodeCountXPathQuery[queryIndex].Query);
                    // Write node value
                    xWriter.WriteValue(nodeCountXPathQuery[queryIndex].Value);
                    // Close the XPathList element
                    xWriter.WriteEndElement();
                }
            }
        }