Fixing an issue with output parameters that are of type IntPtr
[castle.git] / Components / Binder / Castle.Components.Binder.Tests / DataReaderIntegrationTestCase.cs
blobcf252983815f702511f04f2e996bba6481279765
1 // Copyright 2004-2008 Castle Project - http://www.castleproject.org/
2 //
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
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
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.Binder.Tests
17 using System;
18 using System.Collections;
19 using System.Data;
21 using Nullables;
23 using NUnit.Framework;
25 [TestFixture]
26 public class DataReaderIntegrationTestCase
28 [Test]
29 public void EmptyReader()
31 ArrayList data = new ArrayList();
33 MockDataReader reader = new MockDataReader(data, new string[] { "name", "address"});
35 DataBinder binder = new DataBinder();
37 Contact[] contacts = (Contact[]) binder.BindObject(typeof(Contact[]), "cont", new DataReaderTreeBuilder().BuildSourceNode(reader, "cont"));
39 Assert.IsNotNull(contacts);
40 Assert.AreEqual(0, contacts.Length);
43 [Test]
44 public void SingleRow()
46 ArrayList data = new ArrayList();
48 data.Add( new object[] { "hammett", "r pereira leite 44" } );
50 MockDataReader reader = new MockDataReader(data, new string[] { "name", "address"}, typeof(String), typeof(String));
52 DataBinder binder = new DataBinder();
54 Contact[] contacts = (Contact[]) binder.BindObject(typeof(Contact[]), "cont", new DataReaderTreeBuilder().BuildSourceNode(reader, "cont"));
56 Assert.IsNotNull(contacts);
57 Assert.AreEqual(1, contacts.Length);
58 Assert.AreEqual("hammett", contacts[0].Name);
59 Assert.AreEqual("r pereira leite 44", contacts[0].Address);
62 [Test]
63 public void UsingTypeConverter()
65 ArrayList data = new ArrayList();
67 data.Add( new object[] { "hammett", "r pereira leite 44", new DateTime(2006,7,16) } );
69 MockDataReader reader = new MockDataReader(data, new string[] { "name", "address", "dob" }, typeof(String), typeof(String), typeof(DateTime));
71 DataBinder binder = new DataBinder();
73 Contact[] contacts = (Contact[]) binder.BindObject(typeof(Contact[]), "cont", new DataReaderTreeBuilder().BuildSourceNode(reader, "cont"));
75 Assert.IsNotNull(contacts);
76 Assert.AreEqual(1, contacts.Length);
77 Assert.AreEqual("hammett", contacts[0].Name);
78 Assert.AreEqual("r pereira leite 44", contacts[0].Address);
79 Assert.IsTrue(contacts[0].DOB.HasValue);
82 [Test]
83 public void TwoRows()
85 ArrayList data = new ArrayList();
87 data.Add( new object[] { 26, "hammett", "r pereira leite 44" } );
88 data.Add( new object[] { 27, "his girl", "barao bananal 100" } );
90 MockDataReader reader = new MockDataReader(data, new string[] { "age", "name", "address"}, typeof(int), typeof(String), typeof(String));
92 DataBinder binder = new DataBinder();
94 Contact[] contacts = (Contact[]) binder.BindObject(typeof(Contact[]), "cont", new DataReaderTreeBuilder().BuildSourceNode(reader, "cont"));
96 Assert.IsNotNull(contacts);
97 Assert.AreEqual(2, contacts.Length);
99 Assert.AreEqual(26, contacts[0].Age);
100 Assert.AreEqual("hammett", contacts[0].Name);
101 Assert.AreEqual("r pereira leite 44", contacts[0].Address);
103 Assert.AreEqual(27, contacts[1].Age);
104 Assert.AreEqual("his girl", contacts[1].Name);
105 Assert.AreEqual("barao bananal 100", contacts[1].Address);
108 [Test]
109 public void UsingTranslator()
111 ArrayList data = new ArrayList();
113 data.Add( new object[] { 26, "hammett", "r pereira leite 44" } );
114 data.Add( new object[] { 27, "his girl", "barao bananal 100" } );
116 MockDataReader reader = new MockDataReader(data, new string[] { "idade", "nome", "endereco"}, typeof(int), typeof(String), typeof(String));
118 DataBinder binder = new DataBinder();
119 binder.Translator = new EnglishToPortugueseTranslator();
121 Contact[] contacts = (Contact[]) binder.BindObject(typeof(Contact[]), "cont", new DataReaderTreeBuilder().BuildSourceNode(reader, "cont"));
123 Assert.IsNotNull(contacts);
124 Assert.AreEqual(2, contacts.Length);
126 Assert.AreEqual(26, contacts[0].Age);
127 Assert.AreEqual("hammett", contacts[0].Name);
128 Assert.AreEqual("r pereira leite 44", contacts[0].Address);
130 Assert.AreEqual(27, contacts[1].Age);
131 Assert.AreEqual("his girl", contacts[1].Name);
132 Assert.AreEqual("barao bananal 100", contacts[1].Address);
136 public class EnglishToPortugueseTranslator : IBinderTranslator
138 public String Translate(Type instanceType, String paramName)
140 if (paramName == "Age")
142 return "idade";
144 else if (paramName == "Name")
146 return "nome";
148 else if (paramName == "Address")
150 return "endereco";
153 return null;
157 public class MockDataReader : IDataReader
159 private readonly IList source;
160 private readonly string[] fields;
161 private readonly Type[] types;
162 private int index = -1;
164 public MockDataReader(IList source, String[] fields, params Type[] types)
166 this.source = source;
167 this.fields = fields;
168 this.types = types;
171 #region IDataReader
173 public void Close()
175 throw new NotImplementedException();
178 public bool NextResult()
180 throw new NotImplementedException();
183 public bool Read()
185 if (index + 1 < source.Count)
187 index++;
188 return true;
191 return false;
194 public DataTable GetSchemaTable()
196 throw new NotImplementedException();
199 public int Depth
201 get { throw new NotImplementedException(); }
204 public bool IsClosed
206 get { throw new NotImplementedException(); }
209 public int RecordsAffected
211 get { throw new NotImplementedException(); }
214 #endregion
216 #region IDisposable
218 public void Dispose()
220 throw new NotImplementedException();
223 #endregion
225 #region IDataRecord
227 public string GetName(int i)
229 return fields[i];
232 public string GetDataTypeName(int i)
234 throw new NotImplementedException();
237 public Type GetFieldType(int i)
239 return types[i];
242 public object GetValue(int i)
244 return ((object[]) source[index])[i];
247 public int GetValues(object[] values)
249 throw new NotImplementedException();
252 public int GetOrdinal(string name)
254 return Array.IndexOf(fields, name.ToLower());
257 public bool GetBoolean(int i)
259 return Convert.ToBoolean( GetValue(i) );
262 public byte GetByte(int i)
264 return Convert.ToByte( GetValue(i) );
267 public long GetBytes(int i, long fieldOffset, byte[] buffer, int bufferoffset, int length)
269 throw new NotImplementedException();
272 public char GetChar(int i)
274 return Convert.ToChar( GetValue(i) );
277 public long GetChars(int i, long fieldoffset, char[] buffer, int bufferoffset, int length)
279 throw new NotImplementedException();
282 public Guid GetGuid(int i)
284 throw new NotImplementedException();
287 public short GetInt16(int i)
289 return Convert.ToInt16( GetValue(i) );
292 public int GetInt32(int i)
294 return Convert.ToInt32( GetValue(i) );
297 public long GetInt64(int i)
299 return Convert.ToInt64( GetValue(i) );
302 public float GetFloat(int i)
304 return Convert.ToSingle( GetValue(i) );
307 public double GetDouble(int i)
309 return Convert.ToDouble( GetValue(i) );
312 public string GetString(int i)
314 return Convert.ToString( GetValue(i) );
317 public decimal GetDecimal(int i)
319 return Convert.ToDecimal( GetValue(i) );
322 public DateTime GetDateTime(int i)
324 return Convert.ToDateTime( GetValue(i) );
327 public IDataReader GetData(int i)
329 throw new NotImplementedException();
332 public bool IsDBNull(int i)
334 return GetValue(i) == null;
337 public int FieldCount
339 get { return fields.Length; }
342 public object this[int i]
344 get { throw new NotImplementedException(); }
347 public object this[string name]
349 get { throw new NotImplementedException(); }
352 #endregion
355 public class Contact
357 private int age;
358 private String name, address;
359 private NullableDateTime dob;
361 public int Age
363 get { return age; }
364 set { age = value; }
367 public string Name
369 get { return name; }
370 set { name = value; }
373 public string Address
375 get { return address; }
376 set { address = value; }
379 public NullableDateTime DOB
381 get { return dob; }
382 set { dob = value; }