View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.plugins.help;
20  
21  import java.io.ByteArrayOutputStream;
22  import java.io.File;
23  import java.io.PrintStream;
24  import java.util.ArrayList;
25  import java.util.List;
26  
27  import org.apache.maven.monitor.logging.DefaultLog;
28  import org.apache.maven.plugin.Mojo;
29  import org.apache.maven.plugin.PluginParameterExpressionEvaluator;
30  import org.apache.maven.plugin.testing.AbstractMojoTestCase;
31  import org.apache.maven.plugin.testing.stubs.MavenProjectStub;
32  import org.apache.maven.settings.Settings;
33  import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluator;
34  import org.codehaus.plexus.components.interactivity.InputHandler;
35  import org.codehaus.plexus.logging.Logger;
36  import org.codehaus.plexus.logging.LoggerManager;
37  
38  import static org.mockito.ArgumentMatchers.anyString;
39  import static org.mockito.Mockito.mock;
40  import static org.mockito.Mockito.times;
41  import static org.mockito.Mockito.verify;
42  import static org.mockito.Mockito.when;
43  
44  /**
45   * Test class for the evaluate mojo of the Help Plugin.
46   */
47  public class EvaluateMojoTest extends AbstractMojoTestCase {
48  
49      private InterceptingLog interceptingLogger;
50  
51      @Override
52      protected void setUp() throws Exception {
53          super.setUp();
54          interceptingLogger =
55                  new InterceptingLog(getContainer().lookup(LoggerManager.class).getLoggerForComponent(Mojo.ROLE));
56      }
57  
58      /**
59       * Tests evaluation of an expression in interactive mode with a mock input handler.
60       * @throws Exception in case of errors.
61       */
62      public void testEvaluateWithoutExpression() throws Exception {
63          File testPom = new File(getBasedir(), "target/test-classes/unit/evaluate/plugin-config.xml");
64  
65          EvaluateMojo mojo = (EvaluateMojo) lookupMojo("evaluate", testPom);
66  
67          InputHandler inputHandler = mock(InputHandler.class);
68          when(inputHandler.readLine()).thenReturn("${project.groupId}", "0");
69  
70          ExpressionEvaluator expressionEvaluator = mock(PluginParameterExpressionEvaluator.class);
71          when(expressionEvaluator.evaluate(anyString())).thenReturn("My result");
72  
73          setUpMojo(mojo, inputHandler, expressionEvaluator);
74  
75          mojo.execute();
76  
77          String ls = System.getProperty("line.separator");
78  
79          assertTrue(interceptingLogger.infoLogs.contains(ls + "My result"));
80          assertTrue(interceptingLogger.warnLogs.isEmpty());
81          verify(expressionEvaluator).evaluate("${project.groupId}");
82          verify(inputHandler, times(2)).readLine();
83      }
84  
85      /**
86       * Tests evaluation of an expression in interactive mode with a mock input handler, when "output" is set.
87       * @throws Exception in case of errors.
88       */
89      public void testEvaluateWithoutExpressionWithOutput() throws Exception {
90          File testPom = new File(getBasedir(), "target/test-classes/unit/evaluate/plugin-config-output.xml");
91  
92          EvaluateMojo mojo = (EvaluateMojo) lookupMojo("evaluate", testPom);
93  
94          InputHandler inputHandler = mock(InputHandler.class);
95          when(inputHandler.readLine()).thenReturn("${project.artifactId}", "0");
96  
97          ExpressionEvaluator expressionEvaluator = mock(PluginParameterExpressionEvaluator.class);
98          when(expressionEvaluator.evaluate(anyString())).thenReturn("My result");
99  
100         setUpMojo(mojo, inputHandler, expressionEvaluator);
101 
102         mojo.execute();
103 
104         String ls = System.getProperty("line.separator");
105 
106         assertTrue(interceptingLogger.infoLogs.contains(ls + "My result"));
107         assertFalse(interceptingLogger.warnLogs.isEmpty());
108         verify(expressionEvaluator).evaluate("${project.artifactId}");
109         verify(inputHandler, times(2)).readLine();
110     }
111 
112     /**
113      * This test will check that only the <code>project.groupId</code> is printed to
114      * stdout nothing else.
115      *
116      * @throws Exception in case of errors.
117      * @see <a href="https://issues.apache.org/jira/browse/MPH-144">MPH-144</a>
118      */
119     public void testEvaluateQuiteModeWithOutputOnStdout() throws Exception {
120         File testPom = new File(getBasedir(), "target/test-classes/unit/evaluate/plugin-config-quiet-stdout.xml");
121 
122         EvaluateMojo mojo = (EvaluateMojo) lookupMojo("evaluate", testPom);
123 
124         ExpressionEvaluator expressionEvaluator = mock(PluginParameterExpressionEvaluator.class);
125         when(expressionEvaluator.evaluate(anyString())).thenReturn("org.apache.maven.its.help");
126 
127         // Quiet mode given on command line.(simulation)
128         interceptingLogger.setInfoEnabled(false);
129 
130         setUpMojo(mojo, null, expressionEvaluator);
131 
132         PrintStream saveOut = System.out;
133         ByteArrayOutputStream baos = new ByteArrayOutputStream();
134         System.setOut(new PrintStream(baos));
135 
136         try {
137             mojo.execute();
138         } finally {
139             System.setOut(saveOut);
140             baos.close();
141         }
142 
143         String stdResult = baos.toString();
144         assertEquals("org.apache.maven.its.help", stdResult);
145         assertTrue(interceptingLogger.warnLogs.isEmpty());
146     }
147 
148     private void setUpMojo(EvaluateMojo mojo, InputHandler inputHandler, ExpressionEvaluator expressionEvaluator)
149             throws IllegalAccessException {
150         setVariableValueToObject(mojo, "inputHandler", inputHandler);
151         setVariableValueToObject(mojo, "log", interceptingLogger);
152         setVariableValueToObject(mojo, "settings", new Settings());
153         setVariableValueToObject(mojo, "project", new MavenProjectStub());
154         setVariableValueToObject(mojo, "evaluator", expressionEvaluator);
155     }
156 
157     private static final class InterceptingLog extends DefaultLog {
158         private boolean isInfoEnabled;
159 
160         final List<String> infoLogs = new ArrayList<>();
161 
162         final List<String> warnLogs = new ArrayList<>();
163 
164         public InterceptingLog(Logger logger) {
165             super(logger);
166             this.isInfoEnabled = true;
167         }
168 
169         public void setInfoEnabled(boolean isInfoEnabled) {
170             this.isInfoEnabled = isInfoEnabled;
171         }
172 
173         public boolean isInfoEnabled() {
174             return isInfoEnabled;
175         }
176 
177         @Override
178         public void info(CharSequence content) {
179             if (this.isInfoEnabled) {
180                 super.info(content);
181                 infoLogs.add(content.toString());
182             }
183         }
184 
185         @Override
186         public void warn(CharSequence content) {
187             super.warn(content);
188             warnLogs.add(content.toString());
189         }
190     }
191 }