View Javadoc
1   package org.apache.maven.scm.provider.accurev.command.login;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import static org.hamcrest.Matchers.is;
23  import static org.junit.Assert.assertThat;
24  import static org.mockito.Matchers.any;
25  import static org.mockito.Matchers.anyString;
26  import static org.mockito.Matchers.eq;
27  import static org.mockito.Mockito.never;
28  import static org.mockito.Mockito.verify;
29  import static org.mockito.Mockito.when;
30  
31  import java.io.File;
32  
33  import org.apache.maven.scm.CommandParameters;
34  import org.apache.maven.scm.ScmFileSet;
35  import org.apache.maven.scm.command.login.LoginScmResult;
36  import org.apache.maven.scm.provider.accurev.command.AbstractAccuRevCommandTest;
37  import org.junit.Test;
38  
39  public class AccuRevLoginCommandTest
40      extends AbstractAccuRevCommandTest
41  {
42  
43      @Test
44      public void testWhenNotLoggedIn()
45          throws Exception
46      {
47  
48          repo.setUser( "myUser" );
49          repo.setPassword( "aPassword" );
50          info.setUser( "(not logged in)" );
51          when( accurev.info( any( File.class ) ) ).thenReturn( info );
52          when( accurev.login( "myUser", "aPassword" ) ).thenReturn( true );
53          AccuRevLoginCommand command = new AccuRevLoginCommand( getLogger() );
54  
55          LoginScmResult result = command.login( repo, new ScmFileSet( basedir ), new CommandParameters() );
56  
57          assertThat( result.isSuccess(), is( true ) );
58          verify( accurev ).login( "myUser", "aPassword" );
59  
60      }
61  
62      @Test
63      public void testWhenAlreadyLoggedInAsSomeoneElse()
64          throws Exception
65      {
66          repo.setUser( "myUser" );
67          repo.setPassword( "aPassword" );
68          info.setUser( "A.N.Other" );
69          when( accurev.info( any( File.class ) ) ).thenReturn( info );
70          when( accurev.login( "myUser", "aPassword" ) ).thenReturn( true );
71          AccuRevLoginCommand command = new AccuRevLoginCommand( getLogger() );
72  
73          LoginScmResult result = command.login( repo, new ScmFileSet( basedir ), new CommandParameters() );
74  
75          assertThat( result.isSuccess(), is( true ) );
76          verify( accurev ).login( "myUser", "aPassword" );
77  
78      }
79  
80      @Test
81      public void testWhenAlreadyLoggedInAsRequiredUser()
82          throws Exception
83      {
84  
85          repo.setUser( "myUser" );
86          repo.setPassword( "aPassword" );
87          info.setUser( "myUser" );
88          when( accurev.info( any( File.class ) ) ).thenReturn( info );
89          AccuRevLoginCommand command = new AccuRevLoginCommand( getLogger() );
90  
91          LoginScmResult result = command.login( repo, new ScmFileSet( basedir ), new CommandParameters() );
92  
93          assertThat( result.isSuccess(), is( true ) );
94          // This is an important case as logging in will start an expiry timer
95          // that might be shorter than the current expiry timer!
96          verify( accurev, never() ).login( eq( "myUser" ), anyString() );
97  
98      }
99  
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 }