package org.apache.maven.release;

/* ====================================================================
 *   Copyright 2001-2004 The Apache Software Foundation.
 *
 *   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.
 * ====================================================================
 */

import junit.framework.TestCase;

import java.io.File;

import java.util.List;

import org.dom4j.Node;

/**
 */
public class VersionTransformerTest
    extends TestCase
{
    /**
     * Constructor.
     *
     * @param name Name of the test.
     */
    public VersionTransformerTest( String name )
    {
        super( name );
    }

    public void testNoVersions()
        throws Exception
    {
        VersionTransformer vt = new VersionTransformer( "1.2", "TEST_1_2" );
        vt.setProject( new File( System.getProperty( "basedir" ), "/src/test-data/test-pom-no-versions.xml" ) );

        assertTrue( "check transform required", vt.transformRequired() );

        vt.transformNodes();
        vt.write();

        List nodes = vt.getDocument().selectNodes( "/project" );

        assertEquals( 1, nodes.size() );

        Node project = ( Node ) nodes.get( 0 );

        assertEquals( "Check currentVersion is 1.2", "1.2", project.selectSingleNode( "currentVersion" ).getText() );

        List versionNodes = project.selectNodes( "versions/version" );

        assertEquals( "Check # versions is 1", 1, versionNodes.size() );

        Node version = ( Node ) versionNodes.get( 0 );

        assertEquals( "Check version id is 1.2", "1.2", version.selectSingleNode( "id" ).getText() );
        assertEquals( "Check version name is 1.2", "1.2", version.selectSingleNode( "name" ).getText() );
        assertEquals( "Check version tag is TEST_1_2", "TEST_1_2", version.selectSingleNode( "tag" ).getText() );
    }

    public void testOneVersion()
        throws Exception
    {
        VersionTransformer vt = new VersionTransformer( "1.2", "TEST_1_2" );
        vt.setProject( new File( System.getProperty( "basedir" ), "/src/test-data/test-pom-versions.xml" ) );

        assertTrue( "check transform required", vt.transformRequired() );

        vt.transformNodes();
        vt.write();

        List nodes = vt.getDocument().selectNodes( "/project" );

        assertEquals( 1, nodes.size() );

        Node project = ( Node ) nodes.get( 0 );

        assertEquals( "Check currentVersion is 1.2", "1.2", project.selectSingleNode( "currentVersion" ).getText() );

        List versionNodes = project.selectNodes( "versions/version" );

        assertEquals( "Check # versions is 2", 2, versionNodes.size() );

        Node version = ( Node ) versionNodes.get( 0 );

        assertEquals( "Check version id is 1.1", "1.1", version.selectSingleNode( "id" ).getText() );
        assertEquals( "Check version name is 1.1", "1.1", version.selectSingleNode( "name" ).getText() );
        assertEquals( "Check version tag is TEST_1_1", "TEST_1_1", version.selectSingleNode( "tag" ).getText() );

        version = ( Node ) versionNodes.get( 1 );

        assertEquals( "Check version id is 1.2", "1.2", version.selectSingleNode( "id" ).getText() );
        assertEquals( "Check version name is 1.2", "1.2", version.selectSingleNode( "name" ).getText() );
        assertEquals( "Check version tag is TEST_1_2", "TEST_1_2", version.selectSingleNode( "tag" ).getText() );
    }

    public void testMultipleVersions()
        throws Exception
    {
        VersionTransformer vt = new VersionTransformer( "1.2", "TEST_1_2" );
        vt.setProject( new File( System.getProperty( "basedir" ), "/src/test-data/test-pom-versions-multiple.xml" ) );

        assertTrue( "check transform required", vt.transformRequired() );

        vt.transformNodes();
        vt.write();

        List nodes = vt.getDocument().selectNodes( "/project" );

        assertEquals( 1, nodes.size() );

        Node project = ( Node ) nodes.get( 0 );

        assertEquals( "Check currentVersion is 1.2", "1.2", project.selectSingleNode( "currentVersion" ).getText() );

        List versionNodes = project.selectNodes( "versions/version" );

        assertEquals( "Check # versions is 3", 3, versionNodes.size() );

        Node version = ( Node ) versionNodes.get( 0 );

        assertEquals( "Check version id is 1.0", "1.0", version.selectSingleNode( "id" ).getText() );
        assertEquals( "Check version name is 1.0", "1.0", version.selectSingleNode( "name" ).getText() );
        assertEquals( "Check version tag is TEST_1_0", "TEST_1_0", version.selectSingleNode( "tag" ).getText() );

        version = ( Node ) versionNodes.get( 1 );

        assertEquals( "Check version id is 1.1", "1.1", version.selectSingleNode( "id" ).getText() );
        assertEquals( "Check version name is 1.1", "1.1", version.selectSingleNode( "name" ).getText() );
        assertEquals( "Check version tag is TEST_1_1", "TEST_1_1", version.selectSingleNode( "tag" ).getText() );

        version = ( Node ) versionNodes.get( 2 );

        assertEquals( "Check version id is 1.2", "1.2", version.selectSingleNode( "id" ).getText() );
        assertEquals( "Check version name is 1.2", "1.2", version.selectSingleNode( "name" ).getText() );
        assertEquals( "Check version tag is TEST_1_2", "TEST_1_2", version.selectSingleNode( "tag" ).getText() );
    }

    public void testMultipleVersionsWrongOrder()
        throws Exception
    {
        VersionTransformer vt = new VersionTransformer( "1.2", "TEST_1_2" );
        vt.setProject( new File( System.getProperty( "basedir" ), "/src/test-data/test-pom-versions-wrong-order.xml" ) );

        assertTrue( "check transform required", vt.transformRequired() );

        vt.transformNodes();
        vt.write();

        List nodes = vt.getDocument().selectNodes( "/project" );

        assertEquals( 1, nodes.size() );

        Node project = ( Node ) nodes.get( 0 );

        assertEquals( "Check currentVersion is 1.2", "1.2", project.selectSingleNode( "currentVersion" ).getText() );

        List versionNodes = project.selectNodes( "versions/version" );

        assertEquals( "Check # versions is 3", 3, versionNodes.size() );

        Node version = ( Node ) versionNodes.get( 0 );

        assertEquals( "Check version id is 1.1", "1.1", version.selectSingleNode( "id" ).getText() );
        assertEquals( "Check version name is 1.1", "1.1", version.selectSingleNode( "name" ).getText() );
        assertEquals( "Check version tag is TEST_1_1", "TEST_1_1", version.selectSingleNode( "tag" ).getText() );

        version = ( Node ) versionNodes.get( 1 );

        assertEquals( "Check version id is 1.0", "1.0", version.selectSingleNode( "id" ).getText() );
        assertEquals( "Check version name is 1.0", "1.0", version.selectSingleNode( "name" ).getText() );
        assertEquals( "Check version tag is TEST_1_0", "TEST_1_0", version.selectSingleNode( "tag" ).getText() );

        version = ( Node ) versionNodes.get( 2 );

        assertEquals( "Check version id is 1.2", "1.2", version.selectSingleNode( "id" ).getText() );
        assertEquals( "Check version name is 1.2", "1.2", version.selectSingleNode( "name" ).getText() );
        assertEquals( "Check version tag is TEST_1_2", "TEST_1_2", version.selectSingleNode( "tag" ).getText() );
    }

    public void testVersionsOverwrite()
        throws Exception
    {
        VersionTransformer vt = new VersionTransformer( "1.2", "TEST_1_2" );
        vt.setProject( new File( System.getProperty( "basedir" ), "/src/test-data/test-pom-versions-overwrite.xml" ) );

        assertTrue( "check transform required", vt.transformRequired() );

        vt.transformNodes();
        vt.write();

        List nodes = vt.getDocument().selectNodes( "/project" );

        assertEquals( 1, nodes.size() );

        Node project = ( Node ) nodes.get( 0 );

        assertEquals( "Check currentVersion is 1.2", "1.2", project.selectSingleNode( "currentVersion" ).getText() );

        List versionNodes = project.selectNodes( "versions/version" );

        assertEquals( "Check # versions is 2", 2, versionNodes.size() );

        Node version = ( Node ) versionNodes.get( 0 );

        assertEquals( "Check version id is 1.0", "1.0", version.selectSingleNode( "id" ).getText() );
        assertEquals( "Check version name is 1.0", "1.0", version.selectSingleNode( "name" ).getText() );
        assertEquals( "Check version tag is TEST_1_0", "TEST_1_0", version.selectSingleNode( "tag" ).getText() );

        version = ( Node ) versionNodes.get( 1 );

        assertEquals( "Check version id is 1.2", "1.2", version.selectSingleNode( "id" ).getText() );
        assertEquals( "Check version name is 1.2", "1.2", version.selectSingleNode( "name" ).getText() );
        assertEquals( "Check version tag is TEST_1_2", "TEST_1_2", version.selectSingleNode( "tag" ).getText() );
    }

    public void testVersionsCorrect()
        throws Exception
    {
        VersionTransformer vt = new VersionTransformer( "1.2", "TEST_1_2" );
        vt.setProject( new File( System.getProperty( "basedir" ), "/src/test-data/test-pom-versions-correct.xml" ) );

        assertFalse( "check transform not required", vt.transformRequired() );

        vt.transformNodes();
        vt.write();

        List nodes = vt.getDocument().selectNodes( "/project" );

        assertEquals( 1, nodes.size() );

        Node project = ( Node ) nodes.get( 0 );

        assertEquals( "Check currentVersion is 1.2", "1.2", project.selectSingleNode( "currentVersion" ).getText() );

        List versionNodes = project.selectNodes( "versions/version" );

        assertEquals( "Check # versions is 2", 2, versionNodes.size() );

        Node version = ( Node ) versionNodes.get( 0 );

        assertEquals( "Check version id is 1.0", "1.0", version.selectSingleNode( "id" ).getText() );
        assertEquals( "Check version name is 1.0", "1.0", version.selectSingleNode( "name" ).getText() );
        assertEquals( "Check version tag is TEST_1_0", "TEST_1_0", version.selectSingleNode( "tag" ).getText() );

        version = ( Node ) versionNodes.get( 1 );

        assertEquals( "Check version id is 1.2", "1.2", version.selectSingleNode( "id" ).getText() );
        assertEquals( "Check version name is 1.2", "1.2", version.selectSingleNode( "name" ).getText() );
        assertEquals( "Check version tag is TEST_1_2", "TEST_1_2", version.selectSingleNode( "tag" ).getText() );
    }

    public void testVersionsTagMatchOnly()
        throws Exception
    {
        VersionTransformer vt = new VersionTransformer( "1.2", "TEST_1_2" );
        vt.setProject( new File( System.getProperty( "basedir" ), "/src/test-data/test-pom-versions-tag-match-only.xml" ) );

        assertTrue( "check transform required", vt.transformRequired() );

        vt.transformNodes();
        vt.write();

        List nodes = vt.getDocument().selectNodes( "/project" );

        assertEquals( 1, nodes.size() );

        Node project = ( Node ) nodes.get( 0 );

        assertEquals( "Check currentVersion is 1.2", "1.2", project.selectSingleNode( "currentVersion" ).getText() );

        List versionNodes = project.selectNodes( "versions/version" );

        assertEquals( "Check # versions is 1", 1, versionNodes.size() );

        Node version = ( Node ) versionNodes.get( 0 );

        assertEquals( "Check version id is 1.2", "1.2", version.selectSingleNode( "id" ).getText() );
        assertEquals( "Check version name is 1.2", "1.2", version.selectSingleNode( "name" ).getText() );
        assertEquals( "Check version tag is TEST_1_2", "TEST_1_2", version.selectSingleNode( "tag" ).getText() );
    }
}

