View Javadoc
1   package org.apache.maven.plugins.enforcer;
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.Mockito.mock;
23  import static org.mockito.Mockito.when;
24  
25  import java.util.Properties;
26  
27  import org.apache.maven.enforcer.rule.api.EnforcerRuleHelper;
28  import org.apache.maven.execution.MavenExecutionRequest;
29  import org.apache.maven.execution.MavenExecutionResult;
30  import org.apache.maven.execution.MavenSession;
31  import org.apache.maven.model.Plugin;
32  import org.apache.maven.plugin.MojoExecution;
33  import org.apache.maven.plugin.PluginParameterExpressionEvaluator;
34  import org.apache.maven.plugin.logging.SystemStreamLog;
35  import org.apache.maven.plugins.enforcer.utils.MockEnforcerExpressionEvaluator;
36  import org.apache.maven.project.MavenProject;
37  import org.codehaus.plexus.PlexusContainer;
38  import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluator;
39  import org.sonatype.aether.RepositorySystemSession;
40  
41  /**
42   * The Class EnforcerTestUtils.
43   *
44   * @author <a href="mailto:brianf@apache.org">Brian Fox</a>
45   */
46  public final class EnforcerTestUtils
47  {
48      /**
49       * Gets the maven session.
50       *
51       * @return the maven session
52       */
53      public static MavenSession getMavenSession()
54      {
55          PlexusContainer mock = mock( PlexusContainer.class );
56  
57          MavenExecutionRequest mer = mock( MavenExecutionRequest.class );
58  
59          Properties systemProperties = new Properties();
60          systemProperties.put( "maven.version", "3.0" );
61          when( mer.getUserProperties() ).thenReturn( new Properties() );
62          when( mer.getSystemProperties() ).thenReturn( systemProperties );
63  
64          MavenExecutionResult meresult = mock( MavenExecutionResult.class );
65          return new MavenSession( mock, (RepositorySystemSession) null, mer, meresult );
66      }
67  
68      /**
69       * Gets the helper.
70       *
71       * @return the helper
72       */
73      public static EnforcerRuleHelper getHelper()
74      {
75          return getHelper( new MockProject(), false );
76      }
77  
78      /**
79       * Gets the helper.
80       *
81       * @param mockExpression the mock expression
82       * @return the helper
83       */
84      public static EnforcerRuleHelper getHelper( boolean mockExpression )
85      {
86          return getHelper( new MockProject(), mockExpression );
87      }
88  
89      /**
90       * Gets the helper.
91       *
92       * @param project the project
93       * @return the helper
94       */
95      public static EnforcerRuleHelper getHelper( MavenProject project )
96      {
97          return getHelper( project, false );
98      }
99  
100     /**
101      * Gets the helper.
102      *
103      * @param project the project
104      * @param mockExpression the mock expression
105      * @return the helper
106      */
107     public static EnforcerRuleHelper getHelper( MavenProject project, boolean mockExpression )
108     {
109         MavenSession session = getMavenSession();
110         ExpressionEvaluator eval;
111         if ( mockExpression )
112         {
113             eval = new MockEnforcerExpressionEvaluator( session );
114         }
115         else
116         {
117             MojoExecution mockExecution = mock( MojoExecution.class );
118             session.setCurrentProject( project );
119             eval = new PluginParameterExpressionEvaluator( session, mockExecution );
120         }
121         return new DefaultEnforcementRuleHelper( session, eval, new SystemStreamLog(), null );
122     }
123 
124     /**
125      * Gets the helper.
126      *
127      * @param project the project
128      * @param eval the expression evaluator to use
129      * @return the helper
130      */
131     public static EnforcerRuleHelper getHelper( MavenProject project, ExpressionEvaluator eval )
132     {
133         MavenSession session = getMavenSession();
134         return new DefaultEnforcementRuleHelper( session, eval, new SystemStreamLog(), null );
135     }
136 
137     /**
138      * New plugin.
139      *
140      * @param groupId the group id
141      * @param artifactId the artifact id
142      * @param version the version
143      * @return the plugin
144      */
145     public static Plugin newPlugin( String groupId, String artifactId, String version )
146     {
147         Plugin plugin = new Plugin();
148         plugin.setArtifactId( artifactId );
149         plugin.setGroupId( groupId );
150         plugin.setVersion( version );
151         return plugin;
152     }
153 }