|
| 1 | +/* |
| 2 | + * Copyright (c) 2012 GitHub Inc. |
| 3 | + * |
| 4 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 | + * of this software and associated documentation files (the "Software"), to |
| 6 | + * deal in the Software without restriction, including without limitation the |
| 7 | + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
| 8 | + * sell copies of the Software, and to permit persons to whom the Software is |
| 9 | + * furnished to do so, subject to the following conditions: |
| 10 | + * |
| 11 | + * The above copyright notice and this permission notice shall be included in |
| 12 | + * all copies or substantial portions of the Software. |
| 13 | + * |
| 14 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 19 | + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
| 20 | + * IN THE SOFTWARE. |
| 21 | + */ |
| 22 | +package com.github.maven.plugins.core; |
| 23 | + |
| 24 | +import java.io.File; |
| 25 | +import static org.junit.Assert.assertNotNull; |
| 26 | +import static org.junit.Assert.assertNull; |
| 27 | +import static org.junit.Assert.assertTrue; |
| 28 | +import static org.junit.Assert.assertFalse; |
| 29 | + |
| 30 | +import java.util.concurrent.atomic.AtomicReference; |
| 31 | + |
| 32 | +import org.apache.maven.execution.MavenSession; |
| 33 | +import org.apache.maven.plugin.MojoExecutionException; |
| 34 | +import org.apache.maven.plugin.MojoFailureException; |
| 35 | +import org.apache.maven.settings.Proxy; |
| 36 | +import org.apache.maven.settings.Settings; |
| 37 | +import org.apache.maven.settings.building.DefaultSettingsBuilderFactory; |
| 38 | +import org.apache.maven.settings.building.DefaultSettingsBuildingRequest; |
| 39 | +import org.apache.maven.settings.building.FileSettingsSource; |
| 40 | +import org.apache.maven.settings.building.SettingsBuilder; |
| 41 | +import org.apache.maven.settings.building.SettingsBuildingResult; |
| 42 | +import org.eclipse.egit.github.core.client.GitHubClient; |
| 43 | +import org.eclipse.egit.github.core.client.IGitHubConstants; |
| 44 | +import org.junit.Test; |
| 45 | + |
| 46 | +/** |
| 47 | + * NonProxy tests for the various configuration |
| 48 | + * |
| 49 | + * @author Kiyofumi Kondoh |
| 50 | + */ |
| 51 | +public class MatchNonProxyTest { |
| 52 | + |
| 53 | + private class TestMojo extends GitHubProjectMojo { |
| 54 | + |
| 55 | + private final AtomicReference<String> host = new AtomicReference<String>(); |
| 56 | + |
| 57 | + protected GitHubClient createClient() { |
| 58 | + host.set(null); |
| 59 | + return super.createClient(); |
| 60 | + } |
| 61 | + |
| 62 | + protected GitHubClient createClient(String hostname) |
| 63 | + throws MojoExecutionException { |
| 64 | + host.set(hostname); |
| 65 | + return super.createClient(hostname); |
| 66 | + } |
| 67 | + |
| 68 | + public GitHubClient createClient(String host, String userName, |
| 69 | + String password, String oauth2Token, String serverId, |
| 70 | + Settings settings, MavenSession session) |
| 71 | + throws MojoExecutionException { |
| 72 | + return super.createClient(host, userName, password, oauth2Token, |
| 73 | + serverId, settings, session); |
| 74 | + } |
| 75 | + |
| 76 | + public void execute() throws MojoExecutionException, |
| 77 | + MojoFailureException { |
| 78 | + // Intentionally left blank |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * matchNonProxy tests with single nonProxyHosts |
| 84 | + */ |
| 85 | + @Test |
| 86 | + public void matchNonProxyWithSingle_nonPorxyHosts() throws Exception |
| 87 | + { |
| 88 | + SettingsBuilder builder = new DefaultSettingsBuilderFactory().newInstance(); |
| 89 | + assertNotNull( builder ); |
| 90 | + |
| 91 | + DefaultSettingsBuildingRequest request = new DefaultSettingsBuildingRequest(); |
| 92 | + request.setSystemProperties( System.getProperties() ); |
| 93 | + FileSettingsSource fileSource = new FileSettingsSource( new File("src/test/resources/settings/proxy/nonproxy-github.xml").getAbsoluteFile() ); |
| 94 | + request.setUserSettingsSource( fileSource ); |
| 95 | + |
| 96 | + SettingsBuildingResult result = builder.build( request ); |
| 97 | + assertNotNull( result ); |
| 98 | + assertNotNull( result.getEffectiveSettings() ); |
| 99 | + |
| 100 | + TestMojo mojo = new TestMojo(); |
| 101 | + assertNotNull( mojo ); |
| 102 | + |
| 103 | + assertNotNull( result.getEffectiveSettings().getProxies() ); |
| 104 | + for ( final Proxy proxy : result.getEffectiveSettings().getProxies() ) |
| 105 | + { |
| 106 | + { |
| 107 | + final boolean isNonProxy = mojo.matchNonProxy( proxy, IGitHubConstants.HOST_DEFAULT ); |
| 108 | + assertTrue( isNonProxy ); |
| 109 | + } |
| 110 | + { |
| 111 | + final boolean isNonProxy = mojo.matchNonProxy( proxy, IGitHubConstants.HOST_API ); |
| 112 | + assertFalse( isNonProxy ); |
| 113 | + } |
| 114 | + { |
| 115 | + final boolean isNonProxy = mojo.matchNonProxy( proxy, "hoge." + IGitHubConstants.HOST_DEFAULT ); |
| 116 | + assertFalse( isNonProxy ); |
| 117 | + } |
| 118 | + { |
| 119 | + final boolean isNonProxy = mojo.matchNonProxy( proxy, "hoge" + IGitHubConstants.HOST_DEFAULT ); |
| 120 | + assertFalse( isNonProxy ); |
| 121 | + } |
| 122 | + { |
| 123 | + final boolean isNonProxy = mojo.matchNonProxy( proxy, mojo.host.get() ); |
| 124 | + assertTrue( isNonProxy ); |
| 125 | + } |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + /** |
| 130 | + * matchNonProxy tests with multiple nonProxyHosts |
| 131 | + */ |
| 132 | + @Test |
| 133 | + public void matchNonProxyWithMultiple_nonPorxyHosts() throws Exception |
| 134 | + { |
| 135 | + SettingsBuilder builder = new DefaultSettingsBuilderFactory().newInstance(); |
| 136 | + assertNotNull( builder ); |
| 137 | + |
| 138 | + DefaultSettingsBuildingRequest request = new DefaultSettingsBuildingRequest(); |
| 139 | + request.setSystemProperties( System.getProperties() ); |
| 140 | + FileSettingsSource fileSource = new FileSettingsSource( new File("src/test/resources/settings/proxy/nonproxy-github_and_api.xml").getAbsoluteFile() ); |
| 141 | + request.setUserSettingsSource( fileSource ); |
| 142 | + |
| 143 | + SettingsBuildingResult result = builder.build( request ); |
| 144 | + assertNotNull( result ); |
| 145 | + assertNotNull( result.getEffectiveSettings() ); |
| 146 | + |
| 147 | + TestMojo mojo = new TestMojo(); |
| 148 | + assertNotNull( mojo ); |
| 149 | + |
| 150 | + assertNotNull( result.getEffectiveSettings().getProxies() ); |
| 151 | + for ( final Proxy proxy : result.getEffectiveSettings().getProxies() ) |
| 152 | + { |
| 153 | + { |
| 154 | + final boolean isNonProxy = mojo.matchNonProxy( proxy, IGitHubConstants.HOST_DEFAULT ); |
| 155 | + assertTrue( isNonProxy ); |
| 156 | + } |
| 157 | + { |
| 158 | + final boolean isNonProxy = mojo.matchNonProxy( proxy, IGitHubConstants.HOST_API ); |
| 159 | + assertTrue( isNonProxy ); |
| 160 | + } |
| 161 | + { |
| 162 | + final boolean isNonProxy = mojo.matchNonProxy( proxy, "hoge." + IGitHubConstants.HOST_DEFAULT ); |
| 163 | + assertFalse( isNonProxy ); |
| 164 | + } |
| 165 | + { |
| 166 | + final boolean isNonProxy = mojo.matchNonProxy( proxy, "hoge" + IGitHubConstants.HOST_DEFAULT ); |
| 167 | + assertFalse( isNonProxy ); |
| 168 | + } |
| 169 | + { |
| 170 | + final boolean isNonProxy = mojo.matchNonProxy( proxy, mojo.host.get() ); |
| 171 | + assertTrue( isNonProxy ); |
| 172 | + } |
| 173 | + } |
| 174 | + } |
| 175 | + |
| 176 | + /** |
| 177 | + * matchNonProxy tests with wildcard nonProxyHosts |
| 178 | + */ |
| 179 | + @Test |
| 180 | + public void matchNonProxyWithWildcard_nonPorxyHosts() throws Exception |
| 181 | + { |
| 182 | + SettingsBuilder builder = new DefaultSettingsBuilderFactory().newInstance(); |
| 183 | + assertNotNull( builder ); |
| 184 | + |
| 185 | + DefaultSettingsBuildingRequest request = new DefaultSettingsBuildingRequest(); |
| 186 | + request.setSystemProperties( System.getProperties() ); |
| 187 | + FileSettingsSource fileSource = new FileSettingsSource( new File("src/test/resources/settings/proxy/nonproxy-github_wildcard.xml").getAbsoluteFile() ); |
| 188 | + request.setUserSettingsSource( fileSource ); |
| 189 | + |
| 190 | + SettingsBuildingResult result = builder.build( request ); |
| 191 | + assertNotNull( result ); |
| 192 | + assertNotNull( result.getEffectiveSettings() ); |
| 193 | + |
| 194 | + TestMojo mojo = new TestMojo(); |
| 195 | + assertNotNull( mojo ); |
| 196 | + |
| 197 | + assertNotNull( result.getEffectiveSettings().getProxies() ); |
| 198 | + for ( final Proxy proxy : result.getEffectiveSettings().getProxies() ) |
| 199 | + { |
| 200 | + { |
| 201 | + final boolean isNonProxy = mojo.matchNonProxy( proxy, IGitHubConstants.HOST_DEFAULT ); |
| 202 | + assertTrue( isNonProxy ); |
| 203 | + } |
| 204 | + { |
| 205 | + final boolean isNonProxy = mojo.matchNonProxy( proxy, IGitHubConstants.HOST_API ); |
| 206 | + assertTrue( isNonProxy ); |
| 207 | + } |
| 208 | + { |
| 209 | + final boolean isNonProxy = mojo.matchNonProxy( proxy, "hoge." + IGitHubConstants.HOST_DEFAULT ); |
| 210 | + assertTrue( isNonProxy ); |
| 211 | + } |
| 212 | + { |
| 213 | + final boolean isNonProxy = mojo.matchNonProxy( proxy, "hoge" + IGitHubConstants.HOST_DEFAULT ); |
| 214 | + assertTrue( isNonProxy ); |
| 215 | + } |
| 216 | + { |
| 217 | + final boolean isNonProxy = mojo.matchNonProxy( proxy, mojo.host.get() ); |
| 218 | + assertTrue( isNonProxy ); |
| 219 | + } |
| 220 | + } |
| 221 | + } |
| 222 | + |
| 223 | + |
| 224 | + |
| 225 | + |
| 226 | + /** |
| 227 | + * getProxy tests with single nonProxyHosts |
| 228 | + */ |
| 229 | + @Test |
| 230 | + public void getProxyWithSingle_nonProxyHosts() throws Exception |
| 231 | + { |
| 232 | + SettingsBuilder builder = new DefaultSettingsBuilderFactory().newInstance(); |
| 233 | + assertNotNull( builder ); |
| 234 | + |
| 235 | + DefaultSettingsBuildingRequest request = new DefaultSettingsBuildingRequest(); |
| 236 | + request.setSystemProperties( System.getProperties() ); |
| 237 | + FileSettingsSource fileSource = new FileSettingsSource( new File("src/test/resources/settings/proxy/nonproxy-github.xml").getAbsoluteFile() ); |
| 238 | + request.setUserSettingsSource( fileSource ); |
| 239 | + |
| 240 | + SettingsBuildingResult result = builder.build( request ); |
| 241 | + assertNotNull( result ); |
| 242 | + assertNotNull( result.getEffectiveSettings() ); |
| 243 | + |
| 244 | + TestMojo mojo = new TestMojo(); |
| 245 | + assertNotNull( mojo ); |
| 246 | + |
| 247 | + { |
| 248 | + Proxy proxy = mojo.getProxy( result.getEffectiveSettings(), "intra_github-test-nonproxy", mojo.host.get() ); |
| 249 | + assertNull( proxy ); |
| 250 | + } |
| 251 | + { |
| 252 | + Proxy proxy = mojo.getProxy( result.getEffectiveSettings(), "intra_github-test-nonproxy", "intra-github.com" ); |
| 253 | + assertNotNull( proxy ); |
| 254 | + } |
| 255 | + } |
| 256 | + |
| 257 | + /** |
| 258 | + * getProxy tests with nonProxyHosts, which have same id |
| 259 | + */ |
| 260 | + @Test |
| 261 | + public void getProxyIntraWithSameId() throws Exception |
| 262 | + { |
| 263 | + SettingsBuilder builder = new DefaultSettingsBuilderFactory().newInstance(); |
| 264 | + assertNotNull( builder ); |
| 265 | + |
| 266 | + DefaultSettingsBuildingRequest request = new DefaultSettingsBuildingRequest(); |
| 267 | + request.setSystemProperties( System.getProperties() ); |
| 268 | + FileSettingsSource fileSource = new FileSettingsSource( new File("src/test/resources/settings/proxy/nonproxy-intra_github.xml").getAbsoluteFile() ); |
| 269 | + request.setUserSettingsSource( fileSource ); |
| 270 | + |
| 271 | + SettingsBuildingResult result = builder.build( request ); |
| 272 | + assertNotNull( result ); |
| 273 | + assertNotNull( result.getEffectiveSettings() ); |
| 274 | + |
| 275 | + TestMojo mojo = new TestMojo(); |
| 276 | + assertNotNull( mojo ); |
| 277 | + |
| 278 | + { |
| 279 | + Proxy proxy = mojo.getProxy( result.getEffectiveSettings(), "intra_github-test-nonproxy", mojo.host.get() ); |
| 280 | + assertNotNull( proxy ); |
| 281 | + } |
| 282 | + { |
| 283 | + Proxy proxy = mojo.getProxy( result.getEffectiveSettings(), "intra_github-test-nonproxy", "intra-github.com" ); |
| 284 | + assertNull( proxy ); |
| 285 | + } |
| 286 | + { |
| 287 | + Proxy proxy = mojo.getProxy( result.getEffectiveSettings(), "intra_github-test-nonproxy", "intra_github.com" ); |
| 288 | + assertNotNull( proxy ); |
| 289 | + } |
| 290 | + } |
| 291 | + |
| 292 | + /** |
| 293 | + * getProxy tests with nonProxyHosts, which doesn't have same id |
| 294 | + */ |
| 295 | + @Test |
| 296 | + public void getProxyIntraNoSameId() throws Exception |
| 297 | + { |
| 298 | + SettingsBuilder builder = new DefaultSettingsBuilderFactory().newInstance(); |
| 299 | + assertNotNull( builder ); |
| 300 | + |
| 301 | + DefaultSettingsBuildingRequest request = new DefaultSettingsBuildingRequest(); |
| 302 | + request.setSystemProperties( System.getProperties() ); |
| 303 | + FileSettingsSource fileSource = new FileSettingsSource( new File("src/test/resources/settings/proxy/nonproxy-intra_github-no_same_id.xml").getAbsoluteFile() ); |
| 304 | + request.setUserSettingsSource( fileSource ); |
| 305 | + |
| 306 | + SettingsBuildingResult result = builder.build( request ); |
| 307 | + assertNotNull( result ); |
| 308 | + assertNotNull( result.getEffectiveSettings() ); |
| 309 | + |
| 310 | + TestMojo mojo = new TestMojo(); |
| 311 | + assertNotNull( mojo ); |
| 312 | + |
| 313 | + { |
| 314 | + Proxy proxy = mojo.getProxy( result.getEffectiveSettings(), "intra_github-test-nonproxy", mojo.host.get() ); |
| 315 | + assertNotNull( proxy ); |
| 316 | + } |
| 317 | + { |
| 318 | + Proxy proxy = mojo.getProxy( result.getEffectiveSettings(), "intra_github-test-nonproxy", "intra-github.com" ); |
| 319 | + assertNull( proxy ); |
| 320 | + } |
| 321 | + { |
| 322 | + Proxy proxy = mojo.getProxy( result.getEffectiveSettings(), "intra_github-test-nonproxy", "intra_github.com" ); |
| 323 | + assertNotNull( proxy ); |
| 324 | + } |
| 325 | + } |
| 326 | + |
| 327 | + |
| 328 | +} |
0 commit comments