View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.enforcer.rules.files;
20  
21  import java.io.File;
22  import java.io.IOException;
23  import java.util.Arrays;
24  import java.util.Collections;
25  
26  import org.apache.maven.enforcer.rule.api.EnforcerRuleException;
27  import org.junit.jupiter.api.Test;
28  import org.junit.jupiter.api.io.TempDir;
29  
30  import static org.junit.jupiter.api.Assertions.assertFalse;
31  import static org.junit.jupiter.api.Assertions.assertNotNull;
32  import static org.junit.jupiter.api.Assertions.assertTrue;
33  import static org.junit.jupiter.api.Assertions.fail;
34  
35  /**
36   * Test the "require files don't exist" rule.
37   *
38   * @author <a href="brianf@apache.org">Brian Fox</a>
39   */
40  class TestRequireFilesDontExist {
41      @TempDir
42      public File temporaryFolder;
43  
44      private final RequireFilesDontExist rule = new RequireFilesDontExist();
45  
46      @Test
47      void testFileExists() throws IOException {
48          File f = File.createTempFile("junit", null, temporaryFolder);
49  
50          rule.setFilesList(Collections.singletonList(f));
51  
52          try {
53              rule.execute();
54              fail("Expected an Exception.");
55          } catch (EnforcerRuleException e) {
56              assertNotNull(e.getMessage());
57          }
58          f.delete();
59      }
60  
61      @Test
62      void testEmptyFile() {
63          rule.setFilesList(Collections.singletonList(null));
64          try {
65              rule.execute();
66              fail("Should get exception");
67          } catch (EnforcerRuleException e) {
68              assertNotNull(e.getMessage());
69          }
70      }
71  
72      @Test
73      void testEmptyFileAllowNull() {
74          rule.setFilesList(Collections.singletonList(null));
75          rule.setAllowNulls(true);
76          try {
77              rule.execute();
78          } catch (EnforcerRuleException e) {
79              fail("Unexpected Exception:" + e.getLocalizedMessage());
80          }
81      }
82  
83      @Test
84      void testEmptyFileList() {
85          rule.setFilesList(Collections.emptyList());
86          assertTrue(rule.getFiles().isEmpty());
87          try {
88              rule.execute();
89              fail("Should get exception");
90          } catch (EnforcerRuleException e) {
91              assertNotNull(e.getMessage());
92          }
93      }
94  
95      @Test
96      void testEmptyFileListAllowNull() {
97          rule.setFilesList(Collections.emptyList());
98          assertTrue(rule.getFiles().isEmpty());
99          rule.setAllowNulls(true);
100         try {
101             rule.execute();
102         } catch (EnforcerRuleException e) {
103             fail("Unexpected Exception:" + e.getLocalizedMessage());
104         }
105     }
106 
107     @Test
108     void testFileDoesNotExist() throws EnforcerRuleException, IOException {
109         File f = File.createTempFile("junit", null, temporaryFolder);
110         f.delete();
111 
112         assertFalse(f.exists());
113 
114         rule.setFilesList(Collections.singletonList(f));
115 
116         rule.execute();
117     }
118 
119     @Test
120     void testFileDoesNotExistSatisfyAny() throws EnforcerRuleException, IOException {
121         File f = File.createTempFile("junit", null, temporaryFolder);
122         f.delete();
123 
124         assertFalse(f.exists());
125 
126         File g = File.createTempFile("junit", null, temporaryFolder);
127 
128         assertTrue(g.exists());
129 
130         rule.setFilesList(Arrays.asList(f, g.getCanonicalFile()));
131         rule.setSatisfyAny(true);
132 
133         rule.execute();
134     }
135 
136     /**
137      * Test id.
138      */
139     @Test
140     void testId() {
141         assertNotNull(rule.getCacheId());
142     }
143 }