001package org.apache.maven.settings.validation; 002 003/* 004 * Licensed to the Apache Software Foundation (ASF) under one 005 * or more contributor license agreements. See the NOTICE file 006 * distributed with this work for additional information 007 * regarding copyright ownership. The ASF licenses this file 008 * to you under the Apache License, Version 2.0 (the 009 * "License"); you may not use this file except in compliance 010 * with the License. You may obtain a copy of the License at 011 * 012 * http://www.apache.org/licenses/LICENSE-2.0 013 * 014 * Unless required by applicable law or agreed to in writing, 015 * software distributed under the License is distributed on an 016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 017 * KIND, either express or implied. See the License for the 018 * specific language governing permissions and limitations 019 * under the License. 020 */ 021 022import java.util.ArrayList; 023import java.util.List; 024 025import junit.framework.TestCase; 026 027import org.apache.maven.settings.Mirror; 028import org.apache.maven.settings.Profile; 029import org.apache.maven.settings.Proxy; 030import org.apache.maven.settings.Repository; 031import org.apache.maven.settings.Server; 032import org.apache.maven.settings.Settings; 033import org.apache.maven.settings.building.SettingsProblemCollector; 034import org.apache.maven.settings.building.SettingsProblem.Severity; 035 036/** 037 * @author mkleint 038 */ 039public class DefaultSettingsValidatorTest 040 extends TestCase 041{ 042 043 private DefaultSettingsValidator validator; 044 045 protected void setUp() 046 throws Exception 047 { 048 super.setUp(); 049 050 validator = new DefaultSettingsValidator(); 051 } 052 053 protected void tearDown() 054 throws Exception 055 { 056 validator = null; 057 058 super.tearDown(); 059 } 060 061 private void assertContains( String msg, String substring ) 062 { 063 assertTrue( "\"" + substring + "\" was not found in: " + msg, msg.contains( substring ) ); 064 } 065 066 public void testValidate() 067 { 068 Settings model = new Settings(); 069 Profile prof = new Profile(); 070 prof.setId( "xxx" ); 071 model.addProfile( prof ); 072 SimpleProblemCollector problems = new SimpleProblemCollector(); 073 validator.validate( model, problems ); 074 assertEquals( 0, problems.messages.size() ); 075 076 Repository repo = new Repository(); 077 prof.addRepository( repo ); 078 problems = new SimpleProblemCollector(); 079 validator.validate( model, problems ); 080 assertEquals( 2, problems.messages.size() ); 081 082 repo.setUrl( "http://xxx.xxx.com" ); 083 problems = new SimpleProblemCollector(); 084 validator.validate( model, problems ); 085 assertEquals( 1, problems.messages.size() ); 086 087 repo.setId( "xxx" ); 088 problems = new SimpleProblemCollector(); 089 validator.validate( model, problems ); 090 assertEquals( 0, problems.messages.size() ); 091 } 092 093 public void testValidateMirror() 094 throws Exception 095 { 096 Settings settings = new Settings(); 097 Mirror mirror = new Mirror(); 098 mirror.setId( "local" ); 099 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<String>(); 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}