### Question:
AccuRevLoginCommand extends AbstractAccuRevCommand { public LoginScmResult login( ScmProviderRepository repository, ScmFileSet fileSet, CommandParameters parameters ) throws ScmException { return (LoginScmResult) execute( repository, fileSet, parameters ); } AccuRevLoginCommand( ScmLogger logger ); LoginScmResult login( ScmProviderRepository repository, ScmFileSet fileSet, CommandParameters parameters );  }### Answer:
@Test public void testWhenNotLoggedIn() throws Exception { repo.setUser( "myUser" ); repo.setPassword( "aPassword" ); info.setUser( "(not logged in)" ); when( accurev.info( any( File.class ) ) ).thenReturn( info ); when( accurev.login( "myUser", "aPassword" ) ).thenReturn( true ); AccuRevLoginCommand command = new AccuRevLoginCommand( getLogger() ); LoginScmResult result = command.login( repo, new ScmFileSet( basedir ), new CommandParameters() ); assertThat( result.isSuccess(), is( true ) ); verify( accurev ).login( "myUser", "aPassword" ); }
@Test public void testWhenAlreadyLoggedInAsSomeoneElse() throws Exception { repo.setUser( "myUser" ); repo.setPassword( "aPassword" ); info.setUser( "A.N.Other" ); when( accurev.info( any( File.class ) ) ).thenReturn( info ); when( accurev.login( "myUser", "aPassword" ) ).thenReturn( true ); AccuRevLoginCommand command = new AccuRevLoginCommand( getLogger() ); LoginScmResult result = command.login( repo, new ScmFileSet( basedir ), new CommandParameters() ); assertThat( result.isSuccess(), is( true ) ); verify( accurev ).login( "myUser", "aPassword" ); }
@Test public void testWhenAlreadyLoggedInAsRequiredUser() throws Exception { repo.setUser( "myUser" ); repo.setPassword( "aPassword" ); info.setUser( "myUser" ); when( accurev.info( any( File.class ) ) ).thenReturn( info ); AccuRevLoginCommand command = new AccuRevLoginCommand( getLogger() ); LoginScmResult result = command.login( repo, new ScmFileSet( basedir ), new CommandParameters() ); assertThat( result.isSuccess(), is( true ) ); verify( accurev, never() ).login( eq( "myUser" ), anyString() ); }
@Test public void testWhenNoUserSuppliedAndAlreadyLoggedIn() throws Exception { repo.setUser( null ); info.setUser( "anyUser" ); when( accurev.info( any( File.class ) ) ).thenReturn( info ); AccuRevLoginCommand command = new AccuRevLoginCommand( getLogger() ); LoginScmResult result = command.login( repo, new ScmFileSet( basedir ), new CommandParameters() ); assertThat( result.isSuccess(), is( true ) ); verify( accurev, never() ).login( anyString(), anyString() ); }
@Test public void testFailsWhenNoUserSuppliedAndNotLoggedIn() throws Exception { repo.setUser( null ); info.setUser( "(not logged in)" ); when( accurev.info( any( File.class ) ) ).thenReturn( info ); AccuRevLoginCommand command = new AccuRevLoginCommand( getLogger() ); LoginScmResult result = command.login( repo, new ScmFileSet( basedir ), new CommandParameters() ); assertThat( result.isSuccess(), is( false ) ); verify( accurev, never() ).login( anyString(), anyString() ); }