View Javadoc
1   package org.apache.maven.scm.provider.accurev.command.export;
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.CoreMatchers.hasItem;
24  import static org.hamcrest.CoreMatchers.is;
25  import static org.hamcrest.CoreMatchers.notNullValue;
26  import static org.hamcrest.MatcherAssert.assertThat;
27  import static org.mockito.ArgumentMatchers.eq;
28  import static org.mockito.Mockito.lenient;
29  import static org.mockito.Mockito.verify;
30  import static org.mockito.Mockito.when;
31  import static org.mockito.hamcrest.MockitoHamcrest.argThat;
32  
33  import java.io.File;
34  import java.util.Collection;
35  import java.util.Collections;
36  import java.util.List;
37  
38  import org.apache.maven.scm.CommandParameter;
39  import org.apache.maven.scm.CommandParameters;
40  import org.apache.maven.scm.ScmException;
41  import org.apache.maven.scm.ScmFileSet;
42  import org.apache.maven.scm.ScmFileStatus;
43  import org.apache.maven.scm.ScmTag;
44  import org.apache.maven.scm.command.checkout.CheckOutScmResult;
45  import org.apache.maven.scm.command.export.ExportScmResult;
46  import org.apache.maven.scm.provider.accurev.AccuRevException;
47  import org.apache.maven.scm.provider.accurev.AccuRevScmProvider;
48  import org.apache.maven.scm.provider.accurev.command.AbstractAccuRevCommandTest;
49  import org.apache.maven.scm.repository.ScmRepository;
50  import org.junit.Test;
51  
52  public class AccuRevExportCommandTest
53      extends AbstractAccuRevCommandTest
54  {
55  
56      @Test
57      public void testExportToVersionPre490()
58          throws Exception
59      {
60       // info defaults to no workspace...
61          info.setWorkSpace( null );
62  
63          when( accurev.info( basedir ) ).thenReturn( info );
64          
65          // A version that does not support pop -t
66          when( accurev.getClientVersion()).thenReturn( "4.7.4b" );
67  
68          List<File> poppedFiles = Collections.singletonList( new File( "exported/file" ) );
69          when(
70                accurev.popExternal( eq( basedir ), eq( "mySnapShot" ), eq( "now" ),
71                                     (Collection<File>) argThat( hasItem( new File( "/./project/dir" ) ) ) ) ).thenReturn(
72                                                                                                                           poppedFiles );
73  
74          AccuRevExportCommand command = new AccuRevExportCommand( getLogger() );
75  
76          CommandParameters params = new CommandParameters();
77          params.setScmVersion( CommandParameter.SCM_VERSION, new ScmTag( "mySnapShot/676" ) );
78  
79          ExportScmResult result = command.export( repo, new ScmFileSet( basedir ), params );
80  
81          assertTrue( result.isSuccess() );
82          assertHasScmFile( result.getExportedFiles(), "exported/file", ScmFileStatus.CHECKED_OUT );
83  
84      }
85      
86      @Test
87      public void testExportToVersion490()
88          throws Exception
89      {
90       // info defaults to no workspace...
91          info.setWorkSpace( null );
92  
93          when( accurev.info( basedir ) ).thenReturn( info );
94          
95          // A version that does not support pop -t
96          when( accurev.getClientVersion()).thenReturn( "4.9.0" );
97  
98          List<File> poppedFiles = Collections.singletonList( new File( "exported/file" ) );
99          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 }