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.Settings;
31  import org.apache.maven.settings.building.SettingsProblemCollector;
32  import org.apache.maven.settings.building.SettingsProblem.Severity;
33  
34  /**
35   * @author mkleint
36   */
37  public class DefaultSettingsValidatorTest
38      extends TestCase
39  {
40  
41      private DefaultSettingsValidator validator;
42  
43      protected void setUp()
44          throws Exception
45      {
46          super.setUp();
47  
48          validator = new DefaultSettingsValidator();
49      }
50  
51      protected void tearDown()
52          throws Exception
53      {
54          validator = null;
55  
56          super.tearDown();
57      }
58  
59      private void assertContains( String msg, String substring )
60      {
61          assertTrue( "\"" + substring + "\" was not found in: " + msg, msg.contains( substring ) );
62      }
63  
64      public void testValidate()
65      {
66          Settings model = new Settings();
67          Profile prof = new Profile();
68          prof.setId( "xxx" );
69          model.addProfile( prof );
70          SimpleProblemCollector problems = new SimpleProblemCollector();
71          validator.validate( model, problems );
72          assertEquals( 0, problems.messages.size() );
73  
74          Repository repo = new Repository();
75          prof.addRepository( repo );
76          problems = new SimpleProblemCollector();
77          validator.validate( model, problems );
78          assertEquals( 2, problems.messages.size() );
79  
80          repo.setUrl( "http://xxx.xxx.com" );
81          problems = new SimpleProblemCollector();
82          validator.validate( model, problems );
83          assertEquals( 1, problems.messages.size() );
84  
85          repo.setId( "xxx" );
86          problems = new SimpleProblemCollector();
87          validator.validate( model, problems );
88          assertEquals( 0, problems.messages.size() );
89      }
90  
91      public void testValidateMirror()
92          throws Exception
93      {
94          Settings settings = new Settings();
95          Mirror mirror = new Mirror();
96          mirror.setId( "local" );
97          settings.addMirror( mirror );
98          mirror = new Mirror();
99          mirror.setId( "illegal\\:/chars" );
100         mirror.setUrl( "http://void" );
101         mirror.setMirrorOf( "void" );
102         settings.addMirror( mirror );
103 
104         SimpleProblemCollector problems = new SimpleProblemCollector();
105         validator.validate( settings, problems );
106         assertEquals( 4, problems.messages.size() );
107         assertContains( problems.messages.get( 0 ), "'mirrors.mirror.id' must not be 'local'" );
108         assertContains( problems.messages.get( 1 ), "'mirrors.mirror.url' for local is missing" );
109         assertContains( problems.messages.get( 2 ), "'mirrors.mirror.mirrorOf' for local is missing" );
110         assertContains( problems.messages.get( 3 ), "'mirrors.mirror.id' must not contain any of these characters" );
111     }
112 
113     public void testValidateRepository()
114         throws Exception
115     {
116         Profile profile = new Profile();
117         Repository repo = new Repository();
118         repo.setId( "local" );
119         profile.addRepository( repo );
120         repo = new Repository();
121         repo.setId( "illegal\\:/chars" );
122         repo.setUrl( "http://void" );
123         profile.addRepository( repo );
124         Settings settings = new Settings();
125         settings.addProfile( profile );
126 
127         SimpleProblemCollector problems = new SimpleProblemCollector();
128         validator.validate( settings, problems );
129         assertEquals( 3, problems.messages.size() );
130         assertContains( problems.messages.get( 0 ), "'repositories.repository.id' must not be 'local'" );
131         assertContains( problems.messages.get( 1 ), "'repositories.repository.url' for local is missing" );
132         assertContains( problems.messages.get( 2 ), "'repositories.repository.id' must not contain any of these characters" );
133     }
134 
135     private static class SimpleProblemCollector
136         implements SettingsProblemCollector
137     {
138 
139         public List<String> messages = new ArrayList<String>();
140 
141         public void add( Severity severity, String message, int line, int column, Exception cause )
142         {
143             messages.add( message );
144         }
145 
146     }
147 
148 }