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   */
46  public abstract class BranchCommandTckTest extends ScmTckTestCase {
47  
48      protected String getBranch() {
49          return "test-branch";
50      }
51  
52      @Test
53      public void testBranchCommandTest() throws Exception {
54          String branch = getBranch();
55  
56          @SuppressWarnings("deprecation")
57          BranchScmResult branchResult = getScmManager()
58                  .getProviderByUrl(getScmUrl())
59                  .branch(getScmRepository(), new ScmFileSet(getWorkingCopy()), branch);
60  
61          assertResultIsSuccess(branchResult);
62  
63          // see https://issues.apache.org/jira/browse/SCM-754
64          // assertEquals( "check all 4 files branched", 4, branchResult.getBranchedFiles().size() );
65  
66          File readmeTxt = new File(getWorkingCopy(), "readme.txt");
67  
68          assertEquals("check readme.txt contents", "/readme.txt", FileUtils.fileRead(readmeTxt));
69  
70          this.edit(getWorkingCopy(), "readme.txt", null, getScmRepository());
71          changeReadmeTxt(readmeTxt.toPath());
72  
73          CommandParameters commandParameters = new CommandParameters();
74          commandParameters.setString(CommandParameter.MESSAGE, "commit message");
75          commandParameters.setString(CommandParameter.SCM_COMMIT_SIGN, "false");
76  
77          CheckInScmResult checkinResult =
78                  getScmManager().checkIn(getScmRepository(), new ScmFileSet(getWorkingCopy()), commandParameters);
79  
80          assertResultIsSuccess(checkinResult);
81  
82          CheckOutScmResult checkoutResult =
83                  getScmManager().checkOut(getScmRepository(), new ScmFileSet(getAssertionCopy()));
84  
85          assertResultIsSuccess(checkoutResult);
86  
87          readmeTxt = new File(getAssertionCopy(), "readme.txt");
88  
89          assertEquals("check readme.txt contents", "changed file", FileUtils.fileRead(readmeTxt));
90  
91          deleteDirectory(getAssertionCopy());
92  
93          assertFalse("check previous assertion copy deleted", getAssertionCopy().exists());
94  
95          checkoutResult = getScmManager()
96                  .getProviderByUrl(getScmUrl())
97                  .checkOut(getScmRepository(), new ScmFileSet(getAssertionCopy()), new ScmBranch(branch));
98  
99          assertResultIsSuccess(checkoutResult);
100 
101         assertEquals(
102                 "check readme.txt contents is from branched version", "/readme.txt", FileUtils.fileRead(readmeTxt));
103     }
104 
105     private void changeReadmeTxt(Path readmeTxt) throws Exception {
106         try (Writer output = Files.newBufferedWriter(readmeTxt)) {
107             output.write("changed file");
108         }
109     }
110 }