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 java.io.File;
27  import java.util.ArrayList;
28  import java.util.Collections;
29  import java.util.List;
30  
31  import org.codehaus.plexus.compiler.CompilerConfiguration;
32  import org.junit.jupiter.api.Assertions;
33  import org.junit.jupiter.api.BeforeEach;
34  import org.junit.jupiter.api.Test;
35  
36  import static org.hamcrest.MatcherAssert.assertThat;
37  import static org.hamcrest.Matchers.contains;
38  import static org.hamcrest.Matchers.is;
39  
40  public class EclipseCompilerConfigurationTest {
41      private static final String PROPERTIES_FILE_NAME =
42              "src/test/resources".replace("/", File.separator) + File.separator
43                      + EclipseCompilerConfigurationTest.class.getName().replace(".", File.separator)
44                      + "-test.properties";
45  
46      private CompilerConfiguration configuration;
47  
48      @BeforeEach
49      protected void setUp() {
50          configuration = new CompilerConfiguration();
51      }
52  
53      @Test
54      public void testProcessCustomArgumentsWithMultipleAddExports() {
55          configuration.addCompilerCustomArgument("--add-exports", "FROM-MOD/package1=OTHER-MOD");
56          configuration.addCompilerCustomArgument("--add-exports", "FROM-MOD/package2=OTHER-MOD");
57          List<String> args = new ArrayList<>();
58          EclipseJavaCompiler.processCustomArguments(configuration, args);
59          assertThat(args.size(), is(4));
60          assertThat(
61                  args,
62                  contains(
63                          "--add-exports",
64                          "FROM-MOD/package1=OTHER-MOD",
65                          "--add-exports",
66                          "FROM-MOD/package2=OTHER-MOD"));
67          //        assertThat( args.get( 0 ), is("--add-exports") );
68          //        assertThat( args.get( 1 ), is("FROM-MOD/package1=OTHER-MOD") );
69          //        assertThat( args.get( 2 ), is("--add-exports") );
70          //        assertThat( args.get( 3 ), is("FROM-MOD/package2=OTHER-MOD") );
71      }
72  
73      @Test
74      public void testProcessCustomArgumentsWithProceedOnError() {
75          configuration.addCompilerCustomArgument("-proceedOnError", null);
76          List<String> args = new ArrayList<>();
77          EclipseJavaCompiler.processCustomArguments(configuration, args);
78          assertThat(args.size(), is(1));
79          assertThat(args, contains("-proceedOnError:Fatal"));
80      }
81  
82      @Test
83      public void testProcessCustomArgumentsWithErrorsAsWarnings() {
84          configuration.addCompilerCustomArgument("errorsAsWarnings", null);
85          List<String> args = new ArrayList<>();
86          final boolean errorsAsWarnings = EclipseJavaCompiler.processCustomArguments(configuration, args);
87          assertThat(errorsAsWarnings, is(true));
88      }
89  
90      @Test
91      public void testProcessCustomArgumentsWithErrorsAsWarningsAndMinus() {
92          configuration.addCompilerCustomArgument("-errorsAsWarnings", null);
93          List<String> args = new ArrayList<>();
94          final boolean errorsAsWarnings = EclipseJavaCompiler.processCustomArguments(configuration, args);
95          assertThat(errorsAsWarnings, is(true));
96      }
97  
98      @Test
99      public void testProcessCustomArgumentsWithPropertiesAndNonExistingFile() {
100         configuration.addCompilerCustomArgument("-properties", "fooBar.txt");
101         IllegalArgumentException expected = Assertions.assertThrows(
102                 IllegalArgumentException.class,
103                 () -> EclipseJavaCompiler.processCustomArguments(configuration, Collections.emptyList()));
104         assertThat(expected.getMessage(), is("Properties file specified by -properties fooBar.txt does not exist"));
105     }
106 
107     @Test
108     public void testProcessCustomArgumentsWithPropertiesAndValidFile() {
109         configuration.addCompilerCustomArgument("-properties", PROPERTIES_FILE_NAME);
110         List<String> args = new ArrayList<>();
111         EclipseJavaCompiler.processCustomArguments(configuration, args);
112         assertThat(args.size(), is(2));
113         assertThat(args, contains("-properties", PROPERTIES_FILE_NAME));
114     }
115 }