[MNG-5159] Extend validation of settings.xml to check for uniqueness of id elements

git-svn-id: https://svn.apache.org/repos/asf/maven/maven-3/trunk@1159534 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Benjamin Bentmann 2011-08-19 08:10:58 +00:00
parent f83b0bf199
commit 51ec875d17
2 changed files with 97 additions and 6 deletions

View File

@ -19,7 +19,9 @@ package org.apache.maven.settings.validation;
* under the License.
*/
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.apache.maven.settings.Mirror;
import org.apache.maven.settings.Profile;
@ -77,11 +79,19 @@ public class DefaultSettingsValidator
if ( servers != null )
{
Set<String> serverIds = new HashSet<String>();
for ( int i = 0; i < servers.size(); i++ )
{
Server server = servers.get( i );
validateStringNotEmpty( problems, "servers.server[" + i + "].id", server.getId(), null );
if ( !serverIds.add( server.getId() ) )
{
addViolation( problems, Severity.WARNING, "servers.server.id", null,
"must be unique but found duplicate server with id " + server.getId() );
}
}
}
@ -113,17 +123,29 @@ public class DefaultSettingsValidator
if ( profiles != null )
{
Set<String> profileIds = new HashSet<String>();
for ( Profile profile : profiles )
{
validateRepositories( problems, profile.getRepositories(), "repositories.repository" );
validateRepositories( problems, profile.getPluginRepositories(),
"pluginRepositories.pluginRepository" );
if ( !profileIds.add( profile.getId() ) )
{
addViolation( problems, Severity.WARNING, "profiles.profile.id", null,
"must be unique but found duplicate profile with id " + profile.getId() );
}
String prefix = "profiles.profile[" + profile.getId() + "].";
validateRepositories( problems, profile.getRepositories(), prefix + "repositories.repository" );
validateRepositories( problems, profile.getPluginRepositories(), prefix
+ "pluginRepositories.pluginRepository" );
}
}
}
private void validateRepositories( SettingsProblemCollector problems, List<Repository> repositories, String prefix )
{
Set<String> repoIds = new HashSet<String>();
for ( Repository repository : repositories )
{
validateStringNotEmpty( problems, prefix + ".id", repository.getId(), repository.getUrl() );
@ -138,6 +160,12 @@ public class DefaultSettingsValidator
+ ", using it for other repositories will corrupt your repository metadata." );
}
if ( !repoIds.add( repository.getId() ) )
{
addViolation( problems, Severity.WARNING, prefix + ".id", null,
"must be unique but found duplicate repository with id " + repository.getId() );
}
validateStringNotEmpty( problems, prefix + ".url", repository.getUrl(), repository.getId() );
if ( "legacy".equals( repository.getLayout() ) )

View File

@ -27,6 +27,7 @@ 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.Server;
import org.apache.maven.settings.Settings;
import org.apache.maven.settings.building.SettingsProblemCollector;
import org.apache.maven.settings.building.SettingsProblem.Severity;
@ -127,9 +128,71 @@ public class DefaultSettingsValidatorTest
SimpleProblemCollector problems = new SimpleProblemCollector();
validator.validate( settings, problems );
assertEquals( 3, problems.messages.size() );
assertContains( problems.messages.get( 0 ), "'repositories.repository.id' must not be 'local'" );
assertContains( problems.messages.get( 1 ), "'repositories.repository.url' for local is missing" );
assertContains( problems.messages.get( 2 ), "'repositories.repository.id' must not contain any of these characters" );
assertContains( problems.messages.get( 0 ),
"'profiles.profile[default].repositories.repository.id' must not be 'local'" );
assertContains( problems.messages.get( 1 ),
"'profiles.profile[default].repositories.repository.url' for local is missing" );
assertContains( problems.messages.get( 2 ),
"'profiles.profile[default].repositories.repository.id' must not contain any of these characters" );
}
public void testValidateUniqueServerId()
throws Exception
{
Settings settings = new Settings();
Server server1 = new Server();
server1.setId( "test" );
settings.addServer( server1 );
Server server2 = new Server();
server2.setId( "test" );
settings.addServer( server2 );
SimpleProblemCollector problems = new SimpleProblemCollector();
validator.validate( settings, problems );
assertEquals( 1, problems.messages.size() );
assertContains( problems.messages.get( 0 ),
"'servers.server.id' must be unique but found duplicate server with id test" );
}
public void testValidateUniqueProfileId()
throws Exception
{
Settings settings = new Settings();
Profile profile1 = new Profile();
profile1.setId( "test" );
settings.addProfile( profile1 );
Profile profile2 = new Profile();
profile2.setId( "test" );
settings.addProfile( profile2 );
SimpleProblemCollector problems = new SimpleProblemCollector();
validator.validate( settings, problems );
assertEquals( 1, problems.messages.size() );
assertContains( problems.messages.get( 0 ),
"'profiles.profile.id' must be unique but found duplicate profile with id test" );
}
public void testValidateUniqueRepositoryId()
throws Exception
{
Settings settings = new Settings();
Profile profile = new Profile();
profile.setId( "pro" );
settings.addProfile( profile );
Repository repo1 = new Repository();
repo1.setUrl( "http://apache.org/" );
repo1.setId( "test" );
profile.addRepository( repo1 );
Repository repo2 = new Repository();
repo2.setUrl( "http://apache.org/" );
repo2.setId( "test" );
profile.addRepository( repo2 );
SimpleProblemCollector problems = new SimpleProblemCollector();
validator.validate( settings, problems );
assertEquals( 1, problems.messages.size() );
assertContains( problems.messages.get( 0 ), "'profiles.profile[pro].repositories.repository.id' must be unique"
+ " but found duplicate repository with id test" );
}
private static class SimpleProblemCollector