View Javadoc
1   package org.codehaus.plexus.compiler;
2   
3   /**
4    * The MIT License
5    *
6    * Copyright (c) 2005, The Codehaus
7    *
8    * Permission is hereby granted, free of charge, to any person obtaining a copy of
9    * this software and associated documentation files (the "Software"), to deal in
10   * the Software without restriction, including without limitation the rights to
11   * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
12   * of the Software, and to permit persons to whom the Software is furnished to do
13   * so, subject to the following conditions:
14   *
15   * The above copyright notice and this permission notice shall be included in all
16   * copies or substantial portions of the Software.
17   *
18   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24   * SOFTWARE.
25   */
26  import javax.inject.Inject;
27  
28  import java.io.File;
29  import java.io.IOException;
30  import java.lang.reflect.Method;
31  import java.util.List;
32  import java.util.Map;
33  
34  import org.codehaus.plexus.testing.PlexusTest;
35  import org.codehaus.plexus.util.FileUtils;
36  import org.junit.jupiter.api.BeforeEach;
37  import org.junit.jupiter.api.Test;
38  import org.junit.jupiter.api.TestInfo;
39  
40  import static org.hamcrest.MatcherAssert.assertThat;
41  import static org.hamcrest.Matchers.containsString;
42  import static org.hamcrest.Matchers.is;
43  import static org.hamcrest.Matchers.notNullValue;
44  
45  /**
46   * @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
47   */
48  @PlexusTest
49  public abstract class AbstractCompilerTckTest {
50      private static final String EOL = System.lineSeparator();
51  
52      protected String roleHint;
53  
54      private TestInfo testInfo;
55  
56      @Inject
57      private Map<String, Compiler> compilers;
58  
59      @BeforeEach
60      final void setup(TestInfo testInfo) {
61          this.testInfo = testInfo;
62      }
63  
64      @Test
65      public void testDeprecation() throws Exception {
66          File foo = new File(getSrc(), "Foo.java");
67  
68          writeFileWithDeprecatedApi(foo, "Foo");
69  
70          // ----------------------------------------------------------------------
71          //
72          // ----------------------------------------------------------------------
73  
74          CompilerConfiguration configuration = new CompilerConfiguration();
75  
76          configuration.setShowDeprecation(true);
77  
78          configuration.addSourceLocation(getSrc().getAbsolutePath());
79  
80          List<CompilerMessage> result = compile(configuration);
81  
82          // ----------------------------------------------------------------------
83          //
84          // ----------------------------------------------------------------------
85  
86          assertThat(result.size(), is(1));
87  
88          CompilerMessage error = result.get(0);
89  
90          assertThat(error.isError(), is(false));
91  
92          assertThat(error.getMessage(), containsString("Date"));
93  
94          assertThat(error.getMessage(), containsString("deprecated"));
95      }
96  
97      @Test
98      public void testWarning() throws Exception {
99          File foo = new File(getSrc(), "Foo.java");
100 
101         writeFileWithWarning(foo, "Foo");
102 
103         // ----------------------------------------------------------------------
104         //
105         // ----------------------------------------------------------------------
106 
107         CompilerConfiguration configuration = new CompilerConfiguration();
108 
109         configuration.setShowWarnings(true);
110 
111         configuration.addSourceLocation(getSrc().getAbsolutePath());
112 
113         List<CompilerMessage> result = compile(configuration);
114 
115         // ----------------------------------------------------------------------
116         //
117         // ----------------------------------------------------------------------
118 
119         assertThat(result.size(), is(1));
120 
121         CompilerMessage error = result.get(0);
122 
123         assertThat(error.isError(), is(false));
124 
125         assertThat(error.getMessage(), containsString("finally block does not complete normally"));
126     }
127 
128     protected List<CompilerMessage> compile(CompilerConfiguration configuration) throws Exception {
129         // ----------------------------------------------------------------------
130         // Set up configuration
131         // ----------------------------------------------------------------------
132 
133         File compilerOutput = getCompilerOutput();
134 
135         if (compilerOutput.exists()) {
136             FileUtils.deleteDirectory(compilerOutput);
137         }
138 
139         configuration.setOutputLocation(compilerOutput.getAbsolutePath());
140 
141         // ----------------------------------------------------------------------
142         // Compile!
143         // ----------------------------------------------------------------------
144 
145         List<CompilerMessage> result =
146                 getCompiler().performCompile(configuration).getCompilerMessages();
147 
148         assertThat(result, notNullValue());
149 
150         return result;
151     }
152 
153     private Compiler getCompiler() {
154         return compilers.get(roleHint);
155     }
156 
157     private File getCompilerOutput() {
158         return new File("target/compiler-output/"
159                 + testInfo.getTestMethod().map(Method::getName).orElseThrow(null));
160     }
161 
162     private File getSrc() {
163         return new File("target/compiler-src/"
164                 + testInfo.getTestMethod().map(Method::getName).orElseThrow(null));
165     }
166 
167     protected void writeFileWithDeprecatedApi(File path, String className) throws IOException {
168         File parent = path.getParentFile();
169 
170         if (!parent.exists()) {
171             assertThat(parent.mkdirs(), is(true));
172         }
173 
174         String source = "import java.util.Date;" + EOL + ""
175                 + EOL + "public class "
176                 + className + "" + EOL + "{"
177                 + EOL + "    private static Date date = new Date( \"foo\" );"
178                 + EOL + "    static "
179                 + EOL + "    { "
180                 + EOL + "        Date date = "
181                 + className + ".date; " + EOL + "        Date date2 = date; "
182                 + EOL + "        date = date2; "
183                 + EOL + "    }"
184                 + EOL + "}";
185 
186         FileUtils.fileWrite(path.getAbsolutePath(), source);
187     }
188 
189     protected void writeFileWithWarning(File path, String className) throws IOException {
190         File parent = path.getParentFile();
191 
192         if (!parent.exists()) {
193             assertThat(parent.mkdirs(), is(true));
194         }
195 
196         String source = "public class " + className + "" + EOL + "{"
197                 + EOL + "    public void foo()"
198                 + EOL + "    {"
199                 + EOL + "        try{ throw new java.io.IOException(); }"
200                 + EOL + "        finally { return; }"
201                 + EOL + "    }"
202                 + EOL + "}";
203 
204         FileUtils.fileWrite(path.getAbsolutePath(), source);
205     }
206 }