1 package org.apache.maven.it;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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.Request;
26 import org.eclipse.jetty.server.Server;
27 import org.eclipse.jetty.server.handler.AbstractHandler;
28
29 import javax.servlet.http.HttpServletRequest;
30 import javax.servlet.http.HttpServletResponse;
31 import java.io.File;
32 import java.io.IOException;
33 import java.io.PrintWriter;
34 import java.util.ArrayList;
35 import java.util.Collections;
36 import java.util.List;
37 import java.util.Properties;
38
39
40
41
42
43
44
45 public class MavenITmng0768OfflineModeTest
46 extends AbstractMavenIntegrationTestCase
47 {
48
49 public MavenITmng0768OfflineModeTest()
50 {
51 super( ALL_MAVEN_VERSIONS );
52 }
53
54
55
56
57
58
59 public void testitMNG768()
60 throws Exception
61 {
62 File testDir = ResourceExtractor.simpleExtractResources( getClass(), "/mng-0768" );
63
64 final List<String> requestedUris = Collections.synchronizedList( new ArrayList<String>() );
65
66 Handler repoHandler = new AbstractHandler()
67 {
68 public void handle( String target, Request baseRequest, HttpServletRequest request,
69 HttpServletResponse response )
70 throws IOException
71 {
72 System.out.println( "Handling " + request.getMethod() + " " + request.getRequestURL() );
73
74 requestedUris.add( request.getRequestURI() );
75
76 PrintWriter writer = response.getWriter();
77
78 response.setStatus( HttpServletResponse.SC_OK );
79
80 if ( request.getRequestURI().endsWith( ".pom" ) )
81 {
82 writer.println( "<project>" );
83 writer.println( " <modelVersion>4.0.0</modelVersion>" );
84 writer.println( " <groupId>org.apache.maven.its.mng0768</groupId>" );
85 writer.println( " <artifactId>dep</artifactId>" );
86 writer.println( " <version>0.1</version>" );
87 writer.println( "</project>" );
88 }
89 else if ( request.getRequestURI().endsWith( ".jar" ) )
90 {
91 writer.println( "empty" );
92 }
93 else if ( request.getRequestURI().endsWith( ".md5" ) || request.getRequestURI().endsWith( ".sha1" ) )
94 {
95 response.setStatus( HttpServletResponse.SC_NOT_FOUND );
96 }
97
98 ( (Request) request ).setHandled( true );
99 }
100 };
101
102 Server server = new Server( 0 );
103 server.setHandler( repoHandler );
104
105 try
106 {
107 server.start();
108 if ( server.isFailed() )
109 {
110 fail( "Couldn't bind the server socket to a free port!" );
111 }
112
113 int port = ( (NetworkConnector) server.getConnectors()[0] ).getLocalPort();
114
115 {
116
117 Verifier verifier = newVerifier( testDir.getAbsolutePath() );
118 verifier.setAutoclean( false );
119 verifier.deleteDirectory( "target" );
120 verifier.deleteArtifacts( "org.apache.maven.its.mng0768" );
121 verifier.setLogFileName( "log1.txt" );
122 Properties props = new Properties();
123 props.put( "@port@", Integer.toString( port ) );
124 verifier.filterFile( "settings-template.xml", "settings.xml", "UTF-8", props );
125 verifier.addCliOption( "--settings" );
126 verifier.addCliOption( "settings.xml" );
127 verifier.executeGoal( "org.apache.maven.its.plugins:maven-it-plugin-dependency-resolution:2.1-SNAPSHOT:compile" );
128 verifier.assertFilePresent( "target/compile.txt" );
129 verifier.verifyErrorFreeLog();
130 verifier.resetStreams();
131 }
132
133 requestedUris.clear();
134
135 {
136
137 Verifier verifier = newVerifier( testDir.getAbsolutePath() );
138 verifier.setAutoclean( false );
139 verifier.deleteDirectory( "target" );
140 verifier.addCliOption( "-o" );
141 verifier.addCliOption( "--settings" );
142 verifier.addCliOption( "settings.xml" );
143 verifier.setLogFileName( "log2.txt" );
144 verifier.executeGoal( "org.apache.maven.its.plugins:maven-it-plugin-dependency-resolution:2.1-SNAPSHOT:compile" );
145 verifier.assertFilePresent( "target/compile.txt" );
146 verifier.verifyErrorFreeLog();
147 verifier.resetStreams();
148 }
149
150 assertTrue( requestedUris.toString(), requestedUris.isEmpty() );
151
152 {
153
154
155 Verifier verifier = newVerifier( testDir.getAbsolutePath() );
156 verifier.setAutoclean( false );
157 verifier.deleteDirectory( "target" );
158 verifier.deleteArtifacts( "org.apache.maven.its.mng0768" );
159 verifier.addCliOption( "-o" );
160 verifier.addCliOption( "--settings" );
161 verifier.addCliOption( "settings.xml" );
162 verifier.setLogFileName( "log3.txt" );
163 try
164 {
165 verifier.executeGoal( "org.apache.maven.its.plugins:maven-it-plugin-dependency-resolution:2.1-SNAPSHOT:compile" );
166 verifier.verifyErrorFreeLog();
167 fail( "Build did not fail to resolve missing dependency although Maven ought to work offline!" );
168 }
169 catch( VerificationException e )
170 {
171
172 }
173 verifier.resetStreams();
174 }
175
176 System.out.println( "Bound server socket to the port " + port );
177
178 assertTrue( requestedUris.toString(), requestedUris.isEmpty() );
179 }
180 finally
181 {
182 server.stop();
183 server.join();
184 }
185 }
186 }