View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.scm.provider.git.command.checkin;
20  
21  import java.io.File;
22  import java.io.IOException;
23  
24  import org.apache.maven.scm.PlexusJUnit4TestCase;
25  import org.apache.maven.scm.ScmFileSet;
26  import org.apache.maven.scm.command.checkin.CheckInScmResult;
27  import org.apache.maven.scm.command.checkout.CheckOutScmResult;
28  import org.apache.maven.scm.provider.git.GitScmTestUtils;
29  import org.apache.maven.scm.repository.ScmRepository;
30  import org.apache.maven.scm.tck.command.checkin.CheckInCommandTckTest;
31  import org.codehaus.plexus.util.FileUtils;
32  import org.junit.Test;
33  
34  import static org.junit.Assert.assertFalse;
35  
36  /**
37   * @author <a href="mailto:struberg@yahoo.de">Mark Struberg</a>
38   *
39   */
40  public abstract class GitCheckInCommandTckTest extends CheckInCommandTckTest {
41  
42      /** {@inheritDoc} */
43      public void initRepo() throws Exception {
44          GitScmTestUtils.initRepo("src/test/resources/repository/", getRepositoryRoot(), getWorkingDirectory());
45      }
46  
47      @Override
48      protected CheckOutScmResult checkOut(File workingDirectory, ScmRepository repository) throws Exception {
49          try {
50              return super.checkOut(workingDirectory, repository);
51          } finally {
52              GitScmTestUtils.setDefaulGitConfig(workingDirectory);
53          }
54      }
55  
56      @Test
57      public void testUpToDatePush() throws Exception {
58          File checkedOutRepo = getWorkingCopy();
59  
60          ScmRepository scmRepository = getScmManager().makeScmRepository(getScmUrl());
61          checkoutRepoInto(checkedOutRepo, scmRepository);
62  
63          // Add a default user to the config
64          GitScmTestUtils.setDefaulGitConfig(checkedOutRepo);
65  
66          CheckInScmResult result =
67                  getScmManager().checkIn(scmRepository, new ScmFileSet(checkedOutRepo), "No change commit message");
68  
69          assertResultIsSuccess(result);
70      }
71  
72      @Test
73      public void testRejectedNonFastForwardPush() throws Exception {
74          File blockingRepo = PlexusJUnit4TestCase.getTestFile("target/scm-test/blocking-repo");
75          File rejectedRepo = PlexusJUnit4TestCase.getTestFile("target/scm-test/rejected-repo");
76  
77          ScmRepository scmRepository = getScmManager().makeScmRepository(getScmUrl());
78          checkoutRepoInto(rejectedRepo, scmRepository);
79          checkoutRepoInto(blockingRepo, scmRepository);
80  
81          // Add a default user to the config
82          GitScmTestUtils.setDefaulGitConfig(rejectedRepo);
83          GitScmTestUtils.setDefaulGitConfig(blockingRepo);
84  
85          ScmFileSet blockingFileSet = createWorkspaceChange(rejectedRepo);
86  
87          CheckInScmResult blockingResult = getScmManager().checkIn(scmRepository, blockingFileSet, "Blocking commit");
88          assertResultIsSuccess(blockingResult);
89  
90          ScmFileSet rejectedFileSet = createWorkspaceChange(blockingRepo);
91  
92          CheckInScmResult checkInScmResult = getScmManager().checkIn(scmRepository, rejectedFileSet, "Rejected commit");
93          assertFalse(
94                  "check-in should have been rejected since fast forward was not possible", checkInScmResult.isSuccess());
95      }
96  
97      private CheckOutScmResult checkoutRepoInto(File workingCopy, ScmRepository scmRepository) throws Exception {
98          FileUtils.deleteDirectory(workingCopy);
99          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 }