KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > barracuda > testbed > TestUtil


1 /*
2  * Copyright (C) 2003 Christian Cryder [christianc@granitepeaks.com]
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * $Id: TestUtil.java,v 1.4 2004/02/01 05:16:32 christianc Exp $
19  */

20 package org.enhydra.barracuda.testbed;
21
22 import org.apache.log4j.*;
23 import java.util.*;
24 import java.text.*;
25
26 /**
27  * This class just defines basic constants (BATCH_MODE, DEBUG_LEVEL)
28  * and parses runtime parameters to set those constants.
29  */

30 public class TestUtil {
31
32     //other runtime parameters
33
public static boolean BATCH_MODE = false;
34     public static int DEBUG_LEVEL = 0;
35
36     /**
37      * This utility method is used to parse an argument string
38      * to automatically adjust any global paramters
39      *
40      * Known paramters:
41      * ---------------
42      * batch - (opt) run the test in batch mode (as opposed to interactive Swing mode,
43      * which is the default)
44      * debug_level - (opt) an int value which determines whether debug msgs show
45      */

46     public static void parseParams (String JavaDoc args[]) {
47         //check to see runtime parameters
48
if (args!=null) {
49             for (int i=0; i<args.length; i++) {
50                 String JavaDoc parm = args[i].toLowerCase();
51                 if (parm.startsWith("batch=")) {
52                     BATCH_MODE = (parm.toLowerCase().indexOf("true")>0);
53                 } else if (parm.startsWith("debug_level=")) {
54                     try {DEBUG_LEVEL = Integer.parseInt(parm.substring(12));}
55                     catch (Exception JavaDoc e) {}
56                 }
57             }
58         }
59
60         //show what we've got
61
System.out.println ("Setting BATCH_MODE="+TestUtil.BATCH_MODE);
62         System.out.println ("Setting DEBUG_LEVEL="+TestUtil.DEBUG_LEVEL);
63         System.out.println ("");
64     }
65
66     public static String JavaDoc dateStringInDefaultLocaleShortForm(String JavaDoc theYear, String JavaDoc theMonth, String JavaDoc theDay) {
67         Calendar aCalendar = Calendar.getInstance();
68         // zero everything
69
aCalendar.clear();
70         aCalendar.set(2000, 0, 1);
71
72         Date aZeroDate = aCalendar.getTime();
73         DateFormat aDefaultDateFormat = DateFormat.getDateInstance(DateFormat.SHORT);
74         FieldPosition yearFieldPosn = new FieldPosition(DateFormat.YEAR_FIELD);
75         FieldPosition monthFieldPosn = new FieldPosition(DateFormat.MONTH_FIELD);
76         FieldPosition dayFieldPosn = new FieldPosition(DateFormat.DATE_FIELD);
77         aDefaultDateFormat.format(aZeroDate, new StringBuffer JavaDoc(), yearFieldPosn);
78         aDefaultDateFormat.format(aZeroDate, new StringBuffer JavaDoc(), monthFieldPosn);
79         aDefaultDateFormat.format(aZeroDate, new StringBuffer JavaDoc(), dayFieldPosn);
80
81         // sort the field positions into descending start index order
82
ArrayList sortedPositions = new ArrayList();
83         sortedPositions.add(yearFieldPosn);
84         if (monthFieldPosn.getBeginIndex() > yearFieldPosn.getBeginIndex()) {
85             sortedPositions.add(0, monthFieldPosn);
86         } else {
87             sortedPositions.add(monthFieldPosn);
88         }
89         if (dayFieldPosn.getBeginIndex() > ((FieldPosition)sortedPositions.get(0)).getBeginIndex()) {
90             sortedPositions.add(0, dayFieldPosn);
91         } else if (dayFieldPosn.getBeginIndex() > ((FieldPosition)sortedPositions.get(1)).getBeginIndex()) {
92             sortedPositions.add(1, dayFieldPosn);
93         } else {
94             sortedPositions.add(dayFieldPosn);
95         }
96
97         // create string buffer with formatted current date in it (to make it sure is correct length)
98
StringBuffer JavaDoc aStrBuff = new StringBuffer JavaDoc(aDefaultDateFormat.format(aZeroDate));
99         for (int i = 0; i < sortedPositions.size(); i++) {
100             FieldPosition currFieldPosn = (FieldPosition)sortedPositions.get(i);
101             String JavaDoc currField = theYear;
102             if (currFieldPosn==monthFieldPosn) {
103                 currField = theMonth;
104             } else if (currFieldPosn==dayFieldPosn) {
105                 currField = theDay;
106             }
107             aStrBuff.replace(currFieldPosn.getBeginIndex(), currFieldPosn.getEndIndex(), currField);
108         }
109         return (aStrBuff.toString());
110     }
111 }
112
Popular Tags