001package org.apache.maven.scm.provider.accurev.command.login;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *   http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import static org.hamcrest.Matchers.is;
023import static org.junit.Assert.assertThat;
024import static org.mockito.Matchers.any;
025import static org.mockito.Matchers.anyString;
026import static org.mockito.Matchers.eq;
027import static org.mockito.Mockito.never;
028import static org.mockito.Mockito.verify;
029import static org.mockito.Mockito.when;
030
031import java.io.File;
032
033import org.apache.maven.scm.CommandParameters;
034import org.apache.maven.scm.ScmFileSet;
035import org.apache.maven.scm.command.login.LoginScmResult;
036import org.apache.maven.scm.provider.accurev.command.AbstractAccuRevCommandTest;
037import org.junit.Test;
038
039public class AccuRevLoginCommandTest
040    extends AbstractAccuRevCommandTest
041{
042
043    @Test
044    public void testWhenNotLoggedIn()
045        throws Exception
046    {
047
048        repo.setUser( "myUser" );
049        repo.setPassword( "aPassword" );
050        info.setUser( "(not logged in)" );
051        when( accurev.info( any( File.class ) ) ).thenReturn( info );
052        when( accurev.login( "myUser", "aPassword" ) ).thenReturn( true );
053        AccuRevLoginCommand command = new AccuRevLoginCommand( getLogger() );
054
055        LoginScmResult result = command.login( repo, new ScmFileSet( basedir ), new CommandParameters() );
056
057        assertThat( result.isSuccess(), is( true ) );
058        verify( accurev ).login( "myUser", "aPassword" );
059
060    }
061
062    @Test
063    public void testWhenAlreadyLoggedInAsSomeoneElse()
064        throws Exception
065    {
066        repo.setUser( "myUser" );
067        repo.setPassword( "aPassword" );
068        info.setUser( "A.N.Other" );
069        when( accurev.info( any( File.class ) ) ).thenReturn( info );
070        when( accurev.login( "myUser", "aPassword" ) ).thenReturn( true );
071        AccuRevLoginCommand command = new AccuRevLoginCommand( getLogger() );
072
073        LoginScmResult result = command.login( repo, new ScmFileSet( basedir ), new CommandParameters() );
074
075        assertThat( result.isSuccess(), is( true ) );
076        verify( accurev ).login( "myUser", "aPassword" );
077
078    }
079
080    @Test
081    public void testWhenAlreadyLoggedInAsRequiredUser()
082        throws Exception
083    {
084
085        repo.setUser( "myUser" );
086        repo.setPassword( "aPassword" );
087        info.setUser( "myUser" );
088        when( accurev.info( any( File.class ) ) ).thenReturn( info );
089        AccuRevLoginCommand command = new AccuRevLoginCommand( getLogger() );
090
091        LoginScmResult result = command.login( repo, new ScmFileSet( basedir ), new CommandParameters() );
092
093        assertThat( result.isSuccess(), is( true ) );
094        // This is an important case as logging in will start an expiry timer
095        // that might be shorter than the current expiry timer!
096        verify( accurev, never() ).login( eq( "myUser" ), anyString() );
097
098    }
099
100    @Test
101    public void testWhenNoUserSuppliedAndAlreadyLoggedIn()
102        throws Exception
103    {
104
105        repo.setUser( null );
106        info.setUser( "anyUser" );
107        when( accurev.info( any( File.class ) ) ).thenReturn( info );
108        AccuRevLoginCommand command = new AccuRevLoginCommand( getLogger() );
109
110        LoginScmResult result = command.login( repo, new ScmFileSet( basedir ), new CommandParameters() );
111
112        assertThat( result.isSuccess(), is( true ) );
113        verify( accurev, never() ).login( anyString(), anyString() );
114
115    }
116
117    @Test
118    public void testFailsWhenNoUserSuppliedAndNotLoggedIn()
119        throws Exception
120    {
121
122        repo.setUser( null );
123        info.setUser( "(not logged in)" );
124        when( accurev.info( any( File.class ) ) ).thenReturn( info );
125        AccuRevLoginCommand command = new AccuRevLoginCommand( getLogger() );
126
127        LoginScmResult result = command.login( repo, new ScmFileSet( basedir ), new CommandParameters() );
128
129        assertThat( result.isSuccess(), is( false ) );
130        verify( accurev, never() ).login( anyString(), anyString() );
131
132    }
133}