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.codehaus.plexus.component.repository.exception.ComponentLookupException;
40  import org.sonatype.aether.RepositorySystemSession;
41  
42  /**
43   * The Class EnforcerTestUtils.
44   *
45   * @author <a href="mailto:brianf@apache.org">Brian Fox</a>
46   */
47  public final class EnforcerTestUtils
48  {
49      /**
50       * Gets the maven session.
51       *
52       * @return the maven session
53       */
54      public static MavenSession getMavenSession()
55      {
56          PlexusContainer mock = mock( PlexusContainer.class );
57  
58          try
59          {
60              when( mock.lookup( "xx" ) ).thenReturn( new MavenProject() );
61          }
62          catch ( ComponentLookupException e )
63          {
64              e.printStackTrace();
65          }
66  
67          MavenExecutionRequest mer = mock( MavenExecutionRequest.class );
68  
69          Properties systemProperties = new Properties();
70          systemProperties.put( "maven.version", "3.0" );
71          when( mer.getUserProperties() ).thenReturn( new Properties() );
72          when(mer.getSystemProperties()).thenReturn( systemProperties );
73  
74          MavenExecutionResult meresult = mock( MavenExecutionResult.class );
75          return new MavenSession( mock, (RepositorySystemSession) null, mer, meresult );
76  
77          // return new MavenSession( new MockPlexusContainer(), (MavenExecutionRequest)null, null,
78          // null );
79  
80          // new MavenSession();
81          // return new MavenSession( new MockPlexusContainer(), null, null, null, null, null, null, new Properties(),
82          // new Date() );
83      }
84  
85      /**
86       * Gets the helper.
87       *
88       * @return the helper
89       */
90      public static EnforcerRuleHelper getHelper()
91      {
92          return getHelper( new MockProject(), false );
93      }
94  
95      /**
96       * Gets the helper.
97       *
98       * @param mockExpression the mock expression
99       * @return the helper
100      */
101     public static EnforcerRuleHelper getHelper( boolean mockExpression )
102     {
103         return getHelper( new MockProject(), mockExpression );
104     }
105 
106     /**
107      * Gets the helper.
108      *
109      * @param project the project
110      * @return the helper
111      */
112     public static EnforcerRuleHelper getHelper( MavenProject project )
113     {
114         return getHelper( project, false );
115     }
116 
117     /**
118      * Gets the helper.
119      *
120      * @param project the project
121      * @param mockExpression the mock expression
122      * @return the helper
123      */
124     public static EnforcerRuleHelper getHelper( MavenProject project, boolean mockExpression )
125     {
126         MavenSession session = getMavenSession();
127         ExpressionEvaluator eval;
128         if ( mockExpression )
129         {
130             eval = new MockEnforcerExpressionEvaluator( session, new MockPathTranslator(), project );
131         }
132         else
133         {
134 //            eval = new DefaultExpressionEvaluator();
135             MojoExecution mockExecution = mock( MojoExecution.class );
136             session.setCurrentProject( project );
137             eval = new PluginParameterExpressionEvaluator( session, mockExecution );
138         }
139         return new DefaultEnforcementRuleHelper( session, eval, new SystemStreamLog(), null );
140     }
141 
142     /**
143      * Gets the helper.
144      *
145      * @param project the project
146      * @param eval the expression evaluator to use
147      * @return the helper
148      */
149     public static EnforcerRuleHelper getHelper( MavenProject project, ExpressionEvaluator eval )
150     {
151         MavenSession session = getMavenSession();
152         return new DefaultEnforcementRuleHelper( session, eval, new SystemStreamLog(), null );
153     }
154 
155     /**
156      * New plugin.
157      *
158      * @param groupId the group id
159      * @param artifactId the artifact id
160      * @param version the version
161      * @return the plugin
162      */
163     public static Plugin newPlugin( String groupId, String artifactId, String version )
164     {
165         Plugin plugin = new Plugin();
166         plugin.setArtifactId( artifactId );
167         plugin.setGroupId( groupId );
168         plugin.setVersion( version );
169         return plugin;
170     }
171 }