implement a whole bunch of simple methods.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. import static org.junit.Assert.*;
  2. import org.junit.After;
  3. import org.junit.Before;
  4. import org.junit.Test;
  5. /**
  6. * The test class StringUtilitiesTest.
  7. *
  8. * @author (your name)
  9. * @version (a version number or a date)
  10. */
  11. public class StringUtilitiesTest {
  12. private StringUtilities stringUtilities;
  13. /**
  14. * Sets up the test fixture.
  15. *
  16. * Called before every test case method.
  17. */
  18. @Before
  19. public void setUp()
  20. {
  21. stringUtilities = new StringUtilities();
  22. }
  23. /**
  24. * Tears down the test fixture.
  25. *
  26. * Called after every test case method.
  27. */
  28. @After
  29. public void tearDown()
  30. {
  31. }
  32. @Test
  33. public void getHelloWorldTest() {
  34. // : Given
  35. String expected = "Hello World";
  36. // : When
  37. String actual = stringUtilities.getHelloWorld();
  38. // : Then
  39. assertEquals(expected, actual);
  40. }
  41. @Test
  42. public void concatenationStringTest(){
  43. // : Given
  44. String one = "Hello";
  45. String two = " Java";
  46. String expected = "Hello Java";
  47. // : When
  48. String actual = stringUtilities.concatenation(one,two);
  49. // : Then
  50. assertEquals(expected, actual);
  51. }
  52. @Test
  53. public void concatenationStringAndIntegerTest(){
  54. // : Given
  55. int one = 1;
  56. String two = " Java";
  57. String expected = "1 Java";
  58. // : When
  59. String actual = stringUtilities.concatenation(one,two);
  60. // : Then
  61. assertEquals(expected, actual);
  62. }
  63. @Test
  64. public void substringBeginTest(){
  65. // : Given
  66. String input = "Hello";
  67. String expected = "Hel";
  68. // : When
  69. String actual = stringUtilities.getPrefix(input);
  70. // : Then
  71. assertEquals(expected, actual);
  72. }
  73. @Test
  74. public void substringEndTest(){
  75. // : Given
  76. String input = "Hello";
  77. String expected = "llo";
  78. // : When
  79. String actual = stringUtilities.getSuffix("Hello");
  80. // : Then
  81. assertEquals(expected, actual);
  82. }
  83. @Test
  84. public void compareToTestEquals(){
  85. // : Given
  86. String inputValue = "Zipcode";
  87. String comparableValue = "Zipcode";
  88. // : When
  89. boolean actual = stringUtilities.compareTwoStrings(inputValue, comparableValue);
  90. // : Then
  91. assertTrue(actual);
  92. }
  93. @Test
  94. public void compareToTestNotEquals(){
  95. // : Given
  96. String inputValue = "Zipcode";
  97. String comparableValue = "Zipcodee";
  98. // : When
  99. boolean actual = stringUtilities.compareTwoStrings(inputValue, comparableValue);
  100. // : Then
  101. assertFalse(actual);
  102. }
  103. @Test
  104. public void getTheMiddleChar1(){
  105. // : Given
  106. String input = "Zipcode";
  107. Character expected = 'c';
  108. // : When
  109. Character actual = stringUtilities.getMiddleCharacter(input);
  110. // : Then
  111. assertEquals(expected.toString(), actual.toString());
  112. }
  113. @Test
  114. public void getTheMiddleChar2(){
  115. // : Given
  116. String input = "Zipcoder";
  117. Character expected = 'c';
  118. // : When
  119. Character actual = stringUtilities.getMiddleCharacter(input);
  120. // : Then
  121. assertEquals(expected.toString(), actual.toString());
  122. }
  123. @Test
  124. public void getTheFirstWord(){
  125. // : Given
  126. String input = "Zipcode Wilmington";
  127. String expected = "Zipcode";
  128. // : When
  129. String actual = stringUtilities.getFirstWord(input);
  130. // : Then
  131. assertEquals(expected, actual);
  132. }
  133. @Test
  134. public void getTheSecondWord(){
  135. // : Given
  136. String input = "Zipcode Wilmington";
  137. String expected = "Wilmington";
  138. // : When
  139. String actual = stringUtilities.getSecondWord(input);
  140. // : Then
  141. assertEquals(expected, actual);
  142. }
  143. @Test
  144. public void reverseThem(){
  145. // : Given
  146. String input = "Zipcode Wilmington";
  147. String expected = "notgnimliW edocpiZ";
  148. // : When
  149. String actual = stringUtilities.reverse(input);
  150. // : Then
  151. assertEquals(expected, actual);
  152. }
  153. @Test
  154. public void removeWhitespace(){
  155. // : Given
  156. String input = "It's a beautiful day in this neighborhood";
  157. String expected = "It'sabeautifuldayinthisneighborhood";
  158. // : When
  159. String actual = stringUtilities.removeWhitespace(input);
  160. // : Then
  161. assertEquals(expected, actual);
  162. }
  163. @Test
  164. public void trimWhitespaceInTheBegginningAndEnd(){
  165. // : Given
  166. String input = " Zipcode Wilmington ";
  167. String expected = "Zipcode Wilmington";
  168. // : When
  169. String actual = stringUtilities.trim(input);
  170. // : Then
  171. assertEquals(expected, actual);
  172. }
  173. }