|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Text; |
| 5 | +using NUnit.Framework; |
| 6 | +using System.Drawing; |
| 7 | +using System.Xml.Serialization; |
| 8 | +using SharpMap.Serialization; |
| 9 | +using System.IO; |
| 10 | +using System.Net; |
| 11 | + |
| 12 | +namespace UnitTests.Serialization |
| 13 | +{ |
| 14 | + [TestFixture] |
| 15 | + class MapDocSerializationTests |
| 16 | + { |
| 17 | + [Test] |
| 18 | + public void TestSerializeWmsLayer() |
| 19 | + { |
| 20 | + SharpMap.Map m = new SharpMap.Map(); |
| 21 | + SharpMap.Layers.WmsLayer l = new SharpMap.Layers.WmsLayer("testwms", "http://mapus.jpl.nasa.gov/wms.cgi?"); |
| 22 | + l.AddChildLayers(l.RootLayer, false); |
| 23 | + m.Layers.Add(l); |
| 24 | + MemoryStream ms = new MemoryStream(); |
| 25 | + SharpMap.Serialization.MapSerialization.SaveMapToStream(m, ms); |
| 26 | + string txt = System.Text.ASCIIEncoding.ASCII.GetString(ms.ToArray()); |
| 27 | + Console.WriteLine(txt); |
| 28 | + Assert.IsTrue(txt.Contains(@"<Layers> |
| 29 | + <MapLayer xsi:type=""WmsLayer""> |
| 30 | + <Name>testwms</Name> |
| 31 | + <MinVisible>0</MinVisible> |
| 32 | + <MaxVisible>1.7976931348623157E+308</MaxVisible> |
| 33 | + <OnlineURL>http://mapus.jpl.nasa.gov/wms.cgi?</OnlineURL> |
| 34 | + <WmsLayers>global_mosaic,global_mosaic_base,us_landsat_wgs84,srtm_mag,daily_planet,daily_afternoon,BMNG,modis,huemapped_srtm,srtmplus,worldwind_dem,us_ned,us_elevation,us_colordem,gdem</WmsLayers> |
| 35 | + </MapLayer> |
| 36 | + </Layers>")); |
| 37 | + ms.Close(); |
| 38 | + } |
| 39 | + |
| 40 | + [Test] |
| 41 | + public void TestDeSerializeWmsLayer() |
| 42 | + { |
| 43 | + |
| 44 | + MemoryStream ms = new MemoryStream(System.Text.ASCIIEncoding.ASCII.GetBytes(@"<?xml version=""1.0""?> |
| 45 | +<MapDefinition xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema""> |
| 46 | + <BackGroundColor>Transparent</BackGroundColor> |
| 47 | + <Extent> |
| 48 | + <Xmin>-0.5</Xmin> |
| 49 | + <Ymin>-0.375</Ymin> |
| 50 | + <Xmax>0.5</Xmax> |
| 51 | + <Ymax>0.375</Ymax> |
| 52 | + </Extent> |
| 53 | + <Layers> |
| 54 | + <MapLayer xsi:type=""WmsLayer""> |
| 55 | + <Name>testwms</Name> |
| 56 | + <MinVisible>0</MinVisible> |
| 57 | + <MaxVisible>1.7976931348623157E+308</MaxVisible> |
| 58 | + <OnlineURL>http://mapus.jpl.nasa.gov/wms.cgi?</OnlineURL> |
| 59 | + </MapLayer> |
| 60 | + </Layers> |
| 61 | + <SRID>4326</SRID> |
| 62 | +</MapDefinition> |
| 63 | +")); |
| 64 | + SharpMap.Map m = SharpMap.Serialization.MapSerialization.LoadMapFromStream(ms); |
| 65 | + Assert.AreEqual(4326, m.SRID); |
| 66 | + Assert.AreEqual(1, m.Layers.Count); |
| 67 | + Assert.IsInstanceOfType(typeof(SharpMap.Layers.WmsLayer), m.Layers[0]); |
| 68 | + Assert.AreEqual("http://mapus.jpl.nasa.gov/wms.cgi?", (m.Layers[0] as SharpMap.Layers.WmsLayer).CapabilitiesUrl); |
| 69 | + ms.Close(); |
| 70 | + } |
| 71 | + |
| 72 | + [Test] |
| 73 | + public void TestDeSerializeWmsLayerWithCredentials() |
| 74 | + { |
| 75 | + |
| 76 | + MemoryStream ms = new MemoryStream(System.Text.ASCIIEncoding.ASCII.GetBytes(@"<?xml version=""1.0""?> |
| 77 | +<MapDefinition xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema""> |
| 78 | + <BackGroundColor>Transparent</BackGroundColor> |
| 79 | + <Extent> |
| 80 | + <Xmin>-0.5</Xmin> |
| 81 | + <Ymin>-0.375</Ymin> |
| 82 | + <Xmax>0.5</Xmax> |
| 83 | + <Ymax>0.375</Ymax> |
| 84 | + </Extent> |
| 85 | + <Layers> |
| 86 | + <MapLayer xsi:type=""WmsLayer""> |
| 87 | + <Name>testwms</Name> |
| 88 | + <MinVisible>0</MinVisible> |
| 89 | + <MaxVisible>1.7976931348623157E+308</MaxVisible> |
| 90 | + <OnlineURL>http://mapus.jpl.nasa.gov/wms.cgi?</OnlineURL> |
| 91 | + <WmsUser>test</WmsUser> |
| 92 | + <WmsPassword>pw</WmsPassword> |
| 93 | + </MapLayer> |
| 94 | + </Layers> |
| 95 | + <SRID>4326</SRID> |
| 96 | +</MapDefinition> |
| 97 | +")); |
| 98 | + SharpMap.Map m = SharpMap.Serialization.MapSerialization.LoadMapFromStream(ms); |
| 99 | + Assert.AreEqual(4326, m.SRID); |
| 100 | + Assert.AreEqual(1, m.Layers.Count); |
| 101 | + Assert.IsInstanceOfType(typeof(SharpMap.Layers.WmsLayer), m.Layers[0]); |
| 102 | + Assert.AreEqual("http://mapus.jpl.nasa.gov/wms.cgi?", (m.Layers[0] as SharpMap.Layers.WmsLayer).CapabilitiesUrl); |
| 103 | + Assert.IsNotNull((m.Layers[0] as SharpMap.Layers.WmsLayer).Credentials); |
| 104 | + Assert.IsInstanceOfType(typeof(NetworkCredential), (m.Layers[0] as SharpMap.Layers.WmsLayer).Credentials); |
| 105 | + Assert.AreEqual("test", ((m.Layers[0] as SharpMap.Layers.WmsLayer).Credentials as NetworkCredential).UserName); |
| 106 | + Assert.AreEqual("pw", ((m.Layers[0] as SharpMap.Layers.WmsLayer).Credentials as NetworkCredential).Password); |
| 107 | + ms.Close(); |
| 108 | + } |
| 109 | + |
| 110 | + |
| 111 | + [Test] |
| 112 | + public void TestSerializeWmsLayerWithCredentials() |
| 113 | + { |
| 114 | + SharpMap.Map m = new SharpMap.Map(); |
| 115 | + SharpMap.Layers.WmsLayer l = new SharpMap.Layers.WmsLayer("testwms", "http://mapus.jpl.nasa.gov/wms.cgi?", TimeSpan.MaxValue, |
| 116 | + System.Net.WebRequest.DefaultWebProxy, new NetworkCredential("test", "pw")); |
| 117 | + m.Layers.Add(l); |
| 118 | + MemoryStream ms = new MemoryStream(); |
| 119 | + SharpMap.Serialization.MapSerialization.SaveMapToStream(m, ms); |
| 120 | + string txt = System.Text.ASCIIEncoding.ASCII.GetString(ms.ToArray()); |
| 121 | + Assert.IsTrue(txt.Contains(@"<Layers> |
| 122 | + <MapLayer xsi:type=""WmsLayer""> |
| 123 | + <Name>testwms</Name> |
| 124 | + <MinVisible>0</MinVisible> |
| 125 | + <MaxVisible>1.7976931348623157E+308</MaxVisible> |
| 126 | + <OnlineURL>http://mapus.jpl.nasa.gov/wms.cgi?</OnlineURL> |
| 127 | + <WmsLayers>global_mosaic,global_mosaic_base,us_landsat_wgs84,srtm_mag,daily_planet,daily_afternoon,BMNG,modis,huemapped_srtm,srtmplus,worldwind_dem,us_ned,us_elevation,us_colordem,gdem</WmsLayers> |
| 128 | + <WmsUser>test</WmsUser> |
| 129 | + <WmsPassword>pw</WmsPassword> |
| 130 | + </MapLayer> |
| 131 | + </Layers>")); |
| 132 | + ms.Close(); |
| 133 | + } |
| 134 | + } |
| 135 | +} |
0 commit comments