1 package org.apache.maven.settings.validation;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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.Proxy;
30 import org.apache.maven.settings.Repository;
31 import org.apache.maven.settings.Server;
32 import org.apache.maven.settings.Settings;
33 import org.apache.maven.settings.building.SettingsProblemCollector;
34 import org.apache.maven.settings.building.SettingsProblem.Severity;
35
36
37
38
39 public class DefaultSettingsValidatorTest
40 extends TestCase
41 {
42
43 private DefaultSettingsValidator validator;
44
45 protected void setUp()
46 throws Exception
47 {
48 super.setUp();
49
50 validator = new DefaultSettingsValidator();
51 }
52
53 protected void tearDown()
54 throws Exception
55 {
56 validator = null;
57
58 super.tearDown();
59 }
60
61 private void assertContains( String msg, String substring )
62 {
63 assertTrue( "\"" + substring + "\" was not found in: " + msg, msg.contains( substring ) );
64 }
65
66 public void testValidate()
67 {
68 Settings model = new Settings();
69 Profile prof = new Profile();
70 prof.setId( "xxx" );
71 model.addProfile( prof );
72 SimpleProblemCollector problems = new SimpleProblemCollector();
73 validator.validate( model, problems );
74 assertEquals( 0, problems.messages.size() );
75
76 Repository repo = new Repository();
77 prof.addRepository( repo );
78 problems = new SimpleProblemCollector();
79 validator.validate( model, problems );
80 assertEquals( 2, problems.messages.size() );
81
82 repo.setUrl( "http://xxx.xxx.com" );
83 problems = new SimpleProblemCollector();
84 validator.validate( model, problems );
85 assertEquals( 1, problems.messages.size() );
86
87 repo.setId( "xxx" );
88 problems = new SimpleProblemCollector();
89 validator.validate( model, problems );
90 assertEquals( 0, problems.messages.size() );
91 }
92
93 public void testValidateMirror()
94 throws Exception
95 {
96 Settings settings = new Settings();
97 Mirror mirror = new Mirror();
98 mirror.setId( "local" );
99 settings.addMirror( mirror );
100 mirror = new Mirror();
101 mirror.setId( "illegal\\:/chars" );
102 mirror.setUrl( "http://void" );
103 mirror.setMirrorOf( "void" );
104 settings.addMirror( mirror );
105
106 SimpleProblemCollector problems = new SimpleProblemCollector();
107 validator.validate( settings, problems );
108 assertEquals( 4, problems.messages.size() );
109 assertContains( problems.messages.get( 0 ), "'mirrors.mirror.id' must not be 'local'" );
110 assertContains( problems.messages.get( 1 ), "'mirrors.mirror.url' for local is missing" );
111 assertContains( problems.messages.get( 2 ), "'mirrors.mirror.mirrorOf' for local is missing" );
112 assertContains( problems.messages.get( 3 ), "'mirrors.mirror.id' must not contain any of these characters" );
113 }
114
115 public void testValidateRepository()
116 throws Exception
117 {
118 Profile profile = new Profile();
119 Repository repo = new Repository();
120 repo.setId( "local" );
121 profile.addRepository( repo );
122 repo = new Repository();
123 repo.setId( "illegal\\:/chars" );
124 repo.setUrl( "http://void" );
125 profile.addRepository( repo );
126 Settings settings = new Settings();
127 settings.addProfile( profile );
128
129 SimpleProblemCollector problems = new SimpleProblemCollector();
130 validator.validate( settings, problems );
131 assertEquals( 3, problems.messages.size() );
132 assertContains( problems.messages.get( 0 ),
133 "'profiles.profile[default].repositories.repository.id' must not be 'local'" );
134 assertContains( problems.messages.get( 1 ),
135 "'profiles.profile[default].repositories.repository.url' for local is missing" );
136 assertContains( problems.messages.get( 2 ),
137 "'profiles.profile[default].repositories.repository.id' must not contain any of these characters" );
138 }
139
140 public void testValidateUniqueServerId()
141 throws Exception
142 {
143 Settings settings = new Settings();
144 Server server1 = new Server();
145 server1.setId( "test" );
146 settings.addServer( server1 );
147 Server server2 = new Server();
148 server2.setId( "test" );
149 settings.addServer( server2 );
150
151 SimpleProblemCollector problems = new SimpleProblemCollector();
152 validator.validate( settings, problems );
153 assertEquals( 1, problems.messages.size() );
154 assertContains( problems.messages.get( 0 ),
155 "'servers.server.id' must be unique but found duplicate server with id test" );
156 }
157
158 public void testValidateUniqueProfileId()
159 throws Exception
160 {
161 Settings settings = new Settings();
162 Profile profile1 = new Profile();
163 profile1.setId( "test" );
164 settings.addProfile( profile1 );
165 Profile profile2 = new Profile();
166 profile2.setId( "test" );
167 settings.addProfile( profile2 );
168
169 SimpleProblemCollector problems = new SimpleProblemCollector();
170 validator.validate( settings, problems );
171 assertEquals( 1, problems.messages.size() );
172 assertContains( problems.messages.get( 0 ),
173 "'profiles.profile.id' must be unique but found duplicate profile with id test" );
174 }
175
176 public void testValidateUniqueRepositoryId()
177 throws Exception
178 {
179 Settings settings = new Settings();
180 Profile profile = new Profile();
181 profile.setId( "pro" );
182 settings.addProfile( profile );
183 Repository repo1 = new Repository();
184 repo1.setUrl( "http://apache.org/" );
185 repo1.setId( "test" );
186 profile.addRepository( repo1 );
187 Repository repo2 = new Repository();
188 repo2.setUrl( "http://apache.org/" );
189 repo2.setId( "test" );
190 profile.addRepository( repo2 );
191
192 SimpleProblemCollector problems = new SimpleProblemCollector();
193 validator.validate( settings, problems );
194 assertEquals( 1, problems.messages.size() );
195 assertContains( problems.messages.get( 0 ), "'profiles.profile[pro].repositories.repository.id' must be unique"
196 + " but found duplicate repository with id test" );
197 }
198
199 public void testValidateUniqueProxyId()
200 throws Exception
201 {
202 Settings settings = new Settings();
203 Proxy proxy = new Proxy();
204 String id = null;
205 proxy.setId( id );
206 proxy.setHost("www.example.com");
207 settings.addProxy( proxy );
208 settings.addProxy( proxy );
209
210 SimpleProblemCollector problems = new SimpleProblemCollector();
211 validator.validate( settings, problems );
212 assertEquals( 1, problems.messages.size() );
213 assertContains( problems.messages.get( 0 ), "'proxies.proxy.id' must be unique"
214 + " but found duplicate proxy with id " + id );
215
216 }
217
218 public void testValidateProxy()
219 throws Exception
220 {
221 Settings settings = new Settings();
222 Proxy proxy1 = new Proxy();
223 settings.addProxy( proxy1 );
224
225 SimpleProblemCollector problems = new SimpleProblemCollector();
226 validator.validate( settings, problems );
227 assertEquals( 1, problems.messages.size() );
228 assertContains( problems.messages.get( 0 ), "'proxies.proxy.host' for default is missing" );
229 }
230
231 private static class SimpleProblemCollector
232 implements SettingsProblemCollector
233 {
234
235 public List<String> messages = new ArrayList<>();
236
237 public void add( Severity severity, String message, int line, int column, Exception cause )
238 {
239 messages.add( message );
240 }
241
242 }
243
244 }