View Javadoc
1   /*
2    * Copyright 2013-2020 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      https://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.springframework.cloud.contract.maven.verifier;
18  
19  import java.io.File;
20  import java.io.IOException;
21  
22  import org.apache.maven.execution.MavenSession;
23  import org.apache.maven.plugin.testing.MojoRule;
24  import org.apache.maven.project.MavenProject;
25  import org.assertj.core.api.BDDAssertions;
26  import org.codehaus.plexus.util.xml.Xpp3Dom;
27  import org.junit.Before;
28  import org.junit.Rule;
29  import org.junit.rules.TemporaryFolder;
30  
31  import org.springframework.boot.test.system.OutputCaptureRule;
32  import org.springframework.util.FileSystemUtils;
33  
34  public abstract class AbstractMojoTest {
35  
36  	@Rule
37  	public MojoRule rule = new MojoRule() {
38  		@Override
39  		protected void before() throws Throwable {
40  		}
41  
42  		@Override
43  		protected void after() {
44  		}
45  	};
46  
47  	@Rule
48  	public TemporaryFolder tmp = new TemporaryFolder();
49  
50  	@Rule
51  	public OutputCaptureRule capture = new OutputCaptureRule();
52  
53  	File tmpFolder;
54  
55  	@Before
56  	public void setupTemp() throws IOException {
57  		this.tmpFolder = this.tmp.newFolder();
58  		File projects = new File("src/test/projects");
59  		FileSystemUtils.copyRecursively(projects, this.tmpFolder);
60  	}
61  
62  	protected File getBasedir(String name) {
63  		return new File(this.tmpFolder, name);
64  	}
65  
66  	protected MavenSession prepareMavenSession(File baseDir) throws Exception {
67  		MavenProject mavenProject = rule.readMavenProject(baseDir);
68  		return rule.newMavenSession(mavenProject);
69  	}
70  
71  	protected void executeMojo(File baseDir, String goal, Xpp3Dom... parameters) throws Exception {
72  		MavenSession mavenSession = prepareMavenSession(baseDir);
73  		executeMojo(mavenSession, goal, parameters);
74  	}
75  
76  	protected void executeMojo(MavenSession mavenSession, String goal, Xpp3Dom... parameters) throws Exception {
77  		rule.executeMojo(mavenSession, mavenSession.getCurrentProject(), goal, parameters);
78  	}
79  
80  	protected void assertFilesPresent(File file, String name) {
81  		BDDAssertions.then(new File(file, name)).exists();
82  	}
83  
84  	protected void assertFilesNotPresent(File file, String name) {
85  		BDDAssertions.then(new File(file, name)).doesNotExist();
86  	}
87  
88  }