validate repositories in the POM

git-svn-id: https://svn.apache.org/repos/asf/maven/components/trunk@307035 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Brett Leslie Porter 2005-10-07 06:47:28 +00:00
parent 245d512c4a
commit 3ad9ca72c5
3 changed files with 69 additions and 12 deletions

View File

@ -24,6 +24,7 @@ import org.apache.maven.model.Model;
import org.apache.maven.model.Plugin;
import org.apache.maven.model.ReportPlugin;
import org.apache.maven.model.Reporting;
import org.apache.maven.model.Repository;
import org.codehaus.plexus.util.StringUtils;
import java.util.Iterator;
@ -64,14 +65,15 @@ public class DefaultModelValidator
validateSubElementStringNotEmpty( d, "dependencies.dependency.type", result, d.getType() );
validateSubElementStringNotEmpty( d, "dependencies.dependency.version", result, d.getVersion() );
if ( Artifact.SCOPE_SYSTEM.equals( d.getScope() ) && StringUtils.isEmpty( d.getSystemPath() ) )
{
result.addMessage( "For dependency " + d + ": system-scoped dependency must specify systemPath." );
}
else if ( !Artifact.SCOPE_SYSTEM.equals( d.getScope() ) && StringUtils.isNotEmpty( d.getSystemPath() ) )
{
result.addMessage( "For dependency " + d + ": only dependency with system scope can specify systemPath." );
result.addMessage(
"For dependency " + d + ": only dependency with system scope can specify systemPath." );
}
}
@ -83,18 +85,20 @@ public class DefaultModelValidator
Dependency d = (Dependency) it.next();
validateSubElementStringNotEmpty( d, "dependencyManagement.dependencies.dependency.artifactId", result,
d.getArtifactId() );
d.getArtifactId() );
validateSubElementStringNotEmpty( d, "dependencyManagement.dependencies.dependency.groupId", result,
d.getGroupId() );
d.getGroupId() );
if ( Artifact.SCOPE_SYSTEM.equals( d.getScope() ) && StringUtils.isEmpty( d.getSystemPath() ) )
{
result.addMessage( "For managed dependency " + d + ": system-scoped dependency must specify systemPath." );
result.addMessage(
"For managed dependency " + d + ": system-scoped dependency must specify systemPath." );
}
else if ( !Artifact.SCOPE_SYSTEM.equals( d.getScope() ) && StringUtils.isNotEmpty( d.getSystemPath() ) )
{
result.addMessage( "For managed dependency " + d + ": only dependency with system scope can specify systemPath." );
result.addMessage(
"For managed dependency " + d + ": only dependency with system scope can specify systemPath." );
}
}
}
@ -125,11 +129,27 @@ public class DefaultModelValidator
}
}
validateRepositories( result, model.getRepositories(), "repositories.repository" );
validateRepositories( result, model.getPluginRepositories(), "pluginRepositories.pluginRepository" );
forcePluginExecutionIdCollision( model, result );
return result;
}
private void validateRepositories( ModelValidationResult result, List repositories, String prefix )
{
for ( Iterator it = repositories.iterator(); it.hasNext(); )
{
Repository repository = (Repository) it.next();
validateStringNotEmpty( prefix + ".id", result, repository.getId() );
validateStringNotEmpty( prefix + ".url", result, repository.getUrl() );
}
}
private void forcePluginExecutionIdCollision( Model model, ModelValidationResult result )
{
Build build = model.getBuild();
@ -194,7 +214,8 @@ public class DefaultModelValidator
* <li><code>string.length > 0</code>
* </ul>
*/
private boolean validateSubElementStringNotEmpty( Object subElementInstance, String fieldName, ModelValidationResult result, String string )
private boolean validateSubElementStringNotEmpty( Object subElementInstance, String fieldName,
ModelValidationResult result, String string )
{
if ( !validateSubElementNotNull( subElementInstance, fieldName, result, string ) )
{
@ -229,7 +250,7 @@ public class DefaultModelValidator
return false;
}
/**
* Asserts:
* <p/>
@ -237,7 +258,8 @@ public class DefaultModelValidator
* <li><code>string != null</code>
* </ul>
*/
private boolean validateSubElementNotNull( Object subElementInstance, String fieldName, ModelValidationResult result, Object object )
private boolean validateSubElementNotNull( Object subElementInstance, String fieldName,
ModelValidationResult result, Object object )
{
if ( object != null )
{

View File

@ -122,7 +122,8 @@ public class DefaultModelValidatorTest
assertEquals( 1, result.getMessageCount() );
assertTrue( result.getMessage( 0 ).indexOf( "'dependencyManagement.dependencies.dependency.artifactId' is missing." ) > -1 );
assertTrue( result.getMessage( 0 ).indexOf(
"'dependencyManagement.dependencies.dependency.artifactId' is missing." ) > -1 );
}
public void testMissingDependencyManagementGroupId()
@ -132,7 +133,8 @@ public class DefaultModelValidatorTest
assertEquals( 1, result.getMessageCount() );
assertTrue( result.getMessage( 0 ).indexOf( "'dependencyManagement.dependencies.dependency.groupId' is missing." ) > -1 );
assertTrue( result.getMessage( 0 ).indexOf(
"'dependencyManagement.dependencies.dependency.groupId' is missing." ) > -1 );
}
public void testMissingAll()
@ -161,6 +163,22 @@ public class DefaultModelValidatorTest
assertEquals( "'build.plugins.plugin.artifactId' is missing.", result.getMessage( 0 ) );
}
public void testMissingRepositoryId()
throws Exception
{
ModelValidationResult result = validate( "missing-repository-id-pom.xml" );
assertEquals( 4, result.getMessageCount() );
assertEquals( "'repositories.repository.id' is missing.", result.getMessage( 0 ) );
assertEquals( "'repositories.repository.url' is missing.", result.getMessage( 1 ) );
assertEquals( "'pluginRepositories.pluginRepository.id' is missing.", result.getMessage( 2 ) );
assertEquals( "'pluginRepositories.pluginRepository.url' is missing.", result.getMessage( 3 ) );
}
private ModelValidationResult validate( String testName )
throws Exception
{

View File

@ -0,0 +1,17 @@
<project>
<modelVersion>4.0.0</modelVersion>
<artifactId>foo</artifactId>
<groupId>foo</groupId>
<version>99.44</version>
<packaging>bleh</packaging>
<repositories>
<repository>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
</pluginRepository>
</pluginRepositories>
</project>