Clover.NET coverage report - Coverage

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

File Stats: LOC: 247   Methods: 5
NCLOC: 159 Classes: 1
 
Source File Conditionals Statements Methods TOTAL
Configuration\Sql\Dynamic\Handlers\ConditionalTagHandler.cs 65.0 % 73.3 % 100.0 % 71.4 %
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   using System;
29   using System.Text;
30  
31   using IBatisNet.DataMapper.Exceptions;
32   using IBatisNet.DataMapper.Configuration.Sql.Dynamic.Elements;
33   using IBatisNet.Common.Utilities.Objects;
34   #endregion
35  
36   namespace IBatisNet.DataMapper.Configuration.Sql.Dynamic.Handlers
37   {
38   /// <summary>
39   /// Description résumée de ConditionalTagHandler.
40   /// </summary>
41   public abstract class ConditionalTagHandler : BaseTagHandler
42   {
43  
44   #region Const
45   /// <summary>
46   ///
47   /// </summary>
48   public const long NOT_COMPARABLE = long.MinValue;
49   #endregion
50  
51   #region Methods
52   /// <summary>
53   ///
54   /// </summary>
55   /// <param name="ctx"></param>
56   /// <param name="tag"></param>
57   /// <param name="parameterObject"></param>
58   /// <returns></returns>
59   public abstract bool IsCondition(SqlTagContext ctx, SqlTag tag, object parameterObject);
60  
61   /// <summary>
62   ///
63   /// </summary>
64   /// <param name="ctx"></param>
65   /// <param name="tag"></param>
66   /// <param name="parameterObject"></param>
67   /// <returns></returns>
68 102 public override int DoStartFragment(SqlTagContext ctx, SqlTag tag, Object parameterObject)
69   {
70 102 if (IsCondition(ctx, tag, parameterObject))
71   {
72 54 return BaseTagHandler.INCLUDE_BODY;
73   }
74   else
75   {
76 48 return BaseTagHandler.SKIP_BODY;
77   }
78   }
79  
80   /// <summary>
81   ///
82   /// </summary>
83   /// <param name="ctx"></param>
84   /// <param name="tag"></param>
85   /// <param name="parameterObject"></param>
86   /// <param name="bodyContent"></param>
87   /// <returns></returns>
88 54 public override int DoEndFragment(SqlTagContext ctx, SqlTag tag, Object parameterObject, StringBuilder bodyContent)
89   {
90 54 return BaseTagHandler.INCLUDE_BODY;
91   }
92  
93   /// <summary>
94   ///
95   /// </summary>
96   /// <param name="ctx"></param>
97   /// <param name="sqlTag"></param>
98   /// <param name="parameterObject"></param>
99   /// <returns></returns>
100 34 protected long Compare(SqlTagContext ctx, SqlTag sqlTag, object parameterObject)
101   {
102 34 Conditional tag = (Conditional)sqlTag;
103 34 string propertyName = tag.Property;
104 34 string comparePropertyName = tag.CompareProperty;
105 34 string compareValue = tag.CompareValue;
106  
107 34 object value1 = null;
108 34 Type type = null;
109 34 if (propertyName != null && propertyName.Length > 0)
110   {
111 24 value1 = ObjectProbe.GetPropertyValue(parameterObject, propertyName);
112 24 type = value1.GetType();
113   }
114   else
115   {
116 10 value1 = parameterObject;
117 10 if (value1 != null)
118   {
119 10 type = parameterObject.GetType();
120   }
121   else
122   {
123 0 type = typeof(object);
124   }
125   }
126 34 if (comparePropertyName != null && comparePropertyName.Length > 0)
127   {
128 2 object value2 = ObjectProbe.GetPropertyValue(parameterObject, comparePropertyName);
129 2 return CompareValues(type, value1, value2);
130   }
131 32 else if (compareValue != null && compareValue != "")
132   {
133 32 return CompareValues(type, value1, compareValue);
134   }
135   else
136   {
137 0 throw new DataMapperException("Error comparing in conditional fragment. Uknown 'compare to' values.");
138   }
139   }
140  
141   /// <summary>
142   ///
143   /// </summary>
144   /// <param name="type"></param>
145   /// <param name="value1"></param>
146   /// <param name="value2"></param>
147   /// <returns></returns>
148 34 protected long CompareValues(Type type, object value1, object value2)
149   {
150 34 long result = NOT_COMPARABLE;
151  
152 34 if (value1 == null || value2 == null)
153   {
154 0 result = value1 == value2 ? 0 : NOT_COMPARABLE;
155   }
156   else
157   {
158 34 if (value2.GetType() != type)
159   {
160 26 value2 = ConvertValue(type, value2.ToString());
161   }
162 34 if (value2 is string && type != typeof(string))
163   {
164 0 value1 = value1.ToString();
165   }
166 34 if (!(value1 is IComparable && value2 is IComparable))
167   {
168 0 value1 = value1.ToString();
169 0 value2 = value2.ToString();
170   }
171 34 result = ((IComparable) value1).CompareTo(value2);
172   }
173  
174 34 return result;
175   }
176  
177   /// <summary>
178   ///
179   /// </summary>
180   /// <param name="type"></param>
181   /// <param name="value"></param>
182   /// <returns></returns>
183 26 protected object ConvertValue(Type type, string value)
184   {
185  
186 26 if (type == typeof(String))
187   {
188 0 return value;
189   }
190 26 else if (type == typeof(bool))
191   {
192 1 return System.Convert.ToBoolean(value);
193   }
194 25 else if (type == typeof(Byte))
195   {
196 0 return System.Convert.ToByte(value);
197   }
198 25 else if (type == typeof(Char))
199   {
200 0 return System.Convert.ToChar(value.Substring(0,1));//new Character(value.charAt(0));
201   }
202 25 else if (type == typeof(DateTime))
203   {
204 1 try
205   {
206 1 return System.Convert.ToDateTime(value);
207   }
208   catch (Exception e)
209   {
210 0 throw new DataMapperException("Error parsing date. Cause: " + e.Message, e);
211   }
212   }
213 24 else if (type == typeof(Decimal))
214   {
215 0 return System.Convert.ToDecimal(value);
216   }
217 24 else if (type == typeof(Double))
218   {
219 0 return System.Convert.ToDouble(value);
220   }
221 24 else if (type == typeof(Int16))
222   {
223 0 return System.Convert.ToInt16(value);
224   }
225 24 else if (type == typeof(Int32))
226   {
227 22 return System.Convert.ToInt32(value);
228   }
229 2 else if (type == typeof(Int64))
230   {
231 2 return System.Convert.ToInt64(value);
232   }
233 0 else if (type == typeof(Single))
234   {
235   return System.Convert.ToSingle(value);
236   }
237   else
238   {
239   return value;
240   }
241  
242   }
243   #endregion
244  
245   }
246   }
247