|
| 1 | +/* |
| 2 | + * The MIT License |
| 3 | + * |
| 4 | + * Copyright (c) 2010-2020 Jason Priem, Bruno P. Kinoshita |
| 5 | + * |
| 6 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | + * of this software and associated documentation files (the "Software"), to deal |
| 8 | + * in the Software without restriction, including without limitation the rights |
| 9 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | + * copies of the Software, and to permit persons to whom the Software is |
| 11 | + * furnished to do so, subject to the following conditions: |
| 12 | + * |
| 13 | + * The above copyright notice and this permission notice shall be included in |
| 14 | + * all copies or substantial portions of the Software. |
| 15 | + * |
| 16 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 22 | + * THE SOFTWARE. |
| 23 | + */ |
| 24 | +package com.tupilabs.human_name_parser; |
| 25 | + |
| 26 | +import static org.junit.Assert.assertEquals; |
| 27 | +import static org.junit.Assert.assertFalse; |
| 28 | +import static org.junit.Assert.assertTrue; |
| 29 | + |
| 30 | +import java.util.Arrays; |
| 31 | + |
| 32 | +import org.junit.Test; |
| 33 | + |
| 34 | +/** |
| 35 | + * Tests for the {@code HumanNameParserBuilder}. |
| 36 | + * @author kinow |
| 37 | + * |
| 38 | + */ |
| 39 | +public class BuilderTest { |
| 40 | + |
| 41 | + // suffixes |
| 42 | + |
| 43 | + @Test |
| 44 | + public void testDefaultSuffixes() { |
| 45 | + HumanNameParserBuilder builder = new HumanNameParserBuilder("john"); |
| 46 | + HumanNameParserParser parser = builder.build(); |
| 47 | + assertTrue(parser.suffixes.contains("senior")); |
| 48 | + assertFalse(parser.suffixes.contains("mage")); |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + public void testReplacingSuffixes() { |
| 53 | + HumanNameParserBuilder builder = new HumanNameParserBuilder("john"); |
| 54 | + HumanNameParserParser parser = builder.withSuffixes(Arrays.asList("mage")).build(); |
| 55 | + assertFalse(parser.suffixes.contains("senior")); |
| 56 | + assertTrue(parser.suffixes.contains("mage")); |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + public void testExtraSuffixes() { |
| 61 | + HumanNameParserBuilder builder = new HumanNameParserBuilder("john"); |
| 62 | + HumanNameParserParser parser = builder.withExtraSuffixes(Arrays.asList("mage")).build(); |
| 63 | + assertTrue(parser.suffixes.contains("senior")); |
| 64 | + assertTrue(parser.suffixes.contains("mage")); |
| 65 | + } |
| 66 | + |
| 67 | + // prefixes |
| 68 | + |
| 69 | + @Test |
| 70 | + public void testDefaultPrefixes() { |
| 71 | + HumanNameParserBuilder builder = new HumanNameParserBuilder("john"); |
| 72 | + HumanNameParserParser parser = builder.build(); |
| 73 | + assertTrue(parser.prefixes.contains("de la")); |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + public void testReplacingPrefixes() { |
| 78 | + HumanNameParserBuilder builder = new HumanNameParserBuilder("john"); |
| 79 | + HumanNameParserParser parser = builder.withPrefixes(Arrays.asList("sama")).build(); |
| 80 | + assertFalse(parser.prefixes.contains("de la")); |
| 81 | + assertTrue(parser.prefixes.contains("sama")); |
| 82 | + } |
| 83 | + |
| 84 | + @Test |
| 85 | + public void testExtraPrefixes() { |
| 86 | + HumanNameParserBuilder builder = new HumanNameParserBuilder("john"); |
| 87 | + HumanNameParserParser parser = builder.withExtraPrefixes(Arrays.asList("sama")).build(); |
| 88 | + assertTrue(parser.prefixes.contains("de la")); |
| 89 | + assertTrue(parser.prefixes.contains("sama")); |
| 90 | + } |
| 91 | + |
| 92 | + // postnominals |
| 93 | + |
| 94 | + @Test |
| 95 | + public void testDefaultPostnominals() { |
| 96 | + HumanNameParserBuilder builder = new HumanNameParserBuilder("john"); |
| 97 | + HumanNameParserParser parser = builder.build(); |
| 98 | + assertTrue(parser.postnominals.contains("phd")); |
| 99 | + } |
| 100 | + |
| 101 | + @Test |
| 102 | + public void testReplacingPostnominals() { |
| 103 | + HumanNameParserBuilder builder = new HumanNameParserBuilder("john"); |
| 104 | + // Au.D. is one of the examples from issue #10 on GitHub |
| 105 | + HumanNameParserParser parser = builder.withPostnominals(Arrays.asList("Au.D.")).build(); |
| 106 | + assertFalse(parser.postnominals.contains("phd")); |
| 107 | + assertTrue(parser.postnominals.contains("Au.D.")); |
| 108 | + } |
| 109 | + |
| 110 | + @Test |
| 111 | + public void testExtraPostnominals() { |
| 112 | + HumanNameParserBuilder builder = new HumanNameParserBuilder("john"); |
| 113 | + HumanNameParserParser parser = builder.withExtraPostnominals(Arrays.asList("Au.D.")).build(); |
| 114 | + assertTrue(parser.postnominals.contains("phd")); |
| 115 | + assertTrue(parser.postnominals.contains("Au.D.")); |
| 116 | + } |
| 117 | + |
| 118 | + // salutations |
| 119 | + |
| 120 | + @Test |
| 121 | + public void testDefaultSalutations() { |
| 122 | + HumanNameParserBuilder builder = new HumanNameParserBuilder("john"); |
| 123 | + HumanNameParserParser parser = builder.build(); |
| 124 | + assertTrue(parser.salutations.contains("judge")); |
| 125 | + } |
| 126 | + |
| 127 | + @Test |
| 128 | + public void testReplacingSalutations() { |
| 129 | + HumanNameParserBuilder builder = new HumanNameParserBuilder("john"); |
| 130 | + HumanNameParserParser parser = builder.withSalutations(Arrays.asList("sinho")).build(); |
| 131 | + assertFalse(parser.salutations.contains("judge")); |
| 132 | + assertTrue(parser.salutations.contains("sinho")); |
| 133 | + } |
| 134 | + |
| 135 | + @Test |
| 136 | + public void testExtraSalutations() { |
| 137 | + HumanNameParserBuilder builder = new HumanNameParserBuilder("john"); |
| 138 | + HumanNameParserParser parser = builder.withExtraSalutations(Arrays.asList("sinho")).build(); |
| 139 | + assertTrue(parser.salutations.contains("judge")); |
| 140 | + assertTrue(parser.salutations.contains("sinho")); |
| 141 | + } |
| 142 | + |
| 143 | + // validations |
| 144 | + |
| 145 | + @Test(expected = NullPointerException.class) |
| 146 | + public void testConstructorFailsWithNullString() { |
| 147 | + new HumanNameParserBuilder((String) null); |
| 148 | + } |
| 149 | + |
| 150 | + @Test(expected = NullPointerException.class) |
| 151 | + public void testConstructorFailsWithNullName() { |
| 152 | + new HumanNameParserBuilder((Name) null); |
| 153 | + } |
| 154 | + |
| 155 | + @Test(expected = NullPointerException.class) |
| 156 | + public void testFailsToBuildWithNullSalutations1() { |
| 157 | + new HumanNameParserBuilder("john").withSalutations(null).build(); |
| 158 | + } |
| 159 | + |
| 160 | + @Test(expected = NullPointerException.class) |
| 161 | + public void testFailsToBuildWithNullSalutations2() { |
| 162 | + new HumanNameParserBuilder("john").withExtraSalutations(null).build(); |
| 163 | + } |
| 164 | + |
| 165 | + @Test(expected = NullPointerException.class) |
| 166 | + public void testFailsToBuildWithNullPostnominals1() { |
| 167 | + new HumanNameParserBuilder("john").withPostnominals(null).build(); |
| 168 | + } |
| 169 | + |
| 170 | + @Test(expected = NullPointerException.class) |
| 171 | + public void testFailsToBuildWithNullPostnominals2() { |
| 172 | + new HumanNameParserBuilder("john").withExtraPostnominals(null).build(); |
| 173 | + } |
| 174 | + |
| 175 | + @Test(expected = NullPointerException.class) |
| 176 | + public void testFailsToBuildWithNullSuffixes1() { |
| 177 | + new HumanNameParserBuilder("john").withSuffixes(null).build(); |
| 178 | + } |
| 179 | + |
| 180 | + @Test(expected = NullPointerException.class) |
| 181 | + public void testFailsToBuildWithNullSuffixes2() { |
| 182 | + new HumanNameParserBuilder("john").withExtraSuffixes(null).build(); |
| 183 | + } |
| 184 | + |
| 185 | + @Test(expected = NullPointerException.class) |
| 186 | + public void testFailsToBuildWithNullPrefixes1() { |
| 187 | + new HumanNameParserBuilder("john").withPrefixes(null).build(); |
| 188 | + } |
| 189 | + |
| 190 | + @Test(expected = NullPointerException.class) |
| 191 | + public void testFailsToBuildWithNullPrefixes2() { |
| 192 | + new HumanNameParserBuilder("john").withExtraPrefixes(null).build(); |
| 193 | + } |
| 194 | + |
| 195 | + @Test |
| 196 | + public void testCreateWithStringOrName() { |
| 197 | + assertEquals("ramon", new HumanNameParserBuilder("don ramon valdez").withSalutations(Arrays.asList("don")).build().getFirst()); |
| 198 | + assertEquals("ramon", new HumanNameParserBuilder(new Name("don ramon valdez")).withSalutations(Arrays.asList("don")).build().getFirst()); |
| 199 | + } |
| 200 | +} |
0 commit comments