package org.apache.maven.plugins.release;

/*
 * Copyright 2005-2006 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 java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.nio.charset.Charset;

import org.apache.maven.model.Contributor;
import org.apache.maven.model.Model;
import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.release.helpers.ScmHelper;
import org.apache.maven.scm.ScmException;
import org.apache.maven.scm.manager.ScmManager;
import org.apache.maven.scm.manager.ScmManagerStub;
import org.codehaus.plexus.PlexusTestCase;
import org.jmock.cglib.Mock;
import org.jmock.core.constraint.IsAnything;
import org.jmock.core.matcher.MethodNameMatcher;
import org.jmock.core.stub.ReturnStub;
import org.jmock.core.stub.ThrowStub;

/**
 * Test for PrepareReleaseMojo
 *
 * @author <a href="mailto:carlos@apache.org">Carlos Sanchez</a>
 * @version $Id$
 */
public class PrepareReleaseMojoTest
    extends PlexusTestCase
{

    private PrepareReleaseMojo mojo;

    private Mock scmHelperMock;

    private ScmManager scmManager;

    protected void setUp()
        throws Exception
    {
        super.setUp();
        mojo = new PrepareReleaseMojo();
        mojo.basedir = new File( getBasedir(), "target/test" );
        mojo.basedir.mkdirs();

        scmManager = new ScmManagerStub();

        scmHelperMock = new Mock( ScmHelper.class );
        ScmHelper scmHelper = (ScmHelper) scmHelperMock.proxy();
        mojo.setScmHelper( scmHelper );
        mojo.setScmManager( scmManager );
    }

    /**
     * Test for writePom with special characters in the pom
     *
     * @throws Exception
     */
    public void testWritePom()
        throws Exception
    {
        System.out.println( Charset.defaultCharset().name() );
        System.out.println( System.getProperty("file.encoding") );

        Model model = new Model();
        Contributor contributor = new Contributor();
        /* hack to avoid problems with sources encoding, this string contains accentuated "aeiou" */
        String s = new String( new byte[] { -61, -95, -61, -87, -61, -83, -61, -77, -61, -70 }, "UTF-8" );

        contributor.setName( s );
        model.addContributor( contributor );
        File file = new File( mojo.basedir, "testWritePom.xml" );

        scmHelperMock.expects( new MethodNameMatcher( new IsAnything() ) );
        scmHelperMock.expects( new MethodNameMatcher( "getScmManager" ) ).will( new ReturnStub( scmManager ) );

        mojo.writePom( file, model, "version" );

        MavenXpp3Reader pomReader = new MavenXpp3Reader();

        Model readModel = pomReader.read( new BufferedReader( new FileReader( file ) ) );
        Contributor readContributor = (Contributor) readModel.getContributors().get( 0 );
        
        String msg = "POM is written in a wrong encoding: \n"
            + "Expected bytes: " + contributor.getName().getBytes() + "\n"
            + "Returned bytes: " + readContributor.getName().getBytes() + "\n"
            + "JVM default charset: " + Charset.defaultCharset() + "\n";
        assertEquals( msg, contributor.getName(), readContributor.getName() );

        scmHelperMock.verify();
    }

    /**
     * Test for writePom when the ScmHelper throws a ScmException
     *
     * @throws Exception
     */
    public void testWritePomWithScmException()
        throws Exception
    {
        Model model = new Model();
        File file = new File( mojo.basedir, "testWritePomWithScmException.xml" );

        scmHelperMock.expects( new MethodNameMatcher( new IsAnything() ) );
        scmHelperMock.expects( new MethodNameMatcher( "getScmManager" ) ).will( new ReturnStub( scmManager ) );
        scmHelperMock.expects( new MethodNameMatcher( "requiresEditMode" ) )
            .will( new ThrowStub( new ScmException( "testing" ) ) );

        try
        {
            mojo.writePom( file, model, "version" );
            fail( "MojoExecutionException was not thrown" );
        }
        catch ( MojoExecutionException e )
        {
            // expected
        }

        scmHelperMock.verify();
    }

}

@@ -769,16 +769,41 @@
 Charset;
+%0Aimport java.util.Arrays;
 %0A%0Aimport
@@ -3525,16 +3525,33 @@
 tes: %22 +
+ Arrays.toString(
  contrib
@@ -3567,32 +3567,34 @@
 ame().getBytes()
+ )
  + %22%5Cn%22%0A        
@@ -3619,16 +3619,33 @@
 tes: %22 +
+ Arrays.toString(
  readCon
@@ -3673,16 +3673,18 @@
 tBytes()
+ )
  + %22%5Cn%22%0A
@@ -3749,16 +3749,109 @@
 rset() +
+ %22%5Cn%22%0A            + %22System property file.encoding: %22 + System.getProperty(%22file.encoding%22) +
  %22%5Cn%22;%0A 

Improve failed test message