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