View Javadoc
1   package org.apache.maven.it;
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 org.apache.maven.it.util.ResourceExtractor;
23  import org.eclipse.jetty.server.Handler;
24  import org.eclipse.jetty.server.NetworkConnector;
25  import org.eclipse.jetty.server.Server;
26  import org.eclipse.jetty.server.handler.DefaultHandler;
27  import org.eclipse.jetty.server.handler.HandlerList;
28  import org.eclipse.jetty.server.handler.ResourceHandler;
29  
30  import java.io.File;
31  import java.net.InetAddress;
32  import java.util.List;
33  import java.util.Properties;
34  
35  /**
36   * This is a test set for <a href="https://issues.apache.org/jira/browse/MNG-4991">MNG-4991</a>.
37   *
38   * @author Benjamin Bentmann
39   */
40  public class MavenITmng4991NonProxyHostsTest
41      extends AbstractMavenIntegrationTestCase
42  {
43  
44      public MavenITmng4991NonProxyHostsTest()
45      {
46          super( "[2.0.3,3.0-alpha-1),[3.0.3,)" );
47      }
48  
49      /**
50       * Verify that the nonProxyHosts settings is respected.
51       *
52       * @throws Exception in case of failure
53       */
54      public void testit()
55          throws Exception
56      {
57          File testDir = ResourceExtractor.simpleExtractResources( getClass(), "/mng-4991" );
58  
59          ResourceHandler resourceHandler = new ResourceHandler();
60          resourceHandler.setResourceBase( new File( testDir, "repo" ).getAbsolutePath() );
61  
62          HandlerList handlers = new HandlerList();
63          handlers.setHandlers( new Handler[] { resourceHandler, new DefaultHandler() } );
64  
65          Server server = new Server( 0 );
66          server.setHandler( handlers );
67  
68          /*
69           * NOTE: To guard against automatic fallback to direct connection when the proxy is unreachable, we set up
70           * a dummy proxy as trap to catch the erroneous proxy usage in all cases.
71           */
72          Server proxy = new Server( 0 );
73          proxy.setHandler( new DefaultHandler() );
74  
75          Verifier verifier = newVerifier( testDir.getAbsolutePath() );
76          try
77          {
78              server.start();
79              if ( server.isFailed() )
80              {
81                  fail( "Couldn't bind the server socket to a free port!" );
82              }
83  
84              proxy.start();
85              if ( proxy.isFailed() )
86              {
87                  fail( "Couldn't bind the server socket to a free port!" );
88              }
89  
90              verifier.setAutoclean( false );
91              verifier.deleteDirectory( "target" );
92              verifier.deleteArtifacts( "org.apache.maven.its.mng4991" );
93              Properties filterProps = verifier.newDefaultFilterProperties();
94              int port = ( (NetworkConnector) server.getConnectors()[0] ).getLocalPort();
95              filterProps.setProperty( "@port@", Integer.toString( port ) );
96              int proxyPort = ( (NetworkConnector) proxy.getConnectors()[0] ).getLocalPort();
97              filterProps.setProperty( "@proxyPort@", Integer.toString( proxyPort ) );
98              filterProps.setProperty( "@localhost@", InetAddress.getLoopbackAddress().getCanonicalHostName() );
99              verifier.filterFile( "settings-template.xml", "settings.xml", "UTF-8", filterProps );
100             verifier.addCliOption( "-s" );
101             verifier.addCliOption( "settings.xml" );
102             verifier.executeGoal( "validate" );
103             verifier.verifyErrorFreeLog();
104         }
105         finally
106         {
107             verifier.resetStreams();
108             server.stop();
109             proxy.stop();
110             server.join();
111             proxy.join();
112         }
113 
114         List<String> compile = verifier.loadLines( "target/compile.txt", "UTF-8" );
115 
116         assertTrue( compile.toString(), compile.contains( "dep-0.1.jar" ) );
117     }
118 }