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 \";.$§%\""; 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 "; cut.execute(new StringReader(statement), true); } @Test public void nonInteractiveScriptShouldTerminateOnEOF() { String invalidStatement = "EXEC foo bar "; cut.execute(new StringReader(invalidStatement), false); } @Test 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); } @Test 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")); } // Example usage: // when(c).execute(argThat(new DerpMatcher>())); public class DerpMatcher extends ArgumentMatcher implements VarargMatcher { @Override public boolean matches(Object argument) { return false; } } }