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