001package org.apache.maven.settings.validation;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *  http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import java.util.ArrayList;
023import java.util.List;
024
025import junit.framework.TestCase;
026
027import org.apache.maven.settings.Mirror;
028import org.apache.maven.settings.Profile;
029import org.apache.maven.settings.Repository;
030import org.apache.maven.settings.Server;
031import org.apache.maven.settings.Settings;
032import org.apache.maven.settings.building.SettingsProblemCollector;
033import org.apache.maven.settings.building.SettingsProblem.Severity;
034
035/**
036 * @author mkleint
037 */
038public class DefaultSettingsValidatorTest
039    extends TestCase
040{
041
042    private DefaultSettingsValidator validator;
043
044    protected void setUp()
045        throws Exception
046    {
047        super.setUp();
048
049        validator = new DefaultSettingsValidator();
050    }
051
052    protected void tearDown()
053        throws Exception
054    {
055        validator = null;
056
057        super.tearDown();
058    }
059
060    private void assertContains( String msg, String substring )
061    {
062        assertTrue( "\"" + substring + "\" was not found in: " + msg, msg.contains( substring ) );
063    }
064
065    public void testValidate()
066    {
067        Settings model = new Settings();
068        Profile prof = new Profile();
069        prof.setId( "xxx" );
070        model.addProfile( prof );
071        SimpleProblemCollector problems = new SimpleProblemCollector();
072        validator.validate( model, problems );
073        assertEquals( 0, problems.messages.size() );
074
075        Repository repo = new Repository();
076        prof.addRepository( repo );
077        problems = new SimpleProblemCollector();
078        validator.validate( model, problems );
079        assertEquals( 2, problems.messages.size() );
080
081        repo.setUrl( "http://xxx.xxx.com" );
082        problems = new SimpleProblemCollector();
083        validator.validate( model, problems );
084        assertEquals( 1, problems.messages.size() );
085
086        repo.setId( "xxx" );
087        problems = new SimpleProblemCollector();
088        validator.validate( model, problems );
089        assertEquals( 0, problems.messages.size() );
090    }
091
092    public void testValidateMirror()
093        throws Exception
094    {
095        Settings settings = new Settings();
096        Mirror mirror = new Mirror();
097        mirror.setId( "local" );
098        settings.addMirror( mirror );
099        mirror = new Mirror();
100        mirror.setId( "illegal\\:/chars" );
101        mirror.setUrl( "http://void" );
102        mirror.setMirrorOf( "void" );
103        settings.addMirror( mirror );
104
105        SimpleProblemCollector problems = new SimpleProblemCollector();
106        validator.validate( settings, problems );
107        assertEquals( 4, problems.messages.size() );
108        assertContains( problems.messages.get( 0 ), "'mirrors.mirror.id' must not be 'local'" );
109        assertContains( problems.messages.get( 1 ), "'mirrors.mirror.url' for local is missing" );
110        assertContains( problems.messages.get( 2 ), "'mirrors.mirror.mirrorOf' for local is missing" );
111        assertContains( problems.messages.get( 3 ), "'mirrors.mirror.id' must not contain any of these characters" );
112    }
113
114    public void testValidateRepository()
115        throws Exception
116    {
117        Profile profile = new Profile();
118        Repository repo = new Repository();
119        repo.setId( "local" );
120        profile.addRepository( repo );
121        repo = new Repository();
122        repo.setId( "illegal\\:/chars" );
123        repo.setUrl( "http://void" );
124        profile.addRepository( repo );
125        Settings settings = new Settings();
126        settings.addProfile( profile );
127
128        SimpleProblemCollector problems = new SimpleProblemCollector();
129        validator.validate( settings, problems );
130        assertEquals( 3, problems.messages.size() );
131        assertContains( problems.messages.get( 0 ),
132                        "'profiles.profile[default].repositories.repository.id' must not be 'local'" );
133        assertContains( problems.messages.get( 1 ),
134                        "'profiles.profile[default].repositories.repository.url' for local is missing" );
135        assertContains( problems.messages.get( 2 ),
136                        "'profiles.profile[default].repositories.repository.id' must not contain any of these characters" );
137    }
138
139    public void testValidateUniqueServerId()
140        throws Exception
141    {
142        Settings settings = new Settings();
143        Server server1 = new Server();
144        server1.setId( "test" );
145        settings.addServer( server1 );
146        Server server2 = new Server();
147        server2.setId( "test" );
148        settings.addServer( server2 );
149
150        SimpleProblemCollector problems = new SimpleProblemCollector();
151        validator.validate( settings, problems );
152        assertEquals( 1, problems.messages.size() );
153        assertContains( problems.messages.get( 0 ),
154                        "'servers.server.id' must be unique but found duplicate server with id test" );
155    }
156
157    public void testValidateUniqueProfileId()
158        throws Exception
159    {
160        Settings settings = new Settings();
161        Profile profile1 = new Profile();
162        profile1.setId( "test" );
163        settings.addProfile( profile1 );
164        Profile profile2 = new Profile();
165        profile2.setId( "test" );
166        settings.addProfile( profile2 );
167
168        SimpleProblemCollector problems = new SimpleProblemCollector();
169        validator.validate( settings, problems );
170        assertEquals( 1, problems.messages.size() );
171        assertContains( problems.messages.get( 0 ),
172                        "'profiles.profile.id' must be unique but found duplicate profile with id test" );
173    }
174
175    public void testValidateUniqueRepositoryId()
176        throws Exception
177    {
178        Settings settings = new Settings();
179        Profile profile = new Profile();
180        profile.setId( "pro" );
181        settings.addProfile( profile );
182        Repository repo1 = new Repository();
183        repo1.setUrl( "http://apache.org/" );
184        repo1.setId( "test" );
185        profile.addRepository( repo1 );
186        Repository repo2 = new Repository();
187        repo2.setUrl( "http://apache.org/" );
188        repo2.setId( "test" );
189        profile.addRepository( repo2 );
190
191        SimpleProblemCollector problems = new SimpleProblemCollector();
192        validator.validate( settings, problems );
193        assertEquals( 1, problems.messages.size() );
194        assertContains( problems.messages.get( 0 ), "'profiles.profile[pro].repositories.repository.id' must be unique"
195            + " but found duplicate repository with id test" );
196    }
197
198    private static class SimpleProblemCollector
199        implements SettingsProblemCollector
200    {
201
202        public List<String> messages = new ArrayList<String>();
203
204        public void add( Severity severity, String message, int line, int column, Exception cause )
205        {
206            messages.add( message );
207        }
208
209    }
210
211}