|
| 1 | +/** |
| 2 | + * |
| 3 | + */ |
| 4 | +package com.baeldung.string; |
| 5 | + |
| 6 | +import static org.junit.Assert.assertEquals; |
| 7 | + |
| 8 | +import org.junit.Test; |
| 9 | + |
| 10 | +/** |
| 11 | + * @author swpraman |
| 12 | + * |
| 13 | + */ |
| 14 | +public class AppendCharAtPositionXUnitTest { |
| 15 | + |
| 16 | + private AppendCharAtPositionX appendCharAtPosition = new AppendCharAtPositionX(); |
| 17 | + private String word = "Titanc"; |
| 18 | + private char letter = 'i'; |
| 19 | + |
| 20 | + @Test |
| 21 | + public void whenUsingCharacterArrayAndCharacterAddedAtBeginning_shouldAddCharacter() { |
| 22 | + assertEquals("iTitanc", appendCharAtPosition.addCharUsingCharArray(word, letter, 0)); |
| 23 | + } |
| 24 | + |
| 25 | + @Test |
| 26 | + public void whenUsingSubstringAndCharacterAddedAtBeginning_shouldAddCharacter() { |
| 27 | + assertEquals("iTitanc", appendCharAtPosition.addCharUsingSubstring(word, letter, 0)); |
| 28 | + } |
| 29 | + |
| 30 | + @Test |
| 31 | + public void whenUsingStringBuilderAndCharacterAddedAtBeginning_shouldAddCharacter() { |
| 32 | + assertEquals("iTitanc", appendCharAtPosition.addCharUsingStringBuilder(word, letter, 0)); |
| 33 | + } |
| 34 | + |
| 35 | + @Test |
| 36 | + public void whenUsingCharacterArrayAndCharacterAddedAtMiddle_shouldAddCharacter() { |
| 37 | + assertEquals("Titianc", appendCharAtPosition.addCharUsingCharArray(word, letter, 3)); |
| 38 | + } |
| 39 | + |
| 40 | + @Test |
| 41 | + public void whenUsingSubstringAndCharacterAddedAtMiddle_shouldAddCharacter() { |
| 42 | + assertEquals("Titianc", appendCharAtPosition.addCharUsingSubstring(word, letter, 3)); |
| 43 | + } |
| 44 | + |
| 45 | + @Test |
| 46 | + public void whenUsingStringBuilderAndCharacterAddedAtMiddle_shouldAddCharacter() { |
| 47 | + assertEquals("Titianc", appendCharAtPosition.addCharUsingStringBuilder(word, letter, 3)); |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + public void whenUsingCharacterArrayAndCharacterAddedAtEnd_shouldAddCharacter() { |
| 52 | + assertEquals("Titanci", appendCharAtPosition.addCharUsingCharArray(word, letter, word.length())); |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + public void whenUsingSubstringAndCharacterAddedAtEnd_shouldAddCharacter() { |
| 57 | + assertEquals("Titanci", appendCharAtPosition.addCharUsingSubstring(word, letter, word.length())); |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + public void whenUsingStringBuilderAndCharacterAddedAtEnd_shouldAddCharacter() { |
| 62 | + assertEquals("Titanci", appendCharAtPosition.addCharUsingStringBuilder(word, letter, word.length())); |
| 63 | + } |
| 64 | + |
| 65 | + @Test(expected=IllegalArgumentException.class) |
| 66 | + public void whenUsingCharacterArrayAndCharacterAddedAtNegativePosition_shouldThrowException() { |
| 67 | + appendCharAtPosition.addCharUsingStringBuilder(word, letter, -1); |
| 68 | + } |
| 69 | + |
| 70 | + @Test(expected=IllegalArgumentException.class) |
| 71 | + public void whenUsingSubstringAndCharacterAddedAtNegativePosition_shouldThrowException() { |
| 72 | + appendCharAtPosition.addCharUsingStringBuilder(word, letter, -1); |
| 73 | + } |
| 74 | + |
| 75 | + @Test(expected=IllegalArgumentException.class) |
| 76 | + public void whenUsingStringBuilderAndCharacterAddedAtNegativePosition_shouldThrowException() { |
| 77 | + appendCharAtPosition.addCharUsingStringBuilder(word, letter, -1); |
| 78 | + } |
| 79 | + |
| 80 | + @Test(expected=IllegalArgumentException.class) |
| 81 | + public void whenUsingCharacterArrayAndCharacterAddedAtInvalidPosition_shouldThrowException() { |
| 82 | + appendCharAtPosition.addCharUsingStringBuilder(word, letter, word.length() + 2); |
| 83 | + } |
| 84 | + |
| 85 | + @Test(expected=IllegalArgumentException.class) |
| 86 | + public void whenUsingSubstringAndCharacterAddedAtInvalidPosition_shouldThrowException() { |
| 87 | + appendCharAtPosition.addCharUsingStringBuilder(word, letter, word.length() + 2); |
| 88 | + } |
| 89 | + |
| 90 | + @Test(expected=IllegalArgumentException.class) |
| 91 | + public void whenUsingStringBuilderAndCharacterAddedAtInvalidPosition_shouldThrowException() { |
| 92 | + appendCharAtPosition.addCharUsingStringBuilder(word, letter, word.length() + 2); |
| 93 | + } |
| 94 | + |
| 95 | + @Test(expected=IllegalArgumentException.class) |
| 96 | + public void whenUsingCharacterArrayAndCharacterAddedAtPositionXAndStringIsNull_shouldThrowException() { |
| 97 | + appendCharAtPosition.addCharUsingStringBuilder(null, letter, 3); |
| 98 | + } |
| 99 | + |
| 100 | + @Test(expected=IllegalArgumentException.class) |
| 101 | + public void whenUsingSubstringAndCharacterAddedAtPositionXAndStringIsNull_shouldThrowException() { |
| 102 | + appendCharAtPosition.addCharUsingStringBuilder(null, letter, 3); |
| 103 | + } |
| 104 | + |
| 105 | + @Test(expected=IllegalArgumentException.class) |
| 106 | + public void whenUsingStringBuilderAndCharacterAddedAtPositionXAndStringIsNull_shouldThrowException() { |
| 107 | + appendCharAtPosition.addCharUsingStringBuilder(null, letter, 3); |
| 108 | + } |
| 109 | + |
| 110 | +} |
0 commit comments