001: /*
002: * ====================================================================
003: * JAFFA - Java Application Framework For All
004: *
005: * Copyright (C) 2002 JAFFA Development Group
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2.1 of the License, or (at your option) any later version.
011: *
012: * This library is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this library; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020: *
021: * Redistribution and use of this software and associated documentation ("Software"),
022: * with or without modification, are permitted provided that the following conditions are met:
023: * 1. Redistributions of source code must retain copyright statements and notices.
024: * Redistributions must also contain a copy of this document.
025: * 2. Redistributions in binary form must reproduce the above copyright notice,
026: * this list of conditions and the following disclaimer in the documentation
027: * and/or other materials provided with the distribution.
028: * 3. The name "JAFFA" must not be used to endorse or promote products derived from
029: * this Software without prior written permission. For written permission,
030: * please contact mail to: jaffagroup@yahoo.com.
031: * 4. Products derived from this Software may not be called "JAFFA" nor may "JAFFA"
032: * appear in their names without prior written permission.
033: * 5. Due credit should be given to the JAFFA Project (http://jaffa.sourceforge.net).
034: *
035: * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
036: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
037: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
038: * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
039: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
040: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
041: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
042: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
043: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
044: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
045: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
046: * SUCH DAMAGE.
047: * ====================================================================
048: */
049:
050: package org.jaffa.datatypes;
051:
052: import org.jaffa.metadata.*;
053: import java.text.*;
054: import junit.framework.*;
055:
056: public class FieldValidatorTest extends TestCase {
057: private String f_in = null;
058: private Object f_out = null;
059: private FieldMetaData f_meta = null;
060:
061: public FieldValidatorTest(java.lang.String testName) {
062: super (testName);
063: }
064:
065: public static void main(java.lang.String[] args) {
066: junit.textui.TestRunner.run(suite());
067: }
068:
069: public static Test suite() {
070: TestSuite suite = new TestSuite(FieldValidatorTest.class);
071:
072: return suite;
073: }
074:
075: public void setUp() {
076: }
077:
078: public void tearDown() {
079: String f_in = null;
080: Object f_out = null;
081: FieldMetaData f_meta = null;
082: }
083:
084: public void testValidateBoolean() {
085: try {
086: f_in = null;
087: f_meta = new BooleanFieldMetaData("someName", "someToken",
088: new Boolean(true), "Yes,No",
089: "(yes|true|1)|(no|false|0)");
090: f_out = FieldValidator.validateData(f_in, f_meta);
091: System.out.println("Boolean: fin " + f_in + " fout "
092: + f_out);
093:
094: f_in = "yES";
095: f_out = FieldValidator.validateData(f_in, f_meta, true);
096: System.out.println("Boolean: fin " + f_in + " fout "
097: + f_out);
098:
099: f_in = "0";
100: f_out = FieldValidator.validateData(f_in, f_meta, true);
101: System.out.println("Boolean: fin " + f_in + " fout "
102: + f_out);
103: } catch (ValidationException e) {
104: e.printStackTrace();
105: fail();
106: }
107: }
108:
109: public void testValidateString() {
110: try {
111: f_in = null;
112: f_meta = new StringFieldMetaData("someName", "someToken",
113: new Boolean(true), "FALSE", new Integer(10),
114: FieldMetaData.UPPER_CASE);
115: f_out = FieldValidator.validateData(f_in, f_meta);
116: System.out
117: .println("String: fin " + f_in + " fout " + f_out);
118:
119: f_in = "faLSe";
120: f_out = FieldValidator.validateData(f_in, f_meta, true);
121: System.out
122: .println("String: fin " + f_in + " fout " + f_out);
123: } catch (ValidationException e) {
124: e.printStackTrace();
125: fail();
126: }
127: }
128:
129: public void testValidateInteger() {
130: try {
131: f_in = null;
132: f_meta = new IntegerFieldMetaData("someName", "someToken",
133: new Boolean(true), null, new Long(10),
134: new Long(100), new Integer(2));
135: f_out = FieldValidator.validateData(f_in, f_meta);
136: System.out.println("Integer: fin " + f_in + " fout "
137: + f_out);
138:
139: f_in = "99";
140: f_out = FieldValidator.validateData(f_in, f_meta, true);
141: System.out.println("Integer: fin " + f_in + " fout "
142: + f_out);
143: } catch (ValidationException e) {
144: e.printStackTrace();
145: fail();
146: }
147: }
148:
149: public void testValidateDecimal() {
150: try {
151: f_in = null;
152: f_meta = new DecimalFieldMetaData("someName", "someToken",
153: new Boolean(true), null, new Double(10),
154: new Double(100), new Integer(2), new Integer(3));
155: f_out = FieldValidator.validateData(f_in, f_meta);
156: System.out.println("Decimal: fin " + f_in + " fout "
157: + f_out);
158:
159: f_in = "99.001";
160: f_out = FieldValidator.validateData(f_in, f_meta, true);
161: System.out.println("Decimal: fin " + f_in + " fout "
162: + f_out);
163: } catch (ValidationException e) {
164: e.printStackTrace();
165: fail();
166: }
167: }
168:
169: public void testValidateCurrency() {
170: try {
171: f_in = null;
172: f_meta = new CurrencyFieldMetaData("someName", "someToken",
173: new Boolean(true), null, new Currency(10),
174: new Currency(100), new Integer(3), new Integer(2));
175: f_out = FieldValidator.validateData(f_in, f_meta);
176: System.out.println("Currency: fin " + f_in + " fout "
177: + f_out);
178:
179: f_in = "$99.99";
180: f_out = FieldValidator.validateData(f_in, f_meta, true);
181: System.out.println("Currency: fin " + f_in + " fout "
182: + f_out);
183: } catch (ValidationException e) {
184: e.printStackTrace();
185: fail();
186: }
187: }
188:
189: public void testValidateDateOnly() {
190: try {
191: f_in = null;
192: f_meta = new DateOnlyFieldMetaData("someName", "someToken",
193: new Boolean(true), null, DateOnly.parse("n+1"),
194: DateOnly.parse("n+10"));
195: f_out = FieldValidator.validateData(f_in, f_meta);
196: System.out.println("DateOnly: fin " + f_in + " fout "
197: + f_out);
198:
199: f_in = "n+2";
200: f_out = FieldValidator.validateData(f_in, f_meta, true);
201: System.out.println("DateOnly: fin " + f_in + " fout "
202: + f_out);
203: } catch (ValidationException e) {
204: e.printStackTrace();
205: fail();
206: } catch (ParseException e) {
207: e.printStackTrace();
208: fail();
209: }
210: }
211:
212: public void testValidateDateTime() {
213: try {
214: f_in = null;
215: f_meta = new DateTimeFieldMetaData("someName", "someToken",
216: new Boolean(true), null, DateTime.parse("n+1h"),
217: DateTime.parse("n+10"));
218: f_out = FieldValidator.validateData(f_in, f_meta);
219: System.out.println("DateTime: fin " + f_in + " fout "
220: + f_out);
221:
222: f_in = "n+10d-2h";
223: f_out = FieldValidator.validateData(f_in, f_meta, true);
224: System.out.println("DateTime: fin " + f_in + " fout "
225: + f_out);
226: } catch (ValidationException e) {
227: e.printStackTrace();
228: fail();
229: } catch (ParseException e) {
230: e.printStackTrace();
231: fail();
232: }
233: }
234:
235: public void testValidateRaw() {
236: try {
237: f_in = null;
238: f_meta = new RawFieldMetaData("someName", "someToken",
239: new Boolean(true));
240: f_out = FieldValidator.validateData(f_in, f_meta);
241: System.out.println("Raw: fin " + f_in + " fout " + f_out);
242:
243: f_in = "LotsaData";
244: f_out = FieldValidator.validateData(f_in, f_meta, true);
245: System.out.println("Raw: fin " + f_in + " fout " + f_out
246: + " byteToString " + new String((byte[]) f_out));
247: } catch (ValidationException e) {
248: e.printStackTrace();
249: fail();
250: }
251: }
252: }
|