Coverage Report - org.apache.maven.plugins.enforcer.RequireFilesSize
 
Classes in this File Line Coverage Branch Coverage Complexity
RequireFilesSize
0%
0/31
0%
0/14
3.6
 
 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  
 
 24  
 import org.apache.maven.enforcer.rule.api.EnforcerRule;
 25  
 import org.apache.maven.enforcer.rule.api.EnforcerRuleException;
 26  
 import org.apache.maven.enforcer.rule.api.EnforcerRuleHelper;
 27  
 import org.apache.maven.plugin.logging.Log;
 28  
 import org.apache.maven.project.MavenProject;
 29  
 import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException;
 30  
 
 31  
 /**
 32  
  * Rule to validate the main artifact is within certain size constraints.
 33  
  *
 34  
  * @author brianf
 35  
  * @author Roman Stumm
 36  
  */
 37  0
 public class RequireFilesSize
 38  
     extends AbstractRequireFiles
 39  
 {
 40  
 
 41  
     /** the max size allowed. */
 42  0
     long maxsize = 10000;
 43  
 
 44  
     /** the min size allowed. */
 45  0
     long minsize = 0;
 46  
 
 47  
     /** The error msg. */
 48  
     private String errorMsg;
 49  
 
 50  
     /** The log. */
 51  
     private Log log;
 52  
 
 53  
     /*
 54  
      * (non-Javadoc)
 55  
      *
 56  
      * @see org.apache.maven.enforcer.rule.api.EnforcerRule#execute(org.apache.maven.enforcer.rule.api.EnforcerRuleHelper)
 57  
      */
 58  
     public void execute( EnforcerRuleHelper helper )
 59  
         throws EnforcerRuleException
 60  
     {
 61  0
         this.log = helper.getLog();
 62  
 
 63  
         // if the file is already defined, use that. Otherwise get the main artifact.
 64  0
         if ( files.length == 0 )
 65  
         {
 66  
             try
 67  
             {
 68  0
                 MavenProject project = (MavenProject) helper.evaluate( "${project}" );
 69  0
                 files = new File[1];
 70  0
                 files[0] = project.getArtifact().getFile();
 71  
 
 72  0
                 super.execute( helper );
 73  
             }
 74  0
             catch ( ExpressionEvaluationException e )
 75  
             {
 76  0
                 throw new EnforcerRuleException( "Unable to retrieve the project.", e );
 77  0
             }
 78  
         }
 79  
         else
 80  
         {
 81  0
             super.execute( helper );
 82  
         }
 83  
 
 84  0
     }
 85  
 
 86  
     /*
 87  
      * (non-Javadoc)
 88  
      *
 89  
      * @see org.apache.maven.enforcer.rule.api.EnforcerRule#isCacheable()
 90  
      */
 91  
     public boolean isCacheable()
 92  
     {
 93  0
         return false;
 94  
     }
 95  
 
 96  
     /*
 97  
      * (non-Javadoc)
 98  
      *
 99  
      * @see org.apache.maven.enforcer.rule.api.EnforcerRule#isResultValid(org.apache.maven.enforcer.rule.api.EnforcerRule)
 100  
      */
 101  
     public boolean isResultValid( EnforcerRule cachedRule )
 102  
     {
 103  0
         return false;
 104  
     }
 105  
 
 106  
     /* (non-Javadoc)
 107  
      * @see org.apache.maven.plugins.enforcer.AbstractRequireFiles#checkFile(java.io.File)
 108  
      */
 109  
     boolean checkFile( File file )
 110  
     {
 111  0
         if (file == null)
 112  
         {
 113  
             //if we get here and it's null, treat it as a success.
 114  0
             return true;
 115  
         }
 116  
 
 117  
         // check the file now
 118  0
         if ( file.exists() )
 119  
         {
 120  0
             long length = file.length();
 121  0
             if ( length < minsize )
 122  
             {
 123  0
                 this.errorMsg = ( file + " size (" + length + ") too small. Min. is " + minsize );
 124  0
                 return false;
 125  
             }
 126  0
             else if ( length > maxsize )
 127  
             {
 128  0
                 this.errorMsg = ( file + " size (" + length + ") too large. Max. is " + maxsize );
 129  0
                 return false;
 130  
             }
 131  
             else
 132  
             {
 133  
 
 134  0
                 this.log.debug( file +
 135  
                     " size (" +
 136  
                     length +
 137  
                     ") is OK (" +
 138  
                     ( minsize == maxsize || minsize == 0 ? ( "max. " + maxsize )
 139  
                                     : ( "between " + minsize + " and " + maxsize ) ) + " byte)." );
 140  
 
 141  0
                 return true;
 142  
             }
 143  
         }
 144  
         else
 145  
         {
 146  0
             this.errorMsg = ( file + " does not exist!" );
 147  0
             return false;
 148  
         }
 149  
     }
 150  
 
 151  
     /* (non-Javadoc)
 152  
      * @see org.apache.maven.plugins.enforcer.AbstractRequireFiles#getErrorMsg()
 153  
      */
 154  
     String getErrorMsg()
 155  
     {
 156  0
         return this.errorMsg;
 157  
     }
 158  
 }