/*
 * Copyright (C) 2012 Red Hat, Inc. (jcasey@redhat.com)
 *
 * Licensed 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.
 */
package org.commonjava.maven.ext.manip.util;

import org.commonjava.maven.ext.manip.fixture.TestUtils;
import org.commonjava.maven.ext.manip.io.PomIO;
import org.commonjava.maven.ext.manip.model.Project;
import org.junit.Test;

import java.io.File;
import java.util.List;

import static org.junit.Assert.assertTrue;

public class ProjectInheritanceTest
{
    private static final String RESOURCE_BASE = "properties/";

    @Test
    public void testVerifyInheritance() throws Exception
    {
        // Locate the PME project pom file. Use that to verify inheritance tracking.
        final File projectroot = new File (TestUtils.resolveFileResource( RESOURCE_BASE, "" )
                                          .getParentFile()
                                          .getParentFile()
                                          .getParentFile()
                                          .getParentFile(), "pom.xml" );
        PomIO pomIO = new PomIO();
        List<Project> projects = pomIO.parseProject( projectroot );
        for ( Project p : projects )
        {
            if ( ! p.getPom().equals( projectroot ) )
            {
                assertTrue ( p.getProjectParent().getPom().equals( projectroot ) );
            }
        }
    }


    @Test
    public void testVerifyInheritanceMultiple() throws Exception
    {
        // Locate the PME project pom file. Use that to verify inheritance tracking.
        final File projectroot = new File (TestUtils.resolveFileResource( RESOURCE_BASE, "" )
                                                    .getParentFile()
                                                    .getParentFile()
                                                    .getParentFile()
                                                    .getParentFile(), "integration-test/src/it/project-inheritance/pom.xml" );
        PomIO pomIO = new PomIO();

        List<Project> projects = pomIO.parseProject( projectroot );

        for ( int i = 0; i<= 3 ; i++ )
        {
            if ( i == 0 )
            {
                // First project should be root
                assertTrue ( projects.get( i ).getProjectParent() == null );
                assertTrue ( projects.get( i ).getPom().equals( projectroot ) );
                assertTrue ( projects.get( i ).getInheritedList().size() == 1 );
            }
            else if ( i == 1 )
            {
                List<Project> inherited = projects.get( i ).getInheritedList();
                assertTrue (inherited.size() == 2 );
                assertTrue ( inherited.get( 1 ).getProjectParent().getPom().equals( projectroot ) );
            }
            else if ( i == 2 )
            {
                List<Project> inherited = projects.get( i ).getInheritedList();
                assertTrue (inherited.size() == 3 );
                assertTrue ( inherited.get( 0 ).getPom().equals( projectroot ) );
            }
        }
    }


    @Test
    public void testVerifyInheritanceReversedMultiple() throws Exception
    {
        // Locate the PME project pom file. Use that to verify inheritance tracking.
        final File projectroot = new File (TestUtils.resolveFileResource( RESOURCE_BASE, "" )
                                                    .getParentFile()
                                                    .getParentFile()
                                                    .getParentFile()
                                                    .getParentFile(), "integration-test/src/it/project-inheritance/pom.xml" );
        PomIO pomIO = new PomIO();

        List<Project> projects = pomIO.parseProject( projectroot );

        for ( int i = 0; i<= 3 ; i++ )
        {
            if ( i == 0 )
            {
                // First project should be root
                assertTrue ( projects.get( i ).getProjectParent() == null );
                assertTrue ( projects.get( i ).getPom().equals( projectroot ) );
                assertTrue ( projects.get( i ).getReverseInheritedList().size() == 1 );
            }
            else if ( i == 1 )
            {
                List<Project> inherited = projects.get( i ).getReverseInheritedList();
                assertTrue (inherited.size() == 2 );
                assertTrue ( inherited.get( 0 ).getProjectParent().getPom().equals( projectroot ) );
            }
            else if ( i == 2 )
            {
                List<Project> inherited = projects.get( i ).getReverseInheritedList();
                assertTrue (inherited.size() == 3 );
                assertTrue ( inherited.get( 2 ).getPom().equals( projectroot ) );
            }
        }
    }
}

@@ -887,24 +887,68 @@
 util.List;%0A%0A
+import static org.junit.Assert.assertFalse;%0A
 import stati
@@ -3582,32 +3582,1213 @@
        %7D%0A    %7D%0A%0A
+    @Test%0A    public void testVerifyExecutionRoot() throws Exception%0A    %7B%0A        // Locate the PME project pom file. Use that to verify inheritance tracking.%0A        final File projectroot = new File( TestUtils.resolveFileResource( RESOURCE_BASE, %22%22 )%0A                                                    .getParentFile()%0A                                                    .getParentFile()%0A                                                    .getParentFile()%0A                                                    .getParentFile(),%0A                                           %22integration-test/src/it/project-inheritance/common/pom.xml%22 );%0A        PomIO pomIO = new PomIO();%0A%0A        List%3CProject%3E projects = pomIO.parseProject( projectroot );%0A%0A        for ( Project p : projects )%0A        %7B%0A            if ( p.getKey().toString().equals( %22io.apiman:apiman-common:1.2.7-SNAPSHOT%22 ))%0A            %7B%0A                assertTrue( p.isExecutionRoot() );%0A                assertTrue( p.isInheritanceRoot() );%0A            %7D%0A            else%0A            %7B%0A                assertFalse( p.isExecutionRoot() );%0A                assertFalse( p.isInheritanceRoot() );%0A            %7D%0A        %7D%0A%0A    %7D%0A
 %0A    @Test%0A    p

Add test for executionRoot