|
| 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 | + |
| 18 | +package opennlp.tools.util; |
| 19 | + |
| 20 | +import org.junit.jupiter.api.Assertions; |
| 21 | +import org.junit.jupiter.api.Test; |
| 22 | + |
| 23 | +class LanguageCodeValidatorTest { |
| 24 | + |
| 25 | + @Test |
| 26 | + void testValidIso639_1Codes() { |
| 27 | + Assertions.assertTrue(LanguageCodeValidator.isValid("en")); |
| 28 | + Assertions.assertTrue(LanguageCodeValidator.isValid("de")); |
| 29 | + Assertions.assertTrue(LanguageCodeValidator.isValid("fr")); |
| 30 | + Assertions.assertTrue(LanguageCodeValidator.isValid("es")); |
| 31 | + Assertions.assertTrue(LanguageCodeValidator.isValid("pt")); |
| 32 | + Assertions.assertTrue(LanguageCodeValidator.isValid("it")); |
| 33 | + Assertions.assertTrue(LanguageCodeValidator.isValid("nl")); |
| 34 | + Assertions.assertTrue(LanguageCodeValidator.isValid("th")); |
| 35 | + Assertions.assertTrue(LanguageCodeValidator.isValid("ja")); |
| 36 | + Assertions.assertTrue(LanguageCodeValidator.isValid("pl")); |
| 37 | + } |
| 38 | + |
| 39 | + @Test |
| 40 | + void testValidIso639_3TerminologicalCodes() { |
| 41 | + Assertions.assertTrue(LanguageCodeValidator.isValid("eng")); |
| 42 | + Assertions.assertTrue(LanguageCodeValidator.isValid("deu")); |
| 43 | + Assertions.assertTrue(LanguageCodeValidator.isValid("fra")); |
| 44 | + Assertions.assertTrue(LanguageCodeValidator.isValid("spa")); |
| 45 | + Assertions.assertTrue(LanguageCodeValidator.isValid("por")); |
| 46 | + Assertions.assertTrue(LanguageCodeValidator.isValid("ita")); |
| 47 | + Assertions.assertTrue(LanguageCodeValidator.isValid("nld")); |
| 48 | + Assertions.assertTrue(LanguageCodeValidator.isValid("tha")); |
| 49 | + Assertions.assertTrue(LanguageCodeValidator.isValid("jpn")); |
| 50 | + Assertions.assertTrue(LanguageCodeValidator.isValid("pol")); |
| 51 | + } |
| 52 | + |
| 53 | + @Test |
| 54 | + void testValidIso639_2BibliographicCodes() { |
| 55 | + Assertions.assertTrue(LanguageCodeValidator.isValid("dut")); |
| 56 | + Assertions.assertTrue(LanguageCodeValidator.isValid("fre")); |
| 57 | + Assertions.assertTrue(LanguageCodeValidator.isValid("ger")); |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + void testUndeterminedCode() { |
| 62 | + Assertions.assertTrue(LanguageCodeValidator.isValid("und")); |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + void testSpecialCodes() { |
| 67 | + Assertions.assertTrue(LanguageCodeValidator.isValid("x-unspecified")); |
| 68 | + } |
| 69 | + |
| 70 | + @Test |
| 71 | + void testInvalidCodes() { |
| 72 | + Assertions.assertFalse(LanguageCodeValidator.isValid("")); |
| 73 | + Assertions.assertFalse(LanguageCodeValidator.isValid("xyz123")); |
| 74 | + Assertions.assertFalse(LanguageCodeValidator.isValid("invalid")); |
| 75 | + Assertions.assertFalse(LanguageCodeValidator.isValid("EN")); |
| 76 | + Assertions.assertFalse(LanguageCodeValidator.isValid("ENG")); |
| 77 | + Assertions.assertFalse(LanguageCodeValidator.isValid("123")); |
| 78 | + Assertions.assertFalse(LanguageCodeValidator.isValid("e")); |
| 79 | + Assertions.assertFalse(LanguageCodeValidator.isValid("en-US")); |
| 80 | + Assertions.assertFalse(LanguageCodeValidator.isValid("abcd")); |
| 81 | + } |
| 82 | + |
| 83 | + @Test |
| 84 | + void testInvalidTwoLetterCode() { |
| 85 | + Assertions.assertFalse(LanguageCodeValidator.isValid("xx")); |
| 86 | + Assertions.assertFalse(LanguageCodeValidator.isValid("zz")); |
| 87 | + } |
| 88 | + |
| 89 | + @Test |
| 90 | + void testNullCode() { |
| 91 | + Assertions.assertThrows(NullPointerException.class, |
| 92 | + () -> LanguageCodeValidator.isValid(null)); |
| 93 | + } |
| 94 | + |
| 95 | + @Test |
| 96 | + void testValidateThrowsForInvalidCode() { |
| 97 | + IllegalArgumentException ex = Assertions.assertThrows(IllegalArgumentException.class, |
| 98 | + () -> LanguageCodeValidator.validateLanguageCode("invalid_code")); |
| 99 | + Assertions.assertTrue(ex.getMessage().contains("invalid_code")); |
| 100 | + } |
| 101 | + |
| 102 | + @Test |
| 103 | + void testValidatePassesForValidCode() { |
| 104 | + Assertions.assertDoesNotThrow( |
| 105 | + () -> LanguageCodeValidator.validateLanguageCode("en")); |
| 106 | + Assertions.assertDoesNotThrow( |
| 107 | + () -> LanguageCodeValidator.validateLanguageCode("eng")); |
| 108 | + Assertions.assertDoesNotThrow( |
| 109 | + () -> LanguageCodeValidator.validateLanguageCode("dut")); |
| 110 | + Assertions.assertDoesNotThrow( |
| 111 | + () -> LanguageCodeValidator.validateLanguageCode("und")); |
| 112 | + Assertions.assertDoesNotThrow( |
| 113 | + () -> LanguageCodeValidator.validateLanguageCode("x-unspecified")); |
| 114 | + } |
| 115 | +} |
0 commit comments