package org.apache.maven.changes;

/* ====================================================================
 *   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.Element;
import org.dom4j.Document;
import org.dom4j.io.SAXReader;

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

    public void testExistingReleaseTargetVersionReleased()
        throws Exception
    {
        File changesFile = new File( System.getProperty( "basedir" ), "/src/test-data/test-existing-release-target-version-released.xml" );
        Document doc = ReleaseVersion.transformVersion( changesFile, "1.1", "1.2", "2004-04-21" );

        assertNull( "check transform required", doc );

        SAXReader r = new SAXReader();

        doc = r.read( changesFile );

        List nodes = doc.selectNodes( "/document/body/release" );

        assertEquals( "check exactly 1 release", 1, nodes.size() );

        Element release = ( Element ) nodes.get( 0 );

        assertEquals( "Check version is 1.2", "1.2", release.attribute( "version" ).getText() );
        assertEquals( "Check date is 2003-03-23", "2003-03-23", release.attribute( "date" ).getText() );
    }

    public void testExistingReleaseTargetVersionInCvs()
        throws Exception
    {
        File changesFile = new File( System.getProperty( "basedir" ), "/src/test-data/test-existing-release-target-version-in-cvs.xml" );
        Document doc = ReleaseVersion.transformVersion( changesFile, "1.1", "1.2", "2004-04-21" );

        assertNotNull( "check transform required", doc );

        List nodes = doc.selectNodes( "/document/body/release" );

        assertEquals( "check exactly 1 release", 1, nodes.size() );

        Element release = ( Element ) nodes.get( 0 );

        assertEquals( "Check version is 1.2", "1.2", release.attribute( "version" ).getText() );
        assertEquals( "Check date is 2004-04-21", "2004-04-21", release.attribute( "date" ).getText() );
    }

    public void testExistingReleaseCurrentVersionReleasedAndTargetVersionInCvs()
        throws Exception
    {
        File changesFile = new File( System.getProperty( "basedir" ), "/src/test-data/test-existing-release-current-version-released-and-target-version-in-cvs.xml" );
        Document doc = ReleaseVersion.transformVersion( changesFile, "1.1", "1.2", "2004-04-21" );

        assertNotNull( "check transform required", doc );

        List nodes = doc.selectNodes( "/document/body/release" );

        assertEquals( "check exactly 2 releases", 2, nodes.size() );

        Element release = ( Element ) nodes.get( 0 );

        assertEquals( "Check version is 1.2", "1.2", release.attribute( "version" ).getText() );
        assertEquals( "Check date is 2004-04-21", "2004-04-21", release.attribute( "date" ).getText() );

        release = ( Element ) nodes.get( 1 );

        assertEquals( "Check version is 1.1", "1.1", release.attribute( "version" ).getText() );
        assertEquals( "Check date is 2003-03-23", "2003-03-23", release.attribute( "date" ).getText() );
    }

    public void testExistingReleaseCurrentVersionReleasedAndTargetVersionReleased()
        throws Exception
    {
        File changesFile = new File( System.getProperty( "basedir" ), "/src/test-data/test-existing-release-current-version-released-and-target-version-released.xml" );
        Document doc = ReleaseVersion.transformVersion( changesFile, "1.1", "1.2", "2004-04-21" );

        assertNull( "check transform required", doc );

        SAXReader r = new SAXReader();
        
        doc = r.read( changesFile );

        List nodes = doc.selectNodes( "/document/body/release" );

        assertEquals( "check exactly 2 releases", 2, nodes.size() );

        Element release = ( Element ) nodes.get( 0 );

        assertEquals( "Check version is 1.2", "1.2", release.attribute( "version" ).getText() );
        assertEquals( "Check date is 2003-03-23", "2003-03-23", release.attribute( "date" ).getText() );

        release = ( Element ) nodes.get( 1 );

        assertEquals( "Check version is 1.1", "1.1", release.attribute( "version" ).getText() );
        assertEquals( "Check date is 2002-02-22", "2002-02-22", release.attribute( "date" ).getText() );
    }

    public void testExistingReleaseCurrentVersionReleased()
        throws Exception
    {
        File changesFile = new File( System.getProperty( "basedir" ), "/src/test-data/test-existing-release-current-version-released.xml" );
        Document doc = ReleaseVersion.transformVersion( changesFile, "1.2", "1.2", "2004-04-21" );

        assertNull( "check transform required", doc );

        SAXReader r = new SAXReader();
        
        doc = r.read( changesFile );

        List nodes = doc.selectNodes( "/document/body/release" );

        assertEquals( "check exactly 1 release", 1, nodes.size() );

        Element release = ( Element ) nodes.get( 0 );

        assertEquals( "Check version is 1.2", "1.2", release.attribute( "version" ).getText() );
        assertEquals( "Check date is 2004-04-21", "2003-03-23", release.attribute( "date" ).getText() );
    }

    public void testExistingReleaseCurrentVersionInCvs()
        throws Exception
    {
        Document doc = ReleaseVersion.transformVersion(
            new File( System.getProperty( "basedir" ), "/src/test-data/test-existing-release-current-version-in-cvs.xml" ),
            "1.2-SNAPSHOT", "1.2", "2004-04-21" );

        assertNotNull( "check transform required", doc );

        List nodes = doc.selectNodes( "/document/body/release" );

        assertEquals( "check exactly 1 release", 1, nodes.size() );

        Element release = ( Element ) nodes.get( 0 );

        assertEquals( "Check version is 1.2", "1.2", release.attribute( "version" ).getText() );
        assertEquals( "Check date is 2004-04-21", "2004-04-21", release.attribute( "date" ).getText() );
    }

    public void testOneUnrelatedRelease()
        throws Exception
    {
        File changesFile = new File( System.getProperty( "basedir" ), "/src/test-data/test-one-unrelated-release.xml" );
        Document doc = ReleaseVersion.transformVersion( changesFile, "1.2-SNAPSHOT", "1.2", "2004-04-21" );

        assertNull( "check transform required", doc );

        SAXReader r = new SAXReader();
        doc = r.read( changesFile );

        List nodes = doc.selectNodes( "/document/body/release" );

        assertEquals( "check exactly 1 releases", 1, nodes.size() );

        Element release = ( Element ) nodes.get( 0 );

        assertEquals( "Check version is 1.1", "1.1", release.attribute( "version" ).getText() );
        assertEquals( "Check date is 2003-03-23", "2003-03-23", release.attribute( "date" ).getText() );

        /* assumes addition of elements
        assertNotNull( "check transform required", doc );

        List nodes = doc.selectNodes( "/document/body/release" );

        assertEquals( "check exactly 2 releases", 2, nodes.size() );

        Element release = ( Element ) nodes.get( 0 );

        assertEquals( "Check version is 1.2", "1.2", release.attribute( "version" ).getText() );
        assertEquals( "Check date is 2004-04-21", "2004-04-21", release.attribute( "date" ).getText() );

        release = ( Element ) nodes.get( 1 );
        assertEquals( "Check version is 1.1", "1.1", release.attribute( "version" ).getText() );
        assertEquals( "Check date is 2003-03-23", "2003-03-23", release.attribute( "date" ).getText() );
*/
    }

    public void testNoReleases()
        throws Exception
    {
        File changesFile = new File( System.getProperty( "basedir" ), "/src/test-data/test-no-versions.xml" );
        Document doc = ReleaseVersion.transformVersion( changesFile, "1.0-SNAPSHOT", "1.0", "2004-04-21" );

        assertNull( "check transform required", doc );
        SAXReader r = new SAXReader();
        doc = r.read( changesFile );

        List nodes = doc.selectNodes( "/document/body/release" );

        assertEquals( "check no release", 0, nodes.size() );

        /* assumes addition of elements
        assertNotNull( "check transform required", doc );

        List nodes = doc.selectNodes( "/document/body/release" );

        assertEquals( "check only 1 release", 1, nodes.size() );

        Element release = ( Element ) nodes.get( 0 );

        assertEquals( "Check version is 1.0", "1.0", release.attribute( "version" ).getText() );
        assertEquals( "Check date is 2004-04-21", "2004-04-21", release.attribute( "date" ).getText() );
        */
    }
}

