/src/Otis.Tests/ComponentMappingTests.cs

http://otis-lib.googlecode.com/ · C# · 140 lines · 106 code · 19 blank · 15 comment · 0 complexity · 414581bd1b6f29aa1889f1e62e298681 MD5 · raw file

  1. /*
  2. * Created by: Zdeslav Vojkovic
  3. * Created: Monday, April 14, 2008
  4. */
  5. using System;
  6. using System.Collections.Generic;
  7. using NUnit.Framework;
  8. using NUnit.Framework.SyntaxHelpers;
  9. using Otis.Tests.Entity;
  10. namespace Otis.Tests
  11. {
  12. [TestFixture]
  13. public class ComponentMappingTests
  14. {
  15. string xmlCfg = "<otis-mapping xmlns='urn:otis-mapping-1.0' >"
  16. + " <class name='Otis.Tests.OrderDTO, Otis.Tests' source='Otis.Tests.Order, Otis.Tests' >"
  17. + " <member name='Id' />"
  18. + " <member name='Customer' expression='$Customer.FirstName + &quot; &quot; + $Customer.LastName' />"
  19. + " <component name='PaymentAddress' >"
  20. + " <member name='Street' expression='$PaymentStreet' />"
  21. + " <component name='City' >"
  22. + " <member name='City' expression='$PaymentCity' />"
  23. + " <member name='ZipCode' expression='$PaymentZip' />"
  24. + " </component>"
  25. + " </component>"
  26. + " <component name='ShippingAddress' >"
  27. + " <member name='Street' expression='$ShippingStreet' />"
  28. + " <component name='City' >"
  29. + " <member name='City' expression='$ShippingCity' />"
  30. + " <member name='ZipCode' expression='$ShippingZip' />"
  31. + " </component>"
  32. + " </component>"
  33. + " </class>"
  34. + "</otis-mapping>";
  35. [SetUp]
  36. public void SetUp() {}
  37. [TearDown]
  38. public void TearDown() {}
  39. [Test]
  40. public void Component_Is_Recognized_By_XmlProvider()
  41. {
  42. IMappingDescriptorProvider provider = ProviderFactory.FromXmlString(xmlCfg);
  43. Assert.AreEqual(1, provider.ClassDescriptors.Count);
  44. CheckDescriptor(provider.ClassDescriptors[0]);
  45. }
  46. [Test]
  47. public void Component_Is_Recognized_By_TypeProvider()
  48. {
  49. IMappingDescriptorProvider provider = ProviderFactory.FromType(typeof(OrderDTO));
  50. Assert.AreEqual(1, provider.ClassDescriptors.Count);
  51. CheckDescriptor(provider.ClassDescriptors[0]);
  52. }
  53. [Test]
  54. public void XmlMapping()
  55. {
  56. Configuration cfg = new Configuration();
  57. cfg.AddXmlString(xmlCfg);
  58. IAssembler<OrderDTO, Order> asm = cfg.GetAssembler<OrderDTO, Order>();
  59. Order user = GetDefaultOrder();
  60. /* UserWithComponentsDTO dto = asm.AssembleFrom(user);
  61. Assert.That("X Y", Is.EqualTo(dto.FullName));
  62. Assert.That(1, Is.EqualTo(dto.Id));
  63. Assert.That("road to nowhere 1", Is.EqualTo(dto.Address.Street));
  64. Assert.That("Whatevertown", Is.EqualTo(dto.Address.City.CityName));
  65. Assert.That("12345", Is.EqualTo(dto.Address.City.ZipCode)); */
  66. }
  67. private Order GetDefaultOrder()
  68. {
  69. Order order = new Order();
  70. order.Id = 1;
  71. order.Customer.FirstName = "X";
  72. order.Customer.LastName = "Y";
  73. order.PaymentStreet = "road to nowhere 1";
  74. order.PaymentCity = "Whatevertown";
  75. order.PaymentZip = "12345";
  76. order.ShippingStreet = "road to nowhere 1";
  77. order.ShippingCity = "Whatevertown";
  78. order.ShippingZip = "12345";
  79. return order;
  80. }
  81. private void CheckDescriptor(ClassMappingDescriptor descriptor)
  82. {
  83. //throw new Exception("!");
  84. }
  85. }
  86. [MapClass(typeof(Order))]
  87. public class OrderDTO
  88. {
  89. public int Id;
  90. public string Customer;
  91. //[Nested("Payment")]
  92. public Address PaymentAddress = new Address();
  93. //[Nested("Shipping")]
  94. public Address ShippingAddress = new Address();
  95. }
  96. [MapClass(typeof(Address))]
  97. public class Address
  98. {
  99. //[Map("$PaymentStreet", NestedContext = "Payment")] // [MapNested("$PaymentStreet")] or [MapNested(OrderDTO.PaymentAddress)]
  100. //[Map("$ShippingStreet", Context = "Shipping")]
  101. [Map("...")]
  102. public string Street;
  103. public CityInfo City = new CityInfo();
  104. }
  105. public class CityInfo
  106. {
  107. public string CityName;
  108. public string ZipCode;
  109. }
  110. public class Order
  111. {
  112. public int Id;
  113. public User Customer = new User();
  114. public string PaymentStreet;
  115. public string PaymentCity;
  116. public string PaymentZip;
  117. public string ShippingStreet;
  118. public string ShippingCity;
  119. public string ShippingZip;
  120. }
  121. }