PageRenderTime 133ms CodeModel.GetById 126ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 1ms

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