View Javadoc
1   package org.apache.maven.settings.validation;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.util.ArrayList;
23  import java.util.List;
24  
25  import junit.framework.TestCase;
26  
27  import org.apache.maven.settings.Mirror;
28  import org.apache.maven.settings.Profile;
29  import org.apache.maven.settings.Repository;
30  import org.apache.maven.settings.Server;
31  import org.apache.maven.settings.Settings;
32  import org.apache.maven.settings.building.SettingsProblemCollector;
33  import org.apache.maven.settings.building.SettingsProblem.Severity;
34  
35  /**
36   * @author mkleint
37   */
38  public class DefaultSettingsValidatorTest
39      extends TestCase
40  {
41  
42      private DefaultSettingsValidator validator;
43  
44      protected void setUp()
45          throws Exception
46      {
47          super.setUp();
48  
49          validator = new DefaultSettingsValidator();
50      }
51  
52      protected void tearDown()
53          throws Exception
54      {
55          validator = null;
56  
57          super.tearDown();
58      }
59  
60      private void assertContains( String msg, String substring )
61      {
62          assertTrue( "\"" + substring + "\" was not found in: " + msg, msg.contains( substring ) );
63      }
64  
65      public void testValidate()
66      {
67          Settings model = new Settings();
68          Profile prof = new Profile();
69          prof.setId( "xxx" );
70          model.addProfile( prof );
71          SimpleProblemCollector problems = new SimpleProblemCollector();
72          validator.validate( model, problems );
73          assertEquals( 0, problems.messages.size() );
74  
75          Repository repo = new Repository();
76          prof.addRepository( repo );
77          problems = new SimpleProblemCollector();
78          validator.validate( model, problems );
79          assertEquals( 2, problems.messages.size() );
80  
81          repo.setUrl( "http://xxx.xxx.com" );
82          problems = new SimpleProblemCollector();
83          validator.validate( model, problems );
84          assertEquals( 1, problems.messages.size() );
85  
86          repo.setId( "xxx" );
87          problems = new SimpleProblemCollector();
88          validator.validate( model, problems );
89          assertEquals( 0, problems.messages.size() );
90      }
91  
92      public void testValidateMirror()
93          throws Exception
94      {
95          Settings settings = new Settings();
96          Mirror mirror = new Mirror();
97          mirror.setId( "local" );
98          settings.addMirror( mirror );
99          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 }