1   /*
2    * $Id: CreditCardValidatorTest.java 232646 2005-08-14 21:19:39Z mrdon $
3    * $Rev: 232646 $
4    * $Date: 2005-08-14 22:19:39 +0100 (Sun, 14 Aug 2005) $
5    *
6    * ====================================================================
7    * Copyright 2001-2005 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_VISA_OTHER = "5817550003933609";
33      private static final String VALID_SHORT_VISA = "4222222222222";
34      private static final String VALID_AMEX = "378282246310005";
35      private static final String VALID_MASTERCARD = "5105105105105100";
36      private static final String VALID_DISCOVER = "6011000990139424";
37      private static final String VALID_DINERS = "30569309025904";
38  
39  	/***
40  	 * Constructor for CreditCardValidatorTest.
41  	 */
42  	public CreditCardValidatorTest(String name) {
43  		super(name);
44  	}
45  
46  	public void testIsValid() {
47          CreditCardValidator ccv = new CreditCardValidator();
48          
49          assertFalse(ccv.isValid(null));
50          assertFalse(ccv.isValid(""));
51          assertFalse(ccv.isValid("123456789012"));   // too short
52          assertFalse(ccv.isValid("12345678901234567890"));   // too long
53          assertFalse(ccv.isValid("4417123456789112"));
54          assertFalse(ccv.isValid("4417q23456w89113"));
55          assertTrue(ccv.isValid(VALID_VISA));
56          assertTrue(ccv.isValid(VALID_VISA_OTHER));
57          assertTrue(ccv.isValid(VALID_SHORT_VISA));
58          assertTrue(ccv.isValid(VALID_AMEX));
59          assertTrue(ccv.isValid(VALID_MASTERCARD));
60          assertTrue(ccv.isValid(VALID_DISCOVER));
61          
62          // disallow Visa so it should fail even with good number
63          ccv = new CreditCardValidator(CreditCardValidator.AMEX);
64          assertFalse(ccv.isValid("4417123456789113"));
65  	}
66      
67      public void testAddAllowedCardType() {
68          CreditCardValidator ccv = new CreditCardValidator(CreditCardValidator.NONE);
69          // Turned off all cards so even valid numbers should fail
70          assertFalse(ccv.isValid(VALID_VISA));
71          assertFalse(ccv.isValid(VALID_AMEX));
72          assertFalse(ccv.isValid(VALID_MASTERCARD));
73          assertFalse(ccv.isValid(VALID_DISCOVER));
74          
75          // test our custom type
76          ccv.addAllowedCardType(new DinersClub());
77          assertTrue(ccv.isValid(VALID_DINERS));
78      }
79      
80      /***
81       * Test a custom implementation of CreditCardType.
82       */
83      private class DinersClub implements CreditCardValidator.CreditCardType {
84          private static final String PREFIX = "300,301,302,303,304,305,";
85          public boolean matches(String card) {
86              String prefix = card.substring(0, 3) + ",";
87              return ((PREFIX.indexOf(prefix) != -1) && (card.length() == 14));
88          }
89      }
90  
91  }