001package org.apache.maven.scm.provider.accurev.cli;
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.hamcrest.CoreMatchers.is;
023import static org.hamcrest.CoreMatchers.nullValue;
024import static org.hamcrest.Matchers.not;
025import static org.junit.Assert.assertThat;
026import static org.junit.Assume.assumeTrue;
027
028import java.io.BufferedReader;
029import java.io.File;
030import java.io.IOException;
031import java.io.InputStream;
032import java.io.InputStreamReader;
033import java.io.StringReader;
034import java.util.ArrayList;
035import java.util.Collections;
036import java.util.List;
037
038import org.apache.maven.scm.ScmTestCase;
039import org.apache.maven.scm.log.ScmLogger;
040import org.apache.maven.scm.provider.accurev.AccuRev;
041import org.apache.maven.scm.provider.accurev.AccuRevStat;
042import org.codehaus.plexus.util.Os;
043import org.codehaus.plexus.util.cli.CommandLineException;
044import org.codehaus.plexus.util.cli.Commandline;
045import org.codehaus.plexus.util.cli.StreamConsumer;
046import org.junit.After;
047import org.junit.Before;
048import org.junit.Test;
049import org.junit.runner.RunWith;
050import org.junit.runners.JUnit4;
051
052@RunWith( JUnit4.class )
053public class AccuRevCommandLineTest
054    extends ScmTestCase
055{
056
057    public class AccuRevCommandLineTester
058        extends AccuRevCommandLine
059    {
060
061        private BufferedReader stdinReader;
062
063        private String response;
064
065        public BufferedReader getStdinReader()
066        {
067
068            return stdinReader;
069        }
070
071        private ScmLogger initLog()
072            throws Exception
073        {
074
075            return AccuRevJUnitUtil.getLogger( getContainer() );
076        }
077
078        public AccuRevCommandLineTester()
079            throws Exception
080        {
081
082            setLogger( initLog() );
083        }
084
085        public AccuRevCommandLineTester( String host, int port )
086            throws Exception
087        {
088
089            super( host, port );
090            setLogger( initLog() );
091        }
092
093        @Override
094        protected int executeCommandLine( Commandline cl, InputStream stdin, CommandOutputConsumer stdout,
095                                          StreamConsumer stderr )
096            throws CommandLineException
097        {
098
099            if ( stdin != null )
100            {
101                stdinReader = new BufferedReader( new InputStreamReader( stdin ) );
102            }
103            else
104            {
105                stdinReader = null;
106            }
107            try
108            {
109                if ( response != null )
110                {
111                    BufferedReader reader = new BufferedReader( new StringReader( response ) );
112                    String line = reader.readLine();
113                    while ( line != null )
114                    {
115                        stdout.consumeLine( line );
116                        line = reader.readLine();
117                    }
118                }
119            }
120            catch ( IOException e )
121            {
122                throw new CommandLineException( "Unexpected error", e );
123            }
124            return 0;
125        }
126
127        public void setResponse( String response )
128        {
129
130            this.response = response;
131
132        }
133
134    }
135
136    @Before
137    @Override
138    public void setUp()
139        throws Exception
140    {
141
142        super.setUp();
143    }
144
145    @After
146    @Override
147    public void tearDown()
148        throws Exception
149    {
150
151        super.tearDown();
152    }
153
154    @Override
155    protected InputStream getCustomConfiguration()
156        throws Exception
157    {
158
159        return AccuRevJUnitUtil.getPlexusConfiguration();
160    }
161
162    @Test
163    public void testPromoteAll()
164        throws Exception
165    {
166
167        AccuRevCommandLineTester accuRevCL = new AccuRevCommandLineTester();
168
169        assertThat( accuRevCL.promoteAll( new File( "/my/workspace" ), "cmt msg" ), not( nullValue() ) );
170        Commandline lastCL = accuRevCL.getCommandline();
171        assertThat( lastCL.getWorkingDirectory(), is( new File( "/my/workspace" ).getCanonicalFile() ) );
172        assertThat( lastCL.getArguments(), is( new String[] { "promote", "-p", "-K", "-c", "cmt msg" } ) );
173
174    }
175
176    @Test
177    public void testPromote()
178        throws Exception
179    {
180
181        AccuRevCommandLineTester accuRevCL = new AccuRevCommandLineTester();
182        List<File> files = new ArrayList<File>();
183        File testfile = new File( "my/test/file" );
184        files.add( testfile );
185
186        assertThat( accuRevCL.promote( new File( "/my/workspace" ), files, "cmt msg" ), not( nullValue() ) );
187        Commandline lastCL = accuRevCL.getCommandline();
188        assertThat( lastCL.getWorkingDirectory(), is( new File( "/my/workspace" ).getCanonicalFile() ) );
189        assertThat( lastCL.getArguments(), is( new String[] { "promote", "-K", "-c", "cmt msg", testfile.getPath() } ) );
190
191    }
192
193    @Test
194    public void testLogin()
195        throws Exception
196    {
197
198        AccuRevCommandLineTester accuRevCL = new AccuRevCommandLineTester();
199        accuRevCL.setResponse( "Password: a124235bacc3ff" );
200        accuRevCL.setExecutable( "accurev.exe" );
201        accuRevCL.login( "aUser", "topSecret" );
202        Commandline lastCL = accuRevCL.getCommandline();
203
204        if ( Os.isFamily( Os.FAMILY_WINDOWS ) )
205        {
206            assertThat( lastCL.getArguments(), is( new String[] { "login", "-A", "aUser", "topSecret" } ) );
207            assertThat( accuRevCL.getStdinReader(), is( nullValue() ) );
208        }
209        else
210        {
211            assertThat( lastCL.getArguments(), is( new String[] { "login", "-A", "aUser" } ) );
212            assertThat( accuRevCL.getStdinReader().readLine(), is( "topSecret" ) );
213        }
214
215        accuRevCL.info( null );
216        assertThat( lastCL.getArguments(), is( new String[] { "info", "-A", "a124235bacc3ff" } ) );
217
218        assumeTrue( !Os.isFamily( Os.FAMILY_WINDOWS ) );
219
220        accuRevCL.login( "anOther", "opensaysme" );
221        assertThat( lastCL.getArguments(), is( new String[] { "login", "-A", "anOther" } ) );
222        assertThat( accuRevCL.getStdinReader().readLine(), is( "opensaysme" ) );
223
224        accuRevCL.login( "AUser", null );
225        assertThat( lastCL.getArguments(), is( new String[] { "login", "-A", "AUser" } ) );
226        assertThat( accuRevCL.getStdinReader().readLine(), is( "" ) );
227
228    }
229
230    @Test
231    public void testPopExternal()
232        throws Exception
233    {
234
235        AccuRevCommandLineTester accuRevCL = new AccuRevCommandLineTester( "aHost", 5051 );
236        accuRevCL.setExecutable( "accurev.exe" );
237        File testfile = new File( "/my/export" );
238        File projectDir = new File( "/./project/dir" );
239        accuRevCL.popExternal( testfile, "stream", "12", Collections.singleton( projectDir ) );
240
241        Commandline lastCL = accuRevCL.getCommandline();
242        assertThat( lastCL.getLiteralExecutable(), is( "accurev.exe" ) );
243        assertThat( lastCL.getArguments(), is( new String[] { "pop", "-H", "aHost:5051", "-v", "stream", "-L",
244            testfile.getAbsolutePath(), "-t", "12", "-R", projectDir.getPath() } ) );
245
246    }
247
248    @Test
249    public void testPopExternalWithTransactionNow()
250        throws Exception
251    {
252        AccuRevCommandLineTester accuRevCL = new AccuRevCommandLineTester( "aHost", 5051 );
253        accuRevCL.setExecutable( "accurev.exe" );
254        File testfile = new File( "/my/export" );
255        File projectDir = new File( "/./project/dir" );
256        accuRevCL.popExternal( testfile, "stream", "now", Collections.singleton( projectDir ) );
257
258        Commandline lastCL = accuRevCL.getCommandline();
259        assertThat( lastCL.getLiteralExecutable(), is( "accurev.exe" ) );
260        assertThat( lastCL.getArguments(), is( new String[] { "pop", "-H", "aHost:5051", "-v", "stream", "-L",
261            testfile.getAbsolutePath(),  "-R", projectDir.getPath() } ) );
262    }
263
264    @Test
265    public void testPopExternalWithTransactionNull()
266        throws Exception
267    {
268        AccuRevCommandLineTester accuRevCL = new AccuRevCommandLineTester( "aHost", 5051 );
269        accuRevCL.setExecutable( "accurev.exe" );
270        File testfile = new File( "/my/export" );
271        File projectDir = new File( "/./project/dir" );
272        accuRevCL.popExternal( testfile, "stream", null, Collections.singleton( projectDir ) );
273
274        Commandline lastCL = accuRevCL.getCommandline();
275        assertThat( lastCL.getLiteralExecutable(), is( "accurev.exe" ) );
276        assertThat( lastCL.getArguments(), is( new String[] { "pop", "-H", "aHost:5051", "-v", "stream", "-L",
277            testfile.getAbsolutePath(), "-R", projectDir.getPath() } ) );
278    }
279
280    @Test
281    public void testPopWorkSpace()
282        throws Exception
283    {
284
285        AccuRevCommandLineTester accuRevCL = new AccuRevCommandLineTester();
286        accuRevCL.setExecutable( "accurev.exe" );
287
288        File testFile = new File( "project/dir" );
289        accuRevCL.pop( new File( "/home/workspace" ), Collections.singleton( testFile ) );
290
291        Commandline lastCL = accuRevCL.getCommandline();
292        assertThat( lastCL.getLiteralExecutable(), is( "accurev.exe" ) );
293        // take care of symlink
294        if (lastCL.getWorkingDirectory().getCanonicalFile().equals( lastCL.getWorkingDirectory().getAbsoluteFile() ))
295        {
296            assertThat( lastCL.getWorkingDirectory(), is( new File( "/home/workspace" ).getCanonicalFile() ) );
297        } else {
298            assertThat( lastCL.getWorkingDirectory(), is( new File( "/home/workspace" ).getAbsoluteFile() ));// .getCanonicalFile() ) );
299        }
300
301        assertThat( lastCL.getArguments(), is( new String[] { "pop", "-R", testFile.getPath() } ) );
302
303    }
304
305    @Test
306    public void testMkws()
307        throws Exception
308    {
309
310        AccuRevCommandLineTester accuRevCL = new AccuRevCommandLineTester();
311        accuRevCL.setExecutable( "accurev2.exe" );
312        File workspaceFile = new File( "/my/workspace/location" );
313        accuRevCL.mkws( "myStream", "myWorkSpaceName", workspaceFile );
314
315        Commandline lastCL = accuRevCL.getCommandline();
316        assertThat( lastCL.getLiteralExecutable(), is( "accurev2.exe" ) );
317        assertThat( lastCL.getWorkingDirectory(), is( workspaceFile.getCanonicalFile() ) );
318        assertThat( lastCL.getArguments(), is( new String[] { "mkws", "-b", "myStream", "-w", "myWorkSpaceName", "-l",
319            workspaceFile.getAbsolutePath() } ) );
320
321    }
322
323    @Test
324    public void testUpdate()
325        throws Exception
326    {
327
328        AccuRevCommandLineTester accuRevCL = new AccuRevCommandLineTester();
329        File workspaceFile = new File( "/my/ws/loc" );
330        accuRevCL.update( workspaceFile, "highest" );
331
332        Commandline lastCL = accuRevCL.getCommandline();
333        assertThat( lastCL.getWorkingDirectory(), is( workspaceFile.getCanonicalFile() ) );
334        assertThat( lastCL.getArguments(), is( new String[] { "update", "-t", "highest" } ) );
335
336    }
337
338    @Test
339    public void testInfo()
340        throws Exception
341    {
342
343        AccuRevCommandLineTester accuRevCL = new AccuRevCommandLineTester();
344        accuRevCL.info( new File( "/my/base/dir" ) );
345
346        Commandline lastCL = accuRevCL.getCommandline();
347        assertThat( lastCL.getWorkingDirectory(), is( new File( "/my/base/dir" ).getCanonicalFile() ) );
348        assertThat( lastCL.getArguments(), is( new String[] { "info" } ) );
349
350    }
351
352    @Test
353    public void testRemoveWorkspace()
354        throws Exception
355    {
356
357        AccuRevCommandLineTester accuRevCL = new AccuRevCommandLineTester();
358        accuRevCL.rmws( "myWorkspaceName" );
359
360        Commandline lastCL = accuRevCL.getCommandline();
361        assertThat( lastCL.getArguments(), is( new String[] { "rmws", "-s", "myWorkspaceName" } ) );
362
363    }
364
365    @Test
366    public void testStatIgnored()
367        throws Exception
368    {
369
370        AccuRevCommandLineTester accuRevCL = new AccuRevCommandLineTester();
371        File testFile = new File( "/my/base/dir" );
372        accuRevCL.stat( testFile );
373
374        Commandline lastCL = accuRevCL.getCommandline();
375        assertThat( lastCL.getArguments(), is( new String[] { "stat", "-fx", testFile.getAbsolutePath() } ) );
376
377    }
378
379    @Test
380    public void testReactivate()
381        throws Exception
382    {
383
384        AccuRevCommandLineTester accuRevCL = new AccuRevCommandLineTester();
385        accuRevCL.reactivate( "ArANdomWorkspaceName" );
386
387        Commandline lastCL = accuRevCL.getCommandline();
388        assertThat( lastCL.getArguments(), is( new String[] { "reactivate", "wspace", "ArANdomWorkspaceName" } ) );
389
390    }
391
392    @Test
393    public void testReset()
394        throws Exception
395    {
396
397        AccuRevCommandLineTester accuRevCL = new AccuRevCommandLineTester();
398        Commandline commandline = accuRevCL.getCommandline();
399        Commandline cl = commandline;
400        List<String> shellCmds = cl.getShell().getShellCommandLine( cl.getArguments() );
401        accuRevCL.reset();
402        assertThat( cl.getShell().getShellCommandLine( cl.getArguments() ), is( shellCmds ) );
403        assertThat( commandline.getLiteralExecutable(), is( "accurev" ) );
404    }
405
406    @Test
407    public void testAdd()
408        throws Exception
409    {
410
411        AccuRevCommandLineTester accuRevCL = new AccuRevCommandLineTester();
412        List<File> files = new ArrayList<File>();
413        File testFile = new File( "my/test/file" );
414        files.add( testFile );
415        assertThat( accuRevCL.add( new File( "/workspace" ), files, "my commit message" ), not( nullValue() ) );
416
417        Commandline lastCL = accuRevCL.getCommandline();
418        assertThat( lastCL.getWorkingDirectory(), is( new File( "/workspace" ).getCanonicalFile() ) );
419        assertThat( lastCL.getArguments(), is( new String[] { "add", "-c", "my commit message", testFile.getPath() } ) );
420
421        assertThat( accuRevCL.add( new File( "/workspace" ), files, "" ), not( nullValue() ) );
422        assertThat( lastCL.getArguments(), is( new String[] { "add", "-c", AccuRev.DEFAULT_ADD_MESSAGE,
423            testFile.getPath() } ) );
424
425    }
426
427    @SuppressWarnings( "unchecked" )
428    @Test
429    public void testRemove()
430        throws Exception
431    {
432
433        AccuRevCommandLineTester accuRevCL = new AccuRevCommandLineTester();
434        List<File> files = new ArrayList<File>();
435        File testFile = new File( "my/test/file" );
436        files.add( testFile );
437        File workspaceFile = new File( "/workspace" );
438        assertThat( accuRevCL.defunct( workspaceFile, files, "my commit message" ), not( nullValue() ) );
439
440        Commandline lastCL = accuRevCL.getCommandline();
441        assertThat( lastCL.getWorkingDirectory(), is( workspaceFile.getCanonicalFile() ) );
442        assertThat( lastCL.getArguments(),
443                    is( new String[] { "defunct", "-c", "my commit message", testFile.getPath() } ) );
444
445        assertThat( accuRevCL.defunct( workspaceFile, files, "" ), not( nullValue() ) );
446        assertThat( lastCL.getArguments(), is( new String[] { "defunct", "-c", AccuRev.DEFAULT_REMOVE_MESSAGE,
447            testFile.getPath() } ) );
448
449        assertThat( accuRevCL.defunct( workspaceFile, Collections.EMPTY_LIST, "" ), not( nullValue() ) );
450        assertThat( lastCL.getArguments(), is( new String[] { "defunct", "-c", AccuRev.DEFAULT_REMOVE_MESSAGE, "." } ) );
451
452        assertThat( accuRevCL.defunct( workspaceFile, null, "" ), not( nullValue() ) );
453        assertThat( lastCL.getArguments(), is( new String[] { "defunct", "-c", AccuRev.DEFAULT_REMOVE_MESSAGE, "." } ) );
454
455    }
456
457    @Test
458    public void testChangeWorkspace()
459        throws Exception
460    {
461
462        AccuRevCommandLineTester accuRevCL = new AccuRevCommandLineTester();
463        accuRevCL.chws( new File( "/my/workspace" ), "the_workspace_name_me", "a-snapshot" );
464
465        Commandline lastCL = accuRevCL.getCommandline();
466        assertThat( lastCL.getWorkingDirectory(), is( new File( "/my/workspace" ).getCanonicalFile() ) );
467        assertThat( lastCL.getArguments(), is( new String[] { "chws", "-s", "the_workspace_name_me", "-b",
468            "a-snapshot", "-l", "." } ) );
469
470    }
471
472    @Test
473    public void testMkSnap()
474        throws Exception
475    {
476
477        AccuRevCommandLineTester accuRevCL = new AccuRevCommandLineTester();
478        accuRevCL.mksnap( "a-snapshot", "basisStream" );
479
480        Commandline lastCL = accuRevCL.getCommandline();
481        assertThat( lastCL.getArguments(), is( new String[] { "mksnap", "-s", "a-snapshot", "-b", "basisStream", "-t",
482            "now" } ) );
483
484    }
485
486    @Test
487    public void testStatTag()
488        throws Exception
489    {
490
491        AccuRevCommandLineTester accuRevCL = new AccuRevCommandLineTester();
492        accuRevCL.statTag( "a-snapshot" );
493
494        Commandline lastCL = accuRevCL.getCommandline();
495        assertThat( lastCL.getArguments(), is( new String[] { "stat", "-a", "-ffl", "-s", "a-snapshot" } ) );
496
497    }
498
499    @Test
500    public void testStatBackingStream()
501        throws Exception
502    {
503
504        AccuRevCommandLineTester accuRevCL = new AccuRevCommandLineTester();
505
506        File basedir = new File( "/my/workspace" );
507        List<File> elements = new ArrayList<File>( 1 );
508        File addedOrModifiedFile = new File( "addedOrModified/file" );
509        elements.add( addedOrModifiedFile );
510        accuRevCL.statBackingStream( basedir, elements );
511        Commandline lastCL = accuRevCL.getCommandline();
512        assertThat( lastCL.getWorkingDirectory(), is( basedir.getCanonicalFile() ) );
513        assertThat( lastCL.getArguments(), is( new String[] { "stat", "-b", "-ffr", addedOrModifiedFile.getPath() } ) );
514
515    }
516
517    @Test
518    public void testStatRecursive()
519        throws Exception
520    {
521
522        File basedir = new File( "/my/workspace" );
523        List<File> noFiles = new ArrayList<File>();
524
525        AccuRevCommandLineTester accuRevCL = new AccuRevCommandLineTester();
526        accuRevCL.stat( basedir, noFiles, AccuRevStat.KEPT );
527        Commandline lastCL = accuRevCL.getCommandline();
528        assertThat( lastCL.getWorkingDirectory(), is( basedir.getCanonicalFile() ) );
529        assertThat( lastCL.getArguments(), is( new String[] { "stat", "-ffr", "-k", "-R", "." } ) );
530
531        noFiles.add( new File( "." ) );
532        accuRevCL.stat( basedir, noFiles, AccuRevStat.DEFUNCT );
533        lastCL = accuRevCL.getCommandline();
534        assertThat( lastCL.getWorkingDirectory(), is( basedir.getCanonicalFile() ) );
535        assertThat( lastCL.getArguments(), is( new String[] { "stat", "-ffr", "-D", "-R", "." } ) );
536    }
537
538    @Test
539    public void testStatSpecificFilesAndDirectories()
540        throws Exception
541    {
542
543        File basedir = new File( "/my/workspace" );
544        List<File> files = new ArrayList<File>();
545        File testDir = new File( "a/dir" );
546        files.add( testDir );
547        File testFile = new File( "a/dir/a.file" );
548        files.add( testFile );
549
550        AccuRevCommandLineTester accuRevCL = new AccuRevCommandLineTester();
551        accuRevCL.stat( basedir, files, AccuRevStat.MISSING );
552        Commandline lastCL = accuRevCL.getCommandline();
553        assertThat( lastCL.getWorkingDirectory(), is( basedir.getCanonicalFile() ) );
554        assertThat( lastCL.getArguments(), is( new String[] { "stat", "-ffr", "-M", testDir.getPath(),
555            testFile.getPath() } ) );
556
557    }
558
559    @Test
560    public void testAnnotate()
561        throws Exception
562    {
563
564        File basedir = new File( "/my/workspace" );
565        File file = new File( "src/main/java/foo.java" );
566
567        AccuRevCommandLineTester accuRevCL = new AccuRevCommandLineTester();
568        accuRevCL.annotate( basedir, file );
569
570        Commandline lastCL = accuRevCL.getCommandline();
571        assertThat( lastCL.getWorkingDirectory(), is( basedir.getCanonicalFile() ) );
572        assertThat( lastCL.getArguments(), is( new String[] { "annotate", "-ftud", file.getPath() } ) );
573
574    }
575
576    @Test
577    public void testDiff()
578        throws Exception
579    {
580        AccuRevCommandLineTester accuRevCL = new AccuRevCommandLineTester();
581
582        accuRevCL.diff( "myStream", "fromSpec", "toSpec" );
583        Commandline lastCL = accuRevCL.getCommandline();
584        assertThat( lastCL.getArguments(), is( new String[] { "diff", "-fx", "-a", "-i", "-v", "myStream", "-V",
585            "myStream", "-t", "fromSpec-toSpec" } ) );
586
587    }
588
589    @Test
590    public void testShowStream()
591        throws Exception
592    {
593        AccuRevCommandLineTester accuRevCL = new AccuRevCommandLineTester();
594        accuRevCL.showStream( "mystream" );
595        Commandline lastCL = accuRevCL.getCommandline();
596        assertThat( lastCL.getArguments(), is( new String[] { "show", "-s", "mystream", "-fx", "streams" } ) );
597        ;
598    }
599}