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