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 junit.framework.TestCase;
23  
24  import org.apache.maven.enforcer.rule.api.EnforcerRuleException;
25  import org.apache.maven.enforcer.rule.api.EnforcerRuleHelper;
26  
27  /**
28   * The Class TestRequireProperty.
29   *
30   * @author Paul Gier
31   */
32  public class TestRequireProperty
33      extends TestCase
34  {
35  
36      /**
37       * Test rule.
38       *
39       * @throws EnforcerRuleException the enforcer rule exception
40       */
41      public void testRule()
42          throws EnforcerRuleException
43      {
44          MockProject project = new MockProject();
45          project.setProperty( "testProp", "This is a test." );
46          EnforcerRuleHelper helper = EnforcerTestUtils.getHelper( project );
47  
48          RequireProperty rule = new RequireProperty();
49          // this property should not be set
50          rule.setProperty( "testPropJunk" );
51  
52          try
53          {
54              rule.execute( helper );
55              fail( "Expected an exception." );
56          }
57          catch ( EnforcerRuleException e )
58          {
59              // expected to catch this.
60          }
61  
62          // this property should be set by the surefire
63          // plugin
64          rule.setProperty( "testProp" );
65          try
66          {
67              rule.execute( helper );
68          }
69          catch ( EnforcerRuleException e )
70          {
71              fail( "This should not throw an exception" );
72          }
73      }
74  
75      /**
76       * Test rule with regex.
77       *
78       * @throws EnforcerRuleException the enforcer rule exception
79       */
80      public void testRuleWithRegex()
81          throws EnforcerRuleException
82      {
83          MockProject project = new MockProject();
84          project.setProperty( "testProp", "This is a test." );
85          EnforcerRuleHelper helper = EnforcerTestUtils.getHelper( project );
86  
87          RequireProperty rule = new RequireProperty();
88          rule.setProperty( "testProp" );
89          // This expression should not match the property
90          // value
91          rule.setRegex( "[^abc]" );
92  
93          try
94          {
95              rule.execute( helper );
96              fail( "Expected an exception." );
97          }
98          catch ( EnforcerRuleException e )
99          {
100             // expected to catch this.
101         }
102 
103         // this expr should match the property
104         rule.setRegex( "[This].*[.]" );
105         try
106         {
107             rule.execute( helper );
108         }
109         catch ( EnforcerRuleException e )
110         {
111             fail( "This should not throw an exception" );
112         }
113     }
114 
115     /**
116      * Test id.
117      */
118     public void testId()
119     {
120         RequireProperty rule = new RequireProperty();
121         rule.getCacheId();
122     }
123 }