1 /* 2 * $Header: /home/cvs/jakarta-commons/validator/src/test/org/apache/commons/validator/CreditCardValidatorTest.java,v 1.6 2004/02/21 17:10:30 rleland Exp $ 3 * $Revision: 1.6 $ 4 * $Date: 2004/02/21 17:10:30 $ 5 * 6 * ==================================================================== 7 * Copyright 2001-2004 The Apache Software Foundation 8 * 9 * Licensed under the Apache License, Version 2.0 (the "License"); 10 * you may not use this file except in compliance with the License. 11 * You may obtain a copy of the License at 12 * 13 * http://www.apache.org/licenses/LICENSE-2.0 14 * 15 * Unless required by applicable law or agreed to in writing, software 16 * distributed under the License is distributed on an "AS IS" BASIS, 17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 * See the License for the specific language governing permissions and 19 * limitations under the License. 20 */ 21 22 package org.apache.commons.validator; 23 24 import junit.framework.TestCase; 25 26 /*** 27 * Test the CreditCardValidator class. 28 */ 29 public class CreditCardValidatorTest extends TestCase { 30 31 private static final String VALID_VISA = "4417123456789113"; 32 private static final String VALID_SHORT_VISA = "4222222222222"; 33 private static final String VALID_AMEX = "378282246310005"; 34 private static final String VALID_MASTERCARD = "5105105105105100"; 35 private static final String VALID_DISCOVER = "6011000990139424"; 36 private static final String VALID_DINERS = "30569309025904"; 37 38 /*** 39 * Constructor for CreditCardValidatorTest. 40 */ 41 public CreditCardValidatorTest(String name) { 42 super(name); 43 } 44 45 public void testIsValid() { 46 CreditCardValidator ccv = new CreditCardValidator(); 47 48 assertFalse(ccv.isValid(null)); 49 assertFalse(ccv.isValid("")); 50 assertFalse(ccv.isValid("123456789012")); // too short 51 assertFalse(ccv.isValid("12345678901234567890")); // too long 52 assertFalse(ccv.isValid("4417123456789112")); 53 assertFalse(ccv.isValid("4417q23456w89113")); 54 assertTrue(ccv.isValid(VALID_VISA)); 55 assertTrue(ccv.isValid(VALID_SHORT_VISA)); 56 assertTrue(ccv.isValid(VALID_AMEX)); 57 assertTrue(ccv.isValid(VALID_MASTERCARD)); 58 assertTrue(ccv.isValid(VALID_DISCOVER)); 59 60 // disallow Visa so it should fail even with good number 61 ccv = new CreditCardValidator(CreditCardValidator.AMEX); 62 assertFalse(ccv.isValid("4417123456789113")); 63 } 64 65 public void testAddAllowedCardType() { 66 CreditCardValidator ccv = new CreditCardValidator(CreditCardValidator.NONE); 67 // Turned off all cards so even valid numbers should fail 68 assertFalse(ccv.isValid(VALID_VISA)); 69 assertFalse(ccv.isValid(VALID_AMEX)); 70 assertFalse(ccv.isValid(VALID_MASTERCARD)); 71 assertFalse(ccv.isValid(VALID_DISCOVER)); 72 73 // test our custom type 74 ccv.addAllowedCardType(new DinersClub()); 75 assertTrue(ccv.isValid(VALID_DINERS)); 76 } 77 78 /*** 79 * Test a custom implementation of CreditCardType. 80 */ 81 private class DinersClub implements CreditCardValidator.CreditCardType { 82 private static final String PREFIX = "300,301,302,303,304,305,"; 83 public boolean matches(String card) { 84 String prefix = card.substring(0, 3) + ","; 85 return ((PREFIX.indexOf(prefix) != -1) && (card.length() == 14)); 86 } 87 } 88 89 }

This page was automatically generated by Maven