[MVERIFIER-36] Code cleanup, move to Java 7
Closes #1
diff --git a/src/main/java/org/apache/maven/plugins/verifier/ConsoleVerificationResultPrinter.java b/src/main/java/org/apache/maven/plugins/verifier/ConsoleVerificationResultPrinter.java
index 64f33a4..90d20b5 100644
--- a/src/main/java/org/apache/maven/plugins/verifier/ConsoleVerificationResultPrinter.java
+++ b/src/main/java/org/apache/maven/plugins/verifier/ConsoleVerificationResultPrinter.java
@@ -20,6 +20,7 @@
*/
import org.apache.maven.plugin.logging.Log;
+import org.apache.maven.plugins.verifier.model.File;
/**
*
@@ -42,37 +43,18 @@
*/
public void print( VerificationResult results )
{
- printExistenceFailures( results );
- printNonExistenceFailures( results );
- printContentFailures( results );
- }
-
- private void printExistenceFailures( VerificationResult results )
- {
- for ( Object o : results.getExistenceFailures() )
+ for ( File file : results.getExistenceFailures() )
{
- org.apache.maven.plugins.verifier.model.File file = (org.apache.maven.plugins.verifier.model.File) o;
-
printMessage( "File not found [" + file.getLocation() + "]" );
}
- }
- private void printNonExistenceFailures( VerificationResult results )
- {
- for ( Object o : results.getNonExistenceFailures() )
+ for ( File file : results.getNonExistenceFailures() )
{
- org.apache.maven.plugins.verifier.model.File file = (org.apache.maven.plugins.verifier.model.File) o;
-
printMessage( "File should not exist [" + file.getLocation() + "]" );
}
- }
- private void printContentFailures( VerificationResult results )
- {
- for ( Object o : results.getContentFailures() )
+ for ( File file : results.getContentFailures() )
{
- org.apache.maven.plugins.verifier.model.File file = (org.apache.maven.plugins.verifier.model.File) o;
-
printMessage( "File [" + file.getLocation() + "] does not match regexp [" + file.getContains() + "]" );
}
}
diff --git a/src/main/java/org/apache/maven/plugins/verifier/VerificationResult.java b/src/main/java/org/apache/maven/plugins/verifier/VerificationResult.java
index c4c7a60..111468b 100644
--- a/src/main/java/org/apache/maven/plugins/verifier/VerificationResult.java
+++ b/src/main/java/org/apache/maven/plugins/verifier/VerificationResult.java
@@ -29,11 +29,11 @@
*/
public class VerificationResult
{
- private List<File> existenceFailures = new ArrayList<File>();
+ private List<File> existenceFailures = new ArrayList<>();
- private List<File> nonExistenceFailures = new ArrayList<File>();
+ private List<File> nonExistenceFailures = new ArrayList<>();
- private List<File> contentFailures = new ArrayList<File>();
+ private List<File> contentFailures = new ArrayList<>();
/**
* @param file {@link File}
diff --git a/src/main/java/org/apache/maven/plugins/verifier/VerifierMojo.java b/src/main/java/org/apache/maven/plugins/verifier/VerifierMojo.java
index 5132efe..c093ec6 100644
--- a/src/main/java/org/apache/maven/plugins/verifier/VerifierMojo.java
+++ b/src/main/java/org/apache/maven/plugins/verifier/VerifierMojo.java
@@ -53,7 +53,7 @@
import org.apache.maven.plugins.verifier.model.Verifications;
import org.apache.maven.plugins.verifier.model.io.xpp3.VerificationsXpp3Reader;
import org.codehaus.plexus.util.FileUtils;
-import org.codehaus.plexus.util.IOUtil;
+import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
/**
* Verifies the existence or non-existence of files/directories and optionally checks file content against a regular
@@ -74,10 +74,9 @@
/**
* The file containing the verifications to perform.
*/
- // CHECKSTYLE_OFF: LineLength
- @Parameter( property = "verifier.verificationFile", defaultValue = "${basedir}/src/test/verifier/verifications.xml", required = true )
+ @Parameter( property = "verifier.verificationFile", defaultValue = "${basedir}/src/test/verifier/verifications.xml",
+ required = true )
private File verificationFile;
- // CHECKSTYLE_ON: LineLength
/**
* Whether the build will fail on verification errors.
@@ -94,7 +93,7 @@
throws MojoExecutionException
{
VerificationResult results = verify();
- this.resultPrinter.print( results );
+ resultPrinter.print( results );
// Fail the build if there are errors
if ( this.failOnError && results.hasFailures() )
@@ -123,17 +122,11 @@
{
VerificationResult results = new VerificationResult();
- Reader reader = null;
- try
+ try ( Reader reader = new FileReader( verificationFile ) )
{
- reader = new FileReader( this.verificationFile );
-
VerificationsXpp3Reader xppReader = new VerificationsXpp3Reader();
Verifications verifications = xppReader.read( reader );
- reader.close();
- reader = null;
-
for ( org.apache.maven.plugins.verifier.model.File file : verifications.getFiles() )
{
// Transform the file to check into an absolute path prefixing the basedir if
@@ -149,18 +142,10 @@
}
}
}
- catch ( org.codehaus.plexus.util.xml.pull.XmlPullParserException e )
+ catch ( XmlPullParserException | IOException e )
{
throw new MojoExecutionException( "Error while verifying files", e );
}
- catch ( IOException e )
- {
- throw new MojoExecutionException( "Error while verifying files", e );
- }
- finally
- {
- IOUtil.close( reader );
- }
return results;
}
@@ -179,8 +164,8 @@
return result;
}
- // CHECKSTYLE_OFF: LineLength
- private boolean verifyFileContent( org.apache.maven.plugins.verifier.model.File fileCheck, VerificationResult results )
+ private boolean verifyFileContent( org.apache.maven.plugins.verifier.model.File fileCheck,
+ VerificationResult results )
throws IOException
{
boolean result = false;
@@ -204,7 +189,6 @@
return result;
}
- // CHECKSTYLE_ON: LineLength
private boolean verifyFileExistence( org.apache.maven.plugins.verifier.model.File fileCheck,
VerificationResult results )
diff --git a/src/test/java/org/apache/maven/plugins/verifier/VerifierMojoTest.java b/src/test/java/org/apache/maven/plugins/verifier/VerifierMojoTest.java
index d6afbdc..0129ee3 100644
--- a/src/test/java/org/apache/maven/plugins/verifier/VerifierMojoTest.java
+++ b/src/test/java/org/apache/maven/plugins/verifier/VerifierMojoTest.java
@@ -1,14 +1,5 @@
package org.apache.maven.plugins.verifier;
-import java.io.File;
-import java.io.UnsupportedEncodingException;
-import java.net.URLDecoder;
-
-import org.apache.maven.plugin.MojoExecutionException;
-import org.apache.maven.plugins.verifier.VerificationResult;
-import org.apache.maven.plugins.verifier.VerificationResultPrinter;
-import org.apache.maven.plugins.verifier.VerifierMojo;
-
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
@@ -28,18 +19,26 @@
* under the License.
*/
-import junit.framework.TestCase;
+import org.apache.maven.plugin.MojoExecutionException;
+import org.junit.Test;
+
+import java.io.File;
+import java.io.UnsupportedEncodingException;
+import java.net.URLDecoder;
+import java.nio.charset.StandardCharsets;
+
+import static org.junit.Assert.*;
public class VerifierMojoTest
- extends TestCase
{
private File getResourceFile( String name ) throws UnsupportedEncodingException
{
String file = getClass().getResource( name ).getFile();
- String decode = URLDecoder.decode( file, "UTF-8" ); // necessary for JDK 1.5+, where spaces are escaped to %20
+ String decode = URLDecoder.decode( file, StandardCharsets.UTF_8.toString() ); // necessary for JDK 1.5+, where spaces are escaped to %20
return new File( decode );
}
+ @Test
public void testPrefixWithBaseDir()
{
VerifierMojo mojo = new VerifierMojo();
@@ -51,6 +50,7 @@
assertEquals( expectedResult.getPath(), result.getPath() );
}
+ @Test
public void testDoNotPrefixWhenAbsolutePath()
{
VerifierMojo mojo = new VerifierMojo();
@@ -62,6 +62,7 @@
assertEquals( absoluteFile.getPath(), result.getPath() );
}
+ @Test
public void testCheckFileThatDoesNotExist()
throws Exception
{
@@ -91,6 +92,7 @@
}
}
+ @Test
public void testCheckFileThatExists()
throws Exception
{
@@ -112,6 +114,7 @@
mojo.execute();
}
+ @Test
public void testCheckForInexistentFile()
throws Exception
{
@@ -132,6 +135,7 @@
mojo.execute();
}
+ @Test
public void testCheckForInexistentFileThatExists()
throws Exception
{
@@ -161,6 +165,7 @@
}
}
+ @Test
public void testCheckFileForContent()
throws Exception
{
@@ -181,6 +186,7 @@
mojo.execute();
}
+ @Test
public void testCheckFileForInvalidContent()
throws Exception
{