View Javadoc
1   package org.apache.maven.scm.provider.accurev.command.checkout;
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.apache.maven.scm.ScmFileMatcher.assertHasScmFile;
23  import static org.hamcrest.Matchers.is;
24  import static org.hamcrest.Matchers.notNullValue;
25  import static org.junit.Assert.assertThat;
26  import static org.mockito.Mockito.verify;
27  import static org.mockito.Mockito.when;
28  
29  import java.io.File;
30  import java.util.Collections;
31  import java.util.List;
32  
33  import org.apache.maven.scm.CommandParameter;
34  import org.apache.maven.scm.CommandParameters;
35  import org.apache.maven.scm.ScmException;
36  import org.apache.maven.scm.ScmFile;
37  import org.apache.maven.scm.ScmFileSet;
38  import org.apache.maven.scm.ScmFileStatus;
39  import org.apache.maven.scm.ScmRevision;
40  import org.apache.maven.scm.ScmTag;
41  import org.apache.maven.scm.command.checkout.CheckOutScmResult;
42  import org.apache.maven.scm.provider.accurev.command.AbstractAccuRevCommandTest;
43  import org.junit.Test;
44  
45  /**
46   * checkout a revision or branch (stream/tranid)-> make workspace. If basedir is empty and represents the top of an
47   * existing workspace, then reparent the workspace if necessary and repopulate missing file, and update If basedir is
48   * not empty or is a subdirectory of an existing workspace throw exception. Otherwise make a workspace and update
49   * Special case for release plugin - checkout a tag to an ignored and empty subdirectory of an existing workspace. Treat
50   * as an export. deactivate the workspace, export with pop -v -L then reactivate the workspace.
51   * 
52   * @author ggardner
53   */
54  public class AccuRevCheckOutCommandTest
55      extends AbstractAccuRevCommandTest
56  {
57  
58      @Test
59      public void testCheckout()
60          throws Exception
61      {
62  
63          when( accurev.mkws( "myStream", AccuRevCheckOutCommand.getWorkSpaceName( basedir, "myStream" ), basedir ) ).thenReturn(
64                                                                                                                                  true );
65  
66          List<File> updatedFiles = Collections.singletonList( new File( "updated/file" ) );
67          when( accurev.update( basedir, "now" ) ).thenReturn( updatedFiles );
68  
69          AccuRevCheckOutCommand command = new AccuRevCheckOutCommand( getLogger() );
70  
71          CheckOutScmResult result = command.checkout( repo, new ScmFileSet( basedir ), new CommandParameters() );
72  
73          assertThat( result.isSuccess(), is( true ) );
74          assertThat( result.getRelativePathProjectDirectory(), is( "/project/dir" ) );
75          List<ScmFile> checkedOutFiles = result.getCheckedOutFiles();
76          assertThat( checkedOutFiles.size(), is( 1 ) );
77          assertHasScmFile( checkedOutFiles, "updated/file", ScmFileStatus.CHECKED_OUT );
78  
79      }
80  
81      @Test
82      public void testCheckoutFailure()
83          throws Exception
84      {
85  
86          when( accurev.mkws( "myStream", AccuRevCheckOutCommand.getWorkSpaceName( basedir, "myStream" ), basedir ) ).thenReturn(
87                                                                                                                                  true );
88          when( accurev.update( basedir, "now" ) ).thenReturn( null );
89  
90          AccuRevCheckOutCommand command = new AccuRevCheckOutCommand( getLogger() );
91  
92          CheckOutScmResult result = command.checkout( repo, new ScmFileSet( basedir ), new CommandParameters() );
93  
94          assertThat( result.isSuccess(), is( false ) );
95          assertThat( result.getProviderMessage(), notNullValue() );
96  
97      }
98  
99      @Test
100     public void testReCheckoutExistingWorkspaceSameBasis()
101         throws Exception
102     {
103 
104         // Set the info result to return a workspace that already exists
105         info.setWorkSpace( "someOldStream_someUser" );
106         info.setBasis( "myStream" );
107         info.setTop( basedir.getAbsolutePath() );
108 
109         List<File> emptyList = Collections.emptyList();
110 
111         when( accurev.pop( basedir, null ) ).thenReturn( emptyList );
112 
113         List<File> updatedFiles = Collections.singletonList( new File( "updated/file" ) );
114         when( accurev.update( basedir, null ) ).thenReturn( updatedFiles );
115 
116         AccuRevCheckOutCommand command = new AccuRevCheckOutCommand( getLogger() );
117 
118         CheckOutScmResult result = command.checkout( repo, new ScmFileSet( basedir ), new CommandParameters() );
119 
120         verify( accurev ).pop( basedir, null );
121 
122         assertThat( result.isSuccess(), is( true ) );
123         assertThat( result.getRelativePathProjectDirectory(), is( "/project/dir" ) );
124 
125     }
126 
127     @Test
128     public void testReCheckoutExistingWorkspaceDifferentBasis()
129         throws Exception
130     {
131         // Set the info result to return a workspace that already exists
132         info.setWorkSpace( "someOldStream_someUser" );
133         info.setBasis( "myStream" );
134         info.setTop( basedir.getAbsolutePath() );
135 
136         when( accurev.chws( basedir, "someOldStream_someUser", "mySnapShot" ) ).thenReturn( true );
137 
138         List<File> emptyPop = Collections.emptyList();
139         when( accurev.popExternal( basedir, null, null, null ) ).thenReturn( emptyPop );
140 
141         List<File> updatedFiles = Collections.singletonList( new File( "updated/file" ) );
142         when( accurev.update( basedir, null ) ).thenReturn( updatedFiles );
143 
144         AccuRevCheckOutCommand command = new AccuRevCheckOutCommand( getLogger() );
145 
146         CommandParameters params = new CommandParameters();
147         params.setScmVersion( CommandParameter.SCM_VERSION, new ScmTag( "mySnapShot" ) );
148 
149         CheckOutScmResult result = command.checkout( repo, new ScmFileSet( basedir ), params );
150 
151         verify( accurev ).chws( basedir, "someOldStream_someUser", "mySnapShot" );
152 
153         assertThat( result.isSuccess(), is( true ) );
154         assertThat( result.getRelativePathProjectDirectory(), is( "/project/dir" ) );
155 
156     }
157 
158     @Test( expected = ScmException.class )
159     public void testReCheckoutSubdirectoryOfExistingWorkspaceThrowsException()
160         throws Exception
161     {
162         // Set the info result to return a workspace that already exists
163         info.setWorkSpace( "someOldStream_someUser" );
164         info.setBasis( "myStream" );
165         info.setTop( basedir.getParentFile().getAbsolutePath() );
166 
167         AccuRevCheckOutCommand command = new AccuRevCheckOutCommand( getLogger() );
168 
169         CommandParameters params = new CommandParameters();
170         params.setScmVersion( CommandParameter.SCM_VERSION, new ScmTag( "mySnapShot" ) );
171 
172         command.checkout( repo, new ScmFileSet( basedir ), params );
173         fail( "Expected exception" );
174 
175     }
176 
177     @Test
178     public void testCheckoutToVersionNewWorkspace()
179         throws Exception
180     {
181 
182         when( accurev.mkws( "anotherStream", AccuRevCheckOutCommand.getWorkSpaceName( basedir, "anotherStream" ), basedir ) ).thenReturn(
183                                                                                                                                 true );
184 
185         List<File> updatedFiles = Collections.singletonList( new File( "updated/file" ) );
186         when( accurev.update( basedir, "now" ) ).thenReturn( updatedFiles );
187 
188         AccuRevCheckOutCommand command = new AccuRevCheckOutCommand( getLogger() );
189 
190         CommandParameters parameters = new CommandParameters();
191         parameters.setScmVersion( CommandParameter.SCM_VERSION, new ScmRevision( "anotherStream/12" ) );
192         
193         CheckOutScmResult result = command.checkout( repo, new ScmFileSet( basedir ), parameters );
194 
195         assertThat( result.isSuccess(), is( true ) );
196         assertThat( result.getCheckedOutFiles().size(), is( 1 ) );
197 
198     }
199     
200     @Test
201     public void testCheckoutToVersionExistingWorkspace()
202         throws Exception
203     {
204 
205         // Set the info result to return a workspace that already exists
206         info.setWorkSpace( "someOldStream_someUser" );
207         info.setBasis( "myStream" );
208         info.setTop( basedir.getAbsolutePath() );
209 
210         List<File> emptyList = Collections.emptyList();
211 
212         when( accurev.pop( basedir, null ) ).thenReturn( emptyList );
213 
214         List<File> updatedFiles = Collections.singletonList( new File( "updated/file" ) );
215         when( accurev.update( basedir, "12" ) ).thenReturn( updatedFiles );
216 
217         AccuRevCheckOutCommand command = new AccuRevCheckOutCommand( getLogger() );
218 
219         CommandParameters parameters = new CommandParameters();
220         parameters.setScmVersion( CommandParameter.SCM_VERSION, new ScmRevision( "myStream/12" ) );
221         CheckOutScmResult result = command.checkout( repo, new ScmFileSet( basedir ), parameters );
222 
223         verify( accurev ).pop( basedir, null );
224 
225         assertThat( result.isSuccess(), is( true ) );
226         assertThat( result.getCheckedOutFiles().size(), is( 1 ) );
227 
228     }
229 
230 }