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.tck.command.branch;
20  
21  import java.io.File;
22  import java.io.Writer;
23  import java.nio.file.Files;
24  import java.nio.file.Path;
25  
26  import org.apache.maven.scm.CommandParameter;
27  import org.apache.maven.scm.CommandParameters;
28  import org.apache.maven.scm.ScmBranch;
29  import org.apache.maven.scm.ScmFileSet;
30  import org.apache.maven.scm.ScmTckTestCase;
31  import org.apache.maven.scm.command.branch.BranchScmResult;
32  import org.apache.maven.scm.command.checkin.CheckInScmResult;
33  import org.apache.maven.scm.command.checkout.CheckOutScmResult;
34  import org.codehaus.plexus.util.FileUtils;
35  import org.junit.Test;
36  
37  import static org.junit.Assert.assertEquals;
38  import static org.junit.Assert.assertFalse;
39  
40  /**
41   * This test tests the branch command.
42   *
43   * @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse</a>
44   */
45  public abstract class BranchCommandTckTest extends ScmTckTestCase {
46  
47      protected String getBranch() {
48          return "test-branch";
49      }
50  
51      @Test
52      public void testBranchCommandTest() throws Exception {
53          String branch = getBranch();
54  
55          @SuppressWarnings("deprecation")
56          BranchScmResult branchResult = getScmManager()
57                  .getProviderByUrl(getScmUrl())
58                  .branch(getScmRepository(), new ScmFileSet(getWorkingCopy()), branch);
59  
60          assertResultIsSuccess(branchResult);
61  
62          // see https://issues.apache.org/jira/browse/SCM-754
63          // assertEquals( "check all 4 files branched", 4, branchResult.getBranchedFiles().size() );
64  
65          File readmeTxt = new File(getWorkingCopy(), "readme.txt");
66  
67          assertEquals("check readme.txt contents", "/readme.txt", FileUtils.fileRead(readmeTxt));
68  
69          this.edit(getWorkingCopy(), "readme.txt", null, getScmRepository());
70          changeReadmeTxt(readmeTxt.toPath());
71  
72          CommandParameters commandParameters = new CommandParameters();
73          commandParameters.setString(CommandParameter.MESSAGE, "commit message");
74  
75          CheckInScmResult checkinResult =
76                  getScmManager().checkIn(getScmRepository(), new ScmFileSet(getWorkingCopy()), commandParameters);
77  
78          assertResultIsSuccess(checkinResult);
79  
80          CheckOutScmResult checkoutResult =
81                  getScmManager().checkOut(getScmRepository(), new ScmFileSet(getAssertionCopy()));
82  
83          assertResultIsSuccess(checkoutResult);
84  
85          readmeTxt = new File(getAssertionCopy(), "readme.txt");
86  
87          assertEquals("check readme.txt contents", "changed file", FileUtils.fileRead(readmeTxt));
88  
89          deleteDirectory(getAssertionCopy());
90  
91          assertFalse("check previous assertion copy deleted", getAssertionCopy().exists());
92  
93          checkoutResult = getScmManager()
94                  .getProviderByUrl(getScmUrl())
95                  .checkOut(getScmRepository(), new ScmFileSet(getAssertionCopy()), new ScmBranch(branch));
96  
97          assertResultIsSuccess(checkoutResult);
98  
99          assertEquals(
100                 "check readme.txt contents is from branched version", "/readme.txt", FileUtils.fileRead(readmeTxt));
101     }
102 
103     private void changeReadmeTxt(Path readmeTxt) throws Exception {
104         try (Writer output = Files.newBufferedWriter(readmeTxt)) {
105             output.write("changed file");
106         }
107     }
108 }