1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.scm;
20
21 import java.io.File;
22 import java.io.FileWriter;
23 import java.io.IOException;
24 import java.util.Calendar;
25 import java.util.Date;
26 import java.util.TimeZone;
27
28 import org.apache.maven.scm.manager.ScmManager;
29 import org.apache.maven.scm.repository.ScmRepository;
30 import org.codehaus.plexus.ContainerConfiguration;
31 import org.codehaus.plexus.PlexusConstants;
32 import org.codehaus.plexus.util.FileUtils;
33 import org.codehaus.plexus.util.StringUtils;
34 import org.codehaus.plexus.util.cli.CommandLineUtils;
35 import org.codehaus.plexus.util.cli.CommandLineUtils.StringStreamConsumer;
36 import org.codehaus.plexus.util.cli.Commandline;
37 import org.junit.Before;
38
39 import static org.junit.Assert.assertEquals;
40 import static org.junit.Assert.assertFalse;
41 import static org.junit.Assert.assertTrue;
42 import static org.junit.Assert.fail;
43 import static org.junit.Assume.assumeTrue;
44
45
46
47
48
49
50
51
52
53
54
55 public abstract class ScmTestCase extends PlexusJUnit4TestCase {
56 protected static final TimeZone GMT_TIME_ZONE = TimeZone.getTimeZone("GMT");
57
58 private static boolean debugExecute;
59
60 private ScmManager scmManager;
61
62 @Before
63 @Override
64 public void setUp() throws Exception {
65 super.setUp();
66
67 deleteDirectory(getRepositoryRoot());
68 assertFalse(getRepositoryRoot().exists());
69 deleteDirectory(getWorkingCopy());
70 assertFalse(getWorkingCopy().exists());
71 deleteDirectory(getWorkingDirectory());
72 assertFalse(getWorkingDirectory().exists());
73 deleteDirectory(getAssertionCopy());
74 assertFalse(getAssertionCopy().exists());
75 deleteDirectory(getUpdatingCopy());
76 assertFalse(getUpdatingCopy().exists());
77
78 scmManager = null;
79 }
80
81 @Override
82 public void customizeContainerConfiguration(final ContainerConfiguration configuration) {
83 configuration.setClassPathScanning(PlexusConstants.SCANNING_INDEX).setAutoWiring(true);
84 }
85
86
87
88
89 protected File getRepositoryRoot() {
90 return PlexusJUnit4TestCase.getTestFile("target/scm-test/repository");
91 }
92
93
94
95
96 protected File getRepository() {
97 return PlexusJUnit4TestCase.getTestFile("/src/test/repository");
98 }
99
100
101
102
103 protected File getWorkingCopy() {
104 return PlexusJUnit4TestCase.getTestFile("target/scm-test/working-copy");
105 }
106
107
108
109
110
111
112 protected File getWorkingDirectory() {
113 return getWorkingCopy();
114 }
115
116
117
118
119 protected File getAssertionCopy() {
120 return PlexusJUnit4TestCase.getTestFile("target/scm-test/assertion-copy");
121 }
122
123
124
125
126 protected File getUpdatingCopy() {
127 return PlexusJUnit4TestCase.getTestFile("target/scm-test/updating-copy");
128 }
129
130 protected ScmManager getScmManager() throws Exception {
131 if (scmManager == null) {
132 scmManager = lookup(ScmManager.class);
133 }
134
135 return scmManager;
136 }
137
138 protected ScmRepository makeScmRepository(String scmUrl) throws Exception {
139 return getScmManager().makeScmRepository(scmUrl);
140 }
141
142 public void assertPath(String expectedPath, String actualPath) throws Exception {
143 assertEquals(expectedPath.replace('\\', '/'), actualPath.replace('\\', '/'));
144 }
145
146 protected void assertFile(File root, String fileName) throws Exception {
147 File file = new File(root, fileName);
148
149 assertTrue("Missing file: '" + file.getAbsolutePath() + "'.", file.exists());
150
151 assertTrue("File isn't a file: '" + file.getAbsolutePath() + "'.", file.isFile());
152
153 String expected = fileName;
154
155 String actual = FileUtils.fileRead(file);
156
157 assertEquals(
158 "The file doesn't contain the expected contents. File: " + file.getAbsolutePath(), expected, actual);
159 }
160
161 protected void assertResultIsSuccess(ScmResult result) {
162 if (result.isSuccess()) {
163 return;
164 }
165
166 printOutputError(result);
167
168 fail("The command result success flag was false.");
169 }
170
171 protected void printOutputError(ScmResult result) {
172 System.err.println("----------------------------------------------------------------------");
173 System.err.println("Provider message");
174 System.err.println("----------------------------------------------------------------------");
175 System.err.println(result.getProviderMessage());
176 System.err.println("----------------------------------------------------------------------");
177
178 System.err.println("----------------------------------------------------------------------");
179 System.err.println("Command output");
180 System.err.println("----------------------------------------------------------------------");
181 System.err.println(result.getCommandOutput());
182 System.err.println("----------------------------------------------------------------------");
183 }
184
185 protected ScmFileSet getScmFileSet() {
186 return new ScmFileSet(getWorkingCopy());
187 }
188
189 protected static void setDebugExecute(boolean debugExecute) {
190 ScmTestCase.debugExecute = debugExecute;
191 }
192
193
194
195
196
197
198
199
200
201
202
203 public static void execute(File workingDirectory, String executable, String arguments) throws Exception {
204 Commandline cl = new Commandline();
205
206 cl.setExecutable(executable);
207
208 cl.setWorkingDirectory(workingDirectory.getAbsolutePath());
209
210 cl.addArguments(CommandLineUtils.translateCommandline(arguments));
211
212 StringStreamConsumer stdout = new CommandLineUtils.StringStreamConsumer();
213
214 StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
215
216 System.out.println("Test command line: " + cl);
217
218 int exitValue = CommandLineUtils.executeCommandLine(cl, stdout, stderr);
219
220 if (debugExecute || exitValue != 0) {
221 System.err.println("-----------------------------------------");
222 System.err.println("Command line: " + cl);
223 System.err.println("Working directory: " + cl.getWorkingDirectory());
224 System.err.println("-----------------------------------------");
225 System.err.println("Standard output: ");
226 System.err.println("-----------------------------------------");
227 System.err.println(stdout.getOutput());
228 System.err.println("-----------------------------------------");
229
230 System.err.println("Standard error: ");
231 System.err.println("-----------------------------------------");
232 System.err.println(stderr.getOutput());
233 System.err.println("-----------------------------------------");
234 }
235
236 if (exitValue != 0) {
237 fail("Exit value wasn't 0, was:" + exitValue);
238 }
239 }
240
241 protected static void makeDirectory(File basedir, String fileName) {
242 File dir = new File(basedir, fileName);
243
244 if (!dir.exists()) {
245 assertTrue(dir.mkdirs());
246 }
247 }
248
249 protected static void makeFile(File basedir, String fileName) throws IOException {
250 makeFile(basedir, fileName, fileName);
251 }
252
253 public static void makeFile(File basedir, String fileName, String contents) throws IOException {
254 File file = new File(basedir, fileName);
255
256 File parent = file.getParentFile();
257
258 if (!parent.exists()) {
259 assertTrue(parent.mkdirs());
260 }
261
262 try (FileWriter writer = new FileWriter(file)) {
263 writer.write(contents);
264 }
265 }
266
267 protected void deleteDirectory(File directory) throws IOException {
268 FileUtils.deleteDirectory(directory);
269 }
270
271 public static Date getDate(int year, int month, int day) {
272 return getDate(year, month, day, 0, 0, 0, null);
273 }
274
275 protected static Date getDate(int year, int month, int day, TimeZone tz) {
276 return getDate(year, month, day, 0, 0, 0, tz);
277 }
278
279 protected static Date getDate(int year, int month, int day, int hourOfDay, int minute, int second, TimeZone tz) {
280 Calendar cal = Calendar.getInstance();
281
282 if (tz != null) {
283 cal.setTimeZone(tz);
284 }
285 cal.set(year, month, day, hourOfDay, minute, second);
286 cal.set(Calendar.MILLISECOND, 0);
287
288 return cal.getTime();
289 }
290
291 public void assertCommandLine(String expectedCommand, File expectedWorkingDirectory, Commandline actualCommand)
292 throws IOException {
293 Commandline cl = new Commandline(expectedCommand);
294 if (expectedWorkingDirectory != null) {
295 cl.setWorkingDirectory(expectedWorkingDirectory.getAbsolutePath());
296 }
297 String expectedCommandLineAsExecuted = StringUtils.join(cl.getShellCommandline(), " ");
298 String actualCommandLineAsExecuted = StringUtils.join(actualCommand.getShellCommandline(), " ");
299 assertEquals(expectedCommandLineAsExecuted, actualCommandLineAsExecuted);
300 }
301
302 public static void checkScmPresence(String scmProviderCommand) {
303 assumeTrue(
304 "Skipping tests because the required command '" + scmProviderCommand + "' is not available.",
305 ScmTestCase.isSystemCmd(scmProviderCommand));
306 }
307
308
309
310
311
312 public static boolean isSystemCmd(String cmd) {
313 try {
314 Runtime.getRuntime().exec(cmd);
315
316 return true;
317 } catch (IOException e) {
318 return false;
319 }
320 }
321 }