1 // Copyright 2004-2008 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
.Binder
.Tests
18 using System
.Collections
;
23 using NUnit
.Framework
;
26 public class DataReaderIntegrationTestCase
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
);
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
);
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
);
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
);
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")
144 else if (paramName
== "Name")
148 else if (paramName
== "Address")
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
;
175 throw new NotImplementedException();
178 public bool NextResult()
180 throw new NotImplementedException();
185 if (index
+ 1 < source
.Count
)
194 public DataTable
GetSchemaTable()
196 throw new NotImplementedException();
201 get { throw new NotImplementedException(); }
206 get { throw new NotImplementedException(); }
209 public int RecordsAffected
211 get { throw new NotImplementedException(); }
218 public void Dispose()
220 throw new NotImplementedException();
227 public string GetName(int i
)
232 public string GetDataTypeName(int i
)
234 throw new NotImplementedException();
237 public Type
GetFieldType(int 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(); }
358 private String name
, address
;
359 private NullableDateTime dob
;
370 set { name = value; }
373 public string Address
375 get { return address; }
376 set { address = value; }
379 public NullableDateTime DOB