have basic validation on settings files, to prevent failures downstream, see MEVENIDE-488 for details

git-svn-id: https://svn.apache.org/repos/asf/maven/components/trunk@507077 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Milos Kleint 2007-02-13 16:26:24 +00:00
parent b994579133
commit fa2051759e
6 changed files with 352 additions and 2 deletions

View File

@ -22,7 +22,6 @@ import org.codehaus.plexus.util.IOUtil;
import org.codehaus.plexus.util.interpolation.EnvarBasedValueSource;
import org.codehaus.plexus.util.interpolation.RegexBasedInterpolator;
import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
@ -30,6 +29,8 @@ import java.io.StringReader;
import java.io.StringWriter;
import java.util.Iterator;
import java.util.List;
import org.apache.maven.settings.validation.SettingsValidationResult;
import org.apache.maven.settings.validation.SettingsValidator;
/**
* @author jdcasey
@ -39,6 +40,9 @@ public class DefaultMavenSettingsBuilder
extends AbstractLogEnabled
implements MavenSettingsBuilder
{
private SettingsValidator validator;
// ----------------------------------------------------------------------
// MavenProfilesBuilder Implementation
// ----------------------------------------------------------------------
@ -70,6 +74,10 @@ public class DefaultMavenSettingsBuilder
userSettings.setRuntimeInfo( new RuntimeInfo( userSettings ) );
}
validateSettings( globalSettings, globalSettingsFile );
validateSettings( userSettings, userSettingsFile );
SettingsUtils.merge( userSettings, globalSettings, TrackableBase.GLOBAL_LEVEL );
activateDefaultProfiles( userSettings );
@ -167,4 +175,15 @@ public class DefaultMavenSettingsBuilder
}
}
}
private void validateSettings(Settings settings, File location) throws IOException {
SettingsValidationResult validationResult = validator.validate( settings );
if ( validationResult.getMessageCount() > 0 )
{
throw new IOException( "Failed to validate Settings file at " + location +
"\n" + validationResult.render("\n") );
}
}
}

View File

@ -0,0 +1,135 @@
package org.apache.maven.settings.validation;
/*
* Copyright 2001-2005 The Apache Software Foundation.
*
* Licensed 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.util.Iterator;
import java.util.List;
import org.apache.maven.settings.Profile;
import org.apache.maven.settings.Repository;
import org.apache.maven.settings.Settings;
/**
* @author Milos Kleint
*/
public class DefaultSettingsValidator
implements SettingsValidator
{
public SettingsValidationResult validate( Settings model )
{
SettingsValidationResult result = new SettingsValidationResult();
List profiles = model.getProfiles();
if (profiles != null) {
Iterator it = profiles.iterator();
while (it.hasNext()) {
Profile prof = (Profile)it.next();
validateRepositories( result, prof.getRepositories(), "repositories.repository" );
validateRepositories( result, prof.getPluginRepositories(), "pluginRepositories.pluginRepository" );
}
}
return result;
}
private void validateRepositories( SettingsValidationResult 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() );
}
}
// ----------------------------------------------------------------------
// Field validation
// ----------------------------------------------------------------------
private boolean validateStringNotEmpty( String fieldName, SettingsValidationResult result, String string )
{
return validateStringNotEmpty( fieldName, result, string, null );
}
/**
* Asserts:
* <p/>
* <ul>
* <li><code>string.length != null</code>
* <li><code>string.length > 0</code>
* </ul>
*/
private boolean validateStringNotEmpty( String fieldName, SettingsValidationResult result, String string, String sourceHint )
{
if ( !validateNotNull( fieldName, result, string, sourceHint ) )
{
return false;
}
if ( string.length() > 0 )
{
return true;
}
if ( sourceHint != null )
{
result.addMessage( "'" + fieldName + "' is missing for " + sourceHint );
}
else
{
result.addMessage( "'" + fieldName + "' is missing." );
}
return false;
}
/**
* Asserts:
* <p/>
* <ul>
* <li><code>string != null</code>
* </ul>
*/
private boolean validateNotNull( String fieldName, SettingsValidationResult result, Object object, String sourceHint )
{
if ( object != null )
{
return true;
}
if ( sourceHint != null )
{
result.addMessage( "'" + fieldName + "' is missing for " + sourceHint );
}
else
{
result.addMessage( "'" + fieldName + "' is missing." );
}
return false;
}
}

