View Javadoc
1   /*
2    * Copyright © 2017, Saleforce.com, Inc
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions are met:
7    *     * Redistributions of source code must retain the above copyright
8    *       notice, this list of conditions and the following disclaimer.
9    *     * Redistributions in binary form must reproduce the above copyright
10   *       notice, this list of conditions and the following disclaimer in the
11   *       documentation and/or other materials provided with the distribution.
12   *     * Neither the name of the <organization> nor the
13   *       names of its contributors may be used to endorse or promote products
14   *       derived from this software without specific prior written permission.
15   *
16   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17   * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18   * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19   * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
20   * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24   * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25   * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26   */
27  package com.salesforce.aptspring.processor.takari;
28  
29  import static org.assertj.core.api.Assertions.assertThat;
30  
31  import java.io.File;
32  import java.util.Arrays;
33  
34  import org.apache.maven.execution.MavenSession;
35  import org.apache.maven.project.MavenProject;
36  import org.codehaus.plexus.util.xml.Xpp3Dom;
37  import org.junit.Rule;
38  import org.junit.Test;
39  import org.junit.runner.RunWith;
40  import org.junit.runners.Parameterized;
41  import org.junit.runners.Parameterized.Parameters;
42  import org.springframework.beans.factory.config.BeanDefinition;
43  import org.springframework.context.annotation.Bean;
44  import org.springframework.core.SpringVersion;
45  
46  import com.salesforce.apt.graph.model.storage.classpath.FileStore;
47  import com.salesforce.aptspring.Verified;
48  import com.salesforce.aptspring.processor.SpringAnnotationParser;
49  
50  import io.takari.maven.testing.TestMavenRuntime;
51  import io.takari.maven.testing.TestResources;
52  
53  @RunWith(Parameterized.class)
54  public class TakariIncrementalCompileTests {
55  
56    private static final String PROC = "proc";
57  
58    @Rule
59    public final TestResources resources = new TestResources();
60  
61    @Rule
62    public final TestMavenRuntime maven = new TestMavenRuntime();
63  
64    protected final String compilerId;
65  
66    public TakariIncrementalCompileTests(String compilerId) {
67      this.compilerId = compilerId;
68    }
69  
70    @Parameters(name = "{0}")
71    public static Iterable<Object[]> compilers() {
72      return Arrays.<Object[]>asList(new Object[] { "javac" }, new Object[] { "forked-javac" }, new Object[] { "jdt" });
73    }
74  
75    private Xpp3Dom newParameter(String name, String value) {
76      Xpp3Dom child = new Xpp3Dom(name);
77      child.setValue(value);
78      return child;
79    }
80  
81    @Test
82    public void testBasic() throws Exception {
83  
84      // this creates a temporary copy of src/test/projects/basic test project
85      File basedir = resources.getBasedir("basic");
86  
87      // create MavenProject model for the test project
88      MavenProject project = maven.readMavenProject(basedir);
89  
90      // add annotation processor to the test project dependencies (i.e.
91      // classpath)
92      File springContext = new File(Bean.class.getProtectionDomain().getCodeSource().getLocation().toURI());
93      maven.newDependency(springContext.getCanonicalFile()).setArtifactId("spring-context").addTo(project);
94  
95      File springCore = new File(SpringVersion.class.getProtectionDomain().getCodeSource().getLocation().toURI());
96      maven.newDependency(springCore.getCanonicalFile()).setArtifactId("spring-core").addTo(project);
97  
98      File springBean = new File(BeanDefinition.class.getProtectionDomain().getCodeSource().getLocation().toURI());
99      maven.newDependency(springBean.getCanonicalFile()).setArtifactId("spring-bean").addTo(project).addTo(project);
100 
101     File verifiedApi = new File(Verified.class.getProtectionDomain().getCodeSource().getLocation().toURI());
102     maven.newDependency(verifiedApi.getCanonicalFile()).setArtifactId("verifiedApi").addTo(project);
103 
104     File verifiedImpl = new File(
105         SpringAnnotationParser.class.getProtectionDomain().getCodeSource().getLocation().toURI());
106     maven.newDependency(verifiedImpl.getCanonicalFile()).setArtifactId("verifiedProcessor").addTo(project);
107 
108     MavenSession session = maven.newMavenSession(project);
109 
110     // run java compiler with annotation processing enabled
111     maven.executeMojo(session, project, "compile", newParameter("compilerId", compilerId), newParameter(PROC, PROC));
112     File compiledClasses = new File(basedir, "target/classes");
113     File generatedSources = new File(basedir, "target/generated-sources/annotations");
114 
115     File configurationSourceFile = (new File(generatedSources,
116         "com/salesforce/aptspring/ComputerHardwareConfiguration_" + FileStore.STANDARD.getPath() + ".java"));
117     assertThat(configurationSourceFile).exists().canRead();
118 
119     File configurationClass = (new File(compiledClasses,
120         "com/salesforce/aptspring/ComputerHardwareConfiguration_" + FileStore.STANDARD.getPath() + ".class"));
121     assertThat(configurationClass).exists().canRead();
122 
123     maven.executeMojo(session, project, "testCompile", newParameter("compilerId", compilerId),
124         newParameter(PROC, PROC));
125     File compiledTestClasses = new File(basedir, "target/test-classes");
126     File configurationTestClass = (new File(compiledTestClasses,
127         "com/salesforce/aptspring/RootApplicationConfiguration_" + FileStore.STANDARD.getPath() + ".class"));
128     assertThat(configurationTestClass).exists().canRead();
129   }
130 }