Skip to content

Instantly share code, notes, and snippets.

@fourcube
Last active August 29, 2015 13:55
Show Gist options
  • Select an option

  • Save fourcube/8692248 to your computer and use it in GitHub Desktop.

Select an option

Save fourcube/8692248 to your computer and use it in GitHub Desktop.

Revisions

  1. fourcube revised this gist Jan 29, 2014. 1 changed file with 1 addition and 2 deletions.
    3 changes: 1 addition & 2 deletions WS2012_Mockito.java
    Original file line number Diff line number Diff line change
    @@ -20,8 +20,7 @@
    public class InterpreterImplTest {
    InterpreterImpl cut;
    @Mock Command c;
    @Rule
    ExpectedException exception = ExpectedException.none();
    @Rule ExpectedException exception = ExpectedException.none();

    @Before
    public void setUp() {
  2. fourcube revised this gist Jan 29, 2014. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion WS2012_Mockito.java
    Original file line number Diff line number Diff line change
    @@ -77,11 +77,13 @@ public void interactiveScriptShouldTerminateOnEOF() {
    cut.execute(new StringReader(statement), true);
    }

    @Test
    public void nonInteractiveScriptShouldTerminateOnEOF() {
    String invalidStatement = "EXEC foo bar <EOF>";
    cut.execute(new StringReader(invalidStatement), false);
    }

    @Test
    public void shouldNotCallExecuteWhenMatchesReturnsFalse() {
    String invalidStatement = "EXEC null;";
    when(c.matches(Arrays.asList("null"))).thenReturn(false);
    @@ -92,6 +94,7 @@ public void shouldNotCallExecuteWhenMatchesReturnsFalse() {
    verifyNoMoreInteractions(c);
    }

    @Test
    public void shouldCallExecuteWhenMatchesReturnsTrue() {
    String validStatement = "EXEC foo bar;";
    when(c.matches(Arrays.asList("foo", "bar"))).thenReturn(true);
    @@ -100,7 +103,6 @@ public void shouldCallExecuteWhenMatchesReturnsTrue() {

    verify(c,times(1)).matches(Arrays.asList("foo", "bar"));
    verify(c,times(1)).execute(Arrays.asList("foo", "bar"));
    verifyNoMoreInteractions(c);
    }

    // Example usage:
  3. fourcube renamed this gist Jan 29, 2014. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  4. fourcube created this gist Jan 29, 2014.
    116 changes: 116 additions & 0 deletions WS2012_Mockito
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,116 @@
    package svv;

    import org.junit.Before;
    import org.junit.Rule;
    import org.junit.Test;
    import org.junit.rules.ExpectedException;
    import org.junit.runner.RunWith;

    import static org.mockito.Mockito.*;

    import org.mockito.ArgumentMatcher;
    import org.mockito.Mock;
    import org.mockito.internal.matchers.VarargMatcher;
    import org.mockito.runners.MockitoJUnitRunner;

    import java.io.StringReader;
    import java.util.Arrays;

    @RunWith(MockitoJUnitRunner.class)
    public class InterpreterImplTest {
    InterpreterImpl cut;
    @Mock Command c;
    @Rule
    ExpectedException exception = ExpectedException.none();

    @Before
    public void setUp() {
    cut = new InterpreterImpl();
    cut.registerCommand("EXEC", c);
    }

    @Test
    public void shouldAcceptInteractiveTokenSequence() {
    String interactiveValidStatement = "EXEC foo bA0123456789_-r \";.$§%\"<EOF>";
    cut.execute(new StringReader(interactiveValidStatement), true);

    verify(c, times(1)).matches(Arrays.asList("foo", "bA0123456789_-r", ";.$§%"));
    verify(c, times(1)).execute(Arrays.asList("foo", "bA0123456789_-r", ";.$§%"));
    }

    @Test
    public void shouldAcceptNonInteractiveSequenceTerminator() {
    String nonInteractiveValidStatement = "EXEC foo bA0123456789_-r \";.$§%\";";
    cut.execute(new StringReader(nonInteractiveValidStatement), false);

    verify(c, times(1)).matches(Arrays.asList("foo", "bA0123456789_-r"));
    verify(c, times(1)).execute(Arrays.asList("foo", "bA0123456789_-r"));
    }

    @Test
    public void shouldNotAcceptNonEscapedSpecialCharacters() {
    String invalidStatement = "EXEC foo \n$%.;";

    exception.expect(SyntaxException.class);
    try {
    cut.execute(new StringReader(invalidStatement), false);
    } finally {
    verifyZeroInteractions(c);
    }
    }

    @Test
    public void shouldNotAcceptArgumentWithoutClosingQuotationMarks() {
    String invalidStatement = "EXEC foo \"$ ;";

    exception.expect(SyntaxException.class);
    try {
    cut.execute(new StringReader(invalidStatement), false);
    } finally {
    verifyZeroInteractions(c);
    }
    }

    @Test
    public void interactiveScriptShouldTerminateOnEOF() {
    String statement = "EXEC foo bar <EOF>";
    cut.execute(new StringReader(statement), true);
    }

    public void nonInteractiveScriptShouldTerminateOnEOF() {
    String invalidStatement = "EXEC foo bar <EOF>";
    cut.execute(new StringReader(invalidStatement), false);
    }

    public void shouldNotCallExecuteWhenMatchesReturnsFalse() {
    String invalidStatement = "EXEC null;";
    when(c.matches(Arrays.asList("null"))).thenReturn(false);

    cut.execute(new StringReader(invalidStatement), false);

    verify(c,times(1)).matches(Arrays.asList("null"));
    verifyNoMoreInteractions(c);
    }

    public void shouldCallExecuteWhenMatchesReturnsTrue() {
    String validStatement = "EXEC foo bar;";
    when(c.matches(Arrays.asList("foo", "bar"))).thenReturn(true);

    cut.execute(new StringReader(validStatement), false);

    verify(c,times(1)).matches(Arrays.asList("foo", "bar"));
    verify(c,times(1)).execute(Arrays.asList("foo", "bar"));
    verifyNoMoreInteractions(c);
    }

    // Example usage:
    // when(c).execute(argThat(new DerpMatcher<List<String>>()));

    public class DerpMatcher<T> extends ArgumentMatcher<T> implements VarargMatcher {

    @Override
    public boolean matches(Object argument) {
    return false;
    }
    }
    }