001package org.apache.maven.scm.provider.accurev.command.checkout;
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.apache.maven.scm.ScmFileMatcher.assertHasScmFile;
023import static org.hamcrest.Matchers.is;
024import static org.hamcrest.Matchers.notNullValue;
025import static org.junit.Assert.assertThat;
026import static org.mockito.Mockito.verify;
027import static org.mockito.Mockito.when;
028
029import java.io.File;
030import java.util.Collections;
031import java.util.List;
032
033import org.apache.maven.scm.CommandParameter;
034import org.apache.maven.scm.CommandParameters;
035import org.apache.maven.scm.ScmException;
036import org.apache.maven.scm.ScmFile;
037import org.apache.maven.scm.ScmFileSet;
038import org.apache.maven.scm.ScmFileStatus;
039import org.apache.maven.scm.ScmRevision;
040import org.apache.maven.scm.ScmTag;
041import org.apache.maven.scm.command.checkout.CheckOutScmResult;
042import org.apache.maven.scm.provider.accurev.command.AbstractAccuRevCommandTest;
043import org.junit.Test;
044
045/**
046 * checkout a revision or branch (stream/tranid)-> make workspace. If basedir is empty and represents the top of an
047 * existing workspace, then reparent the workspace if necessary and repopulate missing file, and update If basedir is
048 * not empty or is a subdirectory of an existing workspace throw exception. Otherwise make a workspace and update
049 * Special case for release plugin - checkout a tag to an ignored and empty subdirectory of an existing workspace. Treat
050 * as an export. deactivate the workspace, export with pop -v -L then reactivate the workspace.
051 * 
052 * @author ggardner
053 */
054public class AccuRevCheckOutCommandTest
055    extends AbstractAccuRevCommandTest
056{
057
058    @Test
059    public void testCheckout()
060        throws Exception
061    {
062
063        when( accurev.mkws( "myStream", AccuRevCheckOutCommand.getWorkSpaceName( basedir, "myStream" ), basedir ) ).thenReturn(
064                                                                                                                                true );
065
066        List<File> updatedFiles = Collections.singletonList( new File( "updated/file" ) );
067        when( accurev.update( basedir, "now" ) ).thenReturn( updatedFiles );
068
069        AccuRevCheckOutCommand command = new AccuRevCheckOutCommand( getLogger() );
070
071        CheckOutScmResult result = command.checkout( repo, new ScmFileSet( basedir ), new CommandParameters() );
072
073        assertThat( result.isSuccess(), is( true ) );
074        assertThat( result.getRelativePathProjectDirectory(), is( "/project/dir" ) );
075        List<ScmFile> checkedOutFiles = result.getCheckedOutFiles();
076        assertThat( checkedOutFiles.size(), is( 1 ) );
077        assertHasScmFile( checkedOutFiles, "updated/file", ScmFileStatus.CHECKED_OUT );
078
079    }
080
081    @Test
082    public void testCheckoutFailure()
083        throws Exception
084    {
085
086        when( accurev.mkws( "myStream", AccuRevCheckOutCommand.getWorkSpaceName( basedir, "myStream" ), basedir ) ).thenReturn(
087                                                                                                                                true );
088        when( accurev.update( basedir, "now" ) ).thenReturn( null );
089
090        AccuRevCheckOutCommand command = new AccuRevCheckOutCommand( getLogger() );
091
092        CheckOutScmResult result = command.checkout( repo, new ScmFileSet( basedir ), new CommandParameters() );
093
094        assertThat( result.isSuccess(), is( false ) );
095        assertThat( result.getProviderMessage(), notNullValue() );
096
097    }
098
099    @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}