Author: jgomes
Date: Wed Feb  9 00:45:42 2011
New Revision: 1068709

URL: http://svn.apache.org/viewvc?rev=1068709&view=rev
Log:
Clean up Uri comparisons to account for parameter re-ordering by 
StringDictionary.

Modified:
    activemq/activemq-dotnet/Apache.NMS/trunk/src/test/csharp/URISupportTest.cs

Modified: 
activemq/activemq-dotnet/Apache.NMS/trunk/src/test/csharp/URISupportTest.cs
URL: 
http://svn.apache.org/viewvc/activemq/activemq-dotnet/Apache.NMS/trunk/src/test/csharp/URISupportTest.cs?rev=1068709&r1=1068708&r2=1068709&view=diff
==============================================================================
--- activemq/activemq-dotnet/Apache.NMS/trunk/src/test/csharp/URISupportTest.cs 
(original)
+++ activemq/activemq-dotnet/Apache.NMS/trunk/src/test/csharp/URISupportTest.cs 
Wed Feb  9 00:45:42 2011
@@ -17,9 +17,7 @@
 
 using System;
 using System.Collections.Specialized;
-
 using Apache.NMS.Util;
-
 using NUnit.Framework;
 
 namespace Apache.NMS.Test
@@ -27,245 +25,252 @@ namespace Apache.NMS.Test
        [TestFixture()]
        public class URISupportTest
        {
-           protected void AssertMapKey(StringDictionary map, String key, 
Object expected)
-        {
-               Assert.AreEqual(expected, map[key], "Map key: " + key);
-           }
-
-        [Test]
-        public void TestCreateSupportedUriVariations(
-            [Values("tcp://127.0.0.1:61616",
-                    "tcp:127.0.0.1:61616",
-                    "failover:tcp://127.0.0.1:61616",
-                    "failover:(tcp://127.0.0.1:61616)",
-                    "failover://(tcp://127.0.0.1:61616)",
-                    "failover://(tcp://127.0.0.1:61616,tcp:192.168.0.1:61616)",
-                    "activemq:failover:(tcp://localhost:61616)",
-                    "activemq:failover:(tcp://${activemqhost}:61616)")]
-               string uriString)
-        {
-            Uri result = 
URISupport.CreateCompatibleUri(NMSTestSupport.ReplaceEnvVar(uriString));
-
-            Assert.IsNotNull(result);
-        }
-
-        [Test]
-        public void TestParseCompositeWithFragment()
-        {
-            string uriString =
-                
"failover:(tcp://localhost:61616,ssl://remotehost:61617?param=true#fragment)#fragment";
-
-            URISupport.CompositeData rc = URISupport.ParseComposite(new 
Uri(uriString));
-
-            Assert.IsTrue(rc.Components.Length == 2);
-            Assert.AreEqual("failover", rc.Scheme);
-            Assert.AreEqual("#fragment", rc.Fragment);
-
-            Uri uri1 = rc.Components[0];
-            Uri uri2 = rc.Components[1];
-
-            Assert.AreEqual("tcp", uri1.Scheme);
-            Assert.AreEqual("ssl", uri2.Scheme);
-            Assert.AreEqual("localhost", uri1.Host);
-            Assert.AreEqual("remotehost", uri2.Host);
-            Assert.AreEqual(61616, uri1.Port);
-            Assert.AreEqual(61617, uri2.Port);
-            Assert.IsTrue(String.IsNullOrEmpty(uri1.Fragment));
-            Assert.IsNotNull(uri2.Fragment);
-            Assert.AreEqual("#fragment", uri2.Fragment);
-            Assert.AreEqual("?param=true", uri2.Query);
-        }
+               protected void AssertMapKey(StringDictionary map, String key, 
Object expected)
+               {
+                       Assert.AreEqual(expected, map[key], "Map key: " + key);
+               }
 
-        [Test]
-        public void TestCreateRemainingUriNonComposite()
-        {
-            string uriStringNoParams = "tcp://localhost:61616";
-            string uriStringWithParams = 
"tcp://localhost:61616?param1=true&param2=false";
+               [Test]
+               public void TestCreateSupportedUriVariations(
+                       [Values("tcp://127.0.0.1:61616",
+                                       "tcp:127.0.0.1:61616",
+                                       "failover:tcp://127.0.0.1:61616",
+                                       "failover:(tcp://127.0.0.1:61616)",
+                                       "failover://(tcp://127.0.0.1:61616)",
+                                       
"failover://(tcp://127.0.0.1:61616,tcp:192.168.0.1:61616)",
+                                       
"activemq:failover:(tcp://localhost:61616)",
+                                       
"activemq:failover:(tcp://${activemqhost}:61616)")]
+               string uriString)
+               {
+                       Uri result = 
URISupport.CreateCompatibleUri(NMSTestSupport.ReplaceEnvVar(uriString));
 
-            Uri uriNoParams = new Uri(uriStringNoParams);
-            Uri uriWithParams = new Uri(uriStringWithParams);
+                       Assert.IsNotNull(result);
+               }
 
-            Uri result = URISupport.CreateRemainingUri(uriNoParams, null);
-            Assert.AreEqual(uriStringNoParams, result.OriginalString);
+               [Test]
+               public void TestParseCompositeWithFragment()
+               {
+                       string uriString =
+                               
"failover:(tcp://localhost:61616,ssl://remotehost:61617?param=true#fragment)#fragment";
 
-            result = URISupport.CreateRemainingUri(uriWithParams, null);
-            Assert.AreEqual(uriStringNoParams, result.OriginalString);
+                       URISupport.CompositeData rc = 
URISupport.ParseComposite(new Uri(uriString));
 
-            StringDictionary parameters = new StringDictionary();
-            parameters.Add("param1", "true");
-            parameters.Add("param2", "false");
+                       Assert.IsTrue(rc.Components.Length == 2);
+                       Assert.AreEqual("failover", rc.Scheme);
+                       Assert.AreEqual("#fragment", rc.Fragment);
+
+                       Uri uri1 = rc.Components[0];
+                       Uri uri2 = rc.Components[1];
+
+                       Assert.AreEqual("tcp", uri1.Scheme);
+                       Assert.AreEqual("ssl", uri2.Scheme);
+                       Assert.AreEqual("localhost", uri1.Host);
+                       Assert.AreEqual("remotehost", uri2.Host);
+                       Assert.AreEqual(61616, uri1.Port);
+                       Assert.AreEqual(61617, uri2.Port);
+                       Assert.IsTrue(String.IsNullOrEmpty(uri1.Fragment));
+                       Assert.IsNotNull(uri2.Fragment);
+                       Assert.AreEqual("#fragment", uri2.Fragment);
+                       Assert.AreEqual("?param=true", uri2.Query);
+               }
 
-            result = URISupport.CreateRemainingUri(uriNoParams, parameters);
-            Assert.AreEqual(uriWithParams, result.OriginalString);
-        }
+               [Test]
+               public void TestCreateRemainingUriNonComposite()
+               {
+                       string uriStringNoParams = "tcp://localhost:61616";
+                       string uriStringWithParams1 = 
"tcp://localhost:61616?param1=true&param2=false";
+                       string uriStringWithParams2 = 
"tcp://localhost:61616?param2=false&param1=true";
+
+                       Uri uriNoParams = new Uri(uriStringNoParams);
+                       Uri uriWithParams1 = new Uri(uriStringWithParams1);
+
+                       Uri result = URISupport.CreateRemainingUri(uriNoParams, 
null);
+                       Assert.AreEqual(uriStringNoParams, 
result.OriginalString);
+
+                       result = URISupport.CreateRemainingUri(uriWithParams1, 
null);
+                       Assert.AreEqual(uriStringNoParams, 
result.OriginalString);
+
+                       StringDictionary parameters = new StringDictionary();
+                       parameters.Add("param1", "true");
+                       parameters.Add("param2", "false");
+
+                       result = URISupport.CreateRemainingUri(uriNoParams, 
parameters);
+                       // Have to test for reordering of parameters.  The 
StringDictionary that is used internally
+                       // does not guarantee ordering of parameters.  Out of 
order parameters are equivalent.
+                       // We just want to test that they were created 
successfully.
+                       bool isEqual = (0 == 
string.Compare(uriStringWithParams1, result.OriginalString)
+                                                       || 0 == 
string.Compare(uriStringWithParams2, result.OriginalString));
+                       Assert.IsTrue(isEqual, string.Format("Error creating 
remaining Uri: {0}", result.OriginalString));
+               }
 
-        [Test]
-        public void TestCreateRemainingUriComposite()
-        {
-            string uriStringNoParams = "failover:tcp://localhost:61616";
-            string uriStringWithParams = 
"failover:tcp://localhost:61616?param1=true&param2=false";
+               [Test]
+               public void TestCreateRemainingUriComposite()
+               {
+                       string uriStringNoParams = 
"failover:tcp://localhost:61616";
+                       string uriStringWithParams1 = 
"failover:tcp://localhost:61616?param1=true&param2=false";
+                       string uriStringWithParams2 = 
"failover:tcp://localhost:61616?param2=false&param1=true";
 
-            Uri uriNoParams = new Uri(uriStringNoParams);
-            Uri uriWithParams = new Uri(uriStringWithParams);
+                       Uri uriNoParams = new Uri(uriStringNoParams);
+                       Uri uriWithParams = new Uri(uriStringWithParams1);
 
-            Uri result = URISupport.CreateRemainingUri(uriNoParams, null);
-            Assert.AreEqual(uriStringNoParams, result.OriginalString);
+                       Uri result = URISupport.CreateRemainingUri(uriNoParams, 
null);
+                       Assert.AreEqual(uriStringNoParams, 
result.OriginalString);
 
-            result = URISupport.CreateRemainingUri(uriWithParams, null);
-            Assert.AreEqual(uriStringNoParams, result.OriginalString);
+                       result = URISupport.CreateRemainingUri(uriWithParams, 
null);
+                       Assert.AreEqual(uriStringNoParams, 
result.OriginalString);
 
-            StringDictionary parameters = new StringDictionary();
-            parameters.Add("param1", "true");
-            parameters.Add("param2", "false");
+                       StringDictionary parameters = new StringDictionary();
+                       parameters.Add("param1", "true");
+                       parameters.Add("param2", "false");
 
-            result = URISupport.CreateRemainingUri(uriNoParams, parameters);
-            Assert.AreEqual(uriWithParams, result.OriginalString);
+                       result = URISupport.CreateRemainingUri(uriNoParams, 
parameters);
+                       bool isEqual = (0 == 
string.Compare(uriStringWithParams1, result.OriginalString)
+                                                       || 0 == 
string.Compare(uriStringWithParams2, result.OriginalString));
+                       Assert.IsTrue(isEqual, string.Format("Error creating 
remaining Uri: {0}", result.OriginalString));
 
-            // Now test a Comopsoite with parends.
+                       // Now test a Composite with parens.
 
-            uriStringNoParams = "failover:(tcp://localhost:61616)";
-            uriStringWithParams = 
"failover:(tcp://localhost:61616?param1=true&param2=false)";
+                       uriStringNoParams = "failover:(tcp://localhost:61616)";
+                       uriStringWithParams1 = 
"failover:(tcp://localhost:61616)?param1=true&param2=false";
+                       uriStringWithParams2 = 
"failover:(tcp://localhost:61616)?param2=false&param1=true";
 
-            uriNoParams = new Uri(uriStringNoParams);
-            uriWithParams = new Uri(uriStringWithParams);
+                       uriNoParams = new Uri(uriStringNoParams);
+                       uriWithParams = new Uri(uriStringWithParams1);
 
-            result = URISupport.CreateRemainingUri(uriNoParams, null);
-            Assert.AreEqual(uriStringNoParams, result.OriginalString);
+                       result = URISupport.CreateRemainingUri(uriNoParams, 
null);
+                       Assert.AreEqual(uriStringNoParams, 
result.OriginalString);
 
-            result = URISupport.CreateRemainingUri(uriWithParams, null);
-            Assert.AreNotEqual(uriStringNoParams, result.OriginalString);
+                       result = URISupport.CreateRemainingUri(uriWithParams, 
null);
+                       Assert.AreNotEqual(uriStringNoParams, 
result.OriginalString);
 
-            result = URISupport.CreateRemainingUri(uriNoParams, parameters);
-            Assert.AreNotEqual(uriWithParams, result.OriginalString);
+                       result = URISupport.CreateRemainingUri(uriNoParams, 
parameters);
+                       isEqual = (0 == string.Compare(uriStringWithParams1, 
result.OriginalString)
+                                                       || 0 == 
string.Compare(uriStringWithParams2, result.OriginalString));
+                       Assert.IsTrue(isEqual, string.Format("Error creating 
remaining Uri: {0}", result.OriginalString));
 
-            string queryString = URISupport.CreateQueryString(parameters);
-            Assert.AreEqual(uriNoParams.OriginalString + "?" + queryString, 
result.OriginalString);
-        }
+                       string queryString = 
URISupport.CreateQueryString(parameters);
+                       Assert.AreEqual(uriNoParams.OriginalString + "?" + 
queryString, result.OriginalString);
+               }
 
                [Test]
-           public void TestEmptyCompositePath()
+               public void TestEmptyCompositePath()
                {
-               URISupport.CompositeData data = URISupport.ParseComposite(
-                new Uri("broker:()/localhost?persistent=false"));
-
-               Assert.AreEqual(0, data.Components.Length);
-           }
+                       URISupport.CompositeData data = 
URISupport.ParseComposite(new Uri("broker:()/localhost?persistent=false"));
+                       Assert.AreEqual(0, data.Components.Length);
+               }
 
                [Test]
-           public void TestCompositePath()
+               public void TestCompositePath()
                {
-               URISupport.CompositeData data = URISupport.ParseComposite(new 
Uri("test:(test:path)/path"));
-               Assert.AreEqual("path", data.Path);
-               data = URISupport.ParseComposite(new Uri("test:test:path"));
-               Assert.IsNull(data.Path);
-           }
+                       URISupport.CompositeData data = 
URISupport.ParseComposite(new Uri("test:(test:path)/path"));
+                       Assert.AreEqual("path", data.Path);
+                       data = URISupport.ParseComposite(new 
Uri("test:test:path"));
+                       Assert.IsNull(data.Path);
+               }
 
                [Test]
-           public void TestSimpleComposite()
+               public void TestSimpleComposite()
                {
-               URISupport.CompositeData data = URISupport.ParseComposite(new 
Uri("test:tcp://part1"));
-               Assert.AreEqual(1, data.Components.Length);
-           }
+                       URISupport.CompositeData data = 
URISupport.ParseComposite(new Uri("test:tcp://part1"));
+                       Assert.AreEqual(1, data.Components.Length);
+               }
 
                [Test]
-           public void TestComposite()
-        {
-               URISupport.CompositeData data = URISupport.ParseComposite(
-                new 
Uri("test:(part1://host,part2://(sub1://part,sube2:part))"));
-               Assert.AreEqual(2, data.Components.Length);
-           }
+               public void TestComposite()
+               {
+                       URISupport.CompositeData data = 
URISupport.ParseComposite(
+                               new 
Uri("test:(part1://host,part2://(sub1://part,sub2:part))"));
+                       Assert.AreEqual(2, data.Components.Length);
+               }
 
                [Test]
-           public void TestCompositeWithComponentParam()
+               public void TestCompositeWithComponentParam()
                {
-               URISupport.CompositeData data = URISupport.ParseComposite(new 
Uri("test:(part1://host?part1=true)?outside=true"));
-               Assert.AreEqual(1, data.Components.Length);
-               Assert.AreEqual(1, data.Parameters.Count);
-               StringDictionary part1Params = 
URISupport.ParseParameters(data.Components[0]);
-               Assert.AreEqual(1, part1Params.Count);
-               Assert.IsTrue(part1Params.ContainsKey("part1"));
-           }
+                       URISupport.CompositeData data = 
URISupport.ParseComposite(new 
Uri("test:(part1://host?part1=true)?outside=true"));
+                       Assert.AreEqual(1, data.Components.Length);
+                       Assert.AreEqual(1, data.Parameters.Count);
+                       StringDictionary part1Params = 
URISupport.ParseParameters(data.Components[0]);
+                       Assert.AreEqual(1, part1Params.Count);
+                       Assert.IsTrue(part1Params.ContainsKey("part1"));
+               }
 
                [Test]
-           public void TestParsingURI()
+               public void TestParsingURI()
                {
-               Uri source = new 
Uri("tcp://localhost:61626/foo/bar?cheese=Edam&x=123");
-
-               StringDictionary map = URISupport.ParseParameters(source);
-
-               Assert.AreEqual(2, map.Count, "Size: " + map);
-               AssertMapKey(map, "cheese", "Edam");
-               AssertMapKey(map, "x", "123");
-
-               Uri result = URISupport.RemoveQuery(source);
+                       Uri source = new 
Uri("tcp://localhost:61626/foo/bar?cheese=Edam&x=123");
+                       StringDictionary map = 
URISupport.ParseParameters(source);
 
-               Assert.AreEqual(new Uri("tcp://localhost:61626/foo/bar"), 
result);
-           }
+                       Assert.AreEqual(2, map.Count, "Size: " + map);
+                       AssertMapKey(map, "cheese", "Edam");
+                       AssertMapKey(map, "x", "123");
+
+                       Uri result = URISupport.RemoveQuery(source);
+                       Assert.AreEqual(new 
Uri("tcp://localhost:61626/foo/bar"), result);
+               }
 
                [Test]
-           public void TestParsingCompositeURI()
+               public void TestParsingCompositeURI()
                {
-               URISupport.CompositeData data = URISupport.ParseComposite(
-                
URISupport.CreateCompatibleUri("failover://(tcp://localhost:61616)?name=foo"));
+                       URISupport.CompositeData data = 
URISupport.ParseComposite(
+                               
URISupport.CreateCompatibleUri("failover://(tcp://localhost:61616)?name=foo"));
 
-               Assert.AreEqual(1, data.Components.Length, "one component");
-            Assert.AreEqual("localhost", data.Components[0].Host, "Component 
Host is incorrect");
-            Assert.AreEqual(61616, data.Components[0].Port, "Component Port is 
incorrect");
-               Assert.AreEqual(1, data.Parameters.Count, "Size: " + 
data.Parameters);
-           }
+                       Assert.AreEqual(1, data.Components.Length, "one 
component");
+                       Assert.AreEqual("localhost", data.Components[0].Host, 
"Component Host is incorrect");
+                       Assert.AreEqual(61616, data.Components[0].Port, 
"Component Port is incorrect");
+                       Assert.AreEqual(1, data.Parameters.Count, "Size: " + 
data.Parameters);
+               }
 
                [Test]
-           public void TestCheckParenthesis()
+               public void TestCheckParenthesis()
                {
-               String str = "fred:(((ddd))";
-               Assert.IsFalse(URISupport.CheckParenthesis(str));
-               str += ")";
-               Assert.IsTrue(URISupport.CheckParenthesis(str));
-           }
+                       String str = "fred:(((ddd))";
+                       Assert.IsFalse(URISupport.CheckParenthesis(str));
+                       str += ")";
+                       Assert.IsTrue(URISupport.CheckParenthesis(str));
+               }
 
-        [Test]
-        public void TestParseQueury()
-        {
-            String query1 = "?param1=false&param2=true";
-            String query2 = "param3=false&param4=true&param5=foo";
-
-            StringDictionary results = URISupport.ParseQuery(query1);
-
-            Assert.IsTrue(results.Count == 2);
-            Assert.AreEqual("false", results["param1"]);
-            Assert.AreEqual("true", results["param2"]);
-
-            results = URISupport.ParseQuery(query2);
-
-            Assert.IsTrue(results.Count == 3);
-            Assert.AreEqual("false", results["param3"]);
-            Assert.AreEqual("true", results["param4"]);
-            Assert.AreEqual("foo", results["param5"]);
+               [Test]
+               public void TestParseQueury()
+               {
+                       String query1 = "?param1=false&param2=true";
+                       String query2 = "param3=false&param4=true&param5=foo";
 
-            String query3 = "?param";
+                       StringDictionary results = 
URISupport.ParseQuery(query1);
 
-            try
-            {
-                URISupport.ParseQuery(query3);
-                Assert.Fail("Should have thrown an Exception on invalid 
parameter.");
-            }
-            catch
-            {
-            }
-        }
+                       Assert.IsTrue(results.Count == 2);
+                       Assert.AreEqual("false", results["param1"]);
+                       Assert.AreEqual("true", results["param2"]);
+
+                       results = URISupport.ParseQuery(query2);
+
+                       Assert.IsTrue(results.Count == 3);
+                       Assert.AreEqual("false", results["param3"]);
+                       Assert.AreEqual("true", results["param4"]);
+                       Assert.AreEqual("foo", results["param5"]);
+
+                       String query3 = "?param";
+
+                       try
+                       {
+                               URISupport.ParseQuery(query3);
+                               Assert.Fail("Should have thrown an Exception on 
invalid parameter.");
+                       }
+                       catch
+                       {
+                       }
+               }
 
                [Test]
-           public void TestCreateWithQuery()
+               public void TestCreateWithQuery()
                {
-               Uri source = new Uri("vm://localhost");
-               Uri dest = URISupport.CreateUriWithQuery(source, 
"network=true&one=two");
-
-               Assert.AreEqual(2, URISupport.ParseParameters(dest).Count, 
"correct param count");
-               Assert.AreEqual(source.Host, dest.Host, "same uri, host");
-               Assert.AreEqual(source.Scheme, dest.Scheme, "same uri, scheme");
-               Assert.IsFalse(dest.Query.Equals(source.Query), "same uri, 
ssp");
-           }
+                       Uri source = new Uri("vm://localhost");
+                       Uri dest = URISupport.CreateUriWithQuery(source, 
"network=true&one=two");
 
+                       Assert.AreEqual(2, 
URISupport.ParseParameters(dest).Count, "correct param count");
+                       Assert.AreEqual(source.Host, dest.Host, "same uri, 
host");
+                       Assert.AreEqual(source.Scheme, dest.Scheme, "same uri, 
scheme");
+                       Assert.IsFalse(dest.Query.Equals(source.Query), "same 
uri, ssp");
+               }
        }
 }
 


Reply via email to