001package org.apache.maven.scm.provider.accurev.command.changelog;
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.ChangeSetMatcher.changeSet;
023import static org.hamcrest.Matchers.is;
024import static org.junit.Assert.assertThat;
025
026import java.io.File;
027import java.io.InputStream;
028import java.util.ArrayList;
029import java.util.Date;
030import java.util.List;
031
032import org.apache.maven.scm.ChangeSet;
033import org.apache.maven.scm.ScmBranch;
034import org.apache.maven.scm.ScmFileSet;
035import org.apache.maven.scm.ScmTestCase;
036import org.apache.maven.scm.command.changelog.ChangeLogScmResult;
037import org.apache.maven.scm.command.checkin.CheckInScmResult;
038import org.apache.maven.scm.provider.ScmProvider;
039import org.apache.maven.scm.provider.accurev.cli.AccuRevCommandLine;
040import org.apache.maven.scm.provider.accurev.cli.AccuRevJUnitUtil;
041import org.apache.maven.scm.provider.accurev.command.AccuRevTckUtil;
042import org.apache.maven.scm.repository.ScmRepository;
043import org.apache.maven.scm.tck.command.changelog.ChangeLogCommandTckTest;
044import org.hamcrest.Matchers;
045import org.junit.After;
046import org.junit.Before;
047import org.junit.Test;
048import org.junit.runner.RunWith;
049import org.junit.runners.JUnit4;
050
051@RunWith( JUnit4.class )
052public class AccuRevChangeLogCommandTckTest
053    extends ChangeLogCommandTckTest
054{
055
056    protected AccuRevTckUtil accurevTckTestUtil = new AccuRevTckUtil();
057
058    @Override
059    @Test
060    public void testChangeLogCommand()
061        throws Exception
062    {
063        super.testChangeLogCommand();
064    }
065
066    @SuppressWarnings( "unchecked" )
067    @Test
068    public void testUpstreamChangesIncludedInChangeLog()
069        throws Exception
070    {
071
072        AccuRevCommandLine accurev = accurevTckTestUtil.getAccuRevCL();
073
074        // UpdatingCopy is a workspace rooted at a substream
075        String workingStream = accurevTckTestUtil.getWorkingStream();
076        String subStream = accurevTckTestUtil.getDepotName() + "_sub_stream";
077        accurev.mkstream( workingStream, subStream );
078
079        ScmRepository mainRepository = getScmRepository();
080        ScmProvider provider = getScmManager().getProviderByRepository( mainRepository );
081
082        // Create a workspace at the updating copy location backed by the substream
083        ScmBranch branch = new ScmBranch( "sub_stream" );
084        provider.checkOut( mainRepository, new ScmFileSet( getUpdatingCopy() ), branch );
085
086        Thread.sleep( 1000 );
087        ScmFileSet fileSet = new ScmFileSet( getWorkingCopy() );
088
089        // Make a timestamp that we know are after initial revision but before the second
090        Date timeBeforeUpstreamCheckin = new Date(); // Current time
091
092        // pause a couple seconds... [SCM-244]
093        Thread.sleep( 2000 );
094
095        // Make a change to the readme.txt and commit the change
096        ScmTestCase.makeFile( getWorkingCopy(), "/readme.txt", "changed readme.txt" );
097        ScmTestCase.makeFile( getWorkingCopy(), "/src/test/java/Test.java", "changed Test.java" );
098        CheckInScmResult checkInResult = provider.checkIn( mainRepository, fileSet, "upstream workspace promote" );
099        assertTrue( "Unable to checkin changes to the repository", checkInResult.isSuccess() );
100
101        Thread.sleep( 2000 );
102
103        Date timeBeforeDownstreamCheckin = new Date();
104
105        Thread.sleep( 2000 );
106
107        ScmFileSet updateFileSet = new ScmFileSet( getUpdatingCopy() );
108        provider.update( mainRepository, updateFileSet );
109        ScmTestCase.makeFile( getUpdatingCopy(), "/pom.xml", "changed pom.xml" );
110        ScmTestCase.makeFile( getUpdatingCopy(), "/src/test/java/Test.java", "changed again Test.java" );
111        checkInResult = provider.checkIn( mainRepository, updateFileSet, "downstream workspace promote" );
112        assertTrue( "Unable to checkin changes to the repository", checkInResult.isSuccess() );
113
114        Thread.sleep( 2000 );
115
116        Date timeBeforeDownstreamPromote = new Date();
117
118        List<File> promotedFiles = new ArrayList<File>();
119        accurev.promoteStream( subStream, "stream promote", promotedFiles );
120
121        Thread.sleep( 2000 );
122
123        Date timeEnd = new Date();
124
125        // Changelog beforeUpstreamCheckin to end should contain both upstream and downstream changes. upstream change
126        // should NOT include Test.java
127
128        ChangeLogScmResult result =
129            provider.changeLog( mainRepository, fileSet, timeBeforeUpstreamCheckin, timeEnd, 0, branch );
130        assertTrue( "changelog beforeUpstreamCheckin to end", result.isSuccess() );
131
132        List<ChangeSet> changeSets = result.getChangeLog().getChangeSets();
133        assertThat( changeSets.size(), is( 2 ) );
134        assertThat( changeSets, Matchers.<ChangeSet>hasItems( changeSet( "Upstream changes", "/readme.txt" ),
135                                                              changeSet( "downstream workspace promote", "/./pom.xml",
136                                                                         "/./src/test/java/Test.java" ) ) );
137
138        // Changelog beforeUpstreamCheckin to beforeDownstreamCheckin should include just upstream change including
139        // Test.java
140        result =
141            provider.changeLog( mainRepository, fileSet, timeBeforeUpstreamCheckin, timeBeforeDownstreamCheckin, 0,
142                                branch );
143        assertTrue( "changelog beforeUpstreamCheckin to beforeDownstreamCheckin", result.isSuccess() );
144
145        changeSets = result.getChangeLog().getChangeSets();
146        assertThat( changeSets.size(), is( 1 ) );
147        assertThat( changeSets.get( 0 ), changeSet( "Upstream changes", "/readme.txt", "/src/test/java/Test.java" ) );
148
149        // Changelog beforeDownstreamCheckin to end should include just downstream change
150        result = provider.changeLog( mainRepository, fileSet, timeBeforeDownstreamCheckin, timeEnd, 0, branch );
151        assertTrue( "changelog beforeDownstreamCheckin to end", result.isSuccess() );
152
153        changeSets = result.getChangeLog().getChangeSets();
154        assertThat( changeSets.size(), is( 1 ) );
155        assertThat( changeSets.get( 0 ), changeSet( "downstream workspace promote", "/./pom.xml",
156                                                    "/./src/test/java/Test.java" ) );
157
158        // Changelog beforeDownstreamPromote to end should be empty
159        result = provider.changeLog( mainRepository, fileSet, timeBeforeDownstreamPromote, timeEnd, 0, branch );
160        assertTrue( "changelog beforeDownstreamPromote to end", result.isSuccess() );
161
162        changeSets = result.getChangeLog().getChangeSets();
163        assertThat( changeSets.size(), is( 0 ) );
164
165    }
166
167    @Override
168    @Before
169    public void setUp()
170        throws Exception
171    {
172        super.setUp();
173    }
174
175    @Override
176    protected File getWorkingCopy()
177    {
178        return accurevTckTestUtil.getWorkingCopy();
179    }
180
181    @Override
182    protected File getAssertionCopy()
183    {
184        return accurevTckTestUtil.getAssertionCopy();
185    }
186
187    @Override
188    protected File getUpdatingCopy()
189    {
190        return accurevTckTestUtil.getUpdatingCopy();
191    }
192
193    @Override
194    public String getScmUrl()
195        throws Exception
196    {
197        return accurevTckTestUtil.getScmUrl();
198    }
199
200    @Override
201    public void initRepo()
202        throws Exception
203    {
204        accurevTckTestUtil.initRepo( getContainer() );
205
206    }
207
208    @Override
209    @After
210    public void tearDown()
211        throws Exception
212    {
213        try
214        {
215            accurevTckTestUtil.tearDown();
216            accurevTckTestUtil.removeWorkSpace( getWorkingCopy() );
217            accurevTckTestUtil.removeWorkSpace( getAssertionCopy() );
218        }
219        finally
220        {
221            super.tearDown();
222        }
223    }
224
225    @Override
226    protected InputStream getCustomConfiguration()
227        throws Exception
228
229    {
230        return AccuRevJUnitUtil.getPlexusConfiguration();
231    }
232
233}