PageRenderTime 98ms CodeModel.GetById 89ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

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