| 
0
 | 
     1 using System;
 | 
| 
 | 
     2 using System.IO;
 | 
| 
 | 
     3 
 | 
| 
 | 
     4 using NUnit.Framework;
 | 
| 
 | 
     5 
 | 
| 
 | 
     6 using BLToolkit.Mapping;
 | 
| 
 | 
     7 using BLToolkit.Reflection.Extension;
 | 
| 
 | 
     8 
 | 
| 
 | 
     9 namespace Reflection.Extension
 | 
| 
 | 
    10 {
 | 
| 
 | 
    11 	[TestFixture]
 | 
| 
 | 
    12 	public class DefaultValueTest
 | 
| 
 | 
    13 	{
 | 
| 
 | 
    14 		[SetUp]
 | 
| 
 | 
    15 		public void SetUp()
 | 
| 
 | 
    16 		{
 | 
| 
 | 
    17 			Map.DefaultSchema = new DefaultMappingSchema();
 | 
| 
 | 
    18 
 | 
| 
 | 
    19 			using (StreamWriter sw = File.CreateText("Mapping.xml"))
 | 
| 
 | 
    20 			{
 | 
| 
 | 
    21 				sw.WriteLine(@"<?xml version='1.0' encoding='utf-8' ?>
 | 
| 
 | 
    22 <Types xmlns='urn:schemas-bltoolkit-net:typeext'>
 | 
| 
 | 
    23     <Type Name='Enum3'>
 | 
| 
 | 
    24         <Member Name='Value1' MapValue='1' MapValue-Type='System.String' />
 | 
| 
 | 
    25         <Member Name='Value2' MapValue='3' MapValue-Type='System.String' DefaultValue=''/>
 | 
| 
 | 
    26     </Type>
 | 
| 
 | 
    27     <Type Name='Enum4'>
 | 
| 
 | 
    28         <Member Name='Value1' MapValue='1' MapValue-Type='System.String' />
 | 
| 
 | 
    29         <Member Name='Value2' MapValue='3' MapValue-Type='System.String' DefaultValue=''/>
 | 
| 
 | 
    30     </Type>
 | 
| 
 | 
    31     <Type Name='Dest'>
 | 
| 
 | 
    32         <Member Name='Field2'>
 | 
| 
 | 
    33             <MapValue Value='1' OrigValue='Value1' />
 | 
| 
 | 
    34             <MapValue Value='2' OrigValue='Value2' />
 | 
| 
 | 
    35             <DefaultValue Value='Value1' />
 | 
| 
 | 
    36         </Member>
 | 
| 
 | 
    37         <Member Name='Field3' DefaultValue='Value1'>
 | 
| 
 | 
    38             <MapValue Value='1' OrigValue='Value1' />
 | 
| 
 | 
    39             <MapValue Value='2' OrigValue='Value2' />
 | 
| 
 | 
    40         </Member>
 | 
| 
 | 
    41     </Type>
 | 
| 
 | 
    42 </Types>");
 | 
| 
 | 
    43 			}
 | 
| 
 | 
    44 		}
 | 
| 
 | 
    45 
 | 
| 
 | 
    46 		[TearDown]
 | 
| 
 | 
    47 		public void TearDown()
 | 
| 
 | 
    48 		{
 | 
| 
 | 
    49 			File.Delete("Mapping.xml");
 | 
| 
 | 
    50 		}
 | 
| 
 | 
    51 
 | 
| 
 | 
    52 		public enum Enum1
 | 
| 
 | 
    53 		{
 | 
| 
 | 
    54 			[MapValue("1")]               Value1 = 11,
 | 
| 
 | 
    55 			[MapValue("2"), DefaultValue] Value2 = 12
 | 
| 
 | 
    56 		}
 | 
| 
 | 
    57 
 | 
| 
 | 
    58 		[MapValue(Enum2.Value1, "1")]
 | 
| 
 | 
    59 		[MapValue(Enum2.Value2, "2")]
 | 
| 
 | 
    60 		[DefaultValue(Enum2.Value2)]
 | 
| 
 | 
    61 		public enum Enum2
 | 
| 
 | 
    62 		{
 | 
| 
 | 
    63 			Value1,
 | 
| 
 | 
    64 			Value2
 | 
| 
 | 
    65 		}
 | 
| 
 | 
    66 
 | 
| 
 | 
    67 		public enum Enum3
 | 
| 
 | 
    68 		{
 | 
| 
 | 
    69 			Value1,
 | 
| 
 | 
    70 			Value2
 | 
| 
 | 
    71 		}
 | 
| 
 | 
    72 
 | 
| 
 | 
    73 		public enum Enum4
 | 
| 
 | 
    74 		{
 | 
| 
 | 
    75 			[MapValue("1")]               Value1,
 | 
| 
 | 
    76 			[MapValue("2"), DefaultValue] Value2
 | 
| 
 | 
    77 		}
 | 
| 
 | 
    78 
 | 
| 
 | 
    79 		public class Source
 | 
| 
 | 
    80 		{
 | 
| 
 | 
    81 			public string Field1 = "11";
 | 
| 
 | 
    82 			public string Field2 = "22";
 | 
| 
 | 
    83 			public string Field3 = "33";
 | 
| 
 | 
    84 		}
 | 
| 
 | 
    85 
 | 
| 
 | 
    86 		public class Dest
 | 
| 
 | 
    87 		{
 | 
| 
 | 
    88 			public Enum1 Field1;
 | 
| 
 | 
    89 			public Enum1 Field2;
 | 
| 
 | 
    90 			public Enum2 Field3;
 | 
| 
 | 
    91 		}
 | 
| 
 | 
    92 
 | 
| 
 | 
    93 		[Test]
 | 
| 
 | 
    94 		public void Test1()
 | 
| 
 | 
    95 		{
 | 
| 
 | 
    96 			Map.Extensions = TypeExtension.GetExtensions("Mapping.xml");
 | 
| 
 | 
    97 
 | 
| 
 | 
    98 			Enum1 e1 = (Enum1)Map.ValueToEnum("3", typeof(Enum1));
 | 
| 
 | 
    99 			Assert.AreEqual(Enum1.Value2, e1);
 | 
| 
 | 
   100 
 | 
| 
 | 
   101 			Enum2 e2 = (Enum2)Map.ValueToEnum("3", typeof(Enum2));
 | 
| 
 | 
   102 			Assert.AreEqual(Enum2.Value2, e2);
 | 
| 
 | 
   103 
 | 
| 
 | 
   104 			Enum3 e3 = (Enum3)Map.ValueToEnum("4", typeof(Enum3));
 | 
| 
 | 
   105 			Assert.AreEqual(Enum3.Value2, e3);
 | 
| 
 | 
   106 
 | 
| 
 | 
   107 			Enum4 e4 = (Enum4)Map.ValueToEnum("4", typeof(Enum4));
 | 
| 
 | 
   108 			Assert.AreEqual(Enum4.Value2, e4);
 | 
| 
 | 
   109 
 | 
| 
 | 
   110 			Dest o = (Dest)Map.ObjectToObject(new Source(), typeof(Dest));
 | 
| 
 | 
   111 
 | 
| 
 | 
   112 			Assert.AreEqual(Enum1.Value2, o.Field1);
 | 
| 
 | 
   113 			Assert.AreEqual(Enum1.Value1, o.Field2);
 | 
| 
 | 
   114 			Assert.AreEqual(Enum2.Value1, o.Field3);
 | 
| 
 | 
   115 		}
 | 
| 
 | 
   116 	}
 | 
| 
 | 
   117 }
 |