001package org.apache.maven.scm.provider.accurev.command.export;
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.CoreMatchers.hasItem;
024import static org.hamcrest.CoreMatchers.is;
025import static org.hamcrest.CoreMatchers.notNullValue;
026import static org.hamcrest.MatcherAssert.assertThat;
027import static org.mockito.ArgumentMatchers.eq;
028import static org.mockito.Mockito.lenient;
029import static org.mockito.Mockito.verify;
030import static org.mockito.Mockito.when;
031import static org.mockito.hamcrest.MockitoHamcrest.argThat;
032
033import java.io.File;
034import java.util.Collection;
035import java.util.Collections;
036import java.util.List;
037
038import org.apache.maven.scm.CommandParameter;
039import org.apache.maven.scm.CommandParameters;
040import org.apache.maven.scm.ScmException;
041import org.apache.maven.scm.ScmFileSet;
042import org.apache.maven.scm.ScmFileStatus;
043import org.apache.maven.scm.ScmTag;
044import org.apache.maven.scm.command.checkout.CheckOutScmResult;
045import org.apache.maven.scm.command.export.ExportScmResult;
046import org.apache.maven.scm.provider.accurev.AccuRevException;
047import org.apache.maven.scm.provider.accurev.AccuRevScmProvider;
048import org.apache.maven.scm.provider.accurev.command.AbstractAccuRevCommandTest;
049import org.apache.maven.scm.repository.ScmRepository;
050import org.junit.Test;
051
052public class AccuRevExportCommandTest
053    extends AbstractAccuRevCommandTest
054{
055
056    @Test
057    public void testExportToVersionPre490()
058        throws Exception
059    {
060     // info defaults to no workspace...
061        info.setWorkSpace( null );
062
063        when( accurev.info( basedir ) ).thenReturn( info );
064        
065        // A version that does not support pop -t
066        when( accurev.getClientVersion()).thenReturn( "4.7.4b" );
067
068        List<File> poppedFiles = Collections.singletonList( new File( "exported/file" ) );
069        when(
070              accurev.popExternal( eq( basedir ), eq( "mySnapShot" ), eq( "now" ),
071                                   (Collection<File>) argThat( hasItem( new File( "/./project/dir" ) ) ) ) ).thenReturn(
072                                                                                                                         poppedFiles );
073
074        AccuRevExportCommand command = new AccuRevExportCommand( getLogger() );
075
076        CommandParameters params = new CommandParameters();
077        params.setScmVersion( CommandParameter.SCM_VERSION, new ScmTag( "mySnapShot/676" ) );
078
079        ExportScmResult result = command.export( repo, new ScmFileSet( basedir ), params );
080
081        assertTrue( result.isSuccess() );
082        assertHasScmFile( result.getExportedFiles(), "exported/file", ScmFileStatus.CHECKED_OUT );
083
084    }
085    
086    @Test
087    public void testExportToVersion490()
088        throws Exception
089    {
090     // info defaults to no workspace...
091        info.setWorkSpace( null );
092
093        when( accurev.info( basedir ) ).thenReturn( info );
094        
095        // A version that does not support pop -t
096        when( accurev.getClientVersion()).thenReturn( "4.9.0" );
097
098        List<File> poppedFiles = Collections.singletonList( new File( "exported/file" ) );
099        when(
100              accurev.popExternal( eq( basedir ), eq( "mySnapShot" ), eq( "676" ),
101                                   (Collection<File>) argThat( hasItem( new File( "/./project/dir" ) ) ) ) ).thenReturn(
102                                                                                                                         poppedFiles );
103
104        AccuRevExportCommand command = new AccuRevExportCommand( getLogger() );
105
106        CommandParameters params = new CommandParameters();
107        params.setScmVersion( CommandParameter.SCM_VERSION, new ScmTag( "mySnapShot/676" ) );
108
109        ExportScmResult result = command.export( repo, new ScmFileSet( basedir ), params );
110
111        assertTrue( result.isSuccess() );
112        assertHasScmFile( result.getExportedFiles(), "exported/file", ScmFileStatus.CHECKED_OUT );
113        verify( accurev).syncReplica();
114
115    }
116    @Test
117    public void testExportVersionOutSideWorkspace()
118        throws Exception
119    {
120
121        // info defaults to no workspace...
122        info.setWorkSpace( null );
123
124        when( accurev.info( basedir ) ).thenReturn( info );
125
126        List<File> poppedFiles = Collections.singletonList( new File( "exported/file" ) );
127        when(
128              accurev.popExternal( eq( basedir ), eq( "mySnapShot" ), eq( (String) null ),
129                                   (Collection<File>) argThat( hasItem( new File( "/./project/dir" ) ) ) ) ).thenReturn(
130                                                                                                                         poppedFiles );
131
132        AccuRevExportCommand command = new AccuRevExportCommand( getLogger() );
133
134        CommandParameters params = new CommandParameters();
135        params.setScmVersion( CommandParameter.SCM_VERSION, new ScmTag( "mySnapShot" ) );
136
137        ExportScmResult result = command.export( repo, new ScmFileSet( basedir ), params );
138
139        assertTrue( result.isSuccess() );
140        assertHasScmFile( result.getExportedFiles(), "exported/file", ScmFileStatus.CHECKED_OUT );
141
142    }
143
144    @Test
145    public void testExportFailure()
146        throws Exception
147    {
148
149        // info defaults to no workspace...
150        info.setWorkSpace( null );
151        when( accurev.info( basedir ) ).thenReturn( info );
152        when( accurev.getClientVersion()).thenReturn( "4.9.0" );
153        when(
154              accurev.popExternal( eq( basedir ), eq( "mySnapShot" ), eq("544"),
155                                   (Collection<File>) argThat( hasItem( new File( "/./project/dir" ) ) ) ) ).thenReturn(
156                                                                                                                         null );
157
158        AccuRevExportCommand command = new AccuRevExportCommand( getLogger() );
159
160        CommandParameters params = new CommandParameters();
161        params.setScmVersion( CommandParameter.SCM_VERSION, new ScmTag( "mySnapShot/544"));
162
163        ExportScmResult result = command.export( repo, new ScmFileSet( basedir ), params );
164
165        assertThat( result.isSuccess(), is( false ) );
166        assertThat( result.getProviderMessage(), notNullValue() );
167    }
168
169    @Test
170    public void testNonPersistentWithinExistingWorkspace()
171        throws Exception
172    {
173
174        // Setup info to return a stream rooted somewhere around here...
175        info.setWorkSpace( "myStream_me" );
176        info.setBasis( "someStream" );
177        info.setTop( basedir.getParent() );
178
179        when( accurev.info( basedir ) ).thenReturn( info );
180        when( accurev.stat( basedir ) ).thenReturn( null );
181        when( accurev.rmws( "myStream_me" ) ).thenReturn( Boolean.TRUE );
182        List<File> poppedFiles = Collections.singletonList( new File( "exported/file" ) );
183        lenient().when(
184              accurev.popExternal( eq( basedir ), eq( "mySnapShot" ), eq( "now" ),
185                                   (Collection<File>) argThat( hasItem( new File( "/./project/dir" ) ) ) ) ).thenReturn(
186                                                                                                                         poppedFiles );
187        when( accurev.reactivate( "myStream_me" ) ).thenReturn( Boolean.TRUE );
188
189        repo.setPersistCheckout( true );
190
191        AccuRevExportCommand command = new AccuRevExportCommand( getLogger() );
192
193        CommandParameters params = new CommandParameters();
194        params.setScmVersion( CommandParameter.SCM_VERSION, new ScmTag( "mySnapShot" ) );
195
196        ExportScmResult result = command.export( repo, new ScmFileSet( basedir ), params );
197
198        verify( accurev ).rmws( "myStream_me" );
199        verify( accurev ).reactivate( "myStream_me" );
200        assertTrue( result.isSuccess() );
201        // TODO - raise JIRA to move relative path dir to repository rather than checkout result
202        // dassertThat( result.getRelativePathProjectDirectory(), is( "/project/dir" ) );
203
204    }
205
206    @Test
207    public void testNonPersistentCheckoutUsesExport()
208        // This is same expectations as above, but using checkout method with setPersist = false.
209        throws AccuRevException, ScmException
210    {
211        // Setup info to return a stream rooted somewhere around here...
212        info.setWorkSpace( "myStream_me" );
213        info.setBasis( "someStream" );
214        info.setTop( basedir.getParent() );
215
216        when( accurev.info( basedir ) ).thenReturn( info );
217        when( accurev.stat( basedir ) ).thenReturn( null );
218        when( accurev.rmws( "myStream_me" ) ).thenReturn( Boolean.TRUE );
219        List<File> poppedFiles = Collections.singletonList( new File( "exported/file" ) );
220        lenient().when(
221              accurev.popExternal( eq( basedir ), eq( "mySnapShot" ), eq( "now" ),
222                                   (Collection<File>) argThat( hasItem( new File( "/./project/dir" ) ) ) ) ).thenReturn(
223                                                                                                                         poppedFiles );
224        when( accurev.reactivate( "myStream_me" ) ).thenReturn( Boolean.TRUE );
225
226        repo.setPersistCheckout( false );
227
228        ScmRepository scmRepo = new ScmRepository( "accurev", repo );
229
230        AccuRevScmProvider provider = new AccuRevScmProvider();
231        CheckOutScmResult result = provider.checkOut( scmRepo, new ScmFileSet( basedir ), new ScmTag( "mySnapShot" ) );
232
233        verify( accurev ).rmws( "myStream_me" );
234        verify( accurev ).reactivate( "myStream_me" );
235
236        assertTrue( result.isSuccess() );
237
238    }
239
240    @Test
241    public void testname()
242        throws Exception
243    {
244        String myString = "Hello " + null;
245        assertThat( myString, is( "Hello null" ) );
246    }
247}