|
| 1 | +/* |
| 2 | + * |
| 3 | + * Credit Card Number |
| 4 | + * https://github.com/sualeh/credit_card_number |
| 5 | + * Copyright (c) 2014-2015, Sualeh Fatehi. |
| 6 | + * |
| 7 | + * This library is free software; you can redistribute it and/or modify it under the terms |
| 8 | + * of the GNU Lesser General Public License as published by the Free Software Foundation; |
| 9 | + * either version 2.1 of the License, or (at your option) any later version. |
| 10 | + * |
| 11 | + * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 12 | + * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 13 | + * See the GNU Lesser General Public License for more details. |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU Lesser General Public License along with this |
| 16 | + * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, |
| 17 | + * Boston, MA 02111-1307, USA. |
| 18 | + * |
| 19 | + */ |
| 20 | +package us.fatehi.test.creditcardnumber; |
| 21 | + |
| 22 | + |
| 23 | +import static org.junit.Assert.assertTrue; |
| 24 | + |
| 25 | +import org.junit.Test; |
| 26 | + |
| 27 | +import us.fatehi.creditcardnumber.CardBrand; |
| 28 | + |
| 29 | +public class CardBrandTest |
| 30 | +{ |
| 31 | + |
| 32 | + // https://www.paypalobjects.com/en_US/vhelp/paypalmanager_help/credit_card_numbers.htm |
| 33 | + @Test |
| 34 | + public void americanExpress() |
| 35 | + { |
| 36 | + final long[] longCardNumbers = { |
| 37 | + 378282246310005L, |
| 38 | + 371449635398431L, |
| 39 | + 378734493671000L |
| 40 | + }; |
| 41 | + for (final long longCardNumber: longCardNumbers) |
| 42 | + { |
| 43 | + test(longCardNumber, CardBrand.AmericanExpress); |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + // https://www.paypalobjects.com/en_US/vhelp/paypalmanager_help/credit_card_numbers.htm |
| 48 | + @Test |
| 49 | + public void dinersClub() |
| 50 | + { |
| 51 | + final long[] longCardNumbers = { |
| 52 | + 30569309025904L, |
| 53 | + 38520000023237L |
| 54 | + }; |
| 55 | + for (final long longCardNumber: longCardNumbers) |
| 56 | + { |
| 57 | + test(longCardNumber, CardBrand.DinersClub); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + // https://www.paypalobjects.com/en_US/vhelp/paypalmanager_help/credit_card_numbers.htm |
| 62 | + @Test |
| 63 | + public void discover() |
| 64 | + { |
| 65 | + final long[] longCardNumbers = { |
| 66 | + 6011000990139424L, |
| 67 | + 6011111111111117L |
| 68 | + }; |
| 69 | + for (final long longCardNumber: longCardNumbers) |
| 70 | + { |
| 71 | + test(longCardNumber, CardBrand.Discover); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + // https://www.paypalobjects.com/en_US/vhelp/paypalmanager_help/credit_card_numbers.htm |
| 76 | + @Test |
| 77 | + public void jcb() |
| 78 | + { |
| 79 | + final long[] longCardNumbers = { |
| 80 | + 3530111333300000L, |
| 81 | + 3566002020360505L |
| 82 | + }; |
| 83 | + for (final long longCardNumber: longCardNumbers) |
| 84 | + { |
| 85 | + test(longCardNumber, CardBrand.JCB); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + // https://www.paypalobjects.com/en_US/vhelp/paypalmanager_help/credit_card_numbers.htm |
| 90 | + @Test |
| 91 | + public void masterCard() |
| 92 | + { |
| 93 | + final long[] longCardNumbers = { |
| 94 | + 5555555555554444L, |
| 95 | + 5105105105105100L |
| 96 | + }; |
| 97 | + for (final long longCardNumber: longCardNumbers) |
| 98 | + { |
| 99 | + test(longCardNumber, CardBrand.MasterCard); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + @Test |
| 104 | + public void masterCard2Series_1negative() |
| 105 | + { |
| 106 | + final long longCardNumber = 2221000000000000L; |
| 107 | + for (long i = 1; i <= 10; i++) |
| 108 | + { |
| 109 | + test(longCardNumber - i, CardBrand.Unknown); |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + @Test |
| 114 | + public void masterCard2Series_1positive() |
| 115 | + { |
| 116 | + final long longCardNumber = 2221000000000000L; |
| 117 | + for (long i = 0; i <= 10; i++) |
| 118 | + { |
| 119 | + test(longCardNumber + i, CardBrand.MasterCard); |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + @Test |
| 124 | + public void masterCard2Series_2negative() |
| 125 | + { |
| 126 | + final long longCardNumber = 2720999999999999L; |
| 127 | + for (long i = 1; i <= 10; i++) |
| 128 | + { |
| 129 | + test(longCardNumber + i, CardBrand.Unknown); |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + @Test |
| 134 | + public void masterCard2Series_2positive() |
| 135 | + { |
| 136 | + final long longCardNumber = 2720999999999999L; |
| 137 | + for (long i = 0; i <= 10; i++) |
| 138 | + { |
| 139 | + test(longCardNumber - i, CardBrand.MasterCard); |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + // https://www.paypalobjects.com/en_US/vhelp/paypalmanager_help/credit_card_numbers.htm |
| 144 | + @Test |
| 145 | + public void visa() |
| 146 | + { |
| 147 | + final long[] longCardNumbers = { |
| 148 | + 4111111111111111L, |
| 149 | + 4012888888881881L, |
| 150 | + 4222222222222L |
| 151 | + }; |
| 152 | + for (final long longCardNumber: longCardNumbers) |
| 153 | + { |
| 154 | + test(longCardNumber, CardBrand.Visa); |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + private void test(final long longCardNumber, |
| 159 | + final CardBrand expectedCardBrand) |
| 160 | + { |
| 161 | + final String cardNumber = String.valueOf(longCardNumber); |
| 162 | + final CardBrand cardBrand = CardBrand.from(cardNumber); |
| 163 | + assertTrue(cardNumber, expectedCardBrand == cardBrand); |
| 164 | + } |
| 165 | + |
| 166 | +} |
0 commit comments