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.hamcrest.MatcherAssert.assertThat;
026import static org.mockito.Mockito.*;
027
028import java.io.File;
029import java.util.Collections;
030import java.util.List;
031
032import org.apache.maven.scm.CommandParameter;
033import org.apache.maven.scm.CommandParameters;
034import org.apache.maven.scm.ScmException;
035import org.apache.maven.scm.ScmFile;
036import org.apache.maven.scm.ScmFileSet;
037import org.apache.maven.scm.ScmFileStatus;
038import org.apache.maven.scm.ScmRevision;
039import org.apache.maven.scm.ScmTag;
040import org.apache.maven.scm.command.checkout.CheckOutScmResult;
041import org.apache.maven.scm.provider.accurev.command.AbstractAccuRevCommandTest;
042import org.junit.Test;
043
044/**
045 * checkout a revision or branch (stream/tranid)-> make workspace. If basedir is empty and represents the top of an
046 * existing workspace, then reparent the workspace if necessary and repopulate missing file, and update If basedir is
047 * not empty or is a subdirectory of an existing workspace throw exception. Otherwise make a workspace and update
048 * Special case for release plugin - checkout a tag to an ignored and empty subdirectory of an existing workspace. Treat
049 * as an export. deactivate the workspace, export with pop -v -L then reactivate the workspace.
050 * 
051 * @author ggardner
052 */
053public class AccuRevCheckOutCommandTest
054    extends AbstractAccuRevCommandTest
055{
056
057    @Test
058    public void testCheckout()
059        throws Exception
060    {
061
062        when( accurev.mkws( "myStream", AccuRevCheckOutCommand.getWorkSpaceName( basedir, "myStream" ), basedir ) ).thenReturn(
063                                                                                                                                true );
064
065        List<File> updatedFiles = Collections.singletonList( new File( "updated/file" ) );
066        when( accurev.update( basedir, "now" ) ).thenReturn( updatedFiles );
067
068        AccuRevCheckOutCommand command = new AccuRevCheckOutCommand( getLogger() );
069
070        CheckOutScmResult result = command.checkout( repo, new ScmFileSet( basedir ), new CommandParameters() );
071
072        assertThat( result.isSuccess(), is( true ) );
073        assertThat( result.getRelativePathProjectDirectory(), is( "/project/dir" ) );
074        List<ScmFile> checkedOutFiles = result.getCheckedOutFiles();
075        assertThat( checkedOutFiles.size(), is( 1 ) );
076        assertHasScmFile( checkedOutFiles, "updated/file", ScmFileStatus.CHECKED_OUT );
077
078    }
079
080    @Test
081    public void testCheckoutFailure()
082        throws Exception
083    {
084
085        when( accurev.mkws( "myStream", AccuRevCheckOutCommand.getWorkSpaceName( basedir, "myStream" ), basedir ) ).thenReturn(
086                                                                                                                                true );
087        when( accurev.update( basedir, "now" ) ).thenReturn( null );
088
089        AccuRevCheckOutCommand command = new AccuRevCheckOutCommand( getLogger() );
090
091        CheckOutScmResult result = command.checkout( repo, new ScmFileSet( basedir ), new CommandParameters() );
092
093        assertThat( result.isSuccess(), is( false ) );
094        assertThat( result.getProviderMessage(), notNullValue() );
095
096    }
097
098    @Test
099    public void testReCheckoutExistingWorkspaceSameBasis()
100        throws Exception
101    {
102
103        // Set the info result to return a workspace that already exists
104        info.setWorkSpace( "someOldStream_someUser" );
105        info.setBasis( "myStream" );
106        info.setTop( basedir.getAbsolutePath() );
107
108        List<File> emptyList = Collections.emptyList();
109
110        when( accurev.pop( basedir, null ) ).thenReturn( emptyList );
111
112        List<File> updatedFiles = Collections.singletonList( new File( "updated/file" ) );
113        when( accurev.update( basedir, null ) ).thenReturn( updatedFiles );
114
115        AccuRevCheckOutCommand command = new AccuRevCheckOutCommand( getLogger() );
116
117        CheckOutScmResult result = command.checkout( repo, new ScmFileSet( basedir ), new CommandParameters() );
118
119        verify( accurev ).pop( basedir, null );
120
121        assertThat( result.isSuccess(), is( true ) );
122        assertThat( result.getRelativePathProjectDirectory(), is( "/project/dir" ) );
123
124    }
125
126    @Test
127    public void testReCheckoutExistingWorkspaceDifferentBasis()
128        throws Exception
129    {
130        // Set the info result to return a workspace that already exists
131        info.setWorkSpace( "someOldStream_someUser" );
132        info.setBasis( "myStream" );
133        info.setTop( basedir.getAbsolutePath() );
134
135        when( accurev.chws( basedir, "someOldStream_someUser", "mySnapShot" ) ).thenReturn( true );
136
137        List<File> emptyPop = Collections.emptyList();
138        lenient().when( accurev.popExternal( basedir, null, null, null ) ).thenReturn( emptyPop );
139
140        List<File> updatedFiles = Collections.singletonList( new File( "updated/file" ) );
141        when( accurev.update( basedir, null ) ).thenReturn( updatedFiles );
142
143        AccuRevCheckOutCommand command = new AccuRevCheckOutCommand( getLogger() );
144
145        CommandParameters params = new CommandParameters();
146        params.setScmVersion( CommandParameter.SCM_VERSION, new ScmTag( "mySnapShot" ) );
147
148        CheckOutScmResult result = command.checkout( repo, new ScmFileSet( basedir ), params );
149
150        verify( accurev ).chws( basedir, "someOldStream_someUser", "mySnapShot" );
151
152        assertThat( result.isSuccess(), is( true ) );
153        assertThat( result.getRelativePathProjectDirectory(), is( "/project/dir" ) );
154
155    }
156
157    @Test( expected = ScmException.class )
158    public void testReCheckoutSubdirectoryOfExistingWorkspaceThrowsException()
159        throws Exception
160    {
161        // Set the info result to return a workspace that already exists
162        info.setWorkSpace( "someOldStream_someUser" );
163        info.setBasis( "myStream" );
164        info.setTop( basedir.getParentFile().getAbsolutePath() );
165
166        AccuRevCheckOutCommand command = new AccuRevCheckOutCommand( getLogger() );
167
168        CommandParameters params = new CommandParameters();
169        params.setScmVersion( CommandParameter.SCM_VERSION, new ScmTag( "mySnapShot" ) );
170
171        command.checkout( repo, new ScmFileSet( basedir ), params );
172        fail( "Expected exception" );
173
174    }
175
176    @Test
177    public void testCheckoutToVersionNewWorkspace()
178        throws Exception
179    {
180
181        when( accurev.mkws( "anotherStream", AccuRevCheckOutCommand.getWorkSpaceName( basedir, "anotherStream" ), basedir ) ).thenReturn(
182                                                                                                                                true );
183
184        List<File> updatedFiles = Collections.singletonList( new File( "updated/file" ) );
185        when( accurev.update( basedir, "now" ) ).thenReturn( updatedFiles );
186
187        AccuRevCheckOutCommand command = new AccuRevCheckOutCommand( getLogger() );
188
189        CommandParameters parameters = new CommandParameters();
190        parameters.setScmVersion( CommandParameter.SCM_VERSION, new ScmRevision( "anotherStream/12" ) );
191        
192        CheckOutScmResult result = command.checkout( repo, new ScmFileSet( basedir ), parameters );
193
194        assertThat( result.isSuccess(), is( true ) );
195        assertThat( result.getCheckedOutFiles().size(), is( 1 ) );
196
197    }
198    
199    @Test
200    public void testCheckoutToVersionExistingWorkspace()
201        throws Exception
202    {
203
204        // Set the info result to return a workspace that already exists
205        info.setWorkSpace( "someOldStream_someUser" );
206        info.setBasis( "myStream" );
207        info.setTop( basedir.getAbsolutePath() );
208
209        List<File> emptyList = Collections.emptyList();
210
211        when( accurev.pop( basedir, null ) ).thenReturn( emptyList );
212
213        List<File> updatedFiles = Collections.singletonList( new File( "updated/file" ) );
214        when( accurev.update( basedir, "12" ) ).thenReturn( updatedFiles );
215
216        AccuRevCheckOutCommand command = new AccuRevCheckOutCommand( getLogger() );
217
218        CommandParameters parameters = new CommandParameters();
219        parameters.setScmVersion( CommandParameter.SCM_VERSION, new ScmRevision( "myStream/12" ) );
220        CheckOutScmResult result = command.checkout( repo, new ScmFileSet( basedir ), parameters );
221
222        verify( accurev ).pop( basedir, null );
223
224        assertThat( result.isSuccess(), is( true ) );
225        assertThat( result.getCheckedOutFiles().size(), is( 1 ) );
226
227    }
228
229}