comparison UnitTests/CS/Reflection/Extension/ExtensionTest.cs @ 0:f990fcb411a9

Копия текущей версии из github
author cin
date Thu, 27 Mar 2014 21:46:09 +0400
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:f990fcb411a9
1 using System.IO;
2
3 using NUnit.Framework;
4
5 using BLToolkit.Mapping;
6 using BLToolkit.Reflection.Extension;
7 using System.Collections.Generic;
8 using System.Xml;
9
10 namespace Reflection.Extension
11 {
12 [TestFixture]
13 public class ExtensionTestTest
14 {
15 [SetUp]
16 public void SetUp()
17 {
18 Map.DefaultSchema = new DefaultMappingSchema();
19
20 using (StreamWriter sw = File.CreateText("Mapping.xml"))
21 {
22 sw.WriteLine(@"<?xml version='1.0' encoding='utf-8' ?>
23 <Types xmlns='urn:schemas-bltoolkit-net:typeext'>
24 <Type Name='Dest'>
25 <Member Name='Field3'>
26 <MapValue Value='-1-' OrigValue='1' OrigValue-Type='System.Double' />
27 <MapValue Value='-2-' OrigValue='2' OrigValue-Type='System.Double' />
28 </Member>
29 </Type>
30 <Type Name='TriState'>
31 <Member Name='Yes' MapValue='yes' MapValue-Type='System.String' />
32 <Member Name='No'>
33 <MapValue Value='no' Type='System.String' />
34 <MapValue Type='System.String'>N</MapValue>
35 </Member>
36 <Member Name='Maybe' MapValue='xz' MapValue-Type='System.String' />
37 <Member Name='NotApplicable' MapValue-Type='System.String' MapValue='(n/a)' DefaultValue='' />
38 </Type>
39 <Type Name='System.Double'>
40 <MapValue OrigValue='1' OrigValue-Type='System.Double' Value='One' Type='System.String' />
41 <MapValue OrigValue='2' Value='Two' Type='System.String' />
42 <DefaultValue Value='54' />
43 </Type>
44 <Type Name='TestType'>
45 <Member Name='SomeRelation'>
46 <Relation>
47 <MasterIndex Name='MasterIndex1'/>
48 <MasterIndex Name='MasterIndex2'/>
49 <SlaveIndex Name='SlaveIndex1'/>
50 <SlaveIndex Name='SlaveIndex2'/>
51 </Relation>
52 </Member>
53 </Type>
54 </Types>");
55 }
56 }
57
58 [TypeExtension("TriState")]
59 public enum TriState { Yes, No, NotApplicable };
60
61 public class Source
62 {
63 public string Field1 = "no";
64 public string Field2 = "One";
65 public string Field3 = "-2-";
66 public string Field4 = "***";
67 }
68
69 [TypeExtension("Dest")]
70 public class Dest
71 {
72 private TriState _field1 = TriState.NotApplicable;
73 public TriState Field1
74 {
75 get { return _field1; }
76 set { _field1 = value; }
77 }
78
79 public double Field2;
80 public double Field3;
81 public double Field4;
82 }
83
84 [Test]
85 public void Test()
86 {
87 Map.Extensions = TypeExtension.GetExtensions("Mapping.xml");
88
89 object o = Map.Extensions["TriState"]["Yes"]["MapValue"].Value;
90
91 Assert.AreEqual("yes", o);
92
93 Source s = new Source();
94 Dest d = (Dest)Map.ObjectToObject(s, typeof(Dest));
95
96 Assert.AreEqual(TriState.No, d.Field1);
97 Assert.AreEqual( 1, d.Field2);
98 Assert.AreEqual( 2, d.Field3);
99 Assert.AreEqual(54, d.Field4);
100 Assert.AreEqual(TriState.NotApplicable, Map.ValueToEnum("1234", typeof(TriState)));
101 }
102
103 public class TestType
104 {
105 public object SomeRelation;
106 }
107
108 [Test]
109 public void MultiKeyRelationTest()
110 {
111 MappingSchema ms = new MappingSchema();
112 ms.Extensions = TypeExtension.GetExtensions("Mapping.xml");
113
114 bool isSet = false;
115
116 List<MapRelationBase> relations = ms.MetadataProvider.GetRelations(ms, ms.Extensions, typeof(TestType), null, out isSet);
117
118 Assert.IsTrue(isSet);
119 Assert.AreEqual(1, relations.Count);
120 Assert.AreEqual(2, relations[0].MasterIndex.Fields.Length);
121 Assert.AreEqual(2, relations[0].SlaveIndex .Fields.Length);
122 }
123
124 [TearDown]
125 public void TearDown()
126 {
127 File.Delete("Mapping.xml");
128 }
129 }
130 }