public void WhenEncounteringABadUrlCompareShouldReturnFalse()
        {
            // GIVEN
            var badUrl = "bad.formated@url";

            try
            {
                new Uri(badUrl);
                Assert.Fail("Precondition failed");
            }
            catch (UriFormatException)
            {
                // OK;
            }
            catch (Exception e)
            {
                Assert.Fail("Precondition: Unexpected exception " + e);
            }

            var comparer = new UriComparer(new Uri("http://www.watin.net"));

            // WHEN
            var compare = comparer.Compare(badUrl);

            // THEN
            Assert.That(compare, Is.False);
        }
예제 #2
0
        public EndpointAddressMessageFilter(EndpointAddress address, bool includeHostNameInComparison)
        {
            Address = address ?? throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(address));
            IncludeHostNameInComparison = includeHostNameInComparison;
            _helper = new EndpointAddressMessageFilterHelper(Address);

            if (includeHostNameInComparison)
            {
                _comparer = HostUriComparer.Value;
            }
            else
            {
                if (address.Uri.Scheme.Equals("http", StringComparison.OrdinalIgnoreCase) ||
                    address.Uri.Scheme.Equals("https", StringComparison.OrdinalIgnoreCase))
                {
                    _comparer = new NoHostUriComparer
                    {
                        ComparePort   = false,
                        CompareScheme = false
                    };
                }
                else
                {
                    _comparer = NoHostUriComparer.Value;
                }
            }
        }
