1 package org.apache.maven.archiva.web.test;
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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
22 import org.codehaus.plexus.util.cli.CommandLineUtils;
23 import org.codehaus.plexus.util.cli.Commandline;
24 import org.codehaus.plexus.util.cli.StreamConsumer;
25 import org.codehaus.plexus.util.cli.WriterStreamConsumer;
26 import org.codehaus.plexus.commandline.ExecutableResolver;
27 import org.codehaus.plexus.commandline.DefaultExecutableResolver;
28 import org.jdom.input.SAXBuilder;
29 import org.jdom.Document;
30 import org.jdom.Element;
31 import org.jdom.output.XMLOutputter;
32 import org.jdom.xpath.XPath;
35 import java.io.Writer;
36 import java.io.FileWriter;
37 import java.io.FileReader;
38 import java.io.BufferedReader;
39 import java.util.List;
40 import java.util.Collections;
44 * Test maven connection to archiva
47 public class MavenConnectionTest
48 extends AbstractArchivaTestCase
50 public static final String PATH_TO_ARCHIVA_XML = "/target/appserver-base/conf/archiva.xml";
52 public static final String PATH_TO_SETTINGS_XML = "/target/local-repo/settings.xml";
54 public static final String NEW_LOCAL_REPO_VALUE = "/target/local-repo";
64 String newValue = getBasedir() + NEW_LOCAL_REPO_VALUE;
65 updateXml( new File( getBasedir(), PATH_TO_ARCHIVA_XML ), newValue );
66 updateXml( new File( getBasedir(), PATH_TO_SETTINGS_XML ), newValue );
70 * Update localRepository element value
76 private void updateXml( File f, String newValue )
79 SAXBuilder builder = new SAXBuilder();
80 FileReader reader = new FileReader( f );
81 Document document = builder.build( reader );
83 Element localRepository =
84 (Element) XPath.newInstance( "./" + "localRepository" ).selectSingleNode( document.getRootElement() );
85 localRepository.setText( newValue );
88 FileWriter writer = new FileWriter( f );
89 XMLOutputter output = new XMLOutputter();
90 output.output( document, writer );
93 private void clickManagedRepositories()
96 submitLoginPage( adminUsername, adminPassword );
98 clickLinkWithText( "Managed Repositories" );
99 assertPage( "Administration" );
100 assertTextPresent( "Administration" );
103 private void removeManagedRepository( String id )
105 clickManagedRepositories();
107 clickLinkWithLocator( "//a[contains(@href, '/admin/deleteRepository!input.action?repoId=" + id + "')]" );
108 clickLinkWithLocator( "deleteRepository_operationdelete-contents", false );
109 clickButtonWithValue( "Go" );
111 assertPage( "Administration" );
115 * Click Settings from the navigation menu
117 private void clickProxiedRepositories()
120 submitLoginPage( adminUsername, adminPassword );
122 clickLinkWithText( "Proxied Repositories" );
123 assertPage( "Administration" );
124 assertTextPresent( "Proxied Repositories" );
128 * Remove the created test repo
130 protected void removeProxiedRepository()
132 if ( !isLinkPresent( "Login" ) )
137 clickProxiedRepositories();
139 if ( isTextPresent( "Delete Repository " ) )
141 clickLinkWithText( "Delete Repository" );
142 assertPage( "Configuration" );
143 clickLinkWithLocator( "deleteProxiedRepository_operationdelete-entry", false );
144 clickButtonWithValue( "Go" );
146 assertPage( "Administration" );
147 assertTextNotPresent( "Test Proxied Repository" );
154 * Execute 'mvn' from commandline
161 private int executeMaven( String workingDir, File outputFile )
165 ExecutableResolver executableResolver = new DefaultExecutableResolver();
167 String actualExecutable = "mvn";
168 File workingDirectory = new File( workingDir );
170 List path = executableResolver.getDefaultPath();
174 path = Collections.EMPTY_LIST;
177 File e = executableResolver.findExecutable( "mvn", path );
181 actualExecutable = e.getAbsolutePath();
184 File actualExecutableFile = new File( actualExecutable );
186 if ( !actualExecutableFile.exists() )
188 actualExecutable = "mvn";
192 Commandline cmd = new Commandline();
194 cmd.addSystemEnvironment();
196 cmd.addEnvironment( "MAVEN_TERMINATE_CMD", "on" );
198 cmd.setExecutable( actualExecutable );
200 cmd.setWorkingDirectory( workingDirectory.getAbsolutePath() );
202 cmd.createArgument().setValue( "clean" );
204 cmd.createArgument().setValue( "install" );
206 cmd.createArgument().setValue( "-s" );
208 cmd.createArgument().setValue( getBasedir() + "/target/local-repo/settings.xml" );
212 Writer writer = new FileWriter( outputFile );
214 StreamConsumer consumer = new WriterStreamConsumer( writer );
216 int exitCode = CommandLineUtils.executeCommandLine( cmd, consumer, consumer );
225 public void testBadDependency()
228 File outputFile = new File( getBasedir(), "/target/projects/bad-dependency/bad-dependency.log" );
229 int exitCode = executeMaven( getBasedir() + "/target/projects/bad-dependency", outputFile );
231 assertEquals( 1, exitCode );
233 File f = new File( getBasedir(),
234 "/target/local-repo/org/apache/mavem/archiva/web/test/foo-bar/1.0-SNAPSHOT/foo-bar-1.0-SNAPSHOT.jar" );
235 assertTrue( !f.exists() );
237 BufferedReader reader = new BufferedReader( new FileReader( outputFile ) );
239 boolean foundSnapshot = false, foundBadDep = false;
241 while ( ( str = reader.readLine() ) != null )
243 //System.out.println( str );
245 "mvn install:install-file -DgroupId=org.apache.maven.archiva.web.test -DartifactId=foo-bar" ) != -1 )
247 foundSnapshot = true;
249 else if ( str.indexOf(
250 "mvn install:install-file -DgroupId=org.apache.maven.archiva.web.test -DartifactId=bad-dependency" ) !=
259 assertTrue( foundSnapshot );
260 assertTrue( foundBadDep );
264 @todo: commented out since tests are currently failing due to MRM-323
266 public void testDownloadArtifactFromManagedRepo()
269 clickManagedRepositories();
271 clickLinkWithText( "Add Repository" );
272 assertTextPresent( "Configuration" );
274 setFieldValue( "addRepository_id", "snapshots" );
275 setFieldValue( "urlName", "snapshots" );
276 setFieldValue( "addRepository_name", "snapshots-repository" );
277 setFieldValue( "addRepository_directory", getBasedir() + "/target/snapshots" );
279 clickButtonWithValue( "Add Repository" );
280 assertPage( "Administration" );
282 clickLinkWithText( "User Management" );
283 clickLinkWithLocator( "//a[contains(@href, '/security/useredit.action?username=admin')]" );
284 clickLinkWithText( "Edit Roles" );
285 checkField( "addRolesToUser_addSelectedRolesRepository Observer - snapshots" );
286 checkField( "addRolesToUser_addSelectedRolesRepository Manager - snapshots" );
288 clickButtonWithValue( "Add Selected Roles" );
289 assertPage( "[Admin] User List" );
293 File outputFile = new File( getBasedir(), "/target/projects/bad-dependency/bad-dependency2.log" );
294 int exitCode = executeMaven( getBasedir() + "/target/projects/bad-dependency",
297 assertEquals( 0, exitCode );
299 File f = new File( getBasedir(),
300 "/target/local-repo/org/apache/maven/archiva/web/test/foo-bar-1.0-SNAPSHOT.jar" );
301 assertTrue( f.exists() );
303 BufferedReader reader = new BufferedReader( new FileReader( outputFile ) );
306 while( ( str = reader.readLine() ) != null)
308 System.out.println( str );
312 removeManagedRepository( "snapshots" );
316 public void testDownloadArtifactFromProxiedRepo()
319 //add managed repository
320 clickManagedRepositories();
322 clickLinkWithText( "Add Repository" );
323 assertTextPresent( "Configuration" );
325 setFieldValue( "addRepository_id", "repository" );
326 setFieldValue( "urlName", "repository" );
327 setFieldValue( "addRepository_name", "repository" );
328 setFieldValue( "addRepository_directory", getBasedir() + "/target/repository" );
330 clickButtonWithValue( "Add Repository" );
332 assertPage( "Administration" );
334 clickLinkWithText( "User Management" );
335 clickLinkWithLocator( "//a[contains(@href, '/security/useredit.action?username=admin')]" );
336 clickLinkWithText( "Edit Roles" );
337 checkField( "addRolesToUser_addSelectedRolesRepository Observer - repository" );
338 checkField( "addRolesToUser_addSelectedRolesRepository Manager - repository" );
340 clickButtonWithValue( "Add Selected Roles" );
341 assertPage( "[Admin] User List" );
344 //add proxied repository
345 clickProxiedRepositories();
346 clickLinkWithText( "Add Repository" );
347 assertPage( "Configuration" );
348 setFieldValue( "id", "central" );
349 setFieldValue( "name", "Central Repository" );
350 setFieldValue( "url", "http://mirrors.ibiblio.org/pub/mirrors/maven2" );
351 clickButtonWithValue( "Add Repository" );
354 assertPage( "Administration" );
355 assertTextPresent( "Central Repository" );
356 assertLinkPresent( "Edit Repository" );
360 File outputFile = new File( getBasedir(), "/target/projects/dependency-in-proxied/dependency-in-proxied.log" );
361 int exitCode = executeMaven( getBasedir() + "/target/projects/dependency-in-proxied",
364 assertEquals( 0, exitCode );
366 File f = new File( getBasedir(),"/target/repository/com/lowagie/itext/1.3/itext-1.3.jar" );
367 assertTrue( f.exists() );
369 f = new File( getBasedir(), "/target/local-repo/com/lowagie/itext/1.3/itext-1.3.jar" );
370 assertTrue( f.exists() );
373 BufferedReader reader = new BufferedReader( new FileReader( outputFile ) );
376 while( ( str = reader.readLine() ) != null)
378 System.out.println( str );
382 removeProxiedRepository();
383 removeManagedRepository( "repository" );
391 public void tearDown()