KickJava   Java API By Example, From Geeks To Geeks.

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


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.lang.reflect.Field JavaDoc;
28 import java.util.Iterator JavaDoc;
29
30 import net.firstpartners.nounit.utility.NoUnitException;
31
32
33 /**
34  * Checks to see the values stored in a Package are only those that have public keys
35  * i.e. public static final string KEY_NAME ="key_to_check_for"
36  * Helps pick up miss-spellings
37  * e.g changing key names , or adding basic defaults (from Parameters file)
38  */

39 public class ExtraValueChecker {
40     
41     /**
42      * checkCommandLineValues
43      * @param inValues - sets of Values to Check
44      * @return successFlag - true if all values match those specified
45      * @exception NoUnitException if values do not match
46      * @exception java.lang.ClassNotFoundException
47      */

48     public boolean checkForExtraKeys(CommandPackage inValues)
49     throws NoUnitException, java.lang.ClassNotFoundException JavaDoc {
50         
51         //Change to appropriate package
52
String JavaDoc acceptableValues;
53         boolean successFlag = true; // will be returned unless exception is thrown
54
Iterator JavaDoc storeNames;
55         String JavaDoc thisName;
56         
57         //Get list of all the acceptable / available keys in web package
58
try {
59             acceptableValues=getConcatinatedFields(CommandPackage.class,inValues);
60         } catch (IllegalAccessException JavaDoc iae) {
61             throw new NoUnitException(iae,"Could not read the list of Values that the System will accept");
62         }
63         
64         //Get the list of incoming values
65
storeNames= inValues.getStoreNames();
66         
67         //Now loop through and check that they all exist
68
while (storeNames.hasNext()) {
69             
70             thisName = (String JavaDoc)storeNames.next();
71             if (acceptableValues.indexOf(thisName)<0) {
72                 if (!(thisName.equals("XXX"))) { //
73
throw new NoUnitException("The System did not recognize the key:"+thisName);
74                 }
75             }
76         }
77         
78         return successFlag;
79     }
80     
81     /**
82      * Get a Concatinated list of fields in a class
83      * @param classToProcess
84      * @param instanceOfClass to use to check available fields
85      * @return concatinatedFields - as list in value1:value2 suitable for indexof search
86      */

87     private String JavaDoc getConcatinatedFields(Class JavaDoc classToProcess, Object JavaDoc instanceOfClass)
88     throws IllegalAccessException JavaDoc {
89         
90         //Inner Variables
91
StringBuffer JavaDoc concatinatedFields=new StringBuffer JavaDoc();
92         Field JavaDoc[] listOfFields;
93         String JavaDoc thisName;
94         Object JavaDoc thisValue;
95         
96         //Get list of Fields and build up string
97
listOfFields = classToProcess.getFields();
98         
99         for (int a=0; a<listOfFields.length;a++) {
100             
101             //Get the field name
102
thisName=listOfFields[a].getName();
103             thisValue=listOfFields[a].get(instanceOfClass);
104             
105             //now get the value of the field
106
if (thisValue!=null) {
107                 concatinatedFields.append(thisValue);
108             }
109             concatinatedFields.append(":\n");
110             
111         }
112         
113         return concatinatedFields.toString();
114         
115     }
116     
117     
118     
119     
120     
121 }
Popular Tags