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