package com.skraylabs.poker;

import static org.hamcrest.CoreMatchers.allOf;
import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.MatcherAssert.assertThat;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;

public class ApplicationTest {

  /**
   * SUT: Application.
   */
  private Application app;

  /**
   * Error code returned from Application under test.
   */
  private int errorCode;

  /**
   * Output stream from Application.main() for test verification.
   */
  private ByteArrayOutputStream output;

  /**
   * Temporary reference to System.out
   */
  private PrintStream console;

  /**
   * Set up test fixture.
   */
  @Before
  public void setUp() throws Exception {
    app = new Application() {
      @Override
      public void exit(int errorCode) {
        ApplicationTest.this.errorCode = errorCode;
      }
    };
    output = new ByteArrayOutputStream();
    console = System.out;
    System.setOut(new PrintStream(output));
  }

  /**
   * Tear down test fixture.
   */
  @After
  public void tearDown() throws Exception {
    System.setOut(console);    
  }

  @Test
  public void testAbortForTooFewArguments() {
    // Exercise
    app.execute();
    // Verify
    String outputString = output.toString();
    assertThat(outputString, allOf(containsString(Application.MSG_TOO_FEW_ARGS),
        containsString(Application.MSG_USAGE)));
  }

  @Test
  public void testAbortForTooManyArguments() {
    // Exercise
    app.execute("1", "2");
    // Verify
    String outputString = output.toString();
    assertThat(outputString, allOf(containsString(Application.MSG_TOO_MANY_ARGS),
        containsString(Application.MSG_USAGE)));
  }

}

@@ -126,16 +126,65 @@
 String;%0A
+import static org.hamcrest.CoreMatchers.equalTo;%0A
 import s
@@ -1238,20 +1238,35 @@
 onsole);
+%0A
     
+errorCode = 0;
 %0A  %7D%0A%0A  
@@ -1537,24 +1537,93 @@
 G_USAGE)));%0A
+    assertThat(errorCode, equalTo(Application.ERROR_CODE_BAD_ARGS));%0A
   %7D%0A%0A  @Test
@@ -1846,32 +1846,32 @@
 TOO_MANY_ARGS),%0A
-
         contains
@@ -1899,19 +1899,88 @@
 G_USAGE)));%0A
+    assertThat(errorCode, equalTo(Application.ERROR_CODE_BAD_ARGS));%0A
   %7D%0A%0A%7D%0A

Add assertions for error code in “abort” tests.