|
| 1 | +using NetTopologySuite.IO.Esri.Dbf.Fields; |
| 2 | +using NetTopologySuite.IO.Esri.Shapefiles.Writers; |
| 3 | +using NUnit.Framework; |
| 4 | +using System; |
| 5 | +using System.Collections.Generic; |
| 6 | +using System.IO; |
| 7 | +using System.Text; |
| 8 | + |
| 9 | +namespace NetTopologySuite.IO.Esri.Test.Issues; |
| 10 | + |
| 11 | +/// <summary> |
| 12 | +/// https://github.com/NetTopologySuite/NetTopologySuite.IO.Esri/issues/53 |
| 13 | +/// </summary> |
| 14 | +internal class Issue053 |
| 15 | +{ |
| 16 | + [Test] |
| 17 | + public void Projection_Utf8_BOM() |
| 18 | + { |
| 19 | + var fields = new List<DbfField>(); |
| 20 | + var fidField = fields.AddNumericInt32Field("fid"); |
| 21 | + var options = new ShapefileWriterOptions(ShapeType.Polygon, fields.ToArray()) |
| 22 | + { |
| 23 | + Projection = "GEOGCS[\"GCS_WGS_1984\",DATUM[\"D_WGS_1984\",SPHEROID[\"WGS_1984\",6378137.0,298.257223563]],PRIMEM[\"Greenwich\",0.0],UNIT[\"Degree\",0.0174532925199433]]" |
| 24 | + }; |
| 25 | + |
| 26 | + var shpPath = TestShapefiles.GetTempShpPath(); |
| 27 | + using (var shpWriter = Shapefile.OpenWrite(shpPath, options)) |
| 28 | + { |
| 29 | + shpWriter.Geometry = SampleGeometry.SampleMultiPolygon; |
| 30 | + fidField.NumericValue = 1; |
| 31 | + shpWriter.Write(); |
| 32 | + } |
| 33 | + |
| 34 | + var expectedProjectionString = options.Projection; |
| 35 | + var expectedProjectionBytes = options.Encoding.GetBytes(options.Projection); |
| 36 | + |
| 37 | + var prjPath = Path.ChangeExtension(shpPath, ".prj"); |
| 38 | + var storedProjectionString = File.ReadAllText(prjPath); |
| 39 | + var storedProjectionBytes = File.ReadAllBytes(prjPath); |
| 40 | + |
| 41 | + TestShapefiles.DeleteShp(shpPath); |
| 42 | + |
| 43 | + Assert.AreEqual(expectedProjectionString, storedProjectionString); |
| 44 | + Assert.AreEqual(expectedProjectionBytes, storedProjectionBytes); |
| 45 | + } |
| 46 | + |
| 47 | + [Test] |
| 48 | + public static void Utf8_BOM_Default() |
| 49 | + { |
| 50 | + var encoding = Encoding.UTF8; |
| 51 | + var filePath = Path.GetTempFileName(); |
| 52 | + var expectedString = "abc"; |
| 53 | + var expectedBytes = encoding.GetBytes(expectedString); |
| 54 | + WriteFile(filePath, expectedString, encoding); |
| 55 | + |
| 56 | + var storedString = File.ReadAllText(filePath, encoding); |
| 57 | + var storedBytes = File.ReadAllBytes(filePath); |
| 58 | + |
| 59 | + Assert.AreEqual(expectedString, storedString); // C# is cleaver enough to ignore BOM when reading |
| 60 | + Assert.AreNotEqual(expectedBytes, storedBytes); // Not equal because of BOM stored by default |
| 61 | + } |
| 62 | + |
| 63 | + [Test] |
| 64 | + public static void Utf8_BOM_Included() |
| 65 | + { |
| 66 | + var encoding = new UTF8Encoding(encoderShouldEmitUTF8Identifier: true); |
| 67 | + var filePath = Path.GetTempFileName(); |
| 68 | + var expectedString = "abc"; |
| 69 | + var expectedBytes = encoding.GetBytes(expectedString); |
| 70 | + WriteFile(filePath, expectedString, encoding); |
| 71 | + |
| 72 | + var storedString = File.ReadAllText(filePath, encoding); |
| 73 | + var storedBytes = File.ReadAllBytes(filePath); |
| 74 | + |
| 75 | + Assert.AreEqual(expectedString, storedString); // C# is cleaver enough to ignore BOM when reading |
| 76 | + Assert.AreNotEqual(expectedBytes, storedBytes); // Not equal because of BOM stored explicitly |
| 77 | + } |
| 78 | + |
| 79 | + [Test] |
| 80 | + public static void Utf8_BOM_Excluded() |
| 81 | + { |
| 82 | + var encoding = new UTF8Encoding(encoderShouldEmitUTF8Identifier: false); |
| 83 | + var filePath = Path.GetTempFileName(); |
| 84 | + var expectedString = "abc"; |
| 85 | + var expectedBytes = encoding.GetBytes(expectedString); |
| 86 | + WriteFile(filePath, expectedString, encoding); |
| 87 | + |
| 88 | + var storedString = File.ReadAllText(filePath, encoding); |
| 89 | + var storedBytes = File.ReadAllBytes(filePath); |
| 90 | + |
| 91 | + Assert.AreEqual(expectedString, storedString); |
| 92 | + Assert.AreEqual(expectedBytes, storedBytes); |
| 93 | + } |
| 94 | + |
| 95 | + private static void WriteFile(string filePath, string content, Encoding encoding) |
| 96 | + { |
| 97 | + using (StreamWriter writer = new StreamWriter(filePath, false, encoding)) |
| 98 | + { |
| 99 | + writer.Write(content); |
| 100 | + } |
| 101 | + } |
| 102 | +} |
0 commit comments