001package org.apache.maven.scm.provider.accurev.command.checkin;
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.lenient;
027import static org.mockito.Mockito.when;
028
029import java.io.File;
030import java.util.ArrayList;
031import java.util.Arrays;
032import java.util.List;
033
034import org.apache.maven.scm.CommandParameter;
035import org.apache.maven.scm.CommandParameters;
036import org.apache.maven.scm.ScmException;
037import org.apache.maven.scm.ScmFileSet;
038import org.apache.maven.scm.ScmFileStatus;
039import org.apache.maven.scm.command.checkin.CheckInScmResult;
040import org.apache.maven.scm.provider.accurev.AccuRevException;
041import org.apache.maven.scm.provider.accurev.AccuRevInfo;
042import org.apache.maven.scm.provider.accurev.command.AbstractAccuRevCommandTest;
043import org.junit.Test;
044
045public class AccuRevCheckInCommandTest
046    extends AbstractAccuRevCommandTest
047{
048
049    @Test
050    public void testCheckInRecursive()
051        throws Exception
052    {
053        // Setup test data so that the checkin area is the repo's project path.
054        final ScmFileSet testFileSet = new ScmFileSet( new File( basedir, "project/dir" ) );
055        final File basedir = testFileSet.getBasedir();
056
057        final AccuRevInfo info = new AccuRevInfo( basedir );
058        info.setTop( basedir.getAbsolutePath() );
059
060        when( accurev.info( basedir ) ).thenReturn( info );
061
062        List<File> promotedFiles = Arrays.asList( new File( "kept/file" ), new File( "promoted/file" ) );
063        when( accurev.promoteAll( basedir, "A commit message" ) ).thenReturn( promotedFiles );
064
065        AccuRevCheckInCommand command = new AccuRevCheckInCommand( getLogger() );
066
067        CommandParameters commandParameters = new CommandParameters();
068        commandParameters.setString( CommandParameter.MESSAGE, "A commit message" );
069        CheckInScmResult result = command.checkIn( repo, testFileSet, commandParameters );
070
071        assertThat( result.isSuccess(), is( true ) );
072        assertThat( result.getCheckedInFiles().size(), is( 2 ) );
073        assertHasScmFile( result.getCheckedInFiles(), "kept/file", ScmFileStatus.CHECKED_IN );
074        assertHasScmFile( result.getCheckedInFiles(), "promoted/file", ScmFileStatus.CHECKED_IN );
075    }
076
077    @Test
078    public void testCheckInFailure()
079        throws Exception
080    {
081        // Setup test data so that the checkin area is the repo's project path.
082        final ScmFileSet testFileSet = new ScmFileSet( new File( basedir, "project/dir" ) );
083        final File basedir = testFileSet.getBasedir();
084
085        final AccuRevInfo info = new AccuRevInfo( basedir );
086        info.setTop( basedir.getAbsolutePath() );
087
088        when( accurev.info( basedir ) ).thenReturn( info );
089
090        when( accurev.promoteAll( basedir, "A commit message" ) ).thenReturn( null );
091
092        AccuRevCheckInCommand command = new AccuRevCheckInCommand( getLogger() );
093
094        CommandParameters commandParameters = new CommandParameters();
095        commandParameters.setString( CommandParameter.MESSAGE, "A commit message" );
096        CheckInScmResult result = command.checkIn( repo, testFileSet, commandParameters );
097
098        assertThat( result.isSuccess(), is( false ) );
099        assertThat( result.getProviderMessage(), notNullValue() );
100    }
101
102    @Test( expected = ScmException.class )
103    public void testCheckinRecursiveSubDirectoryNotSupported()
104        throws AccuRevException, ScmException
105    {
106        final ScmFileSet testFileSet = new ScmFileSet( basedir );
107        final AccuRevInfo info = new AccuRevInfo( basedir );
108        info.setTop( basedir.getParent() );
109
110        // TODO test basedir is top + project path. is OK.
111        when( accurev.info( basedir ) ).thenReturn( info );
112
113        AccuRevCheckInCommand command = new AccuRevCheckInCommand( getLogger() );
114
115        CommandParameters commandParameters = new CommandParameters();
116        commandParameters.setString( CommandParameter.MESSAGE, "Commit message" );
117        command.checkIn( repo, testFileSet, commandParameters );
118        fail( "Expected ScmException" );
119    }
120
121    @Test
122    public void testCheckinExplicitFiles()
123        throws Exception
124    {
125        final List<File> files = new ArrayList<File>();
126
127        files.add( new File( "project/dir/pom.xml" ) );
128        files.add( new File( "project/dir/src/main/java/Bar.java" ) );
129
130        final ScmFileSet testFileSet = new ScmFileSet( basedir, files );
131
132        lenient().when( accurev.info( basedir ) ).thenReturn( info );
133
134        when( accurev.promote( basedir, files, "A commit message" ) ).thenReturn( files );
135
136        AccuRevCheckInCommand command = new AccuRevCheckInCommand( getLogger() );
137
138        CommandParameters commandParameters = new CommandParameters();
139        commandParameters.setString( CommandParameter.MESSAGE, "A commit message" );
140        CheckInScmResult result = command.checkIn( repo, testFileSet, commandParameters );
141
142        assertThat( result.isSuccess(), is( true ) );
143        assertThat( result.getCheckedInFiles().size(), is( 2 ) );
144        assertHasScmFile( result.getCheckedInFiles(), "project/dir/pom.xml", ScmFileStatus.CHECKED_IN );
145        assertHasScmFile( result.getCheckedInFiles(), "project/dir/src/main/java/Bar.java", ScmFileStatus.CHECKED_IN );
146
147    }
148}