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.util.ArrayList;
23  import java.util.Collections;
24  import java.util.List;
25  
26  import org.apache.maven.enforcer.rule.api.EnforcerRuleError;
27  import org.apache.maven.enforcer.rule.api.EnforcerRuleException;
28  import org.apache.maven.enforcer.rules.AbstractStandardEnforcerRule;
29  
30  /**
31   * Contains the common code to compare an array of files against a requirement.
32   *
33   * @author <a href="mailto:brianf@apache.org">Brian Fox</a>
34   */
35  abstract class AbstractRequireFiles extends AbstractStandardEnforcerRule {
36  
37      /** List of files to check. */
38      private List<File> files = Collections.emptyList();
39  
40      /** if null file handles should be allowed. If they are allowed, it means treat it as a success. */
41      private boolean allowNulls = false;
42  
43      /** Allow that a single one of the files can make the rule to pass. */
44      private boolean satisfyAny;
45  
46      // check the file for the specific condition
47      /**
48       * Check one file.
49       *
50       * @param file the file
51       * @return <code>true</code> if successful
52       */
53      abstract boolean checkFile(File file);
54  
55      // return standard error message
56      /**
57       * Gets the error msg.
58       *
59       * @return the error msg
60       */
61      abstract String getErrorMsg();
62  
63      @Override
64      public void execute() throws EnforcerRuleException {
65  
66          if (!allowNulls && files.isEmpty()) {
67              throw new EnforcerRuleError("The file list is empty and Null files are disabled.");
68          }
69  
70          List<File> failures = new ArrayList<>();
71          for (File file : files) {
72              if (!allowNulls && file == null) {
73                  failures.add(file);
74              } else if (!checkFile(file)) {
75                  failures.add(file);
76              }
77          }
78  
79          if (satisfyAny) {
80              int passed = files.size() - failures.size();
81              if (passed == 0) {
82                  fail(failures);
83              }
84          }
85          // if anything was found, log it with the optional message.
86          else if (!failures.isEmpty()) {
87              fail(failures);
88          }
89      }
90  
91      private void fail(List<File> failures) throws EnforcerRuleException {
92          String message = getMessage();
93  
94          StringBuilder buf = new StringBuilder();
95          if (message != null) {
96              buf.append(message + System.lineSeparator());
97          }
98          buf.append(getErrorMsg());
99  
100         for (File file : failures) {
101             if (file != null) {
102                 buf.append(file.getAbsolutePath() + System.lineSeparator());
103             } else {
104                 buf.append("(an empty filename was given and allowNulls is false)" + System.lineSeparator());
105             }
106         }
107 
108         throw new EnforcerRuleException(buf.toString());
109     }
110 
111     @Override
112     public String getCacheId() {
113         return Integer.toString(files.hashCode());
114     }
115 
116     void setFilesList(List<File> files) {
117         this.files = files;
118     }
119 
120     // method using for testing purpose ...
121 
122     List<File> getFiles() {
123         return files;
124     }
125 
126     void setAllowNulls(boolean allowNulls) {
127         this.allowNulls = allowNulls;
128     }
129 
130     void setSatisfyAny(boolean satisfyAny) {
131         this.satisfyAny = satisfyAny;
132     }
133 
134     @Override
135     public String toString() {
136         return String.format(
137                 "%s[message=%s, files=%s, allowNulls=%b, satisfyAny=%b]",
138                 getClass().getSimpleName(), getMessage(), files, allowNulls, satisfyAny);
139     }
140 }