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