KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > firstpartners > nounit > ui > common > AbstractValueChecker


1 package net.firstpartners.nounit.ui.common;
2
3 /**
4  * Title: NoUnit - Identify Classes that are not being unit Tested
5  *
6  * Copyright (C) 2001 Paul Browne , FirstPartners.net
7  *
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License
11  * as published by the Free Software Foundation; either version 2
12  * of the License, or (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22  *
23  * @author Paul Browne
24  * @version 0.7
25  */

26
27 import java.io.File JavaDoc;
28 import java.util.Enumeration JavaDoc;
29 import java.util.HashMap JavaDoc;
30 import java.util.Iterator JavaDoc;
31 import java.util.Map JavaDoc;
32 import java.util.Vector JavaDoc;
33
34 import org.apache.log4j.Logger;
35
36 import net.firstpartners.nounit.utility.NoUnitException;
37
38
39 /**
40  * Abstract class with methods to check values
41  */

42 abstract public class AbstractValueChecker {
43
44     
45
46   /**
47    * Override this method to provide value checking in sub-classes
48    * @param inValues - AbstractPackage of incoming values
49    * @return boolean
50    */

51   public abstract boolean checkValues(AbstractPackage inValues)
52     throws NoUnitException, java.lang.ClassNotFoundException JavaDoc, java.sql.SQLException JavaDoc;
53
54
55   /**
56    * Checks for Null, empty and instance of Strings under Named keys in AbstractPackage
57    * @param inValues - AbstractPackage to check
58    * @param namesToCheck - Hashmap of Names to carry out checks on , plus 'English Names'
59    * @exception NoUnitException
60    * @exception NumberFormatException
61    */

62   protected void checkForNullEmptyString(AbstractPackage inValues,
63                                            HashMap JavaDoc namesToCheck)
64     throws NoUnitException {
65
66     //Internal Variables
67
Object JavaDoc tmpObject = null;
68     String JavaDoc thisName=null;
69     String JavaDoc thisEnglishName=null;
70     Iterator JavaDoc myNames=namesToCheck.keySet().iterator();
71
72     //Loop to check for the values
73
while(myNames.hasNext()) {
74
75       thisName = (String JavaDoc)myNames.next();
76       tmpObject = inValues.getValue(thisName);
77
78       //Check for not Null
79
if (tmpObject == null) {
80          thisEnglishName= getUserFriendlyName(thisName,namesToCheck);
81          throw new NoUnitException("Please make sure that "
82                                           +thisEnglishName
83                                           +" is not empty or void");
84       }
85
86       //Check for Not String
87
if (!(tmpObject instanceof String JavaDoc)) {
88          thisEnglishName= getUserFriendlyName(thisName,namesToCheck);
89          throw new NoUnitException("Please make sure that "
90                                           +thisEnglishName
91                                           +" is a valid piece of text");
92       }
93
94       //Check for Empty String
95
if ((tmpObject.toString().equals(""))) {
96          thisEnglishName= getUserFriendlyName(thisName,namesToCheck);
97          throw new NoUnitException("Please make sure that "
98                                           +thisEnglishName
99                                           +" contains a value");
100       }
101     }
102
103   }
104
105     /**
106    * Checks for Numbers under Named keys in AbstractPackage
107    * Objects can be integers or vectors of Integers
108    * @param inValues - AbstractPackage to check
109    * @param namesToCheck - Hashmap of name (in AbstractPackage) , english name to carry out checks on
110    * @return boolean - true if all values check out ok
111    * @exception NoUnitException
112    */

113   boolean checkForNumbers(AbstractPackage inValues,
114                             HashMap JavaDoc namesToCheck)
115     throws NoUnitException {
116
117     //Internal Variables
118
Object JavaDoc inObject =null;
119       Object JavaDoc tmpObject = null;
120       Integer JavaDoc tmpInteger =null;
121       Vector JavaDoc tmpVector = null;
122       Enumeration JavaDoc myVectorList;
123       String JavaDoc tmpNumberString;
124       String JavaDoc thisName="";
125       String JavaDoc thisEnglishName=null;
126       Iterator JavaDoc myNames=namesToCheck.keySet().iterator();
127       
128       //handle to logger
129
Logger log = Logger.getLogger(AbstractValueChecker.class);
130
131     //Loop to check for the values
132
while (myNames.hasNext()) {
133
134       thisName=(String JavaDoc)myNames.next();
135
136       inObject = inValues.getValue(thisName);
137
138       //Build a loop vector (to check all values under this number)
139
if (inObject instanceof Vector JavaDoc) {
140         myVectorList = ((Vector JavaDoc)inObject).elements();
141       } else {
142         tmpVector = new Vector JavaDoc();
143         tmpVector.add(inObject);
144         myVectorList = tmpVector.elements();
145       }
146
147       //Loop through the 1+ values
148
while(myVectorList.hasMoreElements()) {
149
150         tmpObject = myVectorList.nextElement();
151
152         //Check for not Null
153
if (tmpObject == null) {
154
155            thisEnglishName= getUserFriendlyName(thisName,namesToCheck);
156
157            log.debug("@@@@@@ problem with "+thisName+" @@@@@");
158
159            throw new NoUnitException("Please make sure that "
160                                           +thisEnglishName
161                                           +" contains a number value");
162         }
163
164         //if not an integer , then do checks...
165
if (!(tmpObject instanceof Integer JavaDoc)) {
166           if (tmpObject instanceof String JavaDoc) {
167
168             try {
169               tmpNumberString = (String JavaDoc)tmpObject;
170               tmpInteger = new Integer JavaDoc(tmpNumberString); // throws exception if not a number
171
} catch (NumberFormatException JavaDoc nfe) {
172                   thisEnglishName= getUserFriendlyName(thisName,namesToCheck);
173                   throw new NoUnitException("Please make sure that "
174                                           +thisEnglishName
175                                           +" contains a number");
176             }
177
178           }
179         }
180       }
181
182     }
183
184     return true; // must be ok if we get this far
185

186 }
187
188    /**
189    * Checks for Vectors under Named keys in AbstractPackage
190    * Objects can be Vectors only
191    * @param inValues - AbstractPackage to check
192    * @param namesToCheck - HashMap of Names / English Names to carry out checks on
193    * @return boolean - true if all values check out ok
194    * @exception NoUnitException
195    */

196   boolean checkForVectors(AbstractPackage inValues,
197                             HashMap JavaDoc namesToCheck)
198     throws NoUnitException {
199
200     //Internal Variables
201
Vector JavaDoc tmpVector =null;
202       String JavaDoc thisName="";
203       String JavaDoc thisEnglishName=null;
204       Iterator JavaDoc myNames=namesToCheck.keySet().iterator();
205
206     //Loop to check for the values
207
while (myNames.hasNext()) {
208
209       thisName=(String JavaDoc)myNames.next();
210       tmpVector = inValues.getVector(thisName);
211
212         //Check for not Null
213
if (tmpVector == null) {
214               thisEnglishName= getUserFriendlyName(thisName,namesToCheck);
215               throw new NoUnitException("Please make sure that "
216                                      +thisEnglishName
217                                      +" contains at least one value");
218         }
219      }
220
221     return true; // must be ok if we get this far
222

223   }
224
225    /**
226    * Checks for Vectors of Strings under Named keys in AbstractPackage
227    * Objects can be Vectors only , containing Strings only
228    * @param inValues - AbstractPackage to check
229    * @param namesToCheck - HashMap of Names / English names to carry out checks on
230    * @return boolean - true if all values check out ok
231    * @exception NoUnitException
232    */

233   boolean checkForStringVectors(AbstractPackage inValues,
234                         HashMap JavaDoc namesToCheck)
235     throws NoUnitException {
236
237     //Internal Variables
238
Vector JavaDoc tmpVector =null;
239       Iterator JavaDoc myList;
240       String JavaDoc thisName="";
241       String JavaDoc thisEnglishName=null;
242       Iterator JavaDoc myNames=namesToCheck.keySet().iterator();
243
244
245     //Check that it is a Vector
246
this.checkForVectors(inValues,namesToCheck);
247
248     //Loop to check for the values
249
while (myNames.hasNext()) {
250
251       thisName=(String JavaDoc)myNames.next();
252       tmpVector = inValues.getVector(thisName);
253
254         //Check contents
255
myList = tmpVector.iterator();
256         while (myList.hasNext()) {
257           if (!(myList.next() instanceof String JavaDoc)) {
258                 thisEnglishName= getUserFriendlyName(thisName,namesToCheck);
259                   throw new NoUnitException("Please make sure that "
260                                           +thisEnglishName
261                                           +" All the values are text");
262              }
263         }
264
265
266      }
267
268     return true; // must be ok if we get this far
269

270   }
271
272   
273    /**
274    * Checks that String is either yes (Y) or no (N)
275    * @param name of String being checked (for use in error message)
276    * @param inCheckString - String to check
277    * @return boolean
278    * @exception NoUnitException
279    */

280   boolean checkYN(String JavaDoc name,String JavaDoc inCheckString)
281     throws NoUnitException {
282
283     if ((!(inCheckString.equals("Y"))&&
284           (!(inCheckString.equals("N")))))
285           {
286               throw new NoUnitException (name,inCheckString, "Value was not Y or N");
287           }
288
289     return true; // must be ok if we get this far
290

291   }
292
293   /**
294    * Gets the user Friendly Name , or returns the key
295    * @param actualKey that the user Friendly Name is stored under
296    * @param names Hashmap of name-userFriendly Names
297    * @return userFriendlyName that was found
298    */

299   private String JavaDoc getUserFriendlyName(String JavaDoc actualKey, Map JavaDoc names)
300     throws NoUnitException {
301
302     //Internal Variables
303
String JavaDoc userFriendlyName = null;
304
305     //Check check
306
if(actualKey == null) {
307         throw new NoUnitException("Please make sure that "+actualKey+" is not null");
308     }
309
310     userFriendlyName = (String JavaDoc)names.get(actualKey);
311     if ((userFriendlyName == null)||(userFriendlyName.equals(""))) {
312       userFriendlyName = actualKey;
313     }
314
315     return userFriendlyName;
316
317   }
318
319    /**
320    * Checks that Directories Exist under the requested Keys
321    * @param inValues - AbstractPackage to check
322    * @param namesToCheck - Hashmap of Names to carry out checks on , plus 'English Names'
323    * @exception NoUnitException
324    */

325   protected void checkForDirsExist(AbstractPackage inValues,
326                                            Map JavaDoc namesToCheck)
327     throws NoUnitException {
328
329     //Internal Variables
330
Object JavaDoc tmpObject = null;
331     String JavaDoc thisName=null;
332     String JavaDoc thisEnglishName=null;
333     File JavaDoc thisDir;
334     Iterator JavaDoc myNames=namesToCheck.keySet().iterator();
335
336     //Loop to check for the values
337
while(myNames.hasNext()) {
338
339       thisName = (String JavaDoc)myNames.next();
340       tmpObject = inValues.getValue(thisName);
341
342       //Check for not Null
343
if (tmpObject == null) {
344          thisEnglishName= getUserFriendlyName(thisName,namesToCheck);
345          throw new NoUnitException("Please make sure that "
346                                           +thisEnglishName
347                                           +" contains a (non-empty) directory name");
348       }
349
350       //Check for Not String
351
if (!(tmpObject instanceof String JavaDoc)) {
352          thisEnglishName= getUserFriendlyName(thisName,namesToCheck);
353          throw new NoUnitException("Please make sure that "
354                                           +thisEnglishName
355                                           +" contains a valid directory name");
356       }
357       
358       //Check that Directory exists
359
thisName=(String JavaDoc)tmpObject;
360       thisDir=new File JavaDoc(thisName);
361       if(!thisDir.isDirectory()) {
362          thisEnglishName= getUserFriendlyName(thisName,namesToCheck);
363          throw new NoUnitException("Please make sure that "
364                                           +thisEnglishName
365                                           +" is a directory");
366       }
367       
368     }
369
370   }
371
372   
373   
374 }
Popular Tags