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.*;
31  
32  /**
33   * Test the "require files exist" rule.
34   *
35   * @author <a href="brett@apache.org">Brett Porter</a>
36   */
37  class TestRequireFilesExist {
38      @TempDir
39      public File temporaryFolder;
40  
41      private final RequireFilesExist rule = new RequireFilesExist();
42  
43      @Test
44      void testFileExists() throws Exception {
45          File f = File.createTempFile("junit", null, temporaryFolder);
46  
47          rule.setFilesList(Collections.singletonList(f.getCanonicalFile()));
48  
49          rule.execute();
50      }
51  
52      @Test
53      void testFileOsIndependentExists() {
54          rule.setFilesList(Collections.singletonList(new File("POM.xml")));
55  
56          EnforcerRuleException e = assertThrows(EnforcerRuleException.class, () -> rule.execute());
57  
58          assertNotNull(e.getMessage());
59      }
60  
61      @Test
62      void testEmptyFile() {
63          rule.setFilesList(Collections.singletonList(null));
64  
65          EnforcerRuleException e = assertThrows(EnforcerRuleException.class, () -> rule.execute());
66  
67          assertNotNull(e.getMessage());
68      }
69  
70      @Test
71      void testEmptyFileAllowNull() throws Exception {
72          rule.setFilesList(Collections.singletonList(null));
73          rule.setAllowNulls(true);
74          rule.execute();
75      }
76  
77      @Test
78      void testEmptyFileList() {
79          rule.setFilesList(Collections.emptyList());
80          assertTrue(rule.getFiles().isEmpty());
81  
82          EnforcerRuleException e = assertThrows(EnforcerRuleException.class, () -> rule.execute());
83  
84          assertNotNull(e.getMessage());
85      }
86  
87      @Test
88      void testEmptyFileListAllowNull() throws Exception {
89          rule.setFilesList(Collections.emptyList());
90          assertTrue(rule.getFiles().isEmpty());
91          rule.setAllowNulls(true);
92          rule.execute();
93      }
94  
95      @Test
96      void testFileDoesNotExist() throws Exception {
97          File f = File.createTempFile("junit", null, temporaryFolder);
98          f.delete();
99  
100         assertFalse(f.exists());
101         rule.setFilesList(Collections.singletonList(f));
102 
103         EnforcerRuleException e = assertThrows(EnforcerRuleException.class, () -> rule.execute());
104 
105         assertNotNull(e.getMessage());
106     }
107 
108     @Test
109     void testFileExistsSatisfyAny() throws EnforcerRuleException, IOException {
110         File f = File.createTempFile("junit", null, temporaryFolder);
111         f.delete();
112 
113         assertFalse(f.exists());
114 
115         File g = File.createTempFile("junit", null, temporaryFolder);
116 
117         assertTrue(g.exists());
118 
119         rule.setFilesList(Arrays.asList(f, g.getCanonicalFile()));
120         rule.setSatisfyAny(true);
121 
122         rule.execute();
123     }
124 
125     /**
126      * Test id.
127      */
128     @Test
129     void testId() {
130         assertNotNull(rule.getCacheId());
131     }
132 }