1 #region Copyright ©  2003-2008 Richard Beauchamp <rbeauchamp@gmail.com>
   2 
   3 /*
   4 * DomainObjects for .NET
   5 * Copyright ©  2003-2008 Richard Beauchamp
   6 *
   7 * This library is free software; you can redistribute it and/or
   8 * modify it under the terms of the GNU Lesser General Public
   9 * License as published by the Free Software Foundation; either
  10 * version 2.1 of the License, or (at your option) any later version.
  11 *
  12 * This library is distributed in the hope that it will be useful,
  13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  15 * Lesser General Public License for more details.
  16 *
  17 * You should have received a copy of the GNU Lesser General Public
  18 * License along with this library; if not, write to the Free Software
  19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
  20 */
  21 
  22 #endregion
  23 
  24 using DomainObjects.Facade.Command;
  25 using DomainObjects.Facade.Test;
  26 using DomainObjects.Test.Domain;
  27 using DomainObjects.Test.TestType.Enum;
  28 using NUnit.Framework;
  29 
  30 namespace DomainObjects.Test.TestFixture
  31 {
  32   /// <summary>
  33   /// Tests the handling of nullable types.
  34   /// </summary>
  35   [TestFixture]
  36   public class NullableTypesTests : DomainObjectsTestFixture
  37   {
  38     #region Int32? Tests
  39 
  40     [Test]
  41     public void NullableInt32Conversion()
  42     {
  43       // retrieve an instance of BaseTypeReference where the BaseTypeId is not null
  44       BaseTypeReference baseTypeReference = Query.FindObjectByPrimaryKey<BaseTypeReference>(1000);
  45       Assert.IsNotNull(baseTypeReference, "Found the instance of BaseTypeReference");
  46       Assert.IsTrue(1003 == baseTypeReference.BaseTypeId, "The correct BaseTypeId exists");
  47 
  48       // retrieve an instance of BaseTypeReference where the BaseTypeId is null
  49       baseTypeReference = Query.FindObjectByPrimaryKey<BaseTypeReference>(1001);
  50       Assert.IsNotNull(baseTypeReference, "Found the instance of BaseTypeReference");
  51       Assert.IsNull(baseTypeReference.BaseTypeId, "The correct BaseTypeId exists");
  52     }
  53 
  54     /// <summary>
  55     /// Tests that bug '[ 1601181 ] Null value returned by FieldConversion causes exception' has been fixed.
  56     /// </summary>
  57     [Test]
  58     public void NullableInt32ToStringConversion()
  59     {
  60       // Create an instance of Product with a null value
  61       // for the field _stringConvertedToString
  62       ProductCategory productCategory = Query.FindObjectByPrimaryKey<ProductCategory>(1);
  63       Product product = new Product(productCategory, "Product with null value for _stringConvertedToString");
  64 
  65       // Persist the new instance
  66       PersistenceFacade.Persist(productCategory, product);
  67 
  68       // Get the primary key
  69       object primaryKey = product.PrimaryKey;
  70 
  71       // Clear the cache
  72       ClearAppDomainAndContextCache();
  73 
  74       // Assert that it was persisted
  75       product = Query.FindObjectByPrimaryKey<Product>(primaryKey);
  76 
  77       Assert.IsNotNull(product, "The product was stored and retrieved successfully from the database.");
  78     }
  79 
  80     #endregion
  81 
  82     #region Nullable Enum Tests
  83 
  84     /// <summary>
  85     /// Validates that DomainObjects correctly converts between
  86     /// a string in the database and a nullable enum in the middle-tier.
  87     /// </summary>
  88     [Test]
  89     public void NullableEnumToStringConversion()
  90     {
  91       // Validate a null value
  92       Project project = new Project().Manage<Project>();
  93       PersistenceFacade.Persist(project);
  94       ClearAppDomainAndContextCache();
  95       Assert.IsNull(project.Category, "The project category is null.");
  96       Assert.IsNull(project.SecondCategory, "The second project category is null.");
  97 
  98       // Validate a non-null value
  99       Project projectTwo = new Project().Manage<Project>();
 100       projectTwo.Category = ProjectCategory.SoftwareDevelopment;
 101       projectTwo.SecondCategory = ProjectCategory.AlternativeEnergy;
 102       PersistenceFacade.Persist(projectTwo);
 103       ClearAppDomainAndContextCache();
 104       Assert.AreEqual(ProjectCategory.SoftwareDevelopment, projectTwo.Category, "The project category is set to SoftwareDevelopment.");
 105       Assert.AreEqual(ProjectCategory.AlternativeEnergy, projectTwo.SecondCategory, "The second project category is set to AlternativeEnergy.");
 106     }
 107 
 108     #endregion
 109   }
 110 }