View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.settings.validation;
20  
21  import java.util.ArrayList;
22  import java.util.List;
23  
24  import junit.framework.TestCase;
25  import org.apache.maven.settings.Mirror;
26  import org.apache.maven.settings.Profile;
27  import org.apache.maven.settings.Proxy;
28  import org.apache.maven.settings.Repository;
29  import org.apache.maven.settings.Server;
30  import org.apache.maven.settings.Settings;
31  import org.apache.maven.settings.building.SettingsProblem.Severity;
32  import org.apache.maven.settings.building.SettingsProblemCollector;
33  
34  /**
35   * @author mkleint
36   */
37  public class DefaultSettingsValidatorTest extends TestCase {
38  
39      private DefaultSettingsValidator validator;
40  
41      protected void setUp() throws Exception {
42          super.setUp();
43  
44          validator = new DefaultSettingsValidator();
45      }
46  
47      protected void tearDown() throws Exception {
48          validator = null;
49  
50          super.tearDown();
51      }
52  
53      private void assertContains(String msg, String substring) {
54          assertTrue("\"" + substring + "\" was not found in: " + msg, msg.contains(substring));
55      }
56  
57      public void testValidate() {
58          Settings model = new Settings();
59          Profile prof = new Profile();
60          prof.setId("xxx");
61          model.addProfile(prof);
62          SimpleProblemCollector problems = new SimpleProblemCollector();
63          validator.validate(model, problems);
64          assertEquals(0, problems.messages.size());
65  
66          Repository repo = new Repository();
67          prof.addRepository(repo);
68          problems = new SimpleProblemCollector();
69          validator.validate(model, problems);
70          assertEquals(2, problems.messages.size());
71  
72          repo.setUrl("http://xxx.xxx.com");
73          problems = new SimpleProblemCollector();
74          validator.validate(model, problems);
75          assertEquals(1, problems.messages.size());
76  
77          repo.setId("xxx");
78          problems = new SimpleProblemCollector();
79          validator.validate(model, problems);
80          assertEquals(0, problems.messages.size());
81      }
82  
83      public void testValidateMirror() throws Exception {
84          Settings settings = new Settings();
85          Mirror mirror = new Mirror();
86          mirror.setId("local");
87          settings.addMirror(mirror);
88          mirror = new Mirror();
89          mirror.setId("illegal\\:/chars");
90          mirror.setUrl("http://void");
91          mirror.setMirrorOf("void");
92          settings.addMirror(mirror);
93  
94          SimpleProblemCollector problems = new SimpleProblemCollector();
95          validator.validate(settings, problems);
96          assertEquals(4, problems.messages.size());
97          assertContains(problems.messages.get(0), "'mirrors.mirror.id' must not be 'local'");
98          assertContains(problems.messages.get(1), "'mirrors.mirror.url' for local is missing");
99          assertContains(problems.messages.get(2), "'mirrors.mirror.mirrorOf' for local is missing");
100         assertContains(problems.messages.get(3), "'mirrors.mirror.id' must not contain any of these characters");
101     }
102 
103     public void testValidateRepository() throws Exception {
104         Profile profile = new Profile();
105         Repository repo = new Repository();
106         repo.setId("local");
107         profile.addRepository(repo);
108         repo = new Repository();
109         repo.setId("illegal\\:/chars");
110         repo.setUrl("http://void");
111         profile.addRepository(repo);
112         Settings settings = new Settings();
113         settings.addProfile(profile);
114 
115         SimpleProblemCollector problems = new SimpleProblemCollector();
116         validator.validate(settings, problems);
117         assertEquals(3, problems.messages.size());
118         assertContains(
119                 problems.messages.get(0), "'profiles.profile[default].repositories.repository.id' must not be 'local'");
120         assertContains(
121                 problems.messages.get(1),
122                 "'profiles.profile[default].repositories.repository.url' for local is missing");
123         assertContains(
124                 problems.messages.get(2),
125                 "'profiles.profile[default].repositories.repository.id' must not contain any of these characters");
126     }
127 
128     public void testValidateUniqueServerId() throws Exception {
129         Settings settings = new Settings();
130         Server server1 = new Server();
131         server1.setId("test");
132         settings.addServer(server1);
133         Server server2 = new Server();
134         server2.setId("test");
135         settings.addServer(server2);
136 
137         SimpleProblemCollector problems = new SimpleProblemCollector();
138         validator.validate(settings, problems);
139         assertEquals(1, problems.messages.size());
140         assertContains(
141                 problems.messages.get(0), "'servers.server.id' must be unique but found duplicate server with id test");
142     }
143 
144     public void testValidateUniqueProfileId() throws Exception {
145         Settings settings = new Settings();
146         Profile profile1 = new Profile();
147         profile1.setId("test");
148         settings.addProfile(profile1);
149         Profile profile2 = new Profile();
150         profile2.setId("test");
151         settings.addProfile(profile2);
152 
153         SimpleProblemCollector problems = new SimpleProblemCollector();
154         validator.validate(settings, problems);
155         assertEquals(1, problems.messages.size());
156         assertContains(
157                 problems.messages.get(0),
158                 "'profiles.profile.id' must be unique but found duplicate profile with id test");
159     }
160 
161     public void testValidateUniqueRepositoryId() throws Exception {
162         Settings settings = new Settings();
163         Profile profile = new Profile();
164         profile.setId("pro");
165         settings.addProfile(profile);
166         Repository repo1 = new Repository();
167         repo1.setUrl("http://apache.org/");
168         repo1.setId("test");
169         profile.addRepository(repo1);
170         Repository repo2 = new Repository();
171         repo2.setUrl("http://apache.org/");
172         repo2.setId("test");
173         profile.addRepository(repo2);
174 
175         SimpleProblemCollector problems = new SimpleProblemCollector();
176         validator.validate(settings, problems);
177         assertEquals(1, problems.messages.size());
178         assertContains(
179                 problems.messages.get(0),
180                 "'profiles.profile[pro].repositories.repository.id' must be unique"
181                         + " but found duplicate repository with id test");
182     }
183 
184     public void testValidateUniqueProxyId() throws Exception {
185         Settings settings = new Settings();
186         Proxy proxy = new Proxy();
187         String id = null;
188         proxy.setId(id);
189         proxy.setHost("www.example.com");
190         settings.addProxy(proxy);
191         settings.addProxy(proxy);
192 
193         SimpleProblemCollector problems = new SimpleProblemCollector();
194         validator.validate(settings, problems);
195         assertEquals(1, problems.messages.size());
196         assertContains(
197                 problems.messages.get(0),
198                 "'proxies.proxy.id' must be unique" + " but found duplicate proxy with id " + id);
199     }
200 
201     public void testValidateProxy() throws Exception {
202         Settings settings = new Settings();
203         Proxy proxy1 = new Proxy();
204         settings.addProxy(proxy1);
205 
206         SimpleProblemCollector problems = new SimpleProblemCollector();
207         validator.validate(settings, problems);
208         assertEquals(1, problems.messages.size());
209         assertContains(problems.messages.get(0), "'proxies.proxy.host' for default is missing");
210     }
211 
212     private static class SimpleProblemCollector implements SettingsProblemCollector {
213 
214         public List<String> messages = new ArrayList<>();
215 
216         public void add(Severity severity, String message, int line, int column, Exception cause) {
217             messages.add(message);
218         }
219     }
220 }