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    *      https://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.routines.checkdigit;
18  
19  import org.junit.jupiter.api.BeforeEach;
20  
21  /**
22   * CAS Number Check Digit Tests.
23   */
24  class CASNumberCheckDigitTest extends AbstractCheckDigitTest {
25  
26      private static final String MIN = "00-01-1"; // theoretical
27      private static final String WATER = "7732-18-5";
28      private static final String ETHANOL = "64-17-5";
29      private static final String ASPIRIN = "50-78-2";
30      private static final String COFFEIN = "58-08-2";
31      private static final String FORMALDEHYDE = "50-00-0";
32      private static final String DEXAMETHASONE = "50-02-2";
33      private static final String ARSENIC = "7440-38-2";
34      private static final String ASBESTOS = "1332-21-4";
35      private static final String MAX = "9999999-99-5"; // theoretical
36  
37      /**
38       * {@inheritDoc}
39       */
40      @Override
41      protected String removeCheckDigit(final String code) {
42          final String cde = (String) CASNumberCheckDigit.REGEX_VALIDATOR.validate(code);
43          if (cde == null || cde.length() <= checkDigitLth) {
44              return null;
45          }
46          return cde.substring(0, cde.length() - checkDigitLth);
47      }
48  
49      /**
50       * Sets up routine & valid codes.
51       */
52      @BeforeEach
53      protected void setUp() {
54          routine = CASNumberCheckDigit.getInstance();
55          valid = new String[] {MIN, WATER, ETHANOL, ASPIRIN, COFFEIN, FORMALDEHYDE, DEXAMETHASONE, ARSENIC, ASBESTOS, MAX};
56      }
57  
58  }