Author: brianf Date: Tue Dec 23 20:51:14 2008 New Revision: 729228 URL: http://svn.apache.org/viewvc?rev=729228&view=rev Log: MENFORCER-56 - enhanced to cover the RequireFilesDontExist and RequireFilesSize rules. Also MENFORCER-46: added ITs for the RequireFilesSize rule (it was completely broken before)
Added: maven/enforcer/trunk/enforcer-rules/src/test/java/org/apache/maven/plugins/enforcer/TestRequireFilesDontExist.java maven/enforcer/trunk/enforcer-rules/src/test/java/org/apache/maven/plugins/enforcer/TestRequireFilesSize.java Modified: maven/enforcer/trunk/enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/AbstractRequireFiles.java maven/enforcer/trunk/enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/RequireFilesDontExist.java maven/enforcer/trunk/enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/RequireFilesExist.java maven/enforcer/trunk/enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/RequireFilesSize.java maven/enforcer/trunk/enforcer-rules/src/site/apt/requireFilesDontExist.apt maven/enforcer/trunk/enforcer-rules/src/site/apt/requireFilesExist.apt maven/enforcer/trunk/enforcer-rules/src/site/apt/requireFilesSize.apt maven/enforcer/trunk/enforcer-rules/src/test/java/org/apache/maven/plugins/enforcer/TestRequireFilesExist.java Modified: maven/enforcer/trunk/enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/AbstractRequireFiles.java URL: http://svn.apache.org/viewvc/maven/enforcer/trunk/enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/AbstractRequireFiles.java?rev=729228&r1=729227&r2=729228&view=diff ============================================================================== --- maven/enforcer/trunk/enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/AbstractRequireFiles.java (original) +++ maven/enforcer/trunk/enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/AbstractRequireFiles.java Tue Dec 23 20:51:14 2008 @@ -38,6 +38,9 @@ /** Array of files to check. */ File[] files; + + //if null file handles should be allowed. If they are allowed, it means treat it as a success. + boolean allowNulls = false; // check the file for the specific condition /** @@ -64,11 +67,20 @@ public void execute( EnforcerRuleHelper helper ) throws EnforcerRuleException { + + if (!allowNulls && files.length == 0) + { + throw new EnforcerRuleException("The file list is empty and Null files are disabled."); + } ArrayList failures = new ArrayList(); for ( int i = 0; i < files.length; i++ ) { - if ( !checkFile( files[i] ) ) + if (!allowNulls && files[i] == null) + { + failures.add(files[i]); + } + else if ( !checkFile( files[i] ) ) { failures.add( files[i] ); } @@ -94,7 +106,7 @@ } else { - buf.append( "(an empty filename was given)\n" ); + buf.append( "(an empty filename was given and allowNulls is false)\n" ); } } Modified: maven/enforcer/trunk/enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/RequireFilesDontExist.java URL: http://svn.apache.org/viewvc/maven/enforcer/trunk/enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/RequireFilesDontExist.java?rev=729228&r1=729227&r2=729228&view=diff ============================================================================== --- maven/enforcer/trunk/enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/RequireFilesDontExist.java (original) +++ maven/enforcer/trunk/enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/RequireFilesDontExist.java Tue Dec 23 20:51:14 2008 @@ -35,7 +35,8 @@ */ boolean checkFile( File file ) { - return !file.exists(); + //if we get here and the handle is null, treat it as a success + return file == null ? true : !file.exists(); } /* Modified: maven/enforcer/trunk/enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/RequireFilesExist.java URL: http://svn.apache.org/viewvc/maven/enforcer/trunk/enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/RequireFilesExist.java?rev=729228&r1=729227&r2=729228&view=diff ============================================================================== --- maven/enforcer/trunk/enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/RequireFilesExist.java (original) +++ maven/enforcer/trunk/enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/RequireFilesExist.java Tue Dec 23 20:51:14 2008 @@ -35,7 +35,8 @@ */ boolean checkFile( File file ) { - return file == null ? false : file.exists(); + //if we get here and the handle is null, treat it as a success + return file == null ? true : file.exists(); } /* Modified: maven/enforcer/trunk/enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/RequireFilesSize.java URL: http://svn.apache.org/viewvc/maven/enforcer/trunk/enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/RequireFilesSize.java?rev=729228&r1=729227&r2=729228&view=diff ============================================================================== --- maven/enforcer/trunk/enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/RequireFilesSize.java (original) +++ maven/enforcer/trunk/enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/RequireFilesSize.java Tue Dec 23 20:51:14 2008 @@ -40,7 +40,7 @@ /** the max size allowed. */ long maxsize = 10000; - /** the max size allowed. */ + /** the min size allowed. */ long minsize = 0; /** The error msg. */ @@ -57,17 +57,17 @@ public void execute( EnforcerRuleHelper helper ) throws EnforcerRuleException { + this.log = helper.getLog(); + // if the file is already defined, use that. Otherwise get the main artifact. if ( files.length == 0 ) { try { MavenProject project = (MavenProject) helper.evaluate( "${project}" ); - + files = new File[1]; files[0] = project.getArtifact().getFile(); - this.log = helper.getLog(); - super.execute( helper ); } catch ( ExpressionEvaluationException e ) @@ -75,6 +75,10 @@ throw new EnforcerRuleException( "Unable to retrieve the project.", e ); } } + else + { + super.execute( helper ); + } } @@ -103,6 +107,12 @@ */ boolean checkFile( File file ) { + if (file == null) + { + //if we get here and it's null, treat it as a success. + return true; + } + // check the file now if ( file.exists() ) { Modified: maven/enforcer/trunk/enforcer-rules/src/site/apt/requireFilesDontExist.apt URL: http://svn.apache.org/viewvc/maven/enforcer/trunk/enforcer-rules/src/site/apt/requireFilesDontExist.apt?rev=729228&r1=729227&r2=729228&view=diff ============================================================================== --- maven/enforcer/trunk/enforcer-rules/src/site/apt/requireFilesDontExist.apt (original) +++ maven/enforcer/trunk/enforcer-rules/src/site/apt/requireFilesDontExist.apt Tue Dec 23 20:51:14 2008 @@ -23,7 +23,7 @@ August 2008 ------ -Require Files Exist +Require Files Don't Exist This rule checks that the specified list of files do not exist. @@ -32,6 +32,7 @@ * message - an optional message to the user if the rule fails. * files - A list of files to check. + * allowNulls - If null files should be allowed. If allowed, they will be treated as if they do not exist. Default is false. [] Modified: maven/enforcer/trunk/enforcer-rules/src/site/apt/requireFilesExist.apt URL: http://svn.apache.org/viewvc/maven/enforcer/trunk/enforcer-rules/src/site/apt/requireFilesExist.apt?rev=729228&r1=729227&r2=729228&view=diff ============================================================================== --- maven/enforcer/trunk/enforcer-rules/src/site/apt/requireFilesExist.apt (original) +++ maven/enforcer/trunk/enforcer-rules/src/site/apt/requireFilesExist.apt Tue Dec 23 20:51:14 2008 @@ -32,6 +32,7 @@ * message - an optional message to the user if the rule fails. * files - A list of files to check. + * allowNulls - If null files should be allowed. If allowed, they will be treated as if they do exist. Default is false. [] Modified: maven/enforcer/trunk/enforcer-rules/src/site/apt/requireFilesSize.apt URL: http://svn.apache.org/viewvc/maven/enforcer/trunk/enforcer-rules/src/site/apt/requireFilesSize.apt?rev=729228&r1=729227&r2=729228&view=diff ============================================================================== --- maven/enforcer/trunk/enforcer-rules/src/site/apt/requireFilesSize.apt (original) +++ maven/enforcer/trunk/enforcer-rules/src/site/apt/requireFilesSize.apt Tue Dec 23 20:51:14 2008 @@ -31,10 +31,10 @@ The following parameters are supported by this rule: * message - an optional message to the user if the rule fails. - * files - A list of files to check. + * files - A list of files to check. If this list is empty, the main project artifact will be checked. * maxsize - maximum size in bytes for this file * minsize - minimum size in bytes for this file - + * allowNulls - If null files should be allowed. If allowed, they will be treated as if they do exist. Default is false. [] Added: maven/enforcer/trunk/enforcer-rules/src/test/java/org/apache/maven/plugins/enforcer/TestRequireFilesDontExist.java URL: http://svn.apache.org/viewvc/maven/enforcer/trunk/enforcer-rules/src/test/java/org/apache/maven/plugins/enforcer/TestRequireFilesDontExist.java?rev=729228&view=auto ============================================================================== --- maven/enforcer/trunk/enforcer-rules/src/test/java/org/apache/maven/plugins/enforcer/TestRequireFilesDontExist.java (added) +++ maven/enforcer/trunk/enforcer-rules/src/test/java/org/apache/maven/plugins/enforcer/TestRequireFilesDontExist.java Tue Dec 23 20:51:14 2008 @@ -0,0 +1,142 @@ +package org.apache.maven.plugins.enforcer; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import java.io.File; +import java.io.IOException; + +import org.apache.maven.enforcer.rule.api.EnforcerRuleException; + +import junit.framework.TestCase; + +/** + * Test the "require files don't exist" rule. + * + * @author <a href="bri...@apache.org">Brian Fox</a> + */ +public class TestRequireFilesDontExist + extends TestCase +{ + RequireFilesDontExist rule = new RequireFilesDontExist(); + + public void testFileExists() + throws EnforcerRuleException, IOException + { + File f = File.createTempFile( "enforcer", "tmp" ); + f.deleteOnExit(); + + rule.files = new File[] { f }; + + try + { + rule.execute( EnforcerTestUtils.getHelper() ); + fail( "Expected an Exception." ); + } + catch ( EnforcerRuleException e ) + { + assertTrue( true ); + } + f.delete(); + } + + public void testEmptyFile() + throws EnforcerRuleException, IOException + { + rule.files = new File[] { null }; + try + { + rule.execute( EnforcerTestUtils.getHelper() ); + fail( "Should get exception" ); + } + catch ( EnforcerRuleException e ) + { + assertTrue( true ); + } + } + + public void testEmptyFileAllowNull() + throws EnforcerRuleException, IOException + { + rule.files = new File[] { null }; + rule.allowNulls = true; + try + { + rule.execute( EnforcerTestUtils.getHelper() ); + } + catch ( EnforcerRuleException e ) + { + fail( "Unexpected Exception:" + e.getLocalizedMessage() ); + } + } + + public void testEmptyFileList() + throws EnforcerRuleException, IOException + { + rule.files = new File[] {}; + assertEquals( 0, rule.files.length ); + try + { + rule.execute( EnforcerTestUtils.getHelper() ); + fail( "Should get exception" ); + } + catch ( EnforcerRuleException e ) + { + assertTrue( true ); + } + } + + public void testEmptyFileListAllowNull() + throws EnforcerRuleException, IOException + { + rule.files = new File[] {}; + assertEquals( 0, rule.files.length ); + rule.allowNulls = true; + try + { + rule.execute( EnforcerTestUtils.getHelper() ); + } + catch ( EnforcerRuleException e ) + { + fail( "Unexpected Exception:" + e.getLocalizedMessage() ); + } + } + + public void testFileDoesNotExist() + throws EnforcerRuleException, IOException + { + File f = File.createTempFile( "enforcer", "tmp" ); + f.delete(); + + assertTrue( !f.exists() ); + + rule.files = new File[] { f }; + + rule.execute( EnforcerTestUtils.getHelper() ); + } + + /** + * Test id. + */ + public void testId() + { + rule.getCacheId(); + } + +} Modified: maven/enforcer/trunk/enforcer-rules/src/test/java/org/apache/maven/plugins/enforcer/TestRequireFilesExist.java URL: http://svn.apache.org/viewvc/maven/enforcer/trunk/enforcer-rules/src/test/java/org/apache/maven/plugins/enforcer/TestRequireFilesExist.java?rev=729228&r1=729227&r2=729228&view=diff ============================================================================== --- maven/enforcer/trunk/enforcer-rules/src/test/java/org/apache/maven/plugins/enforcer/TestRequireFilesExist.java (original) +++ maven/enforcer/trunk/enforcer-rules/src/test/java/org/apache/maven/plugins/enforcer/TestRequireFilesExist.java Tue Dec 23 20:51:14 2008 @@ -53,7 +53,37 @@ throws EnforcerRuleException, IOException { rule.files = new File[] { null }; + try + { + rule.execute( EnforcerTestUtils.getHelper() ); + fail( "Should get exception" ); + } + catch ( EnforcerRuleException e ) + { + assertTrue( true ); + } + } + + public void testEmptyFileAllowNull() + throws EnforcerRuleException, IOException + { + rule.files = new File[] { null }; + rule.allowNulls = true; + try + { + rule.execute( EnforcerTestUtils.getHelper() ); + } + catch ( EnforcerRuleException e ) + { + fail( "Unexpected Exception:" + e.getLocalizedMessage() ); + } + } + public void testEmptyFileList() + throws EnforcerRuleException, IOException + { + rule.files = new File[] {}; + assertEquals(0,rule.files.length); try { rule.execute( EnforcerTestUtils.getHelper() ); @@ -65,12 +95,28 @@ } } + public void testEmptyFileListAllowNull() + throws EnforcerRuleException, IOException + { + rule.files = new File[] {}; + assertEquals(0,rule.files.length); + rule.allowNulls = true; + try + { + rule.execute( EnforcerTestUtils.getHelper() ); + } + catch ( EnforcerRuleException e ) + { + fail( "Unexpected Exception:" + e.getLocalizedMessage() ); + } + } + public void testFileDoesNotExist() throws EnforcerRuleException, IOException { File f = File.createTempFile( "enforcer", "tmp" ); f.delete(); - + assertTrue(!f.exists()); rule.files = new File[] { f }; try Added: maven/enforcer/trunk/enforcer-rules/src/test/java/org/apache/maven/plugins/enforcer/TestRequireFilesSize.java URL: http://svn.apache.org/viewvc/maven/enforcer/trunk/enforcer-rules/src/test/java/org/apache/maven/plugins/enforcer/TestRequireFilesSize.java?rev=729228&view=auto ============================================================================== --- maven/enforcer/trunk/enforcer-rules/src/test/java/org/apache/maven/plugins/enforcer/TestRequireFilesSize.java (added) +++ maven/enforcer/trunk/enforcer-rules/src/test/java/org/apache/maven/plugins/enforcer/TestRequireFilesSize.java Tue Dec 23 20:51:14 2008 @@ -0,0 +1,191 @@ +package org.apache.maven.plugins.enforcer; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import java.io.OutputStreamWriter; + +import org.apache.maven.artifact.Artifact; +import org.apache.maven.artifact.DefaultArtifact; +import org.apache.maven.artifact.factory.ArtifactFactory; +import org.apache.maven.enforcer.rule.api.EnforcerRuleException; +import org.apache.maven.plugin.testing.ArtifactStubFactory; +import org.codehaus.plexus.PlexusTestCase; + +import junit.framework.TestCase; + +/** + * Test the "require files exist" rule. + * + * @author <a href="bri...@apache.org">Brian Fox</a> + */ +public class TestRequireFilesSize + extends TestCase +{ + RequireFilesSize rule = new RequireFilesSize(); + + public void testFileExists() + throws EnforcerRuleException, IOException + { + File f = File.createTempFile( "enforcer", "tmp" ); + f.deleteOnExit(); + + rule.files = new File[] { f }; + + rule.execute( EnforcerTestUtils.getHelper() ); + + f.delete(); + } + + public void testEmptyFile() + throws EnforcerRuleException, IOException + { + rule.files = new File[] { null }; + try + { + rule.execute( EnforcerTestUtils.getHelper() ); + fail( "Should get exception" ); + } + catch ( EnforcerRuleException e ) + { + assertTrue( true ); + } + } + + public void testEmptyFileAllowNull() + throws EnforcerRuleException, IOException + { + rule.files = new File[] { null }; + rule.allowNulls = true; + try + { + rule.execute( EnforcerTestUtils.getHelper() ); + } + catch ( EnforcerRuleException e ) + { + fail( "Unexpected Exception:" + e.getLocalizedMessage() ); + } + } + + public void testEmptyFileList() + throws EnforcerRuleException, IOException + { + rule.files = new File[] {}; + + assertEquals( 0, rule.files.length ); + + MockProject project = new MockProject(); + File f = File.createTempFile( "enforcer", "tmp" ); + ArtifactStubFactory factory = new ArtifactStubFactory(); + Artifact a = factory.getReleaseArtifact(); + a.setFile( f ); + + project.setArtifact(a); + + // sanity check the mockProject + assertSame( f, project.getArtifact().getFile() ); + + rule.execute( EnforcerTestUtils.getHelper(project) ); + + } + + public void testFileDoesNotExist() + throws EnforcerRuleException, IOException + { + File f = File.createTempFile( "enforcer", "tmp" ); + f.delete(); + assertTrue( !f.exists() ); + rule.files = new File[] { f }; + + try + { + rule.execute( EnforcerTestUtils.getHelper() ); + fail( "Should get exception" ); + } + catch ( EnforcerRuleException e ) + { + assertTrue( true ); + } + } + + public void testFileTooSmall() + throws EnforcerRuleException, IOException + { + File f = File.createTempFile( "enforcer", "tmp" ); + f.deleteOnExit(); + rule.files = new File[] { f }; + rule.minsize = 10; + try + { + rule.execute( EnforcerTestUtils.getHelper() ); + fail( "Should get exception" ); + } + catch ( EnforcerRuleException e ) + { + assertTrue( true ); + } + } + + public void testFileTooBig() + throws EnforcerRuleException, IOException + { + File f = File.createTempFile( "enforcer", "tmp" ); + f.deleteOnExit(); + try + { + // Create file + FileWriter fstream = new FileWriter( f ); + BufferedWriter out = new BufferedWriter( fstream ); + out.write( "123456789101112131415" ); + // Close the output stream + out.close(); + fstream.close(); + } + catch ( Exception e ) + {// Catch exception if any + System.err.println( "Error: " + e.getMessage() ); + } + + rule.files = new File[] { f }; + rule.maxsize = 10; + assertTrue( f.length() > 10 ); + try + { + rule.execute( EnforcerTestUtils.getHelper() ); + fail( "Should get exception" ); + } + catch ( EnforcerRuleException e ) + { + assertTrue( true ); + } + } + + /** + * Test id. + */ + public void testId() + { + rule.getCacheId(); + } + +}