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.hg;
20  
21  import java.io.File;
22  import java.io.IOException;
23  import java.util.ArrayList;
24  import java.util.List;
25  
26  import org.apache.maven.scm.ScmFileSet;
27  import org.apache.maven.scm.ScmResult;
28  import org.apache.maven.scm.ScmTestCase;
29  import org.apache.maven.scm.provider.hg.command.HgCommandConstants;
30  import org.codehaus.plexus.util.FileUtils;
31  
32  /**
33   * Common code used in all tests.
34   *
35   * @author <a href="mailto:thurner.rupert@ymono.net">thurner rupert</a>
36   */
37  public class HgRepoUtils extends ScmTestCase {
38      /** 'hg' command line */
39      public static final String HG_COMMAND_LINE = "hg";
40  
41      public static final String[] filesInTestBranch =
42              new String[] {"pom.xml", "readme.txt", "src/main/java/Application.java", "src/test/java/Test.java"};
43  
44      public static final String TCK_FILE_CONSTANT = "/";
45  
46      public static final String BRANCH_NAME = "target" + File.separator + "test-branch";
47  
48      public static final File WORKING_DIR = new File(getBasedir(), BRANCH_NAME);
49  
50      public static final String COMMIT_MESSAGE = "Add files to test branch";
51  
52      public static String getScmUrl() throws Exception {
53          return "scm:hg:" + WORKING_DIR.getAbsolutePath();
54      }
55  
56      public static void initRepo() throws Exception {
57          // Prepare tmp directory
58          if (WORKING_DIR.exists()) {
59              FileUtils.deleteDirectory(WORKING_DIR);
60  
61              if (WORKING_DIR.exists()) {
62                  throw new IOException(WORKING_DIR.getAbsolutePath() + " wasn't deleted.");
63              }
64          }
65  
66          boolean workingDirReady = WORKING_DIR.mkdirs();
67  
68          if (!workingDirReady) {
69              throw new IOException("Could not initiate test branch at: " + WORKING_DIR);
70          }
71  
72          // Init repository
73          String[] init_cmd = new String[] {HgCommandConstants.INIT_CMD};
74          HgUtils.execute(WORKING_DIR, init_cmd);
75  
76          // Create and add files to repository
77          List<File> files = new ArrayList<>();
78          for (int i = 0; i < filesInTestBranch.length; i++) {
79              File file = new File(WORKING_DIR.getAbsolutePath(), filesInTestBranch[i]);
80              if (file.getParentFile() != null && !file.getParentFile().exists()) {
81                  boolean success = file.getParentFile().mkdirs();
82                  if (!success) {
83                      throw new IOException("Could not create directories in branch for: " + file);
84                  }
85              }
86              file.createNewFile();
87  
88              FileUtils.fileWrite(file.getAbsolutePath(), TCK_FILE_CONSTANT + filesInTestBranch[i]);
89  
90              files.add(file);
91          }
92  
93          // Add to repository
94          String[] add_cmd = new String[] {HgCommandConstants.ADD_CMD};
95          ScmFileSet filesToAdd = new ScmFileSet(new File(""), files);
96          add_cmd = HgUtils.expandCommandLine(add_cmd, filesToAdd);
97          ScmResult result = HgUtils.execute(WORKING_DIR, add_cmd);
98          if (!result.isSuccess()) {
99              String message =
100                     "Provider message: " + result.getProviderMessage() + "\n" + "Output: " + result.getCommandOutput();
101             throw new Exception(message);
102         }
103 
104         // Commit the initial repository
105         String[] commit_cmd =
106                 new String[] {HgCommandConstants.COMMIT_CMD, HgCommandConstants.MESSAGE_OPTION, COMMIT_MESSAGE};
107         result = HgUtils.execute(WORKING_DIR, commit_cmd);
108         if (!result.isSuccess()) {
109             String message =
110                     "Provider message: " + result.getProviderMessage() + "\n" + "Output: " + result.getCommandOutput();
111             throw new Exception(message);
112         }
113     }
114 }