/src/Otis.Tests/GenericsMappingTests.cs

http://otis-lib.googlecode.com/ · C# · 78 lines · 63 code · 12 blank · 3 comment · 0 complexity · 2b877b53f35a0447befb2d057f4eb33e MD5 · raw file

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using NUnit.Framework;
  5. using NUnit.Framework.SyntaxHelpers;
  6. using Otis.Tests.Entity;
  7. using System.Reflection;
  8. namespace Otis.Tests
  9. {
  10. [TestFixture]
  11. public class GenericsMappingTests
  12. {
  13. private GenericEntity<int> m_source;
  14. [SetUp]
  15. public void TestFixtureSetup()
  16. {
  17. m_source = new GenericEntity<int>();
  18. m_source.Id = 1;
  19. m_source.NullableProperty = 2;
  20. }
  21. public IAssembler<XmlGenericEntityDTO, GenericEntity<int>> CreateXmlAssembler()
  22. {
  23. Configuration cfg = new Configuration();
  24. cfg.AddAssemblyReference(Assembly.GetAssembly(typeof(GenericsMappingTests)));
  25. cfg.AddAssemblyResources(Assembly.GetAssembly(typeof(XmlGenericEntityDTO)), "otis.xml");
  26. return cfg.GetAssembler<XmlGenericEntityDTO, GenericEntity<int>>();
  27. }
  28. public IAssembler<AttributedGenericEntityDTO, GenericEntity<int>> CreateAttributedAssembler()
  29. {
  30. Configuration cfg = new Configuration();
  31. cfg.AddType(typeof(AttributedGenericEntityDTO));
  32. // the following 2 lines are there to init Nullable transformation
  33. cfg.AddAssemblyReference(Assembly.GetAssembly(typeof(GenericsMappingTests)));
  34. cfg.AddAssemblyResources(Assembly.GetAssembly(typeof(XmlGenericEntityDTO)), "otis.xml");
  35. return cfg.GetAssembler<AttributedGenericEntityDTO, GenericEntity<int>>();
  36. }
  37. [Test]
  38. public void TestAttributedGenerics()
  39. {
  40. IAssembler<AttributedGenericEntityDTO, GenericEntity<int>> asm = CreateAttributedAssembler();
  41. AttributedGenericEntityDTO dto = asm.AssembleFrom(m_source);
  42. Assert.That(dto.Id, Is.EqualTo(1));
  43. Assert.That(dto.NullableProperty, Is.EqualTo(2));
  44. // test nullables
  45. m_source.NullableProperty = null;
  46. dto = asm.AssembleFrom(m_source);
  47. Assert.That(dto.NullableProperty, Is.Null);
  48. }
  49. [Test]
  50. public void TestXmlGenerics()
  51. {
  52. IAssembler<XmlGenericEntityDTO, GenericEntity<int>> asm = CreateXmlAssembler();
  53. XmlGenericEntityDTO xmlDto = asm.AssembleFrom(m_source);
  54. Assert.That(xmlDto.Id,Is.EqualTo(1));
  55. Assert.That(xmlDto.NullableProperty, Is.EqualTo(2));
  56. // test nullables
  57. m_source.NullableProperty = null;
  58. xmlDto = asm.AssembleFrom(m_source);
  59. Assert.That(xmlDto.NullableProperty, Is.Null);
  60. }
  61. public static void ConvertByRef<T>(ref T target, ref T source)
  62. {
  63. target = source;
  64. }
  65. }
  66. }