Advertisement
Guest User

DotSpatial Unit Test - mdcuesta

a guest
Mar 9th, 2012
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 13.66 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using DotSpatial.Data;
  6. using DotSpatial.Topology;
  7. using NUnit.Framework;
  8. using Moq;
  9.  
  10. namespace DotSpatial.Data.Tests
  11. {
  12.     [TestFixture]
  13.     public class ShapeTest
  14.     {
  15.         #region Constructor Tests
  16.         /// <summary>
  17.         /// Empty parameter constructor test
  18.         /// </summary>
  19.         [Test]
  20.         public void TestConstructShape()
  21.         {
  22.             var shape = new Shape();
  23.             Assert.IsNotNull(shape);
  24.         }
  25.  
  26.         /// <summary>
  27.         /// Constructor test passing FeatureType
  28.         /// </summary>
  29.         [Test]
  30.         public void TestConstructShapeFromFeatureType()
  31.         {
  32.             var shape = new Shape(FeatureType.Point);
  33.             Assert.IsNotNull(shape);
  34.         }
  35.  
  36.         /// <summary>
  37.         /// Constructor test passing IFeature
  38.         /// Test Intended to Pass
  39.         /// </summary>
  40.         [Test]
  41.         public void TestConstructShapeFromIFeature()
  42.         {
  43.            
  44.             var feature = new Mock<IFeature>();
  45.             var geometry = new Mock<IBasicGeometry>();
  46.             var envelope = new Mock<IEnvelope>();
  47.             var coordinate1 = new Coordinate(2, 30);
  48.             var coordinate2 = new Coordinate(10, 40);
  49.  
  50.             var coordinateList = new List<Coordinate> {coordinate1, coordinate2};
  51.  
  52.             //setup geometry
  53.             envelope.Setup(e => e.Minimum).Returns(coordinate1);
  54.             envelope.Setup(e => e.Maximum).Returns(coordinate2);
  55.  
  56.             geometry.Setup(g => g.Envelope).Returns(envelope.Object);
  57.            
  58.             var dataTable = new System.Data.DataTable("FeatureAttributesTable");
  59.             dataTable.Columns.Add("Column1");
  60.             var row = dataTable.NewRow();
  61.             row.ItemArray = new object[] {"attribute"};
  62.             dataTable.Rows.Add(row);
  63.  
  64.  
  65.             feature.Setup(f => f.DataRow).Returns(row);
  66.             feature.Setup(f => f.NumPoints).Returns(50);
  67.             feature.Setup(f => f.BasicGeometry).Returns(geometry.Object);
  68.             feature.Setup(f => f.Coordinates).Returns(coordinateList);
  69.            
  70.             var shape = new Shape(feature.Object);
  71.  
  72.             Assert.IsNotNull(shape);
  73.         }
  74.  
  75.         /// <summary>
  76.         /// Constructor test passing IFeature where NumPoints is 0
  77.         /// Fail: When NumPoints is 0, this constructor fails
  78.         /// Maybe NumPoints is not really intended to be 0.
  79.         /// Implementing IFeature and setting NumPoints to 0 will cause an exception
  80.         /// NumPoints with value 0 needs to be checked or at least pass a proper exception message
  81.         /// </summary>
  82.         [Test]
  83.         public void TestConstructShapeFromIFeatureWhereNumPointsIsZero()
  84.         {
  85.  
  86.             var feature = new Mock<IFeature>();
  87.             var geometry = new Mock<IBasicGeometry>();
  88.             var envelope = new Mock<IEnvelope>();
  89.             var coordinate1 = new Coordinate(2, 30);
  90.             var coordinate2 = new Coordinate(10, 40);
  91.  
  92.             var coordinateList = new List<Coordinate> { coordinate1, coordinate2 };
  93.  
  94.             //setup geometry
  95.             envelope.Setup(e => e.Minimum).Returns(coordinate1);
  96.             envelope.Setup(e => e.Maximum).Returns(coordinate2);
  97.  
  98.             geometry.Setup(g => g.Envelope).Returns(envelope.Object);
  99.  
  100.             var dataTable = new System.Data.DataTable("FeatureAttributesTable");
  101.             dataTable.Columns.Add("Column1");
  102.             var row = dataTable.NewRow();
  103.             row.ItemArray = new object[] { "attribute" };
  104.             dataTable.Rows.Add(row);
  105.  
  106.  
  107.             feature.Setup(f => f.DataRow).Returns(row);
  108.             feature.Setup(f => f.NumPoints).Returns(0);
  109.             feature.Setup(f => f.BasicGeometry).Returns(geometry.Object);
  110.             feature.Setup(f => f.Coordinates).Returns(coordinateList);
  111.  
  112.             var shape = new Shape(feature.Object);
  113.  
  114.             Assert.IsNotNull(shape);
  115.         }
  116.  
  117.         /// <summary>
  118.         /// Constructor test passing IGeometry
  119.         /// Test Intended to Pass
  120.         /// </summary>
  121.         [Test]
  122.         public void TestConstructShapeFromIGeometry()
  123.         {
  124.             var geometry = new Mock<IGeometry>();
  125.  
  126.             //Coordinates
  127.             var coordinate1 = new Coordinate(2, 30);
  128.             var coordinate2 = new Coordinate(10, 40);
  129.             var coordinateList = new List<Coordinate> { coordinate1, coordinate2 };
  130.            
  131.             //Envelope
  132.             var envelope = new Mock<IEnvelope>();
  133.             envelope.Setup(e => e.Minimum).Returns(coordinate1);
  134.             envelope.Setup(e => e.Maximum).Returns(coordinate2);
  135.  
  136.             geometry.Setup(g => g.NumPoints).Returns(50);
  137.             geometry.Setup(g => g.Coordinates).Returns(coordinateList);
  138.             geometry.Setup(g => g.Envelope).Returns(envelope.Object);
  139.  
  140.             var shape = new Shape(geometry.Object);
  141.             Assert.IsNotNull(shape);
  142.         }
  143.  
  144.         /// <summary>
  145.         /// Constructor test passing IGeometry where NumPoints is 0
  146.         /// Fail: When NumPoints is 0, this constructor fails
  147.         /// Maybe NumPoints is not really intended to be 0.
  148.         /// Implementing IGeometry and setting NumPoints to 0 will cause an exception
  149.         /// NumPoints with value 0 needs to be checked or at least pass a proper exception message
  150.         /// </summary>
  151.         [Test]
  152.         public void TestConstructShapeFromIGeometryWhereNumPointsIsZero()
  153.         {
  154.             var geometry = new Mock<IGeometry>();
  155.  
  156.             //Coordinates
  157.             var coordinate1 = new Coordinate(2, 30);
  158.             var coordinate2 = new Coordinate(10, 40);
  159.             var coordinateList = new List<Coordinate> { coordinate1, coordinate2 };
  160.  
  161.             //Envelope
  162.             var envelope = new Mock<IEnvelope>();
  163.             envelope.Setup(e => e.Minimum).Returns(coordinate1);
  164.             envelope.Setup(e => e.Maximum).Returns(coordinate2);
  165.  
  166.             geometry.Setup(g => g.NumPoints).Returns(1);
  167.             geometry.Setup(g => g.Coordinates).Returns(coordinateList);
  168.             geometry.Setup(g => g.Envelope).Returns(envelope.Object);
  169.  
  170.             var shape = new Shape(geometry.Object);
  171.             Assert.IsNotNull(shape);
  172.         }
  173.  
  174.         /// <summary>
  175.         /// Constructor test passing Coordinate as parameter
  176.         /// </summary>
  177.         [Test]
  178.         public void TestConstructShapeFromCoordinate()
  179.         {
  180.             var coordinate = new Coordinate(2, 30);
  181.             var shape = new Shape(coordinate);
  182.             Assert.IsNotNull(shape);
  183.  
  184.         }
  185.  
  186.         /// <summary>
  187.         /// Constructor test passing Vertex as parameter
  188.         /// </summary>
  189.         [Test]
  190.         public void TestConstructShapeFromVertex()
  191.         {
  192.             var vertext = new Vertex(10, 40);
  193.             var shape = new Shape(vertext);
  194.             Assert.IsNotNull(shape);
  195.         }
  196.  
  197.         /// <summary>
  198.         /// Constructor test passing extent as parameter
  199.         /// </summary>
  200.         [Test]
  201.         public void TestConstructShapeFromExtent()
  202.         {
  203.             var extent = new Extent(1, 1, 50, 50);
  204.             var shape = new Shape(extent);
  205.             Assert.IsNotNull(shape);
  206.         }
  207.  
  208.         /// <summary>
  209.         /// Constructor test passing envelope as parameter
  210.         /// </summary>
  211.         [Test]
  212.         public void TestConstrucShapeFromEnvelope()
  213.         {
  214.             var coordinate1 = new Coordinate(1, 1);
  215.             var coordinate2 = new Coordinate(10, 10);
  216.             var envelope = new Envelope(coordinate1, coordinate2);
  217.             var shape = new Shape(envelope);
  218.             Assert.IsNotNull(shape);
  219.         }
  220.  
  221.         #region ArgumentNullChecking
  222.         //Argument Null Checking by throwing ArgumentNullException, unless if a null parameter is ok
  223.         //This prevents throwing the NullReferenceException whose details are dull
  224.         //By this users of the library will automatically know whats wrong with the instantiation
  225.         //Below are constructor overloads which would require the argument not to be null
  226.  
  227.         /// <summary>
  228.         /// Constructor test passing null IFeature
  229.         /// </summary>
  230.         [Test]
  231.         public void TestConstructorPassingNullIFeature()
  232.         {
  233.             try
  234.             {
  235.                 IFeature feature = null;
  236.                 var shape = new Shape(feature);
  237.             }
  238.             catch (Exception ex)
  239.             {
  240.                 Assert.IsTrue(ex is ArgumentNullException);
  241.             }
  242.         }
  243.  
  244.  
  245.         /// <summary>
  246.         /// Constructor test passing null IGeometry
  247.         /// </summary>
  248.         [Test]
  249.         public void TestConstructorPassingNullIGeometry()
  250.         {
  251.             try
  252.             {
  253.                 IGeometry geometry = null;
  254.                 var shape = new Shape(geometry);
  255.             }
  256.             catch (Exception ex)
  257.             {
  258.                 Assert.IsTrue(ex is ArgumentNullException);
  259.             }
  260.         }
  261.  
  262.         /// <summary>
  263.         /// Constructor test passing null Coordinate
  264.         /// </summary>
  265.         [Test]
  266.         public void TestContstructorPassingNullCoordinate()
  267.         {
  268.             try
  269.             {
  270.                 Coordinate coordinate = null;
  271.                 var shape = new Shape(coordinate);
  272.             }
  273.             catch (Exception ex)
  274.             {
  275.                 Assert.IsTrue(ex is ArgumentNullException);
  276.             }
  277.         }
  278.  
  279.         /// <summary>
  280.         /// Constructor test passing null Extent
  281.         /// </summary>
  282.         [Test]
  283.         public void TestConstructorPassingNullExtent()
  284.         {
  285.             try
  286.             {
  287.                 Extent extent = null;
  288.                 var shape = new Shape(extent);
  289.             }
  290.             catch (Exception ex)
  291.             {
  292.                 Assert.IsTrue(ex is ArgumentNullException);
  293.             }
  294.  
  295.         }
  296.  
  297.         /// <summary>
  298.         /// Consturctor test passing null IEnvelope
  299.         /// </summary>
  300.         [Test]
  301.         public void TestConstuctorPassingNullEnvelope()
  302.         {
  303.             try
  304.             {
  305.                 IEnvelope envelope = null;
  306.                 var shape = new Shape(envelope);
  307.             }
  308.             catch (Exception ex)
  309.             {
  310.                 Assert.IsTrue(ex is ArgumentNullException);
  311.             }
  312.         }
  313.         #endregion
  314.  
  315.         #endregion
  316.  
  317.         #region Method Tests
  318.         /// <summary>
  319.         /// Tests method ToGeometry()
  320.         /// Fail: _shapeRange.FeatureType is not defined
  321.         /// Creating an instance of shape without passing a parameter then calling ToGeometry method
  322.         /// throws a NullReferenceException (member: _shapeRange.FeatureType)
  323.         /// If the code content inside method ToGeometry(IGeometryFactory factory)
  324.         /// is relly intended to check _shapeRange.FeatureType, we need to show at least
  325.         /// an exception message saying it is not set
  326.         /// </summary>
  327.         [Test]
  328.         public void TestToGeometry()
  329.         {
  330.             var shape = new Shape();
  331.             var geometry = shape.ToGeometry();
  332.             Assert.IsNotNull(geometry);
  333.         }
  334.  
  335.         /// <summary>
  336.         /// Tests method ToGeometry(IGeometryFactory factory)
  337.         /// Fail: _shapeRange.FeatureType is not defined
  338.         /// Creating an instance of shape without passing a parameter then calling ToGeometry method
  339.         /// throws a NullReferenceException (member: _shapeRange.FeatureType)
  340.         /// If the code content inside method ToGeometry(IGeometryFactory factory)
  341.         /// is relly intended to check _shapeRange.FeatureType, we need to show at least
  342.         /// an exception message saying it is not set
  343.         /// </summary>
  344.         [Test]
  345.         public void TestToGeometryUsingIGeometryFactory()
  346.         {
  347.             var geometryFactory = new Mock<IGeometryFactory>();
  348.             var shape = new Shape();
  349.             var geometry = shape.ToGeometry(geometryFactory.Object);
  350.             Assert.IsNotNull(geometry);
  351.         }
  352.  
  353.         /// <summary>
  354.         /// Test CopyAttributes method passing IFeature
  355.         /// </summary>
  356.         [Test]
  357.         public void TestCopyAttributesUsingIFeature()
  358.         {
  359.             var feature = new Mock<IFeature>();
  360.             var geometry = new Mock<IBasicGeometry>();
  361.             var envelope = new Mock<IEnvelope>();
  362.             var coordinate1 = new Coordinate(2, 30);
  363.             var coordinate2 = new Coordinate(10, 40);
  364.  
  365.             var coordinateList = new List<Coordinate> { coordinate1, coordinate2 };
  366.  
  367.             //setup geometry
  368.             envelope.Setup(e => e.Minimum).Returns(coordinate1);
  369.             envelope.Setup(e => e.Maximum).Returns(coordinate2);
  370.  
  371.             geometry.Setup(g => g.Envelope).Returns(envelope.Object);
  372.  
  373.             var dataTable = new System.Data.DataTable("FeatureAttributesTable");
  374.             dataTable.Columns.Add("Column1");
  375.             var row = dataTable.NewRow();
  376.             row.ItemArray = new object[] { "attribute" };
  377.             dataTable.Rows.Add(row);
  378.  
  379.  
  380.             feature.Setup(f => f.DataRow).Returns(row);
  381.             feature.Setup(f => f.NumPoints).Returns(0);
  382.             feature.Setup(f => f.BasicGeometry).Returns(geometry.Object);
  383.             feature.Setup(f => f.Coordinates).Returns(coordinateList);
  384.  
  385.             var shape = new Shape();
  386.             try
  387.             {
  388.                 shape.CopyAttributes(feature.Object);
  389.                 Assert.IsTrue(true);
  390.             }
  391.             catch
  392.             {
  393.                 Assert.IsTrue(false);
  394.                 throw;
  395.             }
  396.         }
  397.  
  398.         #endregion
  399.     }
  400. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement