1 // Copyright 2004-2007 Castle Project - http://www.castleproject.org/
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
7 // http://www.apache.org/licenses/LICENSE-2.0
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 namespace Castle
.Components
.DictionaryAdapter
.Tests
18 using System
.Collections
;
19 using System
.Collections
.Generic
;
20 using NUnit
.Framework
;
23 public class DictionaryAdapterFactoryTestCase
25 private IDictionary dictionary
;
26 private DictionaryAdapterFactory factory
;
31 dictionary
= new Hashtable();
32 factory
= new DictionaryAdapterFactory();
36 public void CreateAdapter_NoPrefixPropertiesOnly_WorksFine()
38 IPerson person
= factory
.GetAdapter
<IPerson
>(dictionary
);
39 Assert
.IsNotNull(person
);
42 [Test
, ExpectedException(typeof(TypeLoadException
))]
43 public void CreateAdapter_NoPrefixWithMethod_ThrowsException()
45 factory
.GetAdapter
<IPersonWithMethod
>(dictionary
);
49 public void CreateAdapter_PrefixPropertiesOnly_WorksFine()
51 IPerson person
= factory
.GetAdapter
<IPersonWithPrefix
>(dictionary
);
52 Assert
.IsNotNull(person
);
56 public void UpdateAdapter_NoPrefix_UpdatesDictionary()
58 IPerson person
= factory
.GetAdapter
<IPerson
>(dictionary
);
59 person
.Name
= "Craig";
61 person
.DOB
= new DateTime(1970, 7, 19);
62 person
.Friends
= new List
<IPerson
>(); // I need some friends
64 Assert
.AreEqual("Craig", dictionary
["Name"]);
65 Assert
.AreEqual(37, dictionary
["Age"]);
66 Assert
.AreEqual(new DateTime(1970, 7, 19), dictionary
["DOB"]);
67 Assert
.AreEqual(0, ((IList
<IPerson
>) dictionary
["Friends"]).Count
);
71 public void UpdateAdapter_Prefix_UpdatesDictionary()
73 IPerson person
= factory
.GetAdapter
<IPersonWithPrefix
>(dictionary
);
74 person
.Name
= "Craig";
76 person
.DOB
= new DateTime(1970, 7, 19);
77 person
.Friends
= new List
<IPerson
>();
78 Assert
.AreEqual("Craig", dictionary
["Person_Name"]);
79 Assert
.AreEqual(37, dictionary
["Person_Age"]);
80 Assert
.AreEqual(new DateTime(1970, 7, 19), dictionary
["Person_DOB"]);
81 Assert
.AreEqual(0, ((IList
<IPerson
>) dictionary
["Person_Friends"]).Count
);
85 public void UpdateAdapterAndRead_NoPrefix_Matches()
87 IPerson person
= factory
.GetAdapter
<IPerson
>(dictionary
);
88 person
.Name
= "Craig";
90 person
.DOB
= new DateTime(1970, 7, 19);
91 person
.Friends
= new List
<IPerson
>();
93 Assert
.AreEqual("Craig", person
.Name
);
94 Assert
.AreEqual(37, person
.Age
);
95 Assert
.AreEqual(new DateTime(1970, 7, 19), person
.DOB
);
96 Assert
.AreEqual(0, person
.Friends
.Count
);
100 public void UpdateAdapterAndRead_Prefix_Matches()
102 IPerson person
= factory
.GetAdapter
<IPersonWithPrefix
>(dictionary
);
103 person
.Name
= "Craig";
105 person
.DOB
= new DateTime(1970, 7, 19);
106 person
.Friends
= new List
<IPerson
>();
108 Assert
.AreEqual("Craig", person
.Name
);
109 Assert
.AreEqual(37, person
.Age
);
110 Assert
.AreEqual(new DateTime(1970, 7, 19), person
.DOB
);
111 Assert
.AreEqual(0, person
.Friends
.Count
);
115 public void UpdateAdapterAndRead_PrefixOverride_Matches()
117 IPerson person
= factory
.GetAdapter
<IPersonWithPrefixOverride
>(dictionary
);
118 person
.Name
= "Craig";
120 Assert
.AreEqual("Craig", dictionary
["Person2_Name"]);
124 public void ReadAdapter_NoPrefixUnitialized_ReturnsDefaults()
126 IPerson person
= factory
.GetAdapter
<IPerson
>(dictionary
);
128 Assert
.AreEqual(default(string), person
.Name
);
129 Assert
.AreEqual(default(int), person
.Age
);
130 Assert
.AreEqual(default(DateTime
), person
.DOB
);
131 Assert
.AreEqual(default(IList
<IPerson
>), person
.Friends
);
135 public void ReadAdapter_PrefixUnitialized_ReturnsDefaults()
137 IPerson person
= factory
.GetAdapter
<IPersonWithPrefix
>(dictionary
);
139 Assert
.AreEqual(default(string), person
.Name
);
140 Assert
.AreEqual(default(int), person
.Age
);
141 Assert
.AreEqual(default(DateTime
), person
.DOB
);
142 Assert
.AreEqual(default(IList
<IPerson
>), person
.Friends
);
146 public interface IPerson
148 string Name { get; set; }
150 int Age { get; set; }
152 DateTime DOB { get; set; }
154 IList
<IPerson
> Friends { get; set; }
157 [DictionaryAdapterKeyPrefix("Person_")]
158 public interface IPersonWithPrefix
: IPerson
162 [DictionaryAdapterKeyPrefix("Person2_")]
163 public interface IPersonWithPrefixOverride
: IPersonWithPrefix
167 public interface IPersonWithMethod
: IPerson