From: Mike W. <mik...@us...> - 2001-03-29 02:37:30
|
Update of /cvsroot/jvalid/jvalid/src/net/sourceforge/jvalid/examples In directory usw-pr-cvs1:/tmp/cvs-serv11610 Modified Files: ValidatorTest.java Log Message: Basically rewrote the example so that hopefully the output makes more sense. Also added code to demonstrate the new getObject() method available in the Validator class. Index: ValidatorTest.java =================================================================== RCS file: /cvsroot/jvalid/jvalid/src/net/sourceforge/jvalid/examples/ValidatorTest.java,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -r1.3 -r1.4 *** ValidatorTest.java 2001/03/02 03:47:02 1.3 --- ValidatorTest.java 2001/03/29 02:37:26 1.4 *************** *** 1,138 **** ! /*------------------------------------------------------------------------------------------ ! * First let me give credit where credit is due! ! * ! * This package is based on the orginal work of Brett McLaughlin in the Javaworld article ! * 'Validation with Java and XML schema, Part 1' which can be found at ! * http://www.javaworld.com/javaworld/jw-09-2000/jw-0908-validation.html ! * ! * ! * Major modifications are noted in the class JavaDoc comment or in the CVS change log. ! *------------------------------------------------------------------------------------------ ! * ! * Legal Stuff ! * ! * JValid is Open Source. ! * ! * Copyright (c) 2000 Mike Williams. All rights reserved. ! * ! * Redistribution and use in source and binary forms, with or without ! * modification, are permitted under the terms of the ! * GNU LESSER GENERAL PUBLIC LICENSE version 2.1 or later. ! * ! * You should have received a copy the license in this distribution. ! * ! * This product includes software developed by the JDOM Project ! * (http://www.jdom.org). Please see LICENSE-JDOM for details. ! * ! * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED ! * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES ! * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE ! * DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, ! * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES ! * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE ! * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS ! * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER ! * IN CONTRACT, STRICT LIABILITY, OR TORT ! * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE ! * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH ! * DAMAGE. ! * ! *------------------------------------------------------------------- ! * C H A N G E L O G ! *------------------------------------------------------------------- ! * $Header$ * $Log$ * Revision 1.3 2001/03/02 03:47:02 mikewill_1998 * Added date validation examples. ! * ! * Revision 1.2 2001/02/25 17:50:04 mikewill_1998 ! * Updated JavaDoc comments. ! * Added acknowledgements for JDOM and Apache. ! * ! * Revision 1.1.1.1 2001/02/22 11:30:38 mikewill_1998 ! * Initial Import ! * ! */ ! ! package net.sourceforge.jvalid.examples; ! ! import java.io.File; ! import java.io.IOException; ! import java.net.URL; ! import java.util.LinkedList; ! import java.util.List; ! import java.util.Iterator; ! ! ! import net.sourceforge.jvalid.Validator; ! ! ! /** ! * <p>Test class for the Java Validation Library.</p> ! * ! * @author Mike Williams ! */ ! public class ValidatorTest ! { ! ! /** ! * <p>Public interface. Used to run the example.</p> ! * ! * @param args Location of the buyShoes.xsd example configuration file. ! */ ! public static void main(String[] args) ! { ! try ! { ! if (args.length != 1) ! { ! System.out.println("Usage: java ValidatorTest [schema URL]"); ! return; ! } ! ! // Create a valiator ! File schemaFile = new File(args[0]); ! Validator validator = Validator.getInstance(schemaFile.toURL()); ! ! // Create some data ! String badDate1 = "02/29/2001"; ! String badDate2 = "02/01/2001"; ! String badDate3 = "52/01/2001"; ! String dateNeeded = "02/28/2001"; ! ! String shoeSize = "13"; ! String width = "F"; ! String brand = "V-Form"; ! String numEyelets = "@2"; ! ! StringBuffer errorBuffer = new StringBuffer(); ! LinkedList errorList = new LinkedList(); ! ! // Validate the data. ! errorList.addAll( validator.checkValidity("dateNeeded", badDate1) ); ! errorList.addAll( validator.checkValidity("dateNeeded", badDate2) ); ! errorList.addAll( validator.checkValidity("dateNeeded", badDate3) ); ! errorList.addAll( validator.checkValidity("dateNeeded", dateNeeded) ); ! errorList.addAll( validator.checkValidity("shoeSize", shoeSize) ); ! errorList.addAll( validator.checkValidity("width", width) ); ! errorList.addAll( validator.checkValidity("brand", brand) ); ! errorList.addAll( validator.checkValidity("numEyelets", numEyelets) ); ! ! Iterator i = errorList.iterator(); ! ! while ( i.hasNext() ) ! { ! errorBuffer.append( i.next() ); ! errorBuffer.append( "\n" ); ! } ! ! String errorMessage = errorBuffer.toString(); ! System.out.println("Error Message: [" + errorMessage + "]"); ! ! } ! catch (Exception e) ! { ! e.printStackTrace(); ! } ! } } --- 1,290 ---- ! /*------------------------------------------------------------------------------------------ ! * First let me give credit where credit is due! ! * ! * This package is based on the orginal work of Brett McLaughlin in the Javaworld article ! * 'Validation with Java and XML schema, Part 1' which can be found at ! * http://www.javaworld.com/javaworld/jw-09-2000/jw-0908-validation.html ! * ! * ! * Major modifications are noted in the class JavaDoc comment or in the CVS change log. ! *------------------------------------------------------------------------------------------ ! * ! * Legal Stuff ! * ! * JValid is Open Source. ! * ! * Copyright (c) 2000 Mike Williams. All rights reserved. ! * ! * Redistribution and use in source and binary forms, with or without ! * modification, are permitted under the terms of the ! * GNU LESSER GENERAL PUBLIC LICENSE version 2.1 or later. ! * ! * You should have received a copy the license in this distribution. ! * ! * This product includes software developed by the JDOM Project ! * (http://www.jdom.org). Please see LICENSE-JDOM for details. ! * ! * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED ! * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES ! * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE ! * DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, ! * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES ! * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE ! * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS ! * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER ! * IN CONTRACT, STRICT LIABILITY, OR TORT ! * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE ! * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH ! * DAMAGE. ! * ! *------------------------------------------------------------------- ! * C H A N G E L O G ! *------------------------------------------------------------------- ! * $Header$ * $Log$ + * Revision 1.4 2001/03/29 02:37:26 mikewill_1998 + * Basically rewrote the example so that hopefully the output makes more sense. + * Also added code to demonstrate the new getObject() method available in the Validator class. + * * Revision 1.3 2001/03/02 03:47:02 mikewill_1998 * Added date validation examples. ! * ! * Revision 1.2 2001/02/25 17:50:04 mikewill_1998 ! * Updated JavaDoc comments. ! * Added acknowledgements for JDOM and Apache. ! * ! * Revision 1.1.1.1 2001/02/22 11:30:38 mikewill_1998 ! * Initial Import ! * ! */ ! ! package net.sourceforge.jvalid.examples; ! ! import java.io.File; ! import java.io.IOException; ! import java.net.URL; ! import java.util.LinkedList; ! import java.util.List; ! import java.util.Iterator; ! import java.lang.StringBuffer; ! import java.util.Date; ! ! ! import net.sourceforge.jvalid.Validator; ! import net.sourceforge.jvalid.DataValidationException; ! ! ! /** ! * <p>Test class for the Java Validation Library.</p> ! * ! * @author Mike Williams ! */ ! public class ValidatorTest ! { ! ! /** ! * <p>Public interface. Used to run the example.</p> ! * ! * @param args Location of the buyShoes.xsd example configuration file. ! */ ! public static void main(String[] args) ! { ! if (args.length != 1) ! { ! System.out.println("Usage: java ValidatorTest [schema URL]"); ! return; ! } ! ! File schemaFile = null; ! Validator validator = null; ! ! try ! { ! // Create a valiator ! schemaFile = new File(args[0]); ! validator = Validator.getInstance(schemaFile.toURL()); ! } ! catch (IOException ioe) ! { ! System.out.println( "Unable to find contraints file " + args[0] + "." ); ! return; ! } ! ! ! StringBuffer errorBuffer = new StringBuffer( "Testing checkValidity()...\n" ); ! errorBuffer.append( "----------------------------------------\n" ); ! errorBuffer.append( "This test calls the checkValidity() method of the Validator class.\n\n" ); ! ! LinkedList errorList = new LinkedList(); ! ! ! // Create some data ! String badDate1 = "02/29/2001"; ! String badDate2 = "02/01/2001"; ! String badDate3 = "52/01/2001"; ! String dateNeeded = "02/28/2001"; ! ! String shoeSize = "13"; ! String width = "F"; ! String brand = "V-Form"; ! String numEyelets = "@2"; ! ! errorBuffer.append( "Test Data: \n" ) ! .append( "Date test 1 (bad) : " ).append( badDate1 ).append( "\n" ) ! .append( "Date test 2 (bad) : " ).append( badDate2 ).append( "\n" ) ! .append( "Date test 3 (bad) : " ).append( badDate3 ).append( "\n" ) ! .append( "Date test 4 (good) : " ).append( dateNeeded ).append( "\n" ) ! .append( "Shoe size (good) : " ).append( shoeSize ).append( "\n" ) ! .append( "Width (bad) : " ).append( width ).append( "\n" ) ! .append( "Brand (good) : " ).append( brand ).append( "\n" ) ! .append( "Number of Eyelets (bad) : " ).append( numEyelets ).append( "\n" ); ! ! ! // Validate the data. ! errorList.addAll( validator.checkValidity("dateNeeded", badDate1) ); ! errorList.addAll( validator.checkValidity("dateNeeded", badDate2) ); ! errorList.addAll( validator.checkValidity("dateNeeded", badDate3) ); ! errorList.addAll( validator.checkValidity("dateNeeded", dateNeeded) ); ! errorList.addAll( validator.checkValidity("shoeSize", shoeSize) ); ! errorList.addAll( validator.checkValidity("width", width) ); ! errorList.addAll( validator.checkValidity("brand", brand) ); ! errorList.addAll( validator.checkValidity("numEyelets", numEyelets) ); ! ! errorBuffer.append( "\nErrors: " ); ! ! Iterator i = errorList.iterator(); ! ! while ( i.hasNext() ) ! { ! errorBuffer.append( "\n" ) ! .append( i.next() ); ! } ! ! errorBuffer.append( "\n\nEnd checkValidity() test.\n" ) ! .append( "----------------------------------------\n\n" ); ! ! System.out.println( errorBuffer.toString() ); ! ! ! // Get ready for the next test. ! errorList.clear(); ! errorBuffer.delete( 0, errorBuffer.length() ); ! ! errorBuffer.append( "Testing getObject()...\n" ) ! .append( "----------------------------------------\n\n" ); ! ! errorBuffer.append( "Test Data: \n" ) ! .append( "Date test 1 (bad) : " ).append( badDate1 ).append( "\n" ) ! .append( " Expecting Date\n" ) ! .append( "Date test 2 (bad) : " ).append( badDate2 ).append( "\n" ) ! .append( " Expecting Date\n" ) ! .append( "Date test 3 (bad) : " ).append( badDate3 ).append( "\n" ) ! .append( " Expecting Date\n" ) ! .append( "Date test 4 (good) : " ).append( dateNeeded ).append( "\n" ) ! .append( " Expecting Date\n" ) ! .append( "Shoe size (good) : " ).append( shoeSize ).append( "\n" ) ! .append( " Expecting Integer\n" ) ! .append( "Width (bad) : " ).append( width ).append( "\n" ) ! .append( " Expecting String\n" ) ! .append( "Brand (good) : " ).append( brand ).append( "\n" ) ! .append( " Expecting String\n" ) ! .append( "Number of Eyelets (bad) : " ).append( numEyelets ).append( "\n" ) ! .append( " Expecting Integer\n" ) ! .append( "\n\nResults:" ); ! ! try ! { ! Date tempDate = (Date) validator.getObject( "dateNeeded", badDate1 ); ! errorBuffer.append( "\nTest Date 1: Got a date!" ); ! } ! catch ( DataValidationException dve ) ! { ! errorList.addAll( dve.getErrorList() ); ! } ! ! try ! { ! Date tempDate = (Date) validator.getObject( "dateNeeded", badDate2 ); ! errorBuffer.append( "\nTest Date 2: Got a date!" ); ! } ! catch ( DataValidationException dve ) ! { ! errorList.addAll( dve.getErrorList() ); ! } ! ! try ! { ! Date tempDate = (Date) validator.getObject( "dateNeeded", badDate3 ); ! errorBuffer.append( "\nTest Date 3: Got a date!" ); ! } ! catch ( DataValidationException dve ) ! { ! errorList.addAll( dve.getErrorList() ); ! } ! ! try ! { ! Date tempDate = (Date) validator.getObject( "dateNeeded", dateNeeded ); ! errorBuffer.append( "\nTest Date 4: Got a date!" ); ! } ! catch ( DataValidationException dve ) ! { ! errorList.addAll( dve.getErrorList() ); ! } ! ! try ! { ! Integer tempInt = (Integer) validator.getObject( "shoeSize", shoeSize ); ! errorBuffer.append( "\nShoe size: Got a Integer!" ); ! } ! catch ( DataValidationException dve ) ! { ! errorList.addAll( dve.getErrorList() ); ! } ! ! try ! { ! String tempInt = (String) validator.getObject( "width", width ); ! errorBuffer.append( "\nWidth: Got a String!" ); ! } ! catch ( DataValidationException dve ) ! { ! errorList.addAll( dve.getErrorList() ); ! } ! ! try ! { ! String tempString = (String) validator.getObject( "brand", brand ); ! errorBuffer.append( "\nBrand: Got a String!" ); ! } ! catch ( DataValidationException dve ) ! { ! errorList.addAll( dve.getErrorList() ); ! } ! ! try ! { ! Integer tempInt = (Integer) validator.getObject( "numEyelets", numEyelets ); ! errorBuffer.append( "\nNumber of Eyelets: Got a String!" ); ! } ! catch ( DataValidationException dve ) ! { ! errorList.addAll( dve.getErrorList() ); ! } ! ! errorBuffer.append( "\n\nErrors: " ); ! ! i = errorList.iterator(); ! ! while ( i.hasNext() ) ! { ! errorBuffer.append( "\n" ) ! .append( i.next() ); ! } ! ! errorBuffer.append( "\n\nEnd getObject() test.\n" ) ! .append( "----------------------------------------\n" ); ! ! System.out.println( errorBuffer.toString() ); ! } } |