예제 #3
0
        INode IXmlNodeProcessor <INode> .ProcessElement <TProvider>(TProvider provider, INode baseNode, Uri originalBaseUri, INode defaultNamespace, ContentIterator <INode> content)
        {
            var  empty    = provider.IsEmpty;
            bool sameBase = provider.BaseUri == originalBaseUri;

            Action <INode> elementInit = null;
            var            elementType = CreateElementType(provider, defaultNamespace);

            elementInit += n => rdf.HandleTriple(n, a, elementType);

            string       id            = null;
            INode        innerBaseNode = baseNode;
            XmlValueInfo info          = default;

            while (provider.MoveToNextAttribute())
            {
                if (provider.IsDefault && !ExportDefault)
                {
                    continue;
                }
                var property = CreateAttributeType(provider, elementType);
                var value    = CreateAttributeValue(provider);
                elementInit += n => rdf.HandleTriple(n, property, value);

                if (value is ILiteralNode literal && UriComparer.Equals(literal.DataType, ID))
                {
                    id = XmlConvert.VerifyNCName(provider.Value);
                }
예제 #4
0
 protected string GetXmlValue(INode node)
 {
     if (node is ILiteralNode literal && UriComparer.Equals(literal.DataType, XMLLiteral))
     {
         return(literal.Value);
     }
     return(null);
 }
예제 #5
0
 private static bool ShouldAddToSiteHosts(HostDefinition host, SiteDefinition siteInformation)
 {
     if (host.Name == "*")
     {
         return(false);
     }
     return(!UriComparer.SchemeAndServerEquals(host.GetUri(), siteInformation.SiteUrl));
 }
        public void ShoudlFindMatchUrlWithEncodedQueryString()
        {
            var url = string.Format("http://www.google.com/search?q={0}", HttpUtility.UrlEncode("a+b"));

            var comparer = new UriComparer(new Uri(url));

            Assert.That(comparer.Compare(url), Is.True);
        }
예제 #7
0
 protected string GetStringValue(INode node)
 {
     if (node is ILiteralNode literal && !UriComparer.Equals(literal.DataType, XMLLiteral))
     {
         return(literal.Value);
     }
     throw new NotImplementedException();
 }
예제 #8
0
            private IEnumerable <NodeInfo> ExpandNodes(INode node)
            {
                if (node == null)
                {
                    yield break;
                }

                // Comments on a node are turned to XML comments or PIs
                foreach (var comment in Context.FindObject(node, comment).OfType <ILiteralNode>())
                {
                    var type = comment.DataType;
                    if (type != null && !UriComparer.Equals(type, xstring) && !UriComparer.Equals(type, langString))
                    {
                        yield return(new NodeInfo(XPathNodeType.ProcessingInstruction, comment));
                    }
                    else
                    {
                        yield return(new NodeInfo(XPathNodeType.Comment, comment));
                    }
                }

                // Non-empty literal value is turned into text or significant whitespace
                if (node is ILiteralNode literal)
                {
                    if (!String.IsNullOrEmpty(literal.Value))
                    {
                        var whitespace = XmlConvert.VerifyWhitespace(literal.Value) != null;
                        yield return(new NodeInfo(whitespace ? XPathNodeType.SignificantWhitespace : XPathNodeType.Text, node));
                    }
                    yield break;
                }

                var elementName = Context.FindObject(node, a).Select(GetQualifiedName).FirstOrDefault(NotNull);

                if (elementName != null)
                {
                    // The node is an element; it will have its own node
                    yield return(new NodeInfo(XPathNodeType.Element, node));
                }
                else
                {
                    foreach (var value in Context.FindObject(node, value))
                    {
                        // Try to see if any other value is expandable
                        var enumerator = ExpandNodes(value).GetEnumerator();
                        if (enumerator.MoveNext())
                        {
                            yield return(enumerator.Current);

                            while (enumerator.MoveNext())
                            {
                                yield return(enumerator.Current);
                            }
                            break;
                        }
                    }
                }
            }
        public void ToStringShouldDescribeTheCondition()
        {
            var comparer = new UriComparer(new Uri("http://www.google.com/"), false);

            Assert.AreEqual("equals uri 'http://www.google.com/'", comparer.ToString());

            comparer = new UriComparer(new Uri("http://www.google.com/q?a%26b"), true);
            Assert.AreEqual("equals uri 'http://www.google.com/q?a%26b' ignoring query parameters", comparer.ToString());
        }
        public void IgnoreQueryStringCompareWithNoQueryStringInValueToBeFound()
        {
            var comparer = new UriComparer(new Uri("http://watin.sourceforge.net"), true);

            Assert.IsTrue(comparer.Compare("http://watin.sourceforge.net/"), "Same site should match");

            Assert.IsFalse(comparer.Compare("http://watin.sourceforge.net/here.aspx?query"), "Should ignore query string");
            Assert.IsFalse(comparer.Compare("http://www.microsoft.com/"), "Should ignore completely different site");
        }
예제 #11
0
        public void ConstructorsShouldSetExpectedDefaultPropertyValue()
        {
            // arrange
            var target = new UriComparer();

            // act

            // assert
            Assert.Equal( UriComponents.AbsoluteUri, target.UriComponents );
            Assert.Equal( UriFormat.Unescaped, target.UriFormat );
            Assert.True( target.IgnoreCase );
        }
예제 #12
0
        public void ConstructorsShouldSetExpectedProperties( UriComponents components, UriFormat format, bool ignoreCase )
        {
            // arrange
            var target = new UriComparer( components, format, ignoreCase );

            // act
            
            // assert
            Assert.Equal( components, target.UriComponents );
            Assert.Equal( format, target.UriFormat );
            Assert.Equal( ignoreCase, target.IgnoreCase );
        }
예제 #13
0
        /// <summary>
        /// Creates a new Base Lucene Search Provider.
        /// </summary>
        /// <param name="ver">Lucene Version.</param>
        /// <param name="indexDir">Directory.</param>
        /// <param name="analyzer">Analyzer.</param>
        /// <param name="schema">Index Schema.</param>
        /// <param name="autoSync">Whether the Search Provider should stay in sync with the underlying index.</param>
        public BaseLuceneSearchProvider(LucUtil.Version ver, Directory indexDir, Analyzer analyzer, IFullTextIndexSchema schema, bool autoSync)
        {
            this._version  = ver;
            this._indexDir = indexDir;
            this._analyzer = analyzer;
            this._schema   = schema;
            this._autoSync = autoSync;

            //Create necessary objects
            this._searcher = new LucSearch.IndexSearcher(this._indexDir, true);
            this._parser   = new QueryParser(this._version, this._schema.IndexField, this._analyzer);

            _uriComparer = new UriComparer();
        }
        public void IgnoreQueryStringCompareWithQueryStringInValueToBeFound()
        {
            var comparer = new UriComparer(new Uri("http://watin.sourceforge.net/here.aspx?query"), true);

            Assert.IsTrue(comparer.Compare("http://watin.sourceforge.net/here.aspx"), "Uri: Match ignoring querystring.");
            Assert.IsTrue(comparer.Compare("http://watin.sourceforge.net/here.aspx?query"),
                          "Uri: Match ignoring querystring (include querystring in compare).");
            Assert.IsTrue(comparer.Compare("http://watin.sourceforge.net/here.aspx?badquery"),
                          "Uri: Match ignoring querystring (include non-matching querystring).");
            Assert.IsFalse(comparer.Compare("http://watin.sourceforge.net"),
                           "Uri: Match incorrectly when ignoring querystring.");
            Assert.IsFalse(comparer.Compare("http://www.something.completely.different.net"),
                           "Uri: Match incorrectly when ignoring querystring.");
        }
예제 #15
0
 public string FindPrefix(Uri ns)
 {
     if (UriComparer.Equals(ns, LocalNamespace))
     {
         return("");
     }
     try
     {
         return(Graph.NamespaceMap.GetPrefix(UriTools.GetNamespacePrefix(ns)));
     }catch (RdfException)
     {
         return(null);
     }
 }
        public void ConstructorWithValueAndUriCompare()
        {
            var comparer = new UriComparer(new Uri("http://watin.sourceforge.net"));

            // Uri Compare
            Assert.IsTrue(comparer.Compare(new Uri("http://watin.sourceforge.net")), "Uri: Exact match should pass.");
            Assert.IsTrue(comparer.Compare(new Uri("HTTP://watin.Sourceforge.net")), "Uri: Match should not be case sensitive");

            Assert.IsFalse(comparer.Compare(new Uri("http://watin.sourceforge.net/index.html")),
                           "Uri: Exact match plus more should not pass.");
            Assert.IsFalse(comparer.Compare(new Uri("http://watin")), "Uri: Partial match should not match");
            Assert.IsFalse(comparer.Compare(new Uri("file://html/main.html")),
                           "Uri: Something completely different should not match");
            Assert.IsFalse(comparer.Compare(null), "Uri: null should not match");
        }
예제 #17
0
 public void IEqualityComparerAndIEqualityOfTGetHashCodeShouldBeTheSame()
 {
     // arrange
     var target = new UriComparer();
     IEqualityComparer c1 = target;
     IEqualityComparer<Uri> c2 = target;
     var uri = new Uri( "http://www.tempuri.org" );
     
     // act
     var r1 = c1.GetHashCode( uri );
     var r2 = c2.GetHashCode( uri );
     
     // assert
     Assert.Equal( r1, r2 );
 }
예제 #18
0
 public void IEqualityComparerAndIEqualityOfTComparerEqualsShouldBeTheSame()
 {
     // arrange
     var target = new UriComparer();
     IEqualityComparer c1 = target;
     IEqualityComparer<Uri> c2 = target;
     var uri1 = new Uri( "about:blank" );
     var uri2 = new Uri( "http://www.tempuri.org" );
     
     // act
     var r1 = c1.Equals( uri1, uri2 );
     var r2 = c2.Equals( uri1, uri2 );
     
     // assert
     Assert.Equal( r1, r2 );
 }
 public EndpointAddressMessageFilter(EndpointAddress address, bool includeHostNameInComparison)
 {
     if (address == null)
     {
         throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("address");
     }
     this.address = address;
     this.includeHostNameInComparison = includeHostNameInComparison;
     this.helper = new EndpointAddressMessageFilterHelper(this.address);
     if (includeHostNameInComparison)
     {
         this.comparer = HostUriComparer.Value;
     }
     else
     {
         this.comparer = NoHostUriComparer.Value;
     }
 }
예제 #20
0
            protected XmlQualifiedName GetQualifiedName(INode node)
            {
                var ncname = Context.FindObject(node, label).OfType <ILiteralNode>().FirstOrDefault(IsNCName);

                if (ncname != null)
                {
                    var ns = Context.FindObject(node, isDefinedBy).OfType <IUriNode>().FirstOrDefault();
                    if (ns != null)
                    {
                        if (UriComparer.Equals(ns.Uri, Context.LocalNamespace) || UriComparer.Equals(Context.FindObject(ns, isDefinedBy).OfType <IUriNode>().FirstOrDefault()?.Uri, Context.LocalNamespace))
                        {
                            return(new XmlQualifiedName(ncname.Value));
                        }
                        else
                        {
                            return(new XmlQualifiedName(ncname.Value, ns.Uri.GetString()));
                        }
                    }
                }
                return(null);
            }
        public void CompareShouldBeCultureInvariant()
        {
            // Get the tr-TR (Turkish-Turkey) culture.
            var turkish = new CultureInfo("tr-TR");

            // Get the culture that is associated with the current thread.
            var thisCulture = Thread.CurrentThread.CurrentCulture;

            try
            {
                // Set the culture to Turkish
                Thread.CurrentThread.CurrentCulture = turkish;

                var comparer = new UriComparer(new Uri("http://watin.sourceforge.net"), true);

                Assert.IsTrue(comparer.Compare("http://WATIN.sourceforge.net/"), "Same site should match");
            }
            finally
            {
                // Set the culture back to the original
                Thread.CurrentThread.CurrentCulture = thisCulture;
            }
        }
예제 #22
0
        public void CompareShouldEqualUriCompare()
        {
            var target = new UriComparer();
            var url1 = new Uri( "about:blank" );
            var url2 = new Uri( "about:Blank" );
            var url3 = new Uri( "http://www.tempuri.org" );

            Assert.Equal( Uri.Compare( null, null, UriComponents.AbsoluteUri, UriFormat.Unescaped, StringComparison.OrdinalIgnoreCase ), target.Compare( null, null ) );
            Assert.Equal( Uri.Compare( url1, null, UriComponents.AbsoluteUri, UriFormat.Unescaped, StringComparison.OrdinalIgnoreCase ), target.Compare( url1, null ) );
            Assert.Equal( Uri.Compare( null, url2, UriComponents.AbsoluteUri, UriFormat.Unescaped, StringComparison.OrdinalIgnoreCase ), target.Compare( null, url2 ) );

            Assert.Equal( Uri.Compare( url1, url2, UriComponents.AbsoluteUri, UriFormat.Unescaped, StringComparison.OrdinalIgnoreCase ), target.Compare( url1, url2 ) );
            Assert.Equal( Uri.Compare( url1, url3, UriComponents.AbsoluteUri, UriFormat.Unescaped, StringComparison.OrdinalIgnoreCase ), target.Compare( url1, url3 ) );

            target = new UriComparer( UriComponents.AbsoluteUri, UriFormat.Unescaped, false );
            Assert.Equal( Uri.Compare( url1, url2, UriComponents.AbsoluteUri, UriFormat.Unescaped, StringComparison.Ordinal ), target.Compare( url1, url2 ) );
            Assert.Equal( Uri.Compare( url1, url3, UriComponents.AbsoluteUri, UriFormat.Unescaped, StringComparison.Ordinal ), target.Compare( url1, url3 ) );

            target = new UriComparer( UriComponents.Scheme, UriFormat.SafeUnescaped );
            Assert.Equal( Uri.Compare( url1, url2, UriComponents.Scheme, UriFormat.SafeUnescaped, StringComparison.OrdinalIgnoreCase ), target.Compare( url1, url2 ) );
            Assert.Equal( Uri.Compare( url1, url3, UriComponents.Scheme, UriFormat.SafeUnescaped, StringComparison.OrdinalIgnoreCase ), target.Compare( url1, url3 ) );

            target = new UriComparer( UriComponents.Scheme, UriFormat.SafeUnescaped, false );
            Assert.Equal( Uri.Compare( url1, url2, UriComponents.Scheme, UriFormat.SafeUnescaped, StringComparison.Ordinal ), target.Compare( url1, url2 ) );
            Assert.Equal( Uri.Compare( url1, url3, UriComponents.Scheme, UriFormat.SafeUnescaped, StringComparison.Ordinal ), target.Compare( url1, url3 ) );
        }
예제 #23
0
        private bool WriteValue(XmlWriter writer, INode node, Context context)
        {
            if (node == null)
            {
                return(true);
            }

            // If raw XML value is provided, use that
            var literalValue = graph.FindObject(node, label).OfType <ILiteralNode>().FirstOrDefault(n => UriComparer.Equals(n.DataType, XMLLiteral) == true);

            if (literalValue != null)
            {
                writer.WriteRaw(literalValue.Value);
                return(true);
            }

            // Write all comments first
            foreach (var comment in graph.FindObject(node, comment).OfType <ILiteralNode>())
            {
                if (comment.DataType != null)
                {
                    var piName = GetXmlName(graph.CreateUriNode(comment.DataType), context.DocumentNode, null);
                    if (piName != null)
                    {
                        writer.WriteProcessingInstruction(piName.Name, comment.Value);
                        continue;
                    }
                }
                writer.WriteComment(comment.Value);
            }

            if (node is ILiteralNode literal)
            {
                if (UriComparer.Equals(literal.DataType, XMLLiteral))
                {
                    writer.WriteRaw(literal.Value);
                }
                else
                {
                    writer.WriteString(literal.Value);
                }
                return(true);
            }

            var(elementType, elementName) = graph.FindObject(node, a).Select(t => (t, n: GetXmlName(t, context.DocumentNode, null))).FirstOrDefault(t => t.n != null);
            if (elementName != null)
            {
                if (context.FindRoot)
                {
                    throw new RootNameSignal(elementName.Name);
                }
                writer.WriteStartElement(null, elementName.Name, elementName.Namespace);

                foreach (var(pred, obj) in graph.FindPredicateObject(node))
                {
                    var attributeName = GetXmlName(pred, context.DocumentNode, elementType);
                    if (attributeName != null)
                    {
                        if (!(obj is ILiteralNode value))
                        {
                            value = graph.FindPredicateObject(obj).Select(t => t.obj).OfType <ILiteralNode>().FirstOrDefault(l => UriComparer.Equals(l.DataType, ID));
                            if (value == null)
                            {
                                continue;
                            }
                        }
                        writer.WriteAttributeString(attributeName.Name, attributeName.Namespace, value.Value);
                    }
                }
            }
            try{
                foreach (var value in GetValues(node))
                {
                    if (WriteValue(writer, value, context))
                    {
                        return(true);
                    }
                }
                var list = EnumerateList(node);
                if (list != null)
                {
                    foreach (var element in list)
                    {
                        if (!WriteValue(writer, element, context))
                        {
                            WriteFallback(writer, element, context);
                        }
                    }
                    return(true);
                }
                bool any = false;
                foreach (var member in graph.FindObject(node, member))
                {
                    if (WriteValue(writer, member, context))
                    {
                        any = true;
                    }
                }
                if (elementName != null)
                {
                    if (!any)
                    {
                        WriteFallback(writer, node, context);
                    }
                    return(true);
                }
                return(any);
            }finally{
                if (elementName != null)
                {
                    writer.WriteEndElement();
                }
            }
        }
예제 #24
0
 /// <summary>
 /// Creates a new Base Lucene Search Provider.
 /// </summary>
 /// <param name="ver">Lucene Version.</param>
 /// <param name="indexDir">Directory.</param>
 /// <param name="analyzer">Analyzer.</param>
 /// <param name="schema">Index Schema.</param>
 public BaseLuceneSearchProvider(LucUtil.Version ver, Directory indexDir, Analyzer analyzer, IFullTextIndexSchema schema)
     : this(ver, indexDir, analyzer, schema, true)
 {
     _uriComparer = new UriComparer();
 }
예제 #25
0
 private bool IsNCName(ILiteralNode node)
 {
     return(UriComparer.Equals(node.DataType, NCName));
 }
예제 #26
0
 private XmlQualifiedName GetXmlName(INode node, INode documentNode, INode elementType)
 {
     if (graph.FindObject(node, label).OfType <ILiteralNode>().FirstOrDefault(n => UriComparer.Equals(n.DataType, NCName))?.Value is string name)
     {
         var ns = graph.FindObject(node, isDefinedBy).OfType <IUriNode>().FirstOrDefault();
         if (ns != null && !ns.Equals(elementType) && !graph.ContainsTriple(ns, isDefinedBy, documentNode))
         {
             return(new XmlQualifiedName(name, ns.Uri.GetString()));
         }
         return(new XmlQualifiedName(name));
     }
     return(null);
 }
예제 #27
0
        public void GetHashCodeShouldReturnCorrectValue()
        {
            // arrange
            var target = new UriComparer();
            var uri = new Uri( "about:blank" );
            var expected = uri.GetHashCode();

            // act
            var actual = target.GetHashCode( uri );
            
            // assert
            Assert.Equal( 0, target.GetHashCode( null ) );
            Assert.Equal( expected, actual );
        }