001package org.apache.maven.scm.provider.accurev.command.update;
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.junit.Assert.assertThat;
026import static org.mockito.Matchers.any;
027import static org.mockito.Matchers.eq;
028import static org.mockito.Mockito.when;
029
030import java.io.File;
031import java.util.Arrays;
032import java.util.Collections;
033import java.util.Date;
034import java.util.List;
035import java.util.Map;
036
037import org.apache.maven.scm.CommandParameter;
038import org.apache.maven.scm.CommandParameters;
039import org.apache.maven.scm.ScmFileSet;
040import org.apache.maven.scm.ScmFileStatus;
041import org.apache.maven.scm.command.update.UpdateScmResult;
042import org.apache.maven.scm.provider.accurev.Transaction;
043import org.apache.maven.scm.provider.accurev.WorkSpace;
044import org.apache.maven.scm.provider.accurev.command.AbstractAccuRevCommandTest;
045import org.hamcrest.core.IsInstanceOf;
046import org.junit.Test;
047
048public class AccurevUpdateCommandTest
049    extends AbstractAccuRevCommandTest
050{
051
052    private ScmFileSet testFileSet;
053
054    private File basedir;
055
056    @Override
057    public void setUp()
058        throws Exception
059    {
060        super.setUp();
061        testFileSet = new ScmFileSet( new File( "/my/workspace/project/dir" ) );
062        basedir = testFileSet.getBasedir();
063
064        info.setWorkSpace( "theWorkSpace" );
065        when( accurev.info( basedir ) ).thenReturn( info );
066
067    }
068
069    @Test
070    public void testUpdate()
071        throws Exception
072    {
073
074        final File keptFile = new File( "updated/file" );
075        final File keptAdded = new File( "new/file" );
076
077        List<File> files = Arrays.asList( keptFile, keptAdded );
078
079        when( accurev.update( eq( basedir ), any( String.class ) ) ).thenReturn( files );
080
081        AccuRevUpdateCommand command = new AccuRevUpdateCommand( getLogger() );
082
083        CommandParameters commandParameters = new CommandParameters();
084        commandParameters.setString( CommandParameter.RUN_CHANGELOG_WITH_UPDATE, Boolean.toString( false ) );
085        UpdateScmResult result = command.update( repo, testFileSet, commandParameters );
086
087        assertThat( result.isSuccess(), is( true ) );
088        assertThat( result.getUpdatedFiles().size(), is( 2 ) );
089        assertHasScmFile( result.getUpdatedFiles(), "updated/file", ScmFileStatus.UPDATED );
090        assertHasScmFile( result.getUpdatedFiles(), "new/file", ScmFileStatus.UPDATED );
091
092    }
093
094    @Test
095    public void testUpdateWithChangeLog()
096        throws Exception
097    {
098
099        final WorkSpace wsBefore = new WorkSpace( "theWorkSpace", 123 );
100
101        Map<String, WorkSpace> workspaces = Collections.singletonMap( "theWorkSpace", wsBefore );
102
103        when( accurev.showWorkSpaces() ).thenReturn( workspaces );
104
105        List<File> emptyList = Collections.emptyList();
106        when( accurev.update( eq( basedir ), any( String.class ) ) ).thenReturn( emptyList );
107
108        final Date currentDate = new Date();
109        List<Transaction> transactions =
110            Collections.singletonList( new Transaction( 197L, currentDate, "type", "user" ) );
111
112        when(
113              accurev.history( any( String.class ), any( String.class ), any( String.class ), eq( 1 ), eq( true ),
114                               eq( true ) ) ).thenReturn( transactions );
115
116        AccuRevUpdateCommand command = new AccuRevUpdateCommand( getLogger() );
117
118        CommandParameters commandParameters = new CommandParameters();
119        commandParameters.setString( CommandParameter.RUN_CHANGELOG_WITH_UPDATE, Boolean.toString( true ) );
120        UpdateScmResult result = command.update( repo, testFileSet, commandParameters );
121
122        assertThat( result.isSuccess(), is( true ) );
123        assertThat( result, IsInstanceOf.instanceOf( AccuRevUpdateScmResult.class ) );
124        AccuRevUpdateScmResult accuRevResult = (AccuRevUpdateScmResult) result;
125        assertThat( accuRevResult.getFromRevision(), is( "theWorkSpace/123" ) );
126        assertThat( accuRevResult.getToRevision(), is( "theWorkSpace/197" ) );
127
128    }
129
130    @Test
131    public void testAccuRevFailure()
132        throws Exception
133    {
134        final ScmFileSet testFileSet = new ScmFileSet( new File( "/my/workspace/project/dir" ) );
135        final File basedir = testFileSet.getBasedir();
136
137        info.setWorkSpace( "theWorkSpace" );
138
139        when( accurev.update( eq( basedir ), any( String.class ) ) ).thenReturn( null );
140
141        AccuRevUpdateCommand command = new AccuRevUpdateCommand( getLogger() );
142
143        CommandParameters commandParameters = new CommandParameters();
144        commandParameters.setString( CommandParameter.RUN_CHANGELOG_WITH_UPDATE, Boolean.toString( false ) );
145        UpdateScmResult result = command.update( repo, testFileSet, commandParameters );
146
147        assertThat( result.isSuccess(), is( false ) );
148        assertThat( result.getProviderMessage(), notNullValue() );
149
150    }
151}