package org.apache.archiva.web.test;

/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

import java.io.File;

import org.apache.archiva.web.test.parent.AbstractBrowseTest;
import org.testng.annotations.Test;

@Test( groups = { "browse" }, dependsOnMethods = { "testAddArtifactNullValues" } )
public class BrowseTest 
	extends AbstractBrowseTest
{
	
	public void testBrowseArtifact()
	{
		goToBrowsePage();
		assertBrowsePage();
	}
	
	public void testClickArtifactFromBrowse()
	{
		goToBrowsePage();
		assertBrowsePage();
		clickLinkWithText( getProperty( "ARTIFACT_ARTIFACTID" ) + "/" );
		assertPage( "Apache Archiva \\ Browse Repository" );
		assertTextPresent( "Artifacts" );
	}

    // MRM-1278
    public void testCorrectRepositoryInBrowse()
    {
        File artifact =
            new File( getBasedir(),
                      "/src/test/it-resources/snapshots/org/apache/maven/archiva/web/test/foo-bar/1.0-SNAPSHOT/foo-bar-1.0-SNAPSHOT.jar" );
                      //"src/test/it-resources/foo-bar-1.0-20091110.181459-4.jar" );
                
        String releasesRepo = getProperty( "RELEASES_REPOSITORY" );
        
        // create releases repository first
        goToRepositoriesPage();
        clickLinkWithText( "Add" );
        addManagedRepository( getProperty( "RELEASES_REPOSITORY" ), "Releases Repository",
                              new File( getBasedir(), "target/repository/releases" ).getPath(), "", "Maven 2.x Repository",
                              "0 0 * * * ?", "", "" );        
        assertTextPresent( "Releases Repository" );
        
        String snapshotsRepo = getProperty( "SNAPSHOTS_REPOSITORY" );

        // upload a snapshot artifact to repository 'releases'        
        addArtifact( "archiva", "archiva-webapp", "1.0-SNAPSHOT", "jar", artifact.getPath(),
                     releasesRepo );
        assertTextPresent( "Artifact 'archiva:archiva-webapp:1.0-SNAPSHOT' was successfully deployed to repository '" + releasesRepo + "'" );

        goToBrowsePage();
        assertBrowsePage();
        assertGroupsPage( "archiva/" );
        assertArtifactsPage( "archiva-webapp/" );
        assertArtifactInfoPage( "1.0-SNAPSHOT/", releasesRepo, "archiva", "archiva-webapp", "1.0-SNAPSHOT", "jar" );

        // upload a snapshot artifact to repository 'snapshots'        
        addArtifact( "continuum", "continuum-core", "1.0-SNAPSHOT", "jar", artifact.getPath(),
                     snapshotsRepo );
        assertTextPresent( "Artifact 'continuum:continuum-core:1.0-SNAPSHOT' was successfully deployed to repository '" + snapshotsRepo + "'" );

        goToBrowsePage();
        assertBrowsePage();
        assertGroupsPage( "continuum/" );
        assertArtifactsPage( "continuum-core/" );
        assertArtifactInfoPage( "1.0-SNAPSHOT/", snapshotsRepo, "continuum", "continuum-core", "1.0-SNAPSHOT", "jar" );
    }

    private void assertArtifactInfoPage( String version, String artifactInfoRepositoryId, String artifactInfoGroupId,
                                         String artifactInfoArtifactId, String artifactInfoVersion, String artifactInfoPackaging )
    {
        clickLinkWithText( version );
        assertPage( "Apache Archiva \\ Browse Repository" );
        assertTextPresent( artifactInfoRepositoryId );
        assertTextPresent( artifactInfoGroupId );
        assertTextPresent( artifactInfoArtifactId );
        assertTextPresent( artifactInfoVersion );
        assertTextPresent( artifactInfoPackaging );
    }

    private void assertArtifactsPage( String artifactId )
    {
        clickLinkWithText( artifactId );
        assertPage( "Apache Archiva \\ Browse Repository" );
        assertTextPresent( "Versions" );
    }

    private void assertGroupsPage( String groupId )
    {
        clickLinkWithText( groupId );
        assertPage( "Apache Archiva \\ Browse Repository" );
        assertTextPresent( "Artifacts" );
    }
}

@@ -1696,33 +1696,32 @@
 SNAPSHOT.jar%22 );
-%0A
                 
@@ -1729,71 +1729,8 @@
     
- //%22src/test/it-resources/foo-bar-1.0-20091110.181459-4.jar%22 );
 %0A   

remove commented out code