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 exist" rule.
31   * 
32   * @author <a href="brett@apache.org">Brett Porter</a>
33   */
34  public class TestRequireFilesExist
35      extends TestCase
36  {
37      RequireFilesExist rule = new RequireFilesExist();
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          rule.execute( EnforcerTestUtils.getHelper() );
48  
49          f.delete();
50      }
51  
52      public void testEmptyFile()
53          throws EnforcerRuleException, IOException
54      {
55          rule.files = new File[] { null };
56          try
57          {
58              rule.execute( EnforcerTestUtils.getHelper() );
59              fail( "Should get exception" );
60          }
61          catch ( EnforcerRuleException e )
62          {
63              assertTrue( true );
64          }
65      }
66  
67      public void testEmptyFileAllowNull()
68          throws EnforcerRuleException, IOException
69      {
70          rule.files = new File[] { null };
71          rule.allowNulls = true;
72          try
73          {
74              rule.execute( EnforcerTestUtils.getHelper() );
75          }
76          catch ( EnforcerRuleException e )
77          {
78              fail( "Unexpected Exception:" + e.getLocalizedMessage() );
79          }
80      }
81  
82      public void testEmptyFileList()
83          throws EnforcerRuleException, IOException
84      {
85          rule.files = new File[] {};
86          assertEquals(0,rule.files.length);
87          try
88          {
89              rule.execute( EnforcerTestUtils.getHelper() );
90              fail( "Should get exception" );
91          }
92          catch ( EnforcerRuleException e )
93          {
94              assertTrue( true );
95          }
96      }
97  
98      public void testEmptyFileListAllowNull()
99          throws EnforcerRuleException, IOException
100     {
101         rule.files = new File[] {};
102         assertEquals(0,rule.files.length);
103         rule.allowNulls = true;
104         try
105         {
106             rule.execute( EnforcerTestUtils.getHelper() );
107         }
108         catch ( EnforcerRuleException e )
109         {
110             fail( "Unexpected Exception:" + e.getLocalizedMessage() );
111         }
112     }
113 
114     public void testFileDoesNotExist()
115         throws EnforcerRuleException, IOException
116     {
117         File f = File.createTempFile( "enforcer", "tmp" );
118         f.delete();
119         assertTrue(!f.exists());
120         rule.files = new File[] { f };
121 
122         try
123         {
124             rule.execute( EnforcerTestUtils.getHelper() );
125             fail( "Should get exception" );
126         }
127         catch ( EnforcerRuleException e )
128         {
129             assertTrue( true );
130         }
131     }
132 
133     /**
134      * Test id.
135      */
136     public void testId()
137     {
138         rule.getCacheId();
139     }
140 
141 }