]> source.dussan.org Git - archiva.git/blob
0b43252a8ff7aa054ff91864cf81874138b4fded
[archiva.git] /
1 package org.apache.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 java.io.File;
23 import java.io.Writer;
24 import java.io.FileWriter;
25 import java.io.FileReader;
26 import java.io.BufferedReader;
27 import java.util.List;
28 import java.util.Collections;
29
30 import org.apache.archiva.web.test.parent.AbstractArchivaTest;
31 import org.testng.Assert;
32 import org.testng.annotations.AfterTest;
33 import org.testng.annotations.BeforeSuite;
34 import org.testng.annotations.BeforeTest;
35 import org.testng.annotations.Test;
36 import org.jdom.Document;
37 import org.jdom.Element;
38 import org.jdom.input.SAXBuilder;
39 import org.jdom.output.XMLOutputter;
40 import org.jdom.xpath.XPath;
41 import org.codehaus.plexus.commandline.ExecutableResolver;
42 import org.codehaus.plexus.commandline.DefaultExecutableResolver;
43 import org.codehaus.plexus.util.cli.CommandLineUtils;
44 import org.codehaus.plexus.util.cli.Commandline;
45 import org.codehaus.plexus.util.cli.StreamConsumer;
46 import org.codehaus.plexus.util.cli.WriterStreamConsumer;
47
48 @Test( groups = { "about" }, alwaysRun = true )
49 public class ArchivaAdminTest 
50         extends AbstractArchivaTest
51 {
52         public static final String PATH_TO_ARCHIVA_XML = "/target/appserver-base/conf/archiva.xml";
53
54     public static final String PATH_TO_SETTINGS_XML = "/target/local-repo/settings.xml";
55
56     public static final String NEW_LOCAL_REPO_VALUE = "/target/local-repo";
57         
58     @BeforeSuite
59     public void initializeArchiva()
60         throws Exception
61     {
62         super.open();
63         getSelenium().open( baseUrl );
64         String title = getSelenium().getTitle();
65         if ( title.equals( "Apache Archiva \\ Create Admin User" ) )
66         {
67             assertCreateAdmin();
68             String fullname = p.getProperty( "ADMIN_FULLNAME" );
69             String username = p.getProperty( "ADMIN_USERNAME" );
70             String mail = p.getProperty( "ADMIN_EMAIL" );
71             String password = p.getProperty( "ADMIN_PASSWORD" );
72             submitAdminData( fullname, mail, password );            
73             assertAuthenticatedPage( username );
74             submit();
75             clickLinkWithText( "Logout" );
76        }
77         super.close();
78     }
79
80     @BeforeTest( groups = { "about" } )
81     public void open()
82         throws Exception
83     {
84         super.open();
85         String newValue = getBasedir() + NEW_LOCAL_REPO_VALUE;
86         updateXml( new File( getBasedir(), PATH_TO_ARCHIVA_XML ), newValue );
87         updateXml( new File( getBasedir(), PATH_TO_SETTINGS_XML ), newValue );
88     }
89
90     /**
91      * Update localRepository element value
92      *
93      * @param f
94      * @param newValue
95      * @throws Exception
96      */
97     private void updateXml( File f, String newValue )
98         throws Exception
99     {
100         SAXBuilder builder = new SAXBuilder();
101         FileReader reader = new FileReader( f );
102         Document document = builder.build( reader );
103
104         Element localRepository =
105             (Element) XPath.newInstance( "./" + "localRepository" ).selectSingleNode( document.getRootElement() );
106         localRepository.setText( newValue );
107
108         // re-write xml file
109         FileWriter writer = new FileWriter( f );
110         XMLOutputter output = new XMLOutputter();
111         output.output( document, writer );
112     }
113     
114     /*private void clickRepositories()
115     {
116         goToLoginPage();
117         submitLoginPage( getAdminUsername() , getAdminPassword() );
118         clickLinkWithText( "Repositories" );
119         assertPage( "Apache Archiva \\ Administration" );
120         assertTextPresent( "Administration - Repositories" );
121     }
122     
123     private void removedManagedRepository( String id)
124     {
125         clickRepositories();
126         clickLinkWithLocator( "//a[contains(@href, '/admin/confirmDeleteRepository.action?repoid=" + id + "')]" );
127         clickButtonWithValue( "Delete Configuration and Contents" );
128     }*/
129     
130         private int executeMaven( String workingDir, File outputFile )
131             throws Exception
132         {
133         
134             ExecutableResolver executableResolver = new DefaultExecutableResolver();
135         
136             String actualExecutable = "mvn";
137             File workingDirectory = new File( workingDir );
138         
139             List path = executableResolver.getDefaultPath();
140         
141             if ( path == null )
142             {
143                 path = Collections.EMPTY_LIST;
144             }
145         
146             File e = executableResolver.findExecutable( "mvn", path );
147         
148             if ( e != null )
149             {
150                 actualExecutable = e.getAbsolutePath();
151             }
152         
153             File actualExecutableFile = new File( actualExecutable );
154         
155             if ( !actualExecutableFile.exists() )
156             {
157                 actualExecutable = "mvn";
158             }
159         
160             // Set command line
161             Commandline cmd = new Commandline();
162         
163             cmd.addSystemEnvironment();
164         
165             cmd.addEnvironment( "MAVEN_TERMINATE_CMD", "on" );
166         
167             cmd.setExecutable( actualExecutable );
168         
169             cmd.setWorkingDirectory( workingDirectory.getAbsolutePath() );
170         
171             cmd.createArgument().setValue( "clean" );
172         
173             cmd.createArgument().setValue( "install" );
174         
175             cmd.createArgument().setValue( "-s" );
176         
177             cmd.createArgument().setValue( getBasedir() + "/target/local-repo/settings.xml" );
178         
179             // Excute command
180         
181             Writer writer = new FileWriter( outputFile );
182         
183             StreamConsumer consumer = new WriterStreamConsumer( writer );
184         
185             int exitCode = CommandLineUtils.executeCommandLine( cmd, consumer, consumer );
186         
187             writer.flush();
188         
189             writer.close();
190         
191             return exitCode;
192         }
193     
194 /*      public void testBadDependency()
195             throws Exception
196         {
197             File outputFile = new File( getBasedir(), "/target/projects/bad-dependency/bad-dependency.log" );
198             int exitCode = executeMaven( getBasedir() + "/target/projects/bad-dependency", outputFile );
199         
200             Assert.assertEquals( 1, exitCode );
201         
202             File f = new File( getBasedir(),
203                                "/target/local-repo/org/apache/maven/archiva/web/test/foo-bar/1.0-SNAPSHOT/foo-bar-1.0-SNAPSHOT.jar" );
204             Assert.assertTrue( !f.exists() );
205         
206             BufferedReader reader = new BufferedReader( new FileReader( outputFile ) );
207             String str;
208             boolean foundSnapshot = false, foundBadDep = false;
209         
210             while ( ( str = reader.readLine() ) != null )
211             {
212                 //System.out.println( str );
213                 if ( str.indexOf(
214                     "mvn install:install-file -DgroupId=org.apache.maven.archiva.web.test -DartifactId=foo-bar" ) != -1 )
215                 {
216                     foundSnapshot = true;
217                 }
218                 else if ( str.indexOf(
219                     "mvn install:install-file -DgroupId=org.apache.maven.archiva.web.test -DartifactId=bad-dependency" ) !=
220                     -1 )
221                 {
222                     foundBadDep = true;
223                 }
224             }
225         
226             reader.close();
227             
228             Assert.assertTrue( foundSnapshot );
229             Assert.assertTrue( foundBadDep );
230         }*/
231         
232     public void displayLandingPage()
233     {
234         getSelenium().open( baseUrl );
235         getSelenium().waitForPageToLoad( maxWaitTimeInMs );
236         assertPage( "Apache Archiva \\ Quick Search" );
237     }
238
239     @Override
240     @AfterTest( groups = { "about" } )
241     public void close()
242         throws Exception
243     {
244         super.close();
245     }
246 }