001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *   http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.apache.maven.scm.provider.git.command.checkin;
020
021import java.io.File;
022import java.io.IOException;
023
024import org.apache.maven.scm.PlexusJUnit4TestCase;
025import org.apache.maven.scm.ScmFileSet;
026import org.apache.maven.scm.command.checkin.CheckInScmResult;
027import org.apache.maven.scm.command.checkout.CheckOutScmResult;
028import org.apache.maven.scm.provider.git.GitScmTestUtils;
029import org.apache.maven.scm.repository.ScmRepository;
030import org.apache.maven.scm.tck.command.checkin.CheckInCommandTckTest;
031import org.codehaus.plexus.util.FileUtils;
032import org.junit.Test;
033
034import static org.junit.Assert.assertFalse;
035
036/**
037 * @author <a href="mailto:struberg@yahoo.de">Mark Struberg</a>
038 *
039 */
040public abstract class GitCheckInCommandTckTest extends CheckInCommandTckTest {
041
042    /** {@inheritDoc} */
043    public void initRepo() throws Exception {
044        GitScmTestUtils.initRepo("src/test/resources/repository/", getRepositoryRoot(), getWorkingDirectory());
045    }
046
047    @Override
048    protected CheckOutScmResult checkOut(File workingDirectory, ScmRepository repository) throws Exception {
049        try {
050            return super.checkOut(workingDirectory, repository);
051        } finally {
052            GitScmTestUtils.setDefaulGitConfig(workingDirectory);
053        }
054    }
055
056    @Test
057    public void testUpToDatePush() throws Exception {
058        File checkedOutRepo = getWorkingCopy();
059
060        ScmRepository scmRepository = getScmManager().makeScmRepository(getScmUrl());
061        checkoutRepoInto(checkedOutRepo, scmRepository);
062
063        // Add a default user to the config
064        GitScmTestUtils.setDefaulGitConfig(checkedOutRepo);
065
066        CheckInScmResult result =
067                getScmManager().checkIn(scmRepository, new ScmFileSet(checkedOutRepo), "No change commit message");
068
069        assertResultIsSuccess(result);
070    }
071
072    @Test
073    public void testRejectedNonFastForwardPush() throws Exception {
074        File blockingRepo = PlexusJUnit4TestCase.getTestFile("target/scm-test/blocking-repo");
075        File rejectedRepo = PlexusJUnit4TestCase.getTestFile("target/scm-test/rejected-repo");
076
077        ScmRepository scmRepository = getScmManager().makeScmRepository(getScmUrl());
078        checkoutRepoInto(rejectedRepo, scmRepository);
079        checkoutRepoInto(blockingRepo, scmRepository);
080
081        // Add a default user to the config
082        GitScmTestUtils.setDefaulGitConfig(rejectedRepo);
083        GitScmTestUtils.setDefaulGitConfig(blockingRepo);
084
085        ScmFileSet blockingFileSet = createWorkspaceChange(rejectedRepo);
086
087        CheckInScmResult blockingResult = getScmManager().checkIn(scmRepository, blockingFileSet, "Blocking commit");
088        assertResultIsSuccess(blockingResult);
089
090        ScmFileSet rejectedFileSet = createWorkspaceChange(blockingRepo);
091
092        CheckInScmResult checkInScmResult = getScmManager().checkIn(scmRepository, rejectedFileSet, "Rejected commit");
093        assertFalse(
094                "check-in should have been rejected since fast forward was not possible", checkInScmResult.isSuccess());
095    }
096
097    private CheckOutScmResult checkoutRepoInto(File workingCopy, ScmRepository scmRepository) throws Exception {
098        FileUtils.deleteDirectory(workingCopy);
099        workingCopy.mkdir();
100
101        CheckOutScmResult result = getScmManager().checkOut(scmRepository, new ScmFileSet(workingCopy), null);
102
103        assertResultIsSuccess(result);
104        return result;
105    }
106
107    private ScmFileSet createWorkspaceChange(File repo) throws IOException {
108        File beerFile = new File(repo.getAbsolutePath(), "beer.xml");
109        FileUtils.fileWrite(beerFile.getAbsolutePath(), "1 litre");
110        return new ScmFileSet(repo, beerFile.getName());
111    }
112}