Advertisement
JeffGrigg

Refactoring after 2nd Test

Feb 8th, 2024
1,646
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 3.56 KB | None | 0 0
  1. import junit.framework.TestCase;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Arrays;
  5. import java.util.List;
  6. import java.util.stream.Collectors;
  7.  
  8. public class NBottlesOfBeerOnTheWallTest extends TestCase {
  9.  
  10.     private int _numberOfBottlesOfBeer;
  11.     private final List<String> _lines = new ArrayList<String>();
  12.  
  13.     public NBottlesOfBeerOnTheWallTest(final int numberOfBottlesOfBeer) {
  14.         _numberOfBottlesOfBeer = numberOfBottlesOfBeer;
  15.     }
  16.  
  17.     public String[] getLyrics() {
  18.         _lines.clear();
  19.         if (_numberOfBottlesOfBeer == 1) {   // [Yes, this condition is intentionally wrong.]
  20.             addLineSayingHowManyBottlesOfBeerAreOnTheWall();
  21.             describeRemovingOneBottleFromTheWall();
  22.             _lines.add("");
  23.         }
  24.         addLineSayingHowManyBottlesOfBeerAreOnTheWall();
  25.         describeRestockingTheBeerWall();
  26.         return _lines.toArray(new String[_lines.size()]);
  27.     }
  28.  
  29.     private void addLineSayingHowManyBottlesOfBeerAreOnTheWall() {
  30.         _lines.add(bottlesOfBeerOnTheWallText(true) + ", " + bottlesOfBeerText() + ".");
  31.     }
  32.  
  33.     private void describeRemovingOneBottleFromTheWall() {
  34.         --_numberOfBottlesOfBeer;
  35.         _lines.add("Take one down and pass it around, " + bottlesOfBeerOnTheWallText() + ".");
  36.     }
  37.  
  38.     private void describeRestockingTheBeerWall() {
  39.         _numberOfBottlesOfBeer = 99;
  40.         _lines.add("Go to the store and buy some more, " + bottlesOfBeerOnTheWallText() + ".");
  41.     }
  42.  
  43.     private String bottlesOfBeerOnTheWallText(final boolean... isStartOfSentence) {
  44.         return bottlesOfBeerText(isStartOfSentence) + " on the wall";
  45.     }
  46.  
  47.     private String bottlesOfBeerText(final boolean... isStartOfSentence) {
  48.  
  49.         switch (_numberOfBottlesOfBeer) {
  50.  
  51.             case 0:
  52.                 if (isStartOfSentence.length > 0) {
  53.                     return "No more bottles of beer";
  54.                 } else {
  55.                     return "no more bottles of beer";
  56.                 }
  57.  
  58.             case 1:
  59.                 return "1 bottle of beer";
  60.  
  61.             default:
  62.                 return "" + _numberOfBottlesOfBeer + " bottles of beer";
  63.         }
  64.     }
  65.  
  66.     /**
  67.      * JUnit test constructor.
  68.      */
  69.     public NBottlesOfBeerOnTheWallTest() {
  70.     }
  71.  
  72.     public void testZeroBottlesOfBeer() {
  73.         assertEqualsArrayValues(new String[]{
  74.                         "No more bottles of beer on the wall, no more bottles of beer.",
  75.                         "Go to the store and buy some more, 99 bottles of beer on the wall."},
  76.                 new NBottlesOfBeerOnTheWallTest(0).getLyrics());
  77.     }
  78.  
  79.     public void testOneBottleOfBeer() {
  80.         assertEqualsArrayValues(new String[]{
  81.                         "1 bottle of beer on the wall, 1 bottle of beer.",
  82.                         "Take one down and pass it around, no more bottles of beer on the wall.",
  83.                         "",
  84.                         "No more bottles of beer on the wall, no more bottles of beer.",
  85.                         "Go to the store and buy some more, 99 bottles of beer on the wall."
  86.                 },
  87.                 new NBottlesOfBeerOnTheWallTest(1).getLyrics());
  88.     }
  89.  
  90.     private static void assertEqualsArrayValues(final String[] expectedLines, final String[] actualLines) {
  91.         final var expectedAsString = Arrays.stream(expectedLines).collect(Collectors.joining(System.lineSeparator()));
  92.         final var actualAsString = Arrays.stream(actualLines).collect(Collectors.joining(System.lineSeparator()));
  93.         assertEquals(expectedAsString, actualAsString);
  94.     }
  95.  
  96. }
  97.  
Tags: 99 Bottles
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement