View Javadoc
1   package org.codehaus.plexus.compiler.eclipse;
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  import javax.inject.Named;
28  
29  import java.io.File;
30  import java.util.Collections;
31  import java.util.HashSet;
32  import java.util.List;
33  import java.util.Set;
34  
35  import org.codehaus.plexus.compiler.Compiler;
36  import org.codehaus.plexus.compiler.CompilerConfiguration;
37  import org.codehaus.plexus.testing.PlexusTest;
38  import org.codehaus.plexus.util.FileUtils;
39  import org.hamcrest.MatcherAssert;
40  import org.hamcrest.Matchers;
41  import org.junit.jupiter.api.Assertions;
42  import org.junit.jupiter.api.Test;
43  
44  import static org.codehaus.plexus.testing.PlexusExtension.getBasedir;
45  
46  /**
47   * @author <a href="mailto:jal@etc.to">Frits Jalvingh</a>
48   * Created on 22-4-18.
49   */
50  @PlexusTest
51  public class EclipseCompilerDashedArgumentsTest {
52  
53      public static final String BAD_DOUBLEDASH_OPTION = "--grubbelparkplace";
54  
55      @Inject
56      @Named("eclipse")
57      Compiler compiler;
58  
59      private CompilerConfiguration getConfig() throws Exception {
60          String sourceDir = getBasedir() + "/src/test-input/src/main";
61  
62          List<String> filenames = FileUtils.getFileNames(new File(sourceDir), "**/*.java", null, false, true);
63          Collections.sort(filenames);
64          Set<File> files = new HashSet<>();
65          for (String filename : filenames) {
66              files.add(new File(filename));
67          }
68  
69          CompilerConfiguration compilerConfig = new CompilerConfiguration();
70          compilerConfig.setDebug(false);
71          compilerConfig.setShowDeprecation(false);
72  
73          //            compilerConfig.setClasspathEntries( getClasspath() );
74          compilerConfig.addSourceLocation(sourceDir);
75          compilerConfig.setOutputLocation(getBasedir() + "/target/eclipse/classes");
76          FileUtils.deleteDirectory(compilerConfig.getOutputLocation());
77          //        compilerConfig.addInclude( filename );
78          compilerConfig.setForceJavacCompilerUse(false);
79          compilerConfig.setSourceFiles(files);
80  
81          compilerConfig.setTargetVersion("1.8");
82          compilerConfig.setSourceVersion("1.8");
83          return compilerConfig;
84      }
85  
86      /**
87       * Start the eclipse compiler with a bad option that has two dashes. It should abort, and the error
88       * message should show the actual bad option with two dashes. This ensures that both dashes are passed
89       * to the compiler proper.
90       *
91       * This also tests that con-compile errors are shown properly, as the error caused by
92       * the invalid option is not part of the error output but part of the data sent to stdout/stderr.
93       */
94      @Test
95      public void testDoubleDashOptionsArePassedWithTwoDashes() throws Exception {
96          CompilerConfiguration config = getConfig();
97          config.addCompilerCustomArgument(BAD_DOUBLEDASH_OPTION, "b0rk3d");
98  
99          EcjFailureException x =
100                 Assertions.assertThrows(EcjFailureException.class, () -> compiler.performCompile(config));
101 
102         MatcherAssert.assertThat(x.getEcjOutput(), Matchers.containsString(BAD_DOUBLEDASH_OPTION));
103         MatcherAssert.assertThat(x.getEcjOutput(), Matchers.not(Matchers.containsString("-" + BAD_DOUBLEDASH_OPTION)));
104     }
105 }