package org.apache.maven.repository.internal;

/*
 * 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.net.MalformedURLException;

import org.apache.maven.repository.internal.util.ConsoleRepositoryListener;
import org.apache.maven.repository.internal.util.ConsoleTransferListener;
import org.codehaus.plexus.PlexusTestCase;
import org.sonatype.aether.artifact.Artifact;
import org.sonatype.aether.collection.CollectRequest;
import org.sonatype.aether.collection.CollectResult;
import org.sonatype.aether.graph.Dependency;
import org.sonatype.aether.repository.LocalRepository;
import org.sonatype.aether.repository.RemoteRepository;
import org.sonatype.aether.util.artifact.DefaultArtifact;
import org.sonatype.aether.RepositorySystem;
import org.sonatype.aether.RepositorySystemSession;

public class RepositorySystemTest
    extends PlexusTestCase
{
    protected RepositorySystem system;

    protected RepositorySystemSession session;

    @Override
    protected void setUp()
        throws Exception
    {
        system = lookup( RepositorySystem.class );
        session = newMavenRepositorySystemSession( system );
    }

    @Override
    protected void tearDown()
        throws Exception
    {
        session = null;
        system = null;
        super.tearDown();
    }

    public void testCollectDependencies()
        throws Exception
    {
        String artifactCoords = "ut.simple:artifact:1.0"; // TODO test extension:classifier
        Artifact artifact = new DefaultArtifact( artifactCoords );

        CollectRequest collectRequest = new CollectRequest();
        collectRequest.setRoot( new Dependency( artifact, "" ) );
        collectRequest.addRepository( newTestRepository() );

        CollectResult collectResult = system.collectDependencies( session, collectRequest );

        assertEquals( 1, collectResult.getRoot().getChildren().size() );
        Artifact dep = collectResult.getRoot().getChildren().get( 0 ).getDependency().getArtifact();
        assertEquals( "ut.simple", dep.getGroupId() );
        assertEquals( "dependency", dep.getArtifactId() );
        assertEquals( "1.0", dep.getVersion() );
    }

    public static RepositorySystemSession newMavenRepositorySystemSession( RepositorySystem system )
    {
        MavenRepositorySystemSession session = new MavenRepositorySystemSession();

        LocalRepository localRepo = new LocalRepository( "target/local-repo" );
        session.setLocalRepositoryManager( system.newLocalRepositoryManager( localRepo ) );

        session.setTransferListener( new ConsoleTransferListener() );
        session.setRepositoryListener( new ConsoleRepositoryListener() );

        return session;
    }

    public static RemoteRepository newTestRepository()
        throws MalformedURLException
    {
        return new RemoteRepository( "repo", "default", getTestFile( "target/test-classes/repo" ).toURI().toURL().toString() );
    }
}

@@ -2642,24 +2642,26 @@
         
-Artifact
+Dependency
  dep = c
@@ -2721,16 +2721,199 @@
 ndency()
+;%0A        assertEquals( %22compile%22, dep.getScope() );%0A        assertFalse( dep.isOptional() );%0A        assertEquals( 0, dep.getExclusions().size() );%0A        Artifact depArtifact = dep
 .getArti
@@ -2954,24 +2954,32 @@
 simple%22, dep
+Artifact
 .getGroupId(
@@ -3018,24 +3018,32 @@
 ndency%22, dep
+Artifact
 .getArtifact
@@ -3078,24 +3078,32 @@
 ( %221.0%22, dep
+Artifact
 .getVersion(
@@ -3103,24 +3103,686 @@
 ersion() );%0A
+        assertEquals( %221.0%22, depArtifact.getBaseVersion() );%0A        assertNull( depArtifact.getFile() );%0A        assertFalse( depArtifact.isSnapshot() );%0A        assertEquals( %22%22, depArtifact.getClassifier() );%0A        assertEquals( %22jar%22, depArtifact.getExtension() );%0A        assertEquals( %22java%22, depArtifact.getProperty( %22language%22, null ) );%0A        assertEquals( %22jar%22, depArtifact.getProperty( %22type%22, null ) );%0A        assertEquals( %22true%22, depArtifact.getProperty( %22constitutesBuildPath%22, null ) );%0A        assertEquals( %22false%22, depArtifact.getProperty( %22includesDependencies%22, null ) );%0A        assertEquals( 4, depArtifact.getProperties().size() );%0A
     %7D%0A%0A    p

check as much attributes as possible