001: /*
002: * $Header: /cvsroot/jvalidate/jvalidate-framework/jvalidate/src/test/java/nl/knowlogy/validation/test/AdressValidationTest.java,v 1.8 2007/04/20 12:53:18 roberthofstra Exp $
003: * $Revision: 1.8 $
004: * $Date: 2007/04/20 12:53:18 $
005: *
006: *
007: * Created on Oct 6, 2004
008: *
009: * All right reserved(c) 2004, Knowlogy
010: *
011: * Copyright 2004 - 2005 Knowlogy, the Netherlands. All rights reserved.
012: * All Knowlogy brand and product names are trademarks or registered trademarks
013: * of Knowlogy in the Netherlands and other countries.
014: *
015: * No part of this publication may be reproduced, transmitted, stored in a retrieval system,
016: * or translated into any human or computer language, in any form, or by any means, electronic,
017: * mechanical, magnetic, optical, chemical, manual, or otherwise,
018: * without the prior written permission of the copyright owner, Knowlogy.
019: *
020: */
021: package nl.knowlogy.validation.test;
022:
023: import junit.framework.TestCase;
024: import nl.knowlogy.validation.ErrorCodes;
025: import nl.knowlogy.validation.Messages;
026: import nl.knowlogy.validation.MessagesImpl;
027: import nl.knowlogy.validation.ValidationEngine;
028:
029: /**
030: *
031: * @author Robert
032: */
033: public class AdressValidationTest extends TestCase {
034: // public void testAdressValidation(){
035: //
036: // Address adress = new Address();
037: // Messages messages = new MessagesImpl();
038: // adress.validate(messages);
039: //
040: // assertNotNull(messages.getMessage(adress,"city"));
041: // assertNotNull(messages.getMessage(adress,"houseNumber"));
042: // assertNotNull(messages.getMessage(adress,"street"));
043: // assertNotNull(messages.getMessage(adress,"typeAdres"));
044: // assertNotNull(messages.getMessage(adress,"zipCode"));
045: //
046: // adress.setZipCode("1212AH");
047: // messages = new MessagesImpl();
048: // adress.validate(messages);
049: // assertNull(messages.getMessage(adress,"zipCode"));
050: // System.out.println(messages.toString());
051: //
052: // }
053:
054: public void testCustomer() {
055: Customer customer = new Customer();
056: customer.setFirstName("Robert");
057:
058: Address shippingAdress = new Address();
059: customer.setShippingAdress(shippingAdress);
060: customer.getShippingAdress().setCity("Groningen");
061:
062: Address homeAdress = new Address();
063: customer.setHomeAdress(homeAdress);
064: customer.getHomeAdress().setHouseNumber(new Integer(19));
065:
066: Messages messages = new MessagesImpl();
067: ValidationEngine.validate(customer, messages);
068:
069: assertNull(messages.getMessage(customer.getHomeAdress(),
070: "houseNumber"));
071: assertNotNull(messages.getMessage(customer.getHomeAdress(),
072: "street"));
073: assertNotNull(messages.getMessage(customer.getHomeAdress(),
074: "typeAdres"));
075: assertNotNull(messages.getMessage(customer.getHomeAdress(),
076: "city"));
077: assertEquals(ErrorCodes.ISREQUIRED_VALUE, messages.getMessage(
078: customer.getHomeAdress(), "city").getMessageCode());
079:
080: assertNotNull(messages.getMessage(customer.getShippingAdress(),
081: "houseNumber"));
082: assertNotNull(messages.getMessage(customer.getShippingAdress(),
083: "street"));
084: assertNotNull(messages.getMessage(customer.getShippingAdress(),
085: "typeAdres"));
086: assertNull(messages.getMessage(customer.getShippingAdress(),
087: "city"));
088:
089: }
090:
091: public void testCustomerGroupValidation() {
092: Customer customer = new Customer();
093:
094: customer.setCreditcardNumber("134234");
095:
096: Messages errors = new MessagesImpl();
097: ValidationEngine.validate(customer, errors);
098: assertNotNull(errors.getMessage(customer, "creditcardNumber"));
099:
100: errors = new MessagesImpl();
101: ValidationEngine.validateGroup(customer, "names", errors);
102: assertNotNull(errors.getMessage(customer, "firstName"));
103: assertNotNull(errors.getMessage(customer, "lastName"));
104:
105: customer.setCreditcardNumber("4111111111111111");
106: errors = new MessagesImpl();
107: ValidationEngine.validate(customer, errors);
108: assertNull(errors.getMessage(customer, "creditcardNumber"));
109:
110: }
111:
112: public void testShopValidation() {
113:
114: Shop programmersHeavenShop = new Shop();
115: programmersHeavenShop.setName("programmers heaven");
116:
117: Customer robert = new Customer();
118: robert.setFirstName("Robert");
119: robert.setLastName("Hofstra");
120: robert.setHomeAdress(new Address());
121: robert.getHomeAdress().setStreet("Diephuisstraat");
122: robert.getHomeAdress().setHouseNumber(new Integer(38));
123: robert.getHomeAdress().setCity("Groningen");
124: robert.getHomeAdress().setZipCode("9581JK");
125:
126: programmersHeavenShop.addCustomer(robert);
127:
128: Customer harry = new Customer();
129: harry.setHomeAdress(new Address());
130:
131: programmersHeavenShop.addCustomer(harry);
132:
133: MessagesImpl errors = new MessagesImpl();
134:
135: programmersHeavenShop.validate(errors);
136:
137: assertNull(errors.getMessage(programmersHeavenShop
138: .getCustomers()[0].getHomeAdress(), "city"));
139: assertNotNull(errors.getMessage(programmersHeavenShop
140: .getCustomers()[1].getHomeAdress(), "city"));
141:
142: }
143:
144: public void testCustomerAdressReferenceValidation() {
145: Customer robert = new Customer();
146: robert.setFirstName("Robert");
147: robert.setLastName("Hofstra");
148:
149: MessagesImpl messages = new MessagesImpl();
150: ValidationEngine.validate(robert, messages);
151:
152: assertNotNull(messages.getMessage(robert, "homeAdress"));
153:
154: }
155:
156: public void testCustomerPropertyValidation() {
157: MessagesImpl messages = new MessagesImpl();
158: ValidationEngine.validateProperty(Customer.class, "firstName",
159: null, messages);
160: assertNotNull(messages.getMessage(null, "firstName"));
161:
162: }
163:
164: public void testInvalidAddresWithValidatorFactory() {
165: Address address = new Address();
166: Messages messages = new MessagesImpl();
167: ValidationEngine.validate(address, messages);
168:
169: assertNotNull(messages.getMessage(address, "street"));
170: }
171:
172: }
|