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 org.apache.maven.settings.Mirror;
25  import org.apache.maven.settings.Profile;
26  import org.apache.maven.settings.Proxy;
27  import org.apache.maven.settings.Repository;
28  import org.apache.maven.settings.Server;
29  import org.apache.maven.settings.Settings;
30  import org.apache.maven.settings.building.SettingsProblem.Severity;
31  import org.apache.maven.settings.building.SettingsProblemCollector;
32  import org.junit.jupiter.api.BeforeEach;
33  import org.junit.jupiter.api.Test;
34  
35  import static org.junit.jupiter.api.Assertions.assertEquals;
36  import static org.junit.jupiter.api.Assertions.assertTrue;
37  
38  /**
39   * @author mkleint
40   */
41  public class DefaultSettingsValidatorTest {
42  
43      private DefaultSettingsValidator validator;
44  
45      @BeforeEach
46      protected void setUp() throws Exception {
47          validator = new DefaultSettingsValidator();
48      }
49  
50      private void assertContains(String msg, String substring) {
51          assertTrue(msg.contains(substring), "\"" + substring + "\" was not found in: " + msg);
52      }
53  
54      @Test
55      public void testValidate() {
56          Settings model = new Settings();
57          Profile prof = new Profile();
58          prof.setId("xxx");
59          model.addProfile(prof);
60          SimpleProblemCollector problems = new SimpleProblemCollector();
61          validator.validate(model, problems);
62          assertEquals(0, problems.messages.size());
63  
64          Repository repo = new Repository();
65          prof.addRepository(repo);
66          problems = new SimpleProblemCollector();
67          validator.validate(model, problems);
68          assertEquals(2, problems.messages.size());
69  
70          repo.setUrl("http://xxx.xxx.com");
71          problems = new SimpleProblemCollector();
72          validator.validate(model, problems);
73          assertEquals(1, problems.messages.size());
74  
75          repo.setId("xxx");
76          problems = new SimpleProblemCollector();
77          validator.validate(model, problems);
78          assertEquals(0, problems.messages.size());
79      }
80  
81      @Test
82      public void testValidateMirror() throws Exception {
83          Settings settings = new Settings();
84          Mirror mirror = new Mirror();
85          mirror.setId("local");
86          settings.addMirror(mirror);
87          mirror = new Mirror();
88          mirror.setId("illegal\\:/chars");
89          mirror.setUrl("http://void");
90          mirror.setMirrorOf("void");
91          settings.addMirror(mirror);
92  
93          SimpleProblemCollector problems = new SimpleProblemCollector();
94          validator.validate(settings, problems);
95          assertEquals(4, problems.messages.size());
96          assertContains(problems.messages.get(0), "'mirrors.mirror.id' must not be 'local'");
97          assertContains(problems.messages.get(1), "'mirrors.mirror.url' for local is missing");
98          assertContains(problems.messages.get(2), "'mirrors.mirror.mirrorOf' for local is missing");
99          assertContains(problems.messages.get(3), "'mirrors.mirror.id' must not contain any of these characters");
100     }
101 
102     @Test
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     @Test
129     public void testValidateUniqueServerId() throws Exception {
130         Settings settings = new Settings();
131         Server server1 = new Server();
132         server1.setId("test");
133         settings.addServer(server1);
134         Server server2 = new Server();
135         server2.setId("test");
136         settings.addServer(server2);
137 
138         SimpleProblemCollector problems = new SimpleProblemCollector();
139         validator.validate(settings, problems);
140         assertEquals(1, problems.messages.size());
141         assertContains(
142                 problems.messages.get(0), "'servers.server.id' must be unique but found duplicate server with id test");
143     }
144 
145     @Test
146     public void testValidateUniqueProfileId() throws Exception {
147         Settings settings = new Settings();
148         Profile profile1 = new Profile();
149         profile1.setId("test");
150         settings.addProfile(profile1);
151         Profile profile2 = new Profile();
152         profile2.setId("test");
153         settings.addProfile(profile2);
154 
155         SimpleProblemCollector problems = new SimpleProblemCollector();
156         validator.validate(settings, problems);
157         assertEquals(1, problems.messages.size());
158         assertContains(
159                 problems.messages.get(0),
160                 "'profiles.profile.id' must be unique but found duplicate profile with id test");
161     }
162 
163     @Test
164     public void testValidateUniqueRepositoryId() throws Exception {
165         Settings settings = new Settings();
166         Profile profile = new Profile();
167         profile.setId("pro");
168         settings.addProfile(profile);
169         Repository repo1 = new Repository();
170         repo1.setUrl("http://apache.org/");
171         repo1.setId("test");
172         profile.addRepository(repo1);
173         Repository repo2 = new Repository();
174         repo2.setUrl("http://apache.org/");
175         repo2.setId("test");
176         profile.addRepository(repo2);
177 
178         SimpleProblemCollector problems = new SimpleProblemCollector();
179         validator.validate(settings, problems);
180         assertEquals(1, problems.messages.size());
181         assertContains(
182                 problems.messages.get(0),
183                 "'profiles.profile[pro].repositories.repository.id' must be unique"
184                         + " but found duplicate repository with id test");
185     }
186 
187     @Test
188     public void testValidateUniqueProxyId() throws Exception {
189         Settings settings = new Settings();
190         Proxy proxy = new Proxy();
191         String id = null;
192         proxy.setId(id);
193         proxy.setHost("www.example.com");
194         settings.addProxy(proxy);
195         settings.addProxy(proxy);
196 
197         SimpleProblemCollector problems = new SimpleProblemCollector();
198         validator.validate(settings, problems);
199         assertEquals(1, problems.messages.size());
200         assertContains(
201                 problems.messages.get(0),
202                 "'proxies.proxy.id' must be unique" + " but found duplicate proxy with id " + id);
203     }
204 
205     @Test
206     public void testValidateProxy() throws Exception {
207         Settings settings = new Settings();
208         Proxy proxy1 = new Proxy();
209         settings.addProxy(proxy1);
210 
211         SimpleProblemCollector problems = new SimpleProblemCollector();
212         validator.validate(settings, problems);
213         assertEquals(1, problems.messages.size());
214         assertContains(problems.messages.get(0), "'proxies.proxy.host' for default is missing");
215     }
216 
217     private static class SimpleProblemCollector implements SettingsProblemCollector {
218 
219         private final List<String> messages = new ArrayList<>();
220 
221         public void add(Severity severity, String message, int line, int column, Exception cause) {
222             messages.add(message);
223         }
224     }
225 }