Last active
January 4, 2016 08:29
-
-
Save fourcube/8595469 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* To change this license header, choose License Headers in Project Properties. | |
* To change this template file, choose Tools | Templates | |
* and open the template in the editor. | |
*/ | |
package de.grieger.ws11_mockito; | |
import java.io.Reader; | |
import java.util.Map; | |
import java.util.regex.Pattern; | |
import org.junit.Before; | |
import org.junit.Test; | |
import org.junit.runner.RunWith; | |
import org.mockito.ArgumentMatcher; | |
import org.mockito.BDDMockito; | |
import org.mockito.Mock; | |
import org.mockito.internal.matchers.VarargMatcher; | |
import org.mockito.runners.MockitoJUnitRunner; | |
import static org.hamcrest.CoreMatchers.is; | |
import static org.junit.Assert.assertThat; | |
import static org.mockito.Mockito.*; | |
import static org.mockito.AdditionalMatchers.*; | |
/** | |
* | |
* @author Chris | |
*/ | |
@RunWith(MockitoJUnitRunner.class) | |
public class CommandInterpreterTest { | |
@Mock Map<String, Command> commandMap; | |
@Mock private Command command; | |
@Mock private Command someCommand; | |
@Mock private Command invalidCommand; | |
private InterpreterImpl interpreter = new InterpreterImpl(commandMap); | |
@Mock private Reader reader; | |
@Mock private Reader someCommandReader; | |
@Mock private Reader exitCommandReader; | |
@Mock private Reader invalidCommandReader; | |
private final Boolean INTERACTIVE = true; | |
private final Boolean NOT_INTERACTIVE = !INTERACTIVE; | |
private final String argumentPattern = "([^\\\\\";]|(?:\"[\\w\\s;\\.]+\"))+"; | |
private final String statementPattern = "[\\w]+" + argumentPattern + ";"; // simple argument | |
@Before | |
public void setUp() throws InterpreterException { | |
// Setup mock behaviour | |
when(command.matches((String[]) argThat(new StringVarargMatcher<>(argumentPattern)))).thenReturn(Boolean.TRUE); | |
when(command.matches((String[]) not(argThat(new StringVarargMatcher<>(argumentPattern))))).thenThrow(NonMatchingArgException.class); | |
when(command.matches()).thenThrow(NonMatchingArgException.class); | |
when(someCommand.matches("a Weird \" argument", "and", "more arguments")).thenReturn(Boolean.FALSE); | |
when(someCommand.matches("a Weird argument", "and", "more arguments")).thenReturn(Boolean.TRUE); | |
doThrow(NonMatchingArgException.class).when(invalidCommand).matches((String[]) anyVararg()); | |
// Configuration Map | |
when(commandMap.get("someCommand")).thenReturn(someCommand); | |
} | |
@Test | |
public void intepreterShouldOnlyCallGetMethodOfConfigurationMap() throws InterpreterException { | |
interpreter.executeCommands(reader, true); | |
verify(commandMap,times(1)).get("someCommand"); | |
verifyNoMoreInteractions(commandMap); | |
} | |
@Test | |
public void interpreterShouldImmediatelyExecuteValidCommand () throws InterpreterException { | |
interpreter.executeCommands(reader, false); | |
verify(command, times(1)).matches((String[]) anyVararg()); | |
verify(command, times(1)).execute((String[]) anyVararg()); | |
} | |
@Test | |
public void interpreterShouldPreprocessStatementAndExecuteIt() throws InterpreterException { | |
interpreter.executeCommands(someCommandReader, true); | |
verify(someCommand, times(1)).matches("a Weird \" argument", "and", "more arguments"); | |
verify(someCommand, times(0)).execute((String[]) anyVararg()); | |
} | |
@Test | |
public void commandShouldVerifySimpleStatementSyntax() throws InterpreterException { | |
assertThat(command.matches("test.txt"), is(true)); | |
} | |
@Test(expected = NonMatchingArgException.class) | |
public void commandShouldVerifyEmptyArguments() throws InterpreterException { | |
command.matches(); | |
} | |
@Test | |
public void commandShouldVerifyStatementSyntaxWithSpecialArguments() throws InterpreterException { | |
assertThat(command.matches("\"te st.txt\""), is(true)); | |
} | |
@Test(expected = NonMatchingArgException.class) | |
public void commandShouldNotMatchUnclosedQuotes() throws InterpreterException { | |
command.matches("\"te st.txt"); | |
} | |
class StringVarargMatcher<T> extends ArgumentMatcher<T[]> implements VarargMatcher { | |
String patternString; | |
Pattern p; | |
public StringVarargMatcher(String p) { | |
this.patternString = p; | |
} | |
@Override public boolean matches(Object varargArgument) { | |
if(p == null) p = Pattern.compile(patternString); | |
if(varargArgument instanceof String) { | |
return p.matcher((String) varargArgument).matches(); | |
} | |
return false; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment