You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by mk...@apache.org on 2007/02/13 17:26:24 UTC

svn commit: r507077 - in /maven/components/trunk/maven-settings/src: main/java/org/apache/maven/settings/ main/java/org/apache/maven/settings/validation/ main/resources/META-INF/plexus/ test/java/org/apache/maven/settings/validation/

Author: mkleint
Date: Tue Feb 13 08:26:24 2007
New Revision: 507077

URL: http://svn.apache.org/viewvc?view=rev&rev=507077
Log:
have basic validation on settings files, to prevent failures downstream, see MEVENIDE-488 for details

Added:
    maven/components/trunk/maven-settings/src/main/java/org/apache/maven/settings/validation/
    maven/components/trunk/maven-settings/src/main/java/org/apache/maven/settings/validation/DefaultSettingsValidator.java
    maven/components/trunk/maven-settings/src/main/java/org/apache/maven/settings/validation/SettingsValidationResult.java
    maven/components/trunk/maven-settings/src/main/java/org/apache/maven/settings/validation/SettingsValidator.java
    maven/components/trunk/maven-settings/src/test/java/org/apache/maven/settings/validation/
    maven/components/trunk/maven-settings/src/test/java/org/apache/maven/settings/validation/DefaultSettingsValidatorTest.java
Modified:
    maven/components/trunk/maven-settings/src/main/java/org/apache/maven/settings/DefaultMavenSettingsBuilder.java
    maven/components/trunk/maven-settings/src/main/resources/META-INF/plexus/components.xml

Modified: maven/components/trunk/maven-settings/src/main/java/org/apache/maven/settings/DefaultMavenSettingsBuilder.java
URL: http://svn.apache.org/viewvc/maven/components/trunk/maven-settings/src/main/java/org/apache/maven/settings/DefaultMavenSettingsBuilder.java?view=diff&rev=507077&r1=507076&r2=507077
==============================================================================
--- maven/components/trunk/maven-settings/src/main/java/org/apache/maven/settings/DefaultMavenSettingsBuilder.java (original)
+++ maven/components/trunk/maven-settings/src/main/java/org/apache/maven/settings/DefaultMavenSettingsBuilder.java Tue Feb 13 08:26:24 2007
@@ -22,7 +22,6 @@
 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.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 @@
     extends AbstractLogEnabled
     implements MavenSettingsBuilder
 {
+    
+    private SettingsValidator validator;
+    
     // ----------------------------------------------------------------------
     // MavenProfilesBuilder Implementation
     // ----------------------------------------------------------------------
@@ -50,7 +54,7 @@
         throws IOException, XmlPullParserException
     {
         Settings globalSettings = readSettings( globalSettingsFile );
-
+        
         if ( userSettingsFile == null )
         {
             userSettingsFile = new File( new File( System.getProperty( "user.home" ) ), ".m2/settings.xml" );
@@ -69,6 +73,10 @@
 
             userSettings.setRuntimeInfo( new RuntimeInfo( userSettings ) );
         }
+        
+        validateSettings( globalSettings, globalSettingsFile );
+        
+        validateSettings( userSettings, userSettingsFile );
 
         SettingsUtils.merge( userSettings, globalSettings, TrackableBase.GLOBAL_LEVEL );
 
@@ -166,5 +174,16 @@
                 settings.addActiveProfile( profile.getId() );
             }
         }
+    }
+    
+    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") );
+        }
+        
     }
 }

Added: maven/components/trunk/maven-settings/src/main/java/org/apache/maven/settings/validation/DefaultSettingsValidator.java
URL: http://svn.apache.org/viewvc/maven/components/trunk/maven-settings/src/main/java/org/apache/maven/settings/validation/DefaultSettingsValidator.java?view=auto&rev=507077
==============================================================================
--- maven/components/trunk/maven-settings/src/main/java/org/apache/maven/settings/validation/DefaultSettingsValidator.java (added)
+++ maven/components/trunk/maven-settings/src/main/java/org/apache/maven/settings/validation/DefaultSettingsValidator.java Tue Feb 13 08:26:24 2007
@@ -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;
+    }
+
+}

Added: maven/components/trunk/maven-settings/src/main/java/org/apache/maven/settings/validation/SettingsValidationResult.java
URL: http://svn.apache.org/viewvc/maven/components/trunk/maven-settings/src/main/java/org/apache/maven/settings/validation/SettingsValidationResult.java?view=auto&rev=507077
==============================================================================
--- maven/components/trunk/maven-settings/src/main/java/org/apache/maven/settings/validation/SettingsValidationResult.java (added)
+++ maven/components/trunk/maven-settings/src/main/java/org/apache/maven/settings/validation/SettingsValidationResult.java Tue Feb 13 08:26:24 2007
@@ -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();
+    }
+}

Added: maven/components/trunk/maven-settings/src/main/java/org/apache/maven/settings/validation/SettingsValidator.java
URL: http://svn.apache.org/viewvc/maven/components/trunk/maven-settings/src/main/java/org/apache/maven/settings/validation/SettingsValidator.java?view=auto&rev=507077
==============================================================================
--- maven/components/trunk/maven-settings/src/main/java/org/apache/maven/settings/validation/SettingsValidator.java (added)
+++ maven/components/trunk/maven-settings/src/main/java/org/apache/maven/settings/validation/SettingsValidator.java Tue Feb 13 08:26:24 2007
@@ -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 );
+}

Modified: maven/components/trunk/maven-settings/src/main/resources/META-INF/plexus/components.xml
URL: http://svn.apache.org/viewvc/maven/components/trunk/maven-settings/src/main/resources/META-INF/plexus/components.xml?view=diff&rev=507077&r1=507076&r2=507077
==============================================================================
--- maven/components/trunk/maven-settings/src/main/resources/META-INF/plexus/components.xml (original)
+++ maven/components/trunk/maven-settings/src/main/resources/META-INF/plexus/components.xml Tue Feb 13 08:26:24 2007
@@ -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>

Added: maven/components/trunk/maven-settings/src/test/java/org/apache/maven/settings/validation/DefaultSettingsValidatorTest.java
URL: http://svn.apache.org/viewvc/maven/components/trunk/maven-settings/src/test/java/org/apache/maven/settings/validation/DefaultSettingsValidatorTest.java?view=auto&rev=507077
==============================================================================
--- maven/components/trunk/maven-settings/src/test/java/org/apache/maven/settings/validation/DefaultSettingsValidatorTest.java (added)
+++ maven/components/trunk/maven-settings/src/test/java/org/apache/maven/settings/validation/DefaultSettingsValidatorTest.java Tue Feb 13 08:26:24 2007
@@ -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());
+        
+    }
+
+}