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.junit.Assert.assertFalse;
23  import static org.junit.Assert.assertTrue;
24  import static org.junit.Assert.fail;
25  
26  import org.apache.maven.enforcer.rule.api.EnforcerRule;
27  import org.apache.maven.plugin.MojoExecutionException;
28  import org.junit.Test;
29  import org.junit.runner.RunWith;
30  import org.mockito.InjectMocks;
31  import org.mockito.runners.MockitoJUnitRunner;
32  
33  /**
34   * Exhaustively check the enforcer mojo.
35   * 
36   * @author <a href="mailto:brianf@apache.org">Brian Fox</a>
37   */
38  @RunWith( MockitoJUnitRunner.class )
39  public class TestEnforceMojo
40  {
41  
42      @InjectMocks
43      EnforceMojo mojo;
44  
45      @Test
46      public void testEnforceMojo()
47          throws MojoExecutionException
48      {
49          mojo.setFail( false );
50          mojo.setSession( EnforcerTestUtils.getMavenSession() );
51          mojo.setProject( new MockProject() );
52  
53          try
54          {
55              mojo.execute();
56              fail( "Expected a Mojo Execution Exception." );
57          }
58          catch ( MojoExecutionException e )
59          {
60              System.out.println( "Caught Expected Exception:" + e.getLocalizedMessage() );
61          }
62  
63          EnforcerRule[] rules = new EnforcerRule[10];
64          rules[0] = new MockEnforcerRule( true );
65          rules[1] = new MockEnforcerRule( true );
66          mojo.setRules( rules );
67  
68          mojo.execute();
69  
70          try
71          {
72              mojo.setFailFast( false );
73              mojo.setFail( true );
74              mojo.execute();
75              fail( "Expected a Mojo Execution Exception." );
76          }
77          catch ( MojoExecutionException e )
78          {
79              System.out.println( "Caught Expected Exception:" + e.getLocalizedMessage() );
80          }
81  
82          try
83          {
84              mojo.setFailFast( true );
85              mojo.setFail( true );
86              mojo.execute();
87              fail( "Expected a Mojo Execution Exception." );
88          }
89          catch ( MojoExecutionException e )
90          {
91              System.out.println( "Caught Expected Exception:" + e.getLocalizedMessage() );
92          }
93  
94          ( (MockEnforcerRule) rules[0] ).setFailRule( false );
95          ( (MockEnforcerRule) rules[1] ).setFailRule( false );
96          mojo.execute();
97  
98      }
99  
100     @Test
101     public void testCaching()
102         throws MojoExecutionException
103     {
104         mojo.setFail( true );
105         mojo.setSession( EnforcerTestUtils.getMavenSession() );
106         mojo.setProject( new MockProject() );
107 
108         MockEnforcerRule[] rules = new MockEnforcerRule[10];
109 
110         // check that basic caching works.
111         rules[0] = new MockEnforcerRule( false, "", true, true );
112         rules[1] = new MockEnforcerRule( false, "", true, true );
113         mojo.setRules( rules );
114 
115         EnforceMojo.cache.clear();
116         mojo.execute();
117 
118         assertTrue( "Expected this rule to be executed.", rules[0].executed );
119         assertFalse( "Expected this rule not to be executed.", rules[1].executed );
120 
121         // check that skip caching works.
122         rules[0] = new MockEnforcerRule( false, "", true, true );
123         rules[1] = new MockEnforcerRule( false, "", true, true );
124         mojo.setRules( rules );
125 
126         EnforceMojo.cache.clear();
127         mojo.ignoreCache = true;
128         mojo.execute();
129 
130         assertTrue( "Expected this rule to be executed.", rules[0].executed );
131         assertTrue( "Expected this rule to be executed.", rules[1].executed );
132 
133         mojo.ignoreCache = false;
134 
135         // check that different ids are compared.
136         rules[0] = new MockEnforcerRule( false, "1", true, true );
137         rules[1] = new MockEnforcerRule( false, "2", true, true );
138         rules[2] = new MockEnforcerRule( false, "2", true, true );
139         mojo.setRules( rules );
140 
141         EnforceMojo.cache.clear();
142         mojo.execute();
143 
144         assertTrue( "Expected this rule to be executed.", rules[0].executed );
145         assertTrue( "Expected this rule to be executed.", rules[1].executed );
146         assertFalse( "Expected this rule not to be executed.", rules[2].executed );
147 
148         // check that future overrides are working
149         rules[0] = new MockEnforcerRule( false, "1", true, true );
150         rules[1] = new MockEnforcerRule( false, "1", false, true );
151         rules[2] = null;
152         mojo.setRules( rules );
153 
154         EnforceMojo.cache.clear();
155         mojo.execute();
156 
157         assertTrue( "Expected this rule to be executed.", rules[0].executed );
158         assertTrue( "Expected this rule to be executed.", rules[1].executed );
159 
160         // check that future isResultValid is used
161         rules[0] = new MockEnforcerRule( false, "1", true, true );
162         rules[1] = new MockEnforcerRule( false, "1", true, false );
163         rules[2] = null;
164         mojo.setRules( rules );
165 
166         EnforceMojo.cache.clear();
167         mojo.execute();
168 
169         assertTrue( "Expected this rule to be executed.", rules[0].executed );
170         assertTrue( "Expected this rule to be executed.", rules[1].executed );
171 
172     }
173 
174     @Test
175     public void testCachePersistence1()
176         throws MojoExecutionException
177     {
178         mojo.setFail( true );
179         mojo.setSession( EnforcerTestUtils.getMavenSession() );
180         mojo.setProject( new MockProject() );
181 
182         MockEnforcerRule[] rules = new MockEnforcerRule[10];
183 
184         // check that basic caching works.
185         rules[0] = new MockEnforcerRule( false, "", true, true );
186         rules[1] = new MockEnforcerRule( false, "", true, true );
187         mojo.setRules( rules );
188 
189         EnforceMojo.cache.clear();
190         mojo.execute();
191 
192         assertTrue( "Expected this rule to be executed.", rules[0].executed );
193         assertFalse( "Expected this rule not to be executed.", rules[1].executed );
194 
195     }
196 
197     @Test
198     public void testCachePersistence2()
199         throws MojoExecutionException
200     {
201         mojo.setFail( true );
202         mojo.setSession( EnforcerTestUtils.getMavenSession() );
203         mojo.setProject( new MockProject() );
204 
205         MockEnforcerRule[] rules = new MockEnforcerRule[10];
206 
207         // check that basic caching works.
208         rules[0] = new MockEnforcerRule( false, "", true, true );
209         rules[1] = new MockEnforcerRule( false, "", true, true );
210         mojo.setRules( rules );
211 
212         mojo.execute();
213 
214         assertFalse( "Expected this rule not to be executed.", rules[0].executed );
215         assertFalse( "Expected this rule not to be executed.", rules[1].executed );
216 
217     }
218 
219     @Test
220     public void testCachePersistence3()
221         throws MojoExecutionException
222     {
223         System.gc();
224 
225         try
226         {
227             Thread.sleep( 1000 );
228         }
229         catch ( InterruptedException e )
230         {
231         }
232 
233         mojo.setFail( true );
234         mojo.setSession( EnforcerTestUtils.getMavenSession() );
235         mojo.setProject( new MockProject() );
236 
237         MockEnforcerRule[] rules = new MockEnforcerRule[10];
238 
239         // check that basic caching works.
240         rules[0] = new MockEnforcerRule( false, "", true, true );
241         rules[1] = new MockEnforcerRule( false, "", true, true );
242         mojo.setRules( rules );
243 
244         mojo.execute();
245 
246         assertFalse( "Expected this rule not to be executed.", rules[0].executed );
247         assertFalse( "Expected this rule not to be executed.", rules[1].executed );
248 
249     }
250 }