public void WhenTwoPrefixesMapToTheSameUriTheUriToPrefixMapIsExtended() { var nsmap = new NamespaceMapper(); nsmap.AddNamespace("foo", new Uri("http://example.org/")); nsmap.AddNamespace("bar", new Uri("http://example.org/")); nsmap.GetPrefix(new Uri("http://example.org/")).Should().BeOneOf("foo", "bar"); nsmap.RemoveNamespace("bar"); nsmap.GetPrefix(new Uri("http://example.org/")).Should().Be("foo"); }
public void NamespaceMapperEvent() { bool eventRaised = false; NamespaceChanged added = delegate(String prefix, Uri u) { eventRaised = true; }; NamespaceChanged changed = delegate(String prefix, Uri u) { eventRaised = true; }; NamespaceChanged removed = delegate(String prefix, Uri u) { eventRaised = true; }; NamespaceMapper nsmap = new NamespaceMapper(); nsmap.NamespaceAdded += added; nsmap.NamespaceModified += changed; nsmap.NamespaceRemoved += removed; Console.WriteLine("Trying to add the RDF Namespace, this should already be defined"); nsmap.AddNamespace("rdf", new Uri(NamespaceMapper.RDF)); Assert.False(eventRaised); eventRaised = false; Console.WriteLine(); Console.WriteLine("Trying to add an example Namespace which isn't defined"); nsmap.AddNamespace("ex", new Uri("http://example.org/")); Assert.True(eventRaised); eventRaised = false; Console.WriteLine(nsmap.GetNamespaceUri("ex").AbsoluteUri); Console.WriteLine(); Console.WriteLine("Trying to modify the example Namespace"); nsmap.AddNamespace("ex", new Uri("http://example.org/test/")); Assert.True(eventRaised); eventRaised = false; Console.WriteLine(nsmap.GetNamespaceUri("ex").AbsoluteUri); Console.WriteLine(); Console.WriteLine("Trying to remove the example Namespace"); nsmap.RemoveNamespace("ex"); Assert.True(eventRaised); eventRaised = false; Console.WriteLine(); Console.WriteLine("Trying to remove a non-existent Namespace"); nsmap.RemoveNamespace("ex"); Assert.False(eventRaised); eventRaised = false; Console.WriteLine(); Console.WriteLine("Adding some example Namespace back in again for an import test"); nsmap.AddNamespace("ex", new Uri("http://example.org/")); nsmap.AddNamespace("ns0", new Uri("http://example.org/clashes/")); Console.WriteLine("Creating another Namespace Mapper with the ex prefix mapped to a different URI"); NamespaceMapper nsmap2 = new NamespaceMapper(); nsmap2.AddNamespace("ex", new Uri("http://example.org/test/")); Console.WriteLine("Importing the new NamespaceMapper into the original"); nsmap.Import(nsmap2); Console.WriteLine("NamespaceMapper now contains the following Namespaces:"); foreach (String prefix in nsmap.Prefixes) { Console.WriteLine("\t" + prefix + " <" + nsmap.GetNamespaceUri(prefix).AbsoluteUri + ">"); } Assert.Equal(nsmap.GetNamespaceUri("ex"), new Uri("http://example.org/")); Assert.Equal(nsmap.GetNamespaceUri("ns1"), new Uri("http://example.org/test/")); }