PageRenderTime 79ms CodeModel.GetById 73ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

/src/Otis.Tests/MappingPreparerTests.cs

http://otis-lib.googlecode.com/
C# | 72 lines | 64 code | 8 blank | 0 comment | 0 complexity | 92b6b93ee40e6c8216a1ed29aa62e7cd MD5 | raw file
 1using System;
 2using System.Collections.Generic;
 3using System.Text;
 4using NUnit.Framework;
 5using Otis.Tests.Entity;
 6
 7namespace Otis.Tests
 8{
 9	[TestFixture]
10	public class MappingPreparerTests
11	{
12		const string errDuplicatePreparer = "Method 'Preparer' on type 'Otis.Tests.MappingPreparerDTO_Duplicate' is marked with [MappingPreparer], but mapping preparer is already set to 'Otis.Tests.Util.Convert'";
13		const string errPreparerIsPrivate = "Non public method 'Preparer' on type 'Otis.Tests.MappingPreparerDTO_NonPublic'is marked with [MappingPreparer]. Only public methods can be used as preparers";
14		private User m_user;
15
16		[SetUp]
17		public void SetUp()
18		{
19			m_user = new User();
20			m_user.Id = 77;
21			m_user.FirstName = "aaa";
22			m_user.LastName = "bbb";
23		}
24
25		[Test]
26		[ExpectedException(typeof(OtisException), ExpectedMessage = errDuplicatePreparer)]
27		public void Configuration_Throws_With_Duplicate_Preparers()
28		{
29			ConfigureType<MappingPreparerDTO_Duplicate>();
30		}
31
32		[Test]
33		[ExpectedException(typeof(OtisException), ExpectedMessage = errPreparerIsPrivate)]
34		public void Configuration_Throws_With_NonPublic_Preparers()
35		{
36			ConfigureType<MappingPreparerDTO_NonPublic>();
37		}
38
39		[Test]
40		public void Non_Static_Preparer()
41		{
42			IAssembler<MappingPreparerDTO_InstancePreparer, User> asm = ConfigureType<MappingPreparerDTO_InstancePreparer>();
43			MappingPreparerDTO_InstancePreparer dto = asm.AssembleFrom(m_user);
44			Assert.AreEqual(77, dto.Id);
45			Assert.AreEqual("custom_mapping_InstancePreparer", dto.FullName);
46		}
47
48		[Test]
49		public void Preparer_Is_Called_Before_Transformation()
50		{
51			IAssembler<MappingPreparerDTO_CheckOrder, User> asm = ConfigureType<MappingPreparerDTO_CheckOrder>();
52			MappingPreparerDTO_CheckOrder dto = asm.AssembleFrom(m_user);
53			Assert.AreEqual(77, dto.Id);  // check it is not -1
54		}
55
56		[Test]
57		public void Static_Preparer()
58		{
59			IAssembler<MappingPreparerDTO_StaticPreparer, User> asm = ConfigureType<MappingPreparerDTO_StaticPreparer>();
60			MappingPreparerDTO_StaticPreparer dto = asm.AssembleFrom(m_user);
61			Assert.AreEqual(77, dto.Id);
62			Assert.AreEqual("custom_mapping_StaticPreparer", dto.FullName);
63		}
64
65		IAssembler<T, User> ConfigureType<T>()
66		{
67			Configuration cfg = new Configuration();
68			cfg.AddType(typeof(T));
69			return cfg.GetAssembler<T, User>();
70		}
71	}
72}