View File

@ -0,0 +1,90 @@
package org.apache.maven.settings.validation;
/*
* Copyright 2001-2005 The Apache Software Foundation.
*
* Licensed 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.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
* @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
* @author Milos Kleint
*/
public final class SettingsValidationResult
{
/** */
private final static String NEWLINE = System.getProperty( "line.separator" );
/** */
private List messages;
public SettingsValidationResult()
{
messages = new ArrayList();
}
public int getMessageCount()
{
return messages.size();
}
public String getMessage( int i )
{
return messages.get( i ).toString();
}
public List getMessages()
{
return Collections.unmodifiableList( messages );
}
public void addMessage( String message )
{
messages.add( message );
}
public String toString()
{
return render( "" );
}
public String render( String indentation )
{
if ( messages.size() == 0 )
{
return indentation + "There were no validation errors.";
}
StringBuffer message = new StringBuffer();
// if ( messages.size() == 1 )
// {
// message.append( "There was 1 validation error: " );
// }
// else
// {
// message.append( "There was " + messages.size() + " validation errors: " + NEWLINE );
// }
//
for ( int i = 0; i < messages.size(); i++ )
{
message.append( indentation + "[" + i + "] " + messages.get( i ).toString() + NEWLINE );
}
return message.toString();
}
}

View File

@ -0,0 +1,30 @@
package org.apache.maven.settings.validation;
/*
* Copyright 2001-2005 The Apache Software Foundation.
*
* Licensed 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 org.apache.maven.settings.Settings;
/**
* @author Milos Kleint
*/
public interface SettingsValidator
{
String ROLE = SettingsValidator.class.getName();
SettingsValidationResult validate( Settings model );
}

View File

@ -1,9 +1,19 @@
<component-set>
<components>
<component>
<role>org.apache.maven.settings.validation.SettingsValidator</role>
<implementation>org.apache.maven.settings.validation.DefaultSettingsValidator</implementation>
</component>
<component>
<role>org.apache.maven.settings.MavenSettingsBuilder</role>
<implementation>org.apache.maven.settings.DefaultMavenSettingsBuilder</implementation>
<requirements>
<requirement>
<role>org.apache.maven.settings.validation.SettingsValidator</role>
</requirement>
</requirements>
<!--
<configuration>
<globalSettingsPath>${maven.home}/conf/settings.xml</globalSettingsPath>

View File

@ -0,0 +1,66 @@
/*
* Copyright 2001-2005 The Apache Software Foundation.
*
* Licensed 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.
*/
package org.apache.maven.settings.validation;
import junit.framework.TestCase;
import org.apache.maven.settings.Profile;
import org.apache.maven.settings.Repository;
import org.apache.maven.settings.Settings;
/**
*
* @author mkleint
*/
public class DefaultSettingsValidatorTest extends TestCase {
public DefaultSettingsValidatorTest(String testName) {
super(testName);
}
protected void setUp() throws Exception {
super.setUp();
}
protected void tearDown() throws Exception {
super.tearDown();
}
public void testValidate() {
Settings model = new Settings();
Profile prof = new Profile();
prof.setId("xxx");
model.addProfile(prof);
DefaultSettingsValidator instance = new DefaultSettingsValidator();
SettingsValidationResult result = instance.validate(model);
assertEquals(0, result.getMessageCount());
Repository repo = new Repository();
prof.addRepository(repo);
result = instance.validate(model);
assertEquals(2, result.getMessageCount());
repo.setUrl("http://xxx.xxx.com");
result = instance.validate(model);
assertEquals(1, result.getMessageCount());
repo.setId("xxx");
result = instance.validate(model);
assertEquals(0, result.getMessageCount());
}
}