• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2016 Network New Technologies Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.networknt.schema;
18 
19 import org.junit.jupiter.api.Test;
20 
21 import static com.networknt.schema.utils.StringChecker.isNumeric;
22 import static org.junit.jupiter.api.Assertions.assertFalse;
23 import static org.junit.jupiter.api.Assertions.assertTrue;
24 
25 public class StringCheckerTest {
26 
27     private static final String[] validNumericValues = {
28             "1", "-1", "1.1", "-1.1", "0E+1", "0E-1", "0E1", "-0E+1", "-0E-1", "-0E1", "0.1E+1", "0.1E-1", "0.1E1",
29             "-0.1E+1", "-0.1E-1", "-0.1E1", "10.1", "-10.1", "10E+1", "10E-1", "10E1", "-10E+1", "-10E-1", "-10E1",
30             "10.1E+1", "10.1E-1", "10.1E1", "-10.1E+1", "-10.1E-1", "-10.1E1", "1E+0", "1E-0", "1E0",
31             "1E00000000000000000000"
32     };
33     private static final String[] invalidNumericValues = {
34             "01.1", "1.", ".1", "0.1.1", "E1", "E+1", "E-1", ".E1", ".E+1", ".E-1", ".1E1", ".1E+1", ".1E-1", "1E-",
35             "1E+", "1E", "+", "-", "1a", "0.1a", "0E1a", "0E-1a", "1.0a", "1.0aE1"
36             //, "+0", "+1" // for backward compatibility, in violation of JSON spec
37     };
38 
39     @Test
testNumericValues()40     void testNumericValues() {
41         for (String validValue : validNumericValues) {
42             assertTrue(isNumeric(validValue), validValue);
43         }
44     }
45 
46     @Test
testNonNumericValues()47     void testNonNumericValues() {
48         for (String invalidValue : invalidNumericValues) {
49             assertFalse(isNumeric(invalidValue), invalidValue);
50         }
51     }
52 }
53