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.Properties;
33  
34  /**
35   * This is a test set for <a href="https://issues.apache.org/jira/browse/MNG-2387">MNG-2387</a>.
36   *
37   * @author Brett Porter
38   *
39   */
40  public class MavenITmng2387InactiveProxyTest
41      extends AbstractMavenIntegrationTestCase
42  {
43      private Server server;
44  
45      private int port;
46  
47      private Server proxyServer;
48  
49      private int proxyPort;
50  
51      private File testDir;
52  
53      public MavenITmng2387InactiveProxyTest()
54      {
55          super( "[2.0.11,2.1.0-M1),[2.1.0,)" ); // 2.0.11+, 2.1.0+
56      }
57  
58      @Override
59      protected void setUp()
60          throws Exception
61      {
62          testDir = ResourceExtractor.simpleExtractResources( getClass(), "/mng-2387" );
63  
64          ResourceHandler resourceHandler = new ResourceHandler();
65          resourceHandler.setResourceBase( new File( testDir, "repo" ).getAbsolutePath() );
66  
67          HandlerList handlers = new HandlerList();
68          handlers.setHandlers( new Handler[] { resourceHandler, new DefaultHandler() } );
69  
70          server = new Server( 0 );
71          server.setHandler( handlers );
72          server.start();
73          if ( server.isFailed() )
74          {
75              fail( "Couldn't bind the server socket to a free port!" );
76          }
77          port = ( (NetworkConnector) server.getConnectors()[0] ).getLocalPort();
78          System.out.println( "Bound server socket to the HTTP port " + port );
79  
80          resourceHandler = new ResourceHandler();
81          resourceHandler.setResourceBase( new File( testDir, "proxy" ).getAbsolutePath() );
82  
83          handlers = new HandlerList();
84          handlers.setHandlers( new Handler[] { resourceHandler, new DefaultHandler() } );
85  
86          proxyServer = new Server( 0 );
87          proxyServer.setHandler( handlers );
88          proxyServer.start();
89          if ( proxyServer.isFailed() )
90          {
91              fail( "Couldn't bind the server socket to a free port!" );
92          }
93          proxyPort = ( (NetworkConnector) proxyServer.getConnectors()[0] ).getLocalPort();
94          System.out.println( "Bound server socket to the HTTPS port " + port );
95      }
96  
97      @Override
98      protected void tearDown()
99          throws Exception
100     {
101         if ( server != null )
102         {
103             server.stop();
104             server.join();
105         }
106         if ( proxyServer != null )
107         {
108             proxyServer.stop();
109             proxyServer.join();
110         }
111     }
112 
113     /**
114      * Test that no proxy is used if none of the configured proxies is actually set as active.
115      *
116      * @throws Exception in case of failure
117      */
118     public void testit()
119         throws Exception
120     {
121         Verifier verifier = newVerifier( testDir.getAbsolutePath() );
122 
123         Properties properties = verifier.newDefaultFilterProperties();
124         properties.setProperty( "@host@", InetAddress.getLoopbackAddress().getCanonicalHostName() );
125         properties.setProperty( "@port@", Integer.toString( port ) );
126         properties.setProperty( "@proxyPort@", Integer.toString( proxyPort ) );
127         verifier.filterFile( "settings-template.xml", "settings.xml", "UTF-8", properties );
128 
129         verifier.setAutoclean( false );
130         verifier.deleteArtifacts( "org.apache.maven.its.mng2387" );
131         verifier.addCliOption( "--settings" );
132         verifier.addCliOption( "settings.xml" );
133         verifier.executeGoal( "validate" );
134         verifier.verifyErrorFreeLog();
135         verifier.resetStreams();
136 
137         verifier.assertArtifactPresent( "org.apache.maven.its.mng2387", "a", "0.1", "jar" );
138     }
139 }