[MNG-3616] Null Pointer Exception when mirrorOf missing from mirror in settings.xml

git-svn-id: https://svn.apache.org/repos/asf/maven/maven-3/trunk@929083 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Benjamin Bentmann 2010-03-30 11:20:29 +00:00
parent 2e814346e6
commit 912a565ffc
2 changed files with 96 additions and 20 deletions

View File

@ -21,12 +21,15 @@ package org.apache.maven.settings.validation;
import java.util.List;
import org.apache.maven.settings.Mirror;
import org.apache.maven.settings.Profile;
import org.apache.maven.settings.Repository;
import org.apache.maven.settings.Server;
import org.apache.maven.settings.Settings;
import org.apache.maven.settings.building.SettingsProblem;
import org.apache.maven.settings.building.SettingsProblemCollector;
import org.codehaus.plexus.component.annotations.Component;
import org.codehaus.plexus.util.StringUtils;
/**
* @author Milos Kleint
@ -36,16 +39,69 @@ public class DefaultSettingsValidator
implements SettingsValidator
{
private static final String ID_REGEX = "[A-Za-z0-9_\\-.]+";
public void validate( Settings settings, SettingsProblemCollector problems )
{
if ( settings.isUsePluginRegistry() )
{
addWarn( problems, "'usePluginRegistry' is deprecated and has no effect." );
}
List<String> pluginGroups = settings.getPluginGroups();
if ( pluginGroups != null )
{
for ( int i = 0; i < pluginGroups.size(); i++ )
{
String pluginGroup = pluginGroups.get( i ).trim();
if ( StringUtils.isBlank( pluginGroup ) )
{
addError( problems, "'pluginGroups.pluginGroup[" + i + "]' must not be empty." );
}
else if ( !pluginGroup.matches( ID_REGEX ) )
{
addError( problems, "'pluginGroups.pluginGroup[" + i
+ "]' must denote a valid group id and match the pattern " + ID_REGEX );
}
}
}
List<Server> servers = settings.getServers();
if ( servers != null )
{
for ( int i = 0; i < servers.size(); i++ )
{
Server server = servers.get( i );
validateStringNotEmpty( problems, "servers.server[" + i + "].id", server.getId(), null );
}
}
List<Mirror> mirrors = settings.getMirrors();
if ( mirrors != null )
{
for ( Mirror mirror : mirrors )
{
validateStringNotEmpty( problems, "mirrors.mirror.id", mirror.getId(), mirror.getUrl() );
validateStringNotEmpty( problems, "mirrors.mirror.url", mirror.getUrl(), mirror.getId() );
validateStringNotEmpty( problems, "mirrors.mirror.mirrorOf", mirror.getMirrorOf(), mirror.getId() );
}
}
List<Profile> profiles = settings.getProfiles();
if ( profiles != null )
{
for ( Profile prof : profiles )
for ( Profile profile : profiles )
{
validateRepositories( problems, prof.getRepositories(), "repositories.repository" );
validateRepositories( problems, prof.getPluginRepositories(), "pluginRepositories.pluginRepository" );
validateRepositories( problems, profile.getRepositories(), "repositories.repository" );
validateRepositories( problems, profile.getPluginRepositories(), "pluginRepositories.pluginRepository" );
}
}
}
@ -54,9 +110,15 @@ public class DefaultSettingsValidator
{
for ( Repository repository : repositories )
{
validateStringNotEmpty( problems, prefix + ".id", repository.getId() );
validateStringNotEmpty( problems, prefix + ".id", repository.getId(), repository.getUrl() );
validateStringNotEmpty( problems, prefix + ".url", repository.getUrl() );
validateStringNotEmpty( problems, prefix + ".url", repository.getUrl(), repository.getId() );
if ( "legacy".equals( repository.getLayout() ) )
{
addWarn( problems, "'" + prefix + ".layout' for " + repository.getId()
+ " uses the deprecated value 'legacy'." );
}
}
}
@ -64,11 +126,6 @@ public class DefaultSettingsValidator
// Field validation
// ----------------------------------------------------------------------
private boolean validateStringNotEmpty( SettingsProblemCollector problems, String fieldName, String string )
{
return validateStringNotEmpty( problems, fieldName, string, null );
}
/**
* Asserts:
* <p/>
@ -137,4 +194,9 @@ public class DefaultSettingsValidator
problems.add( SettingsProblem.Severity.ERROR, msg, -1, -1, null );
}
private void addWarn( SettingsProblemCollector problems, String msg )
{
problems.add( SettingsProblem.Severity.WARNING, msg, -1, -1, null );
}
}

View File

@ -24,6 +24,7 @@ import java.util.List;
import junit.framework.TestCase;
import org.apache.maven.settings.Mirror;
import org.apache.maven.settings.Profile;
import org.apache.maven.settings.Repository;
import org.apache.maven.settings.Settings;
@ -31,27 +32,27 @@ import org.apache.maven.settings.building.SettingsProblemCollector;
import org.apache.maven.settings.building.SettingsProblem.Severity;
/**
*
* @author mkleint
*/
public class DefaultSettingsValidatorTest
extends TestCase
{
public DefaultSettingsValidatorTest( String testName )
{
super( testName );
}
private DefaultSettingsValidator validator;
protected void setUp()
throws Exception
{
super.setUp();
validator = new DefaultSettingsValidator();
}
protected void tearDown()
throws Exception
{
validator = null;
super.tearDown();
}
@ -61,27 +62,40 @@ public class DefaultSettingsValidatorTest
Profile prof = new Profile();
prof.setId( "xxx" );
model.addProfile( prof );
DefaultSettingsValidator instance = new DefaultSettingsValidator();
SimpleProblemCollector problems = new SimpleProblemCollector();
instance.validate( model, problems );
validator.validate( model, problems );
assertEquals( 0, problems.messages.size() );
Repository repo = new Repository();
prof.addRepository( repo );
problems = new SimpleProblemCollector();
instance.validate( model, problems );
validator.validate( model, problems );
assertEquals( 2, problems.messages.size() );
repo.setUrl( "http://xxx.xxx.com" );
problems = new SimpleProblemCollector();
instance.validate( model, problems );
validator.validate( model, problems );
assertEquals( 1, problems.messages.size() );
repo.setId( "xxx" );
problems = new SimpleProblemCollector();
instance.validate( model, problems );
validator.validate( model, problems );
assertEquals( 0, problems.messages.size() );
}
public void testValidateMirror()
throws Exception
{
Mirror mirror = new Mirror();
Settings settings = new Settings();
settings.addMirror( mirror );
SimpleProblemCollector problems = new SimpleProblemCollector();
validator.validate( settings, problems );
assertEquals( 2, problems.messages.size() );
assertTrue( problems.messages.get( 0 ), problems.messages.get( 0 ).contains( "'mirrors.mirror.url' is missing" ) );
assertTrue( problems.messages.get( 1 ),
problems.messages.get( 1 ).contains( "'mirrors.mirror.mirrorOf' is missing" ) );
}
private static class SimpleProblemCollector