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