Clover.NET coverage report - Coverage

Coverage timestamp: Friday, May 20, 2005 9:17:00 PM

File Stats: LOC: 352   Methods: 19
NCLOC: 222 Classes: 1
 
Source File Conditionals Statements Methods TOTAL
Configuration\ResultMapping\ResultMap.cs 77.3 % 89.2 % 89.5 % 86.8 %
coverage coverage
1  
2   #region Apache Notice
3   /*****************************************************************************
4   * $Header: $
5   * $Revision: $
6   * $Date: $
7   *
8   * iBATIS.NET Data Mapper
9   * Copyright (C) 2004 - Gilles Bayon
10   *
11   *
12   * Licensed under the Apache License, Version 2.0 (the "License");
13   * you may not use this file except in compliance with the License.
14   * You may obtain a copy of the License at
15   *
16   * http://www.apache.org/licenses/LICENSE-2.0
17   *
18   * Unless required by applicable law or agreed to in writing, software
19   * distributed under the License is distributed on an "AS IS" BASIS,
20   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21   * See the License for the specific language governing permissions and
22   * limitations under the License.
23   *
24   ********************************************************************************/
25   #endregion
26  
27   #region Imports
28  
29   using System;
30   using System.Collections;
31   using System.Data;
32   using System.Reflection;
33   using System.Xml;
34   using System.Xml.Serialization;
35   using IBatisNet.Common.Exceptions;
36   using IBatisNet.Common.Utilities.Objects;
37   using IBatisNet.Common.Utilities.TypesResolver;
38   using IBatisNet.DataMapper.Scope;
39  
40   #endregion
41  
42   namespace IBatisNet.DataMapper.Configuration.ResultMapping
43   {
44   /// <summary>
45   /// Summary description for ResultMap.
46   /// </summary>
47   [Serializable]
48   [XmlRoot("resultMap")]
49   public class ResultMap
50   {
51   #region Fields
52   [NonSerialized]
53   private string _id = string.Empty;
54   [NonSerialized]
55   private string _className = string.Empty;
56   [NonSerialized]
57   private string _extendMap = string.Empty;
58   [NonSerialized]
59   private Type _class = null;
60   //(columnName, property)
61   [NonSerialized]
62   private Hashtable _columnsToPropertiesMap = new Hashtable();
63   [NonSerialized]
64   private Discriminator _discriminator = null;
65   [NonSerialized]
66   private string _sqlMapNameSpace = string.Empty;
67   #endregion
68  
69   #region Properties
70  
71   /// <summary>
72   /// The sqlMap namespace
73   /// </summary>
74   [XmlIgnore]
75   public string SqlMapNameSpace
76   {
77 680 get
78   {
79 680 return _sqlMapNameSpace;
80   }
81 4930 set
82   {
83 4930 _sqlMapNameSpace = value;
84   }
85   }
86  
87   /// <summary>
88   /// The discriminator used to choose the good SubMap
89   /// </summary>
90   [XmlIgnore]
91   public Discriminator Discriminator
92   {
93 6630 get
94   {
95 6630 return _discriminator;
96   }
97 340 set
98   {
99 340 _discriminator = value;
100   }
101   }
102  
103   /// <summary>
104   /// The collection of result properties.
105   /// </summary>
106   [XmlIgnore]
107   public Hashtable ColumnsToPropertiesMap
108   {
109 6954 get { return _columnsToPropertiesMap; }
110   }
111  
112   /// <summary>
113   /// Identifier used to identify the resultMap amongst the others.
114   /// </summary>
115   /// <example>GetProduct</example>
116   [XmlAttribute("id")]
117   public string Id
118   {
119 14790 get { return _id; }
120 9860 set
121   {
122 9860 if ((value == null) || (value.Length < 1))
123 0 throw new ArgumentNullException("The id attribute is mandatory in a ResultMap tag.");
124  
125 9860 _id = value;
126   }
127   }
128  
129   /// <summary>
130   /// Extend ResultMap attribute
131   /// </summary>
132   [XmlAttribute("extends")]
133   public string ExtendMap
134   {
135 16830 get { return _extendMap; }
136 5950 set { _extendMap = value; }
137   }
138  
139   /// <summary>
140   /// The output type class of the resultMap.
141   /// </summary>
142 0 [XmlIgnore]
143   public Type Class
144   {
145   get { return _class; }
146   }
147  
148  
149   /// <summary>
150   /// The output class name of the resultMap.
151   /// </summary>
152   /// <example>Com.Site.Domain.Product</example>
153   [XmlAttribute("class")]
154   public string ClassName
155   {
156 0 get { return _className; }
157 4930 set
158   {
159 4930 if ((value == null) || (value.Length < 1))
160 0 throw new ArgumentNullException("The class attribute is mandatory in a ResultMap tag.");
161  
162 4930 _className = value;
163   }
164   }
165   #endregion
166  
167   #region Constructor (s) / Destructor
168   /// <summary>
169   /// Do not use direclty, only for serialization.
170   /// </summary>
171 5029 public ResultMap()
172   {
173   }
174   #endregion
175  
176   #region Methods
177  
178   #region Configuration
179   /// <summary>
180   /// Initialize the resultMap from an xmlNode..
181   /// </summary>
182   /// <param name="configScope"></param>
183 4930 public void Initialize( ConfigurationScope configScope )
184   {
185 4930 try
186   {
187 4930 _class = configScope.SqlMapper.GetType(_className);
188  
189   // Load the child node
190 4930 GetChildNode(configScope);
191   }
192   catch(Exception e)
193   {
194 0 throw new ConfigurationException(
195   string.Format("Could not configure ResultMap. ResultMap named \"{0}\" not found, failed. \n Cause: {1}", _id, e.Message)
196   );
197   }
198   }
199  
200  
201   /// <summary>
202   /// Get the result properties and the subMap properties.
203   /// </summary>
204   /// <param name="configScope"></param>
205 4930 private void GetChildNode(ConfigurationScope configScope)
206   {
207 4930 XmlSerializer serializer = null;
208 4930 ResultProperty mapping = null;
209 4930 SubMap subMap = null;
210  
211   #region Load the Result Properties
212  
213 4930 serializer = new XmlSerializer(typeof(ResultProperty));
214 4930 foreach ( XmlNode resultNode in configScope.NodeContext.SelectNodes("result") )
215   {
216 24480 mapping = (ResultProperty) serializer.Deserialize(new XmlNodeReader(resultNode));
217  
218 24480 configScope.ErrorContext.MoreInfo = "initialize result property :"+mapping.PropertyName;
219   //
220   // PropertyInfo propertyInfo = null;
221   //
222   // if ( mapping.PropertyName != "value" && !typeof(IDictionary).IsAssignableFrom(_class) )
223   // {
224   // propertyInfo = ReflectionInfo.GetInstance(_class).GetSetter( mapping.PropertyName );
225   // }
226 24480 mapping.Initialize( configScope, _class );
227  
228 24480 this.AddResultPropery( mapping );
229   }
230   #endregion
231  
232   #region Load the Discriminator Property
233  
234 4930 serializer = new XmlSerializer(typeof(Discriminator));
235 4930 XmlNode discriminatorNode = configScope.NodeContext.SelectSingleNode("discriminator");
236 4930 if (discriminatorNode != null)
237   {
238 340 configScope.ErrorContext.MoreInfo = "initialize discriminator";
239  
240 340 this.Discriminator = (Discriminator) serializer.Deserialize(new XmlNodeReader(discriminatorNode));
241  
242 340 this.Discriminator.SetMapping( configScope, _class );
243   }
244   #endregion
245  
246   #region Load the SubMap Properties
247  
248 4930 serializer = new XmlSerializer(typeof(SubMap));
249 4930 if (configScope.NodeContext.SelectNodes("subMap").Count>0 && this.Discriminator==null)
250   {
251 0 throw new ConfigurationException("The discriminator is null, but somehow a subMap was reached. This is a bug.");
252   }
253 4930 foreach ( XmlNode resultNode in configScope.NodeContext.SelectNodes("subMap") )
254   {
255 680 configScope.ErrorContext.MoreInfo = "initialize subMap";
256 680 subMap = (SubMap) serializer.Deserialize(new XmlNodeReader(resultNode));
257 680 subMap.ResultMapName = this.SqlMapNameSpace + DomSqlMapBuilder.DOT + subMap.ResultMapName;
258 680 this.Discriminator.Add( subMap );
259   }
260   #endregion
261   }
262  
263   #endregion
264  
265   /// <summary>
266   /// Create an instance Of result.
267   /// </summary>
268   /// <returns>An object.</returns>
269 232 public object CreateInstanceOfResult()
270   {
271 232 TypeCode typeCode = Type.GetTypeCode(_class);
272  
273 232 if (typeCode == TypeCode.Object)
274   {
275 210 return Activator.CreateInstance(_class);
276   }
277   else
278   {
279 22 return TypeAliasResolver.InstantiatePrimitiveType(typeCode);
280   }
281   }
282  
283   /// <summary>
284   /// Add a ResultProperty to the list of ResultProperty.
285   /// </summary>
286   /// <param name="property">The property to add.</param>
287 31578 public void AddResultPropery(ResultProperty property)
288   {
289 31578 _columnsToPropertiesMap.Add( property.PropertyName, property );
290   }
291  
292   /// <summary>
293   /// Set the value of an object property.
294   /// </summary>
295   /// <param name="target">The object to set the property.</param>
296   /// <param name="property">The result property to use.</param>
297   /// <param name="dataBaseValue">The database value to set.</param>
298 1207 public void SetValueOfProperty( ref object target, ResultProperty property, object dataBaseValue )
299   {
300 1207 if (target is Hashtable)
301   {
302 33 ((Hashtable) target).Add(property.PropertyName, dataBaseValue);
303   }
304   else
305   {
306 1174 if ( target.GetType() != _class )
307   {
308 0 throw new ArgumentException( "Could not set value of type '"+ target.GetType() +"' in property '"+property.PropertyName+"' of type '"+_class+"'" );
309   }
310  
311 1174 if ( property.PropertyInfo != null )
312   {
313 1152 property.PropertyInfo.SetValue( target, dataBaseValue, null );
314   }
315   else // Primitive type ('value')
316   {
317 22 target = dataBaseValue;
318   }
319   }
320   }
321  
322   /// <summary>
323   ///
324   /// </summary>
325   /// <param name="dataReader"></param>
326   /// <returns></returns>
327 244 public ResultMap ResolveSubMap(IDataReader dataReader)
328   {
329 244 ResultMap subMap = this;
330 244 if (_discriminator != null)
331   {
332 18 ResultProperty mapping = _discriminator.ResultProperty;
333 18 object dataBaseValue = mapping.GetDataBaseValue( dataReader );
334 18 subMap = _discriminator.GetSubMap( dataBaseValue.ToString() );
335  
336 18 if (subMap == null)
337   {
338 6 subMap = this;
339   }
340 12 else if (subMap != this)
341   {
342 12 subMap = subMap.ResolveSubMap(dataReader);
343   }
344   }
345 244 return subMap;
346   }
347  
348  
349   #endregion
350   }
351   }
352