001: package net.firstpartners.nounit.ui.common;
002:
003: /**
004: * Title: NoUnit - Identify Classes that are not being unit Tested
005: *
006: * Copyright (C) 2001 Paul Browne , FirstPartners.net
007: *
008: *
009: * This program is free software; you can redistribute it and/or
010: * modify it under the terms of the GNU General Public License
011: * as published by the Free Software Foundation; either version 2
012: * of the License, or (at your option) any later version.
013: *
014: * This program is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
017: * GNU General Public License for more details.
018: *
019: * You should have received a copy of the GNU General Public License
020: * along with this program; if not, write to the Free Software
021: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
022: *
023: * @author Paul Browne
024: * @version 0.7
025: */
026:
027: import java.io.File;
028: import java.util.Enumeration;
029: import java.util.HashMap;
030: import java.util.Iterator;
031: import java.util.Map;
032: import java.util.Vector;
033:
034: import org.apache.log4j.Logger;
035:
036: import net.firstpartners.nounit.utility.NoUnitException;
037:
038: /**
039: * Abstract class with methods to check values
040: */
041: abstract public class AbstractValueChecker {
042:
043: /**
044: * Override this method to provide value checking in sub-classes
045: * @param inValues - AbstractPackage of incoming values
046: * @return boolean
047: */
048: public abstract boolean checkValues(AbstractPackage inValues)
049: throws NoUnitException, java.lang.ClassNotFoundException,
050: java.sql.SQLException;
051:
052: /**
053: * Checks for Null, empty and instance of Strings under Named keys in AbstractPackage
054: * @param inValues - AbstractPackage to check
055: * @param namesToCheck - Hashmap of Names to carry out checks on , plus 'English Names'
056: * @exception NoUnitException
057: * @exception NumberFormatException
058: */
059: protected void checkForNullEmptyString(AbstractPackage inValues,
060: HashMap namesToCheck) throws NoUnitException {
061:
062: //Internal Variables
063: Object tmpObject = null;
064: String this Name = null;
065: String this EnglishName = null;
066: Iterator myNames = namesToCheck.keySet().iterator();
067:
068: //Loop to check for the values
069: while (myNames.hasNext()) {
070:
071: this Name = (String) myNames.next();
072: tmpObject = inValues.getValue(this Name);
073:
074: //Check for not Null
075: if (tmpObject == null) {
076: this EnglishName = getUserFriendlyName(this Name,
077: namesToCheck);
078: throw new NoUnitException("Please make sure that "
079: + this EnglishName + " is not empty or void");
080: }
081:
082: //Check for Not String
083: if (!(tmpObject instanceof String)) {
084: this EnglishName = getUserFriendlyName(this Name,
085: namesToCheck);
086: throw new NoUnitException("Please make sure that "
087: + this EnglishName + " is a valid piece of text");
088: }
089:
090: //Check for Empty String
091: if ((tmpObject.toString().equals(""))) {
092: this EnglishName = getUserFriendlyName(this Name,
093: namesToCheck);
094: throw new NoUnitException("Please make sure that "
095: + this EnglishName + " contains a value");
096: }
097: }
098:
099: }
100:
101: /**
102: * Checks for Numbers under Named keys in AbstractPackage
103: * Objects can be integers or vectors of Integers
104: * @param inValues - AbstractPackage to check
105: * @param namesToCheck - Hashmap of name (in AbstractPackage) , english name to carry out checks on
106: * @return boolean - true if all values check out ok
107: * @exception NoUnitException
108: */
109: boolean checkForNumbers(AbstractPackage inValues,
110: HashMap namesToCheck) throws NoUnitException {
111:
112: //Internal Variables
113: Object inObject = null;
114: Object tmpObject = null;
115: Integer tmpInteger = null;
116: Vector tmpVector = null;
117: Enumeration myVectorList;
118: String tmpNumberString;
119: String this Name = "";
120: String this EnglishName = null;
121: Iterator myNames = namesToCheck.keySet().iterator();
122:
123: //handle to logger
124: Logger log = Logger.getLogger(AbstractValueChecker.class);
125:
126: //Loop to check for the values
127: while (myNames.hasNext()) {
128:
129: this Name = (String) myNames.next();
130:
131: inObject = inValues.getValue(this Name);
132:
133: //Build a loop vector (to check all values under this number)
134: if (inObject instanceof Vector) {
135: myVectorList = ((Vector) inObject).elements();
136: } else {
137: tmpVector = new Vector();
138: tmpVector.add(inObject);
139: myVectorList = tmpVector.elements();
140: }
141:
142: //Loop through the 1+ values
143: while (myVectorList.hasMoreElements()) {
144:
145: tmpObject = myVectorList.nextElement();
146:
147: //Check for not Null
148: if (tmpObject == null) {
149:
150: this EnglishName = getUserFriendlyName(this Name,
151: namesToCheck);
152:
153: log.debug("@@@@@@ problem with " + this Name
154: + " @@@@@");
155:
156: throw new NoUnitException("Please make sure that "
157: + this EnglishName
158: + " contains a number value");
159: }
160:
161: //if not an integer , then do checks...
162: if (!(tmpObject instanceof Integer)) {
163: if (tmpObject instanceof String) {
164:
165: try {
166: tmpNumberString = (String) tmpObject;
167: tmpInteger = new Integer(tmpNumberString); // throws exception if not a number
168: } catch (NumberFormatException nfe) {
169: this EnglishName = getUserFriendlyName(
170: this Name, namesToCheck);
171: throw new NoUnitException(
172: "Please make sure that "
173: + this EnglishName
174: + " contains a number");
175: }
176:
177: }
178: }
179: }
180:
181: }
182:
183: return true; // must be ok if we get this far
184:
185: }
186:
187: /**
188: * Checks for Vectors under Named keys in AbstractPackage
189: * Objects can be Vectors only
190: * @param inValues - AbstractPackage to check
191: * @param namesToCheck - HashMap of Names / English Names to carry out checks on
192: * @return boolean - true if all values check out ok
193: * @exception NoUnitException
194: */
195: boolean checkForVectors(AbstractPackage inValues,
196: HashMap namesToCheck) throws NoUnitException {
197:
198: //Internal Variables
199: Vector tmpVector = null;
200: String this Name = "";
201: String this EnglishName = null;
202: Iterator myNames = namesToCheck.keySet().iterator();
203:
204: //Loop to check for the values
205: while (myNames.hasNext()) {
206:
207: this Name = (String) myNames.next();
208: tmpVector = inValues.getVector(this Name);
209:
210: //Check for not Null
211: if (tmpVector == null) {
212: this EnglishName = getUserFriendlyName(this Name,
213: namesToCheck);
214: throw new NoUnitException("Please make sure that "
215: + this EnglishName
216: + " contains at least one value");
217: }
218: }
219:
220: return true; // must be ok if we get this far
221:
222: }
223:
224: /**
225: * Checks for Vectors of Strings under Named keys in AbstractPackage
226: * Objects can be Vectors only , containing Strings only
227: * @param inValues - AbstractPackage to check
228: * @param namesToCheck - HashMap of Names / English names to carry out checks on
229: * @return boolean - true if all values check out ok
230: * @exception NoUnitException
231: */
232: boolean checkForStringVectors(AbstractPackage inValues,
233: HashMap namesToCheck) throws NoUnitException {
234:
235: //Internal Variables
236: Vector tmpVector = null;
237: Iterator myList;
238: String this Name = "";
239: String this EnglishName = null;
240: Iterator myNames = namesToCheck.keySet().iterator();
241:
242: //Check that it is a Vector
243: this .checkForVectors(inValues, namesToCheck);
244:
245: //Loop to check for the values
246: while (myNames.hasNext()) {
247:
248: this Name = (String) myNames.next();
249: tmpVector = inValues.getVector(this Name);
250:
251: //Check contents
252: myList = tmpVector.iterator();
253: while (myList.hasNext()) {
254: if (!(myList.next() instanceof String)) {
255: this EnglishName = getUserFriendlyName(this Name,
256: namesToCheck);
257: throw new NoUnitException("Please make sure that "
258: + this EnglishName
259: + " All the values are text");
260: }
261: }
262:
263: }
264:
265: return true; // must be ok if we get this far
266:
267: }
268:
269: /**
270: * Checks that String is either yes (Y) or no (N)
271: * @param name of String being checked (for use in error message)
272: * @param inCheckString - String to check
273: * @return boolean
274: * @exception NoUnitException
275: */
276: boolean checkYN(String name, String inCheckString)
277: throws NoUnitException {
278:
279: if ((!(inCheckString.equals("Y")) && (!(inCheckString
280: .equals("N"))))) {
281: throw new NoUnitException(name, inCheckString,
282: "Value was not Y or N");
283: }
284:
285: return true; // must be ok if we get this far
286:
287: }
288:
289: /**
290: * Gets the user Friendly Name , or returns the key
291: * @param actualKey that the user Friendly Name is stored under
292: * @param names Hashmap of name-userFriendly Names
293: * @return userFriendlyName that was found
294: */
295: private String getUserFriendlyName(String actualKey, Map names)
296: throws NoUnitException {
297:
298: //Internal Variables
299: String userFriendlyName = null;
300:
301: //Check check
302: if (actualKey == null) {
303: throw new NoUnitException("Please make sure that "
304: + actualKey + " is not null");
305: }
306:
307: userFriendlyName = (String) names.get(actualKey);
308: if ((userFriendlyName == null) || (userFriendlyName.equals(""))) {
309: userFriendlyName = actualKey;
310: }
311:
312: return userFriendlyName;
313:
314: }
315:
316: /**
317: * Checks that Directories Exist under the requested Keys
318: * @param inValues - AbstractPackage to check
319: * @param namesToCheck - Hashmap of Names to carry out checks on , plus 'English Names'
320: * @exception NoUnitException
321: */
322: protected void checkForDirsExist(AbstractPackage inValues,
323: Map namesToCheck) throws NoUnitException {
324:
325: //Internal Variables
326: Object tmpObject = null;
327: String this Name = null;
328: String this EnglishName = null;
329: File this Dir;
330: Iterator myNames = namesToCheck.keySet().iterator();
331:
332: //Loop to check for the values
333: while (myNames.hasNext()) {
334:
335: this Name = (String) myNames.next();
336: tmpObject = inValues.getValue(this Name);
337:
338: //Check for not Null
339: if (tmpObject == null) {
340: this EnglishName = getUserFriendlyName(this Name,
341: namesToCheck);
342: throw new NoUnitException("Please make sure that "
343: + this EnglishName
344: + " contains a (non-empty) directory name");
345: }
346:
347: //Check for Not String
348: if (!(tmpObject instanceof String)) {
349: this EnglishName = getUserFriendlyName(this Name,
350: namesToCheck);
351: throw new NoUnitException("Please make sure that "
352: + this EnglishName
353: + " contains a valid directory name");
354: }
355:
356: //Check that Directory exists
357: this Name = (String) tmpObject;
358: this Dir = new File(this Name);
359: if (!this Dir.isDirectory()) {
360: this EnglishName = getUserFriendlyName(this Name,
361: namesToCheck);
362: throw new NoUnitException("Please make sure that "
363: + this EnglishName + " is a directory");
364: }
365:
366: }
367:
368: }
369:
370: }
|