1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.resolver.internal.ant;
20
21 import java.io.File;
22 import java.io.PrintStream;
23
24 import org.apache.tools.ant.BuildException;
25 import org.apache.tools.ant.BuildFileRule;
26 import org.apache.tools.ant.DefaultLogger;
27 import org.apache.tools.ant.Project;
28 import org.eclipse.aether.internal.test.util.TestFileUtils;
29 import org.junit.After;
30 import org.junit.Before;
31 import org.junit.Rule;
32
33 import static org.junit.Assert.assertTrue;
34
35 public abstract class AntBuildsTest {
36
37 private static final File BASE_DIR;
38
39 protected static final File BUILD_DIR;
40 protected File buildFile;
41
42 static {
43 System.setProperty("aether.metadataResolver.threads", "1");
44 System.setProperty("aether.connector.basic.threads", "1");
45 BASE_DIR = new File("").getAbsoluteFile();
46 BUILD_DIR = new File(BASE_DIR, "target/ant");
47 }
48
49 public AntBuildsTest() {
50 projectDir = new File(new File(BASE_DIR, "src/test/resources/ant"), getProjectDirName());
51 buildFile = new File(projectDir, "ant.xml");
52 }
53
54 public AntBuildsTest(File projectFile) {
55 projectDir = projectFile.getParentFile();
56 buildFile = projectFile;
57 }
58
59 @Rule
60 public final BuildFileRule buildRule = new BuildFileRule();
61
62 protected File projectDir;
63
64 protected File localRepoDir;
65
66 protected File distRepoDir;
67
68 protected String getProjectDirName() {
69 String name = getClass().getSimpleName();
70 if (name.endsWith("Test")) {
71 name = name.substring(0, name.length() - 4);
72 }
73 return name;
74 }
75
76 protected void setUpProperties() throws Exception {
77
78 }
79
80 @Before
81 public void setUp() throws Exception {
82 TestFileUtils.deleteFile(BUILD_DIR);
83
84 localRepoDir = new File(BUILD_DIR, "local-repo");
85 distRepoDir = new File(BUILD_DIR, "dist-repo");
86
87 System.setProperty("project.dir", projectDir.getAbsolutePath());
88 System.setProperty("build.dir", BUILD_DIR.getAbsolutePath());
89 System.setProperty("maven.repo.local", localRepoDir.getAbsolutePath());
90 System.setProperty("project.distrepo.url", distRepoDir.toURI().toASCIIString());
91 setUpProperties();
92
93 configureProject(buildFile.getAbsolutePath(), Project.MSG_VERBOSE);
94 }
95
96 @After
97 public void tearDown() throws Exception {
98 ProjectWorkspaceReader.dropInstance();
99 TestFileUtils.deleteFile(BUILD_DIR);
100 }
101
102 public void configureProject(String filename, int logLevel) throws BuildException {
103 buildRule.configureProject(filename, logLevel);
104 DefaultLogger logger = new DefaultLogger() {
105 @Override
106 protected void printMessage(String message, PrintStream stream, int priority) {
107 message = System.currentTimeMillis() + " " + message;
108 super.printMessage(message, stream, priority);
109 }
110 };
111 logger.setMessageOutputLevel(logLevel);
112 logger.setOutputPrintStream(System.out);
113 logger.setErrorPrintStream(System.err);
114 buildRule.getProject().addBuildListener(logger);
115 }
116
117 protected void assertLogContaining(String substring) {
118 String realLog = getLog();
119 assertTrue(
120 "expecting log to contain \"" + substring + "\" log was \"" + realLog + "\"",
121 realLog.contains(substring));
122 }
123
124 protected void executeTarget(String targetName) {
125 buildRule.executeTarget(targetName);
126 }
127
128 protected String getLog() {
129 return buildRule.getLog();
130 }
131
132 protected Project getProject() {
133 return buildRule.getProject();
134 }
135 }