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