]> source.dussan.org Git - archiva.git/blob
32433fc85c2d314045500465c786fd8a923d53dc
[archiva.git] /
1 package org.apache.maven.archiva.web.test;
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.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;
33
34 import java.io.File;
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;
41
42
43 /**
44  * Test maven connection to archiva
45  *
46  */
47 public class MavenConnectionTest
48     extends AbstractArchivaTestCase
49 {
50     public static final String PATH_TO_ARCHIVA_XML = "/target/appserver-base/conf/archiva.xml";
51
52     public static final String PATH_TO_SETTINGS_XML = "/target/local-repo/settings.xml";
53
54     public static final String NEW_LOCAL_REPO_VALUE = "/target/local-repo";
55
56     /**
57      * @throws Exception
58      */
59     public void setUp()
60         throws Exception
61     {
62         super.setUp();
63
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 );
67     }
68
69     /**
70      * Update localRepository element value
71      *
72      * @param f
73      * @param newValue
74      * @throws Exception
75      */
76     private void updateXml( File f, String newValue )
77         throws Exception
78     {
79         SAXBuilder builder = new SAXBuilder();
80         FileReader reader = new FileReader( f );
81         Document document = builder.build( reader );
82
83         Element localRepository =
84             (Element) XPath.newInstance( "./" + "localRepository" ).selectSingleNode( document.getRootElement() );
85         localRepository.setText( newValue );
86
87         // re-write xml file
88         FileWriter writer = new FileWriter( f );
89         XMLOutputter output = new XMLOutputter();
90         output.output( document, writer );
91     }
92
93     private void clickManagedRepositories()
94     {
95         goToLoginPage();
96         submitLoginPage( adminUsername, adminPassword );
97
98         clickLinkWithText( "Managed Repositories" );
99         assertPage( "Administration" );
100         assertTextPresent( "Administration" );
101     }
102
103     private void removeManagedRepository( String id )
104     {
105         clickManagedRepositories();
106
107         clickLinkWithLocator( "//a[contains(@href, '/admin/deleteRepository!input.action?repoId=" + id + "')]" );
108         clickLinkWithLocator( "deleteRepository_operationdelete-contents", false );
109         clickButtonWithValue( "Go" );
110
111         assertPage( "Administration" );
112     }
113
114     /**
115      * Click Settings from the navigation menu
116      */
117     private void clickProxiedRepositories()
118     {
119         goToLoginPage();
120         submitLoginPage( adminUsername, adminPassword );
121
122         clickLinkWithText( "Proxied Repositories" );
123         assertPage( "Administration" );
124         assertTextPresent( "Proxied Repositories" );
125     }
126
127     /**
128      * Remove the created test repo
129      */
130     protected void removeProxiedRepository()
131     {
132         if ( !isLinkPresent( "Login" ) )
133         {
134             logout();
135         }
136
137         clickProxiedRepositories();
138
139         if ( isTextPresent( "Delete Repository " ) )
140         {
141             clickLinkWithText( "Delete Repository" );
142             assertPage( "Configuration" );
143             clickLinkWithLocator( "deleteProxiedRepository_operationdelete-entry", false );
144             clickButtonWithValue( "Go" );
145
146             assertPage( "Administration" );
147             assertTextNotPresent( "Test Proxied Repository" );
148         }
149
150         logout();
151     }
152
153     /**
154      * Execute 'mvn' from commandline
155      *
156      * @param workingDir
157      * @param outputFile
158      * @return
159      * @throws Exception
160      */
161     private int executeMaven( String workingDir, File outputFile )
162         throws Exception
163     {
164
165         ExecutableResolver executableResolver = new DefaultExecutableResolver();
166
167         String actualExecutable = "mvn";
168         File workingDirectory = new File( workingDir );
169
170         List path = executableResolver.getDefaultPath();
171
172         if ( path == null )
173         {
174             path = Collections.EMPTY_LIST;
175         }
176
177         File e = executableResolver.findExecutable( "mvn", path );
178
179         if ( e != null )
180         {
181             actualExecutable = e.getAbsolutePath();
182         }
183
184         File actualExecutableFile = new File( actualExecutable );
185
186         if ( !actualExecutableFile.exists() )
187         {
188             actualExecutable = "mvn";
189         }
190
191         // Set command line
192         Commandline cmd = new Commandline();
193
194         cmd.addSystemEnvironment();
195
196         cmd.addEnvironment( "MAVEN_TERMINATE_CMD", "on" );
197
198         cmd.setExecutable( actualExecutable );
199
200         cmd.setWorkingDirectory( workingDirectory.getAbsolutePath() );
201
202         cmd.createArgument().setValue( "clean" );
203
204         cmd.createArgument().setValue( "install" );
205
206         cmd.createArgument().setValue( "-s" );
207
208         cmd.createArgument().setValue( getBasedir() + "/target/local-repo/settings.xml" );
209
210         // Excute command
211
212         Writer writer = new FileWriter( outputFile );
213
214         StreamConsumer consumer = new WriterStreamConsumer( writer );
215
216         int exitCode = CommandLineUtils.executeCommandLine( cmd, consumer, consumer );
217
218         writer.flush();
219
220         writer.close();
221
222         return exitCode;
223     }
224
225     public void testBadDependency()
226         throws Exception
227     {
228         File outputFile = new File( getBasedir(), "/target/projects/bad-dependency/bad-dependency.log" );
229         int exitCode = executeMaven( getBasedir() + "/target/projects/bad-dependency", outputFile );
230
231         assertEquals( 1, exitCode );
232
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() );
236
237         BufferedReader reader = new BufferedReader( new FileReader( outputFile ) );
238         String str;
239         boolean foundSnapshot = false, foundBadDep = false;
240
241         while ( ( str = reader.readLine() ) != null )
242         {
243             //System.out.println( str );
244             if ( str.indexOf(
245                 "mvn install:install-file -DgroupId=org.apache.maven.archiva.web.test -DartifactId=foo-bar" ) != -1 )
246             {
247                 foundSnapshot = true;
248             }
249             else if ( str.indexOf(
250                 "mvn install:install-file -DgroupId=org.apache.maven.archiva.web.test -DartifactId=bad-dependency" ) !=
251                 -1 )
252             {
253                 foundBadDep = true;
254             }
255         }
256
257         reader.close();
258
259         assertTrue( foundSnapshot );
260         assertTrue( foundBadDep );
261     }
262
263     /*
264     @todo: commented out since tests are currently failing due to MRM-323
265
266     public void testDownloadArtifactFromManagedRepo()
267         throws Exception
268     {
269         clickManagedRepositories();
270         
271         clickLinkWithText( "Add Repository" );
272         assertTextPresent( "Configuration" );
273
274         setFieldValue( "addRepository_id", "snapshots" );
275         setFieldValue( "urlName", "snapshots" );
276         setFieldValue( "addRepository_name", "snapshots-repository" );
277         setFieldValue( "addRepository_directory", getBasedir() + "/target/snapshots" );
278
279         clickButtonWithValue( "Add Repository" );
280         assertPage( "Administration" );
281
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" );
287
288         clickButtonWithValue( "Add Selected Roles" );
289         assertPage( "[Admin] User List" );
290
291         logout();
292        
293         File outputFile = new File( getBasedir(), "/target/projects/bad-dependency/bad-dependency2.log" );
294         int exitCode = executeMaven( getBasedir() + "/target/projects/bad-dependency",
295             outputFile );
296
297         assertEquals( 0, exitCode );
298
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() );
302
303         BufferedReader reader = new BufferedReader( new FileReader( outputFile ) );
304         String str;
305                  
306         while( ( str = reader.readLine() ) != null)
307         {
308             System.out.println( str );
309         }
310         reader.close();
311
312         removeManagedRepository( "snapshots" );
313     }
314
315
316     public void testDownloadArtifactFromProxiedRepo()
317         throws Exception
318     {
319         //add managed repository
320         clickManagedRepositories();
321
322         clickLinkWithText( "Add Repository" );
323         assertTextPresent( "Configuration" );
324
325         setFieldValue( "addRepository_id", "repository" );
326         setFieldValue( "urlName", "repository" );
327         setFieldValue( "addRepository_name", "repository" );
328         setFieldValue( "addRepository_directory", getBasedir() + "/target/repository" );
329         
330         clickButtonWithValue( "Add Repository" );
331         waitPage();
332         assertPage( "Administration" );
333
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" );
339
340         clickButtonWithValue( "Add Selected Roles" );
341         assertPage( "[Admin] User List" );
342         logout();
343
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" );
352         waitPage();
353
354         assertPage( "Administration" );
355         assertTextPresent( "Central Repository" );
356         assertLinkPresent( "Edit Repository" );
357
358         logout();
359
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",
362             outputFile );
363
364         assertEquals( 0, exitCode );
365
366         File f = new File( getBasedir(),"/target/repository/com/lowagie/itext/1.3/itext-1.3.jar" );
367         assertTrue( f.exists() );
368
369         f = new File( getBasedir(), "/target/local-repo/com/lowagie/itext/1.3/itext-1.3.jar" );
370         assertTrue( f.exists() );
371
372
373         BufferedReader reader = new BufferedReader( new FileReader( outputFile ) );
374         String str;
375
376         while( ( str = reader.readLine() ) != null)
377         {
378             System.out.println( str );
379         }
380         reader.close();
381
382         removeProxiedRepository();
383         removeManagedRepository( "repository" );        
384     }
385
386     */
387
388     /**
389      * @throws Exception
390      */
391     public void tearDown()
392         throws Exception
393     {
394         super.tearDown();
395     }
396 }