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.io.File;
23  import java.io.IOException;
24  
25  import org.apache.maven.enforcer.rule.api.EnforcerRuleException;
26  
27  import junit.framework.TestCase;
28  
29  /**
30   * Test the "require files don't exist" rule.
31   * 
32   * @author <a href="brianf@apache.org">Brian Fox</a>
33   */
34  public class TestRequireFilesDontExist
35      extends TestCase
36  {
37      RequireFilesDontExist rule = new RequireFilesDontExist();
38  
39      public void testFileExists()
40          throws EnforcerRuleException, IOException
41      {
42          File f = File.createTempFile( "enforcer", "tmp" );
43          f.deleteOnExit();
44  
45          rule.files = new File[] { f };
46  
47          try
48          {
49              rule.execute( EnforcerTestUtils.getHelper() );
50              fail( "Expected an Exception." );
51          }
52          catch ( EnforcerRuleException e )
53          {
54              assertTrue( true );
55          }
56          f.delete();
57      }
58  
59      public void testEmptyFile()
60          throws EnforcerRuleException, IOException
61      {
62          rule.files = new File[] { null };
63          try
64          {
65              rule.execute( EnforcerTestUtils.getHelper() );
66              fail( "Should get exception" );
67          }
68          catch ( EnforcerRuleException e )
69          {
70              assertTrue( true );
71          }
72      }
73  
74      public void testEmptyFileAllowNull()
75          throws EnforcerRuleException, IOException
76      {
77          rule.files = new File[] { null };
78          rule.allowNulls = true;
79          try
80          {
81              rule.execute( EnforcerTestUtils.getHelper() );
82          }
83          catch ( EnforcerRuleException e )
84          {
85              fail( "Unexpected Exception:" + e.getLocalizedMessage() );
86          }
87      }
88  
89      public void testEmptyFileList()
90          throws EnforcerRuleException, IOException
91      {
92          rule.files = new File[] {};
93          assertEquals( 0, rule.files.length );
94          try
95          {
96              rule.execute( EnforcerTestUtils.getHelper() );
97              fail( "Should get exception" );
98          }
99          catch ( EnforcerRuleException e )
100         {
101             assertTrue( true );
102         }
103     }
104 
105     public void testEmptyFileListAllowNull()
106         throws EnforcerRuleException, IOException
107     {
108         rule.files = new File[] {};
109         assertEquals( 0, rule.files.length );
110         rule.allowNulls = true;
111         try
112         {
113             rule.execute( EnforcerTestUtils.getHelper() );
114         }
115         catch ( EnforcerRuleException e )
116         {
117             fail( "Unexpected Exception:" + e.getLocalizedMessage() );
118         }
119     }
120 
121     public void testFileDoesNotExist()
122         throws EnforcerRuleException, IOException
123     {
124         File f = File.createTempFile( "enforcer", "tmp" );
125         f.delete();
126 
127         assertTrue( !f.exists() );
128 
129         rule.files = new File[] { f };
130 
131         rule.execute( EnforcerTestUtils.getHelper() );
132     }
133 
134     /**
135      * Test id.
136      */
137     public void testId()
138     {
139         rule.getCacheId();
140     }
141 
142 }