1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
34
35
36
37 public class HgRepoUtils extends ScmTestCase {
38
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
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
73 String[] init_cmd = new String[] {HgCommandConstants.INIT_CMD};
74 HgUtils.execute(WORKING_DIR, init_cmd);
75
76
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
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
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 }