/*
 * Copyright 2001-2005 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.
 */
package org.codehaus.mojo.webstart.generator;

import junit.framework.Assert;
import junit.framework.TestCase;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.DefaultArtifact;
import org.apache.maven.artifact.versioning.VersionRange;
import org.apache.maven.plugin.MojoExecutionException;
import org.codehaus.mojo.webstart.JarResource;
import org.codehaus.plexus.util.ReaderFactory;
import org.custommonkey.xmlunit.Diff;
import org.xml.sax.SAXException;

import javax.xml.parsers.ParserConfigurationException;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Tests the {@link VersionXmlGenerator} class.
 *
 * @author Kevin Stembridge
 * @author $LastChangedBy$
 * @version $Revision$
 * @since 7 Jun 2007
 */
public class VersionXmlGeneratorTest
    extends TestCase
{

    private final File outputDir;

    private File expectedFile;

    /**
     * Creates a new {@code VersionXmlGeneratorTest}.
     */
    public VersionXmlGeneratorTest()
    {
        super();

        this.outputDir = new File( System.getProperty( "java.io.tmpdir" ), "versionXmlDir" );
        this.outputDir.deleteOnExit();
        this.outputDir.mkdir();

    }

    /**
     * {@inheritDoc}
     */
    public void setUp()
    {
        this.expectedFile = new File( this.outputDir, "version.xml" );
        this.expectedFile.deleteOnExit();

        if ( this.expectedFile.exists() )
        {
            if ( !this.expectedFile.delete() )
            {
                throw new RuntimeException( "Unable to delete a file from a previous test run [" + expectedFile + "]" );
            }

        }

    }

    /**
     * {@inheritDoc}
     */
    public void tearDown()
    {
        this.expectedFile.delete();
    }

    public void testWithNullOutputDir()
        throws MojoExecutionException
    {

        try
        {
            new VersionXmlGenerator( "utf-8" ).generate( null, new ArrayList() );
            Assert.fail( "Should have thrown an IllegalArgumentException" );
        }
        catch ( IllegalArgumentException e )
        {
            //do nothing, test succeeded
        }

    }

    public void testWithEmptyJarResourcesList()
        throws MojoExecutionException, IOException, SAXException, ParserConfigurationException
    {

        List jarResources = new ArrayList();
        new VersionXmlGenerator( "utf-8" ).generate( this.outputDir, jarResources );

        Assert.assertTrue( "Assert expectedFile exists", this.expectedFile.exists() );

        String expectedXml = "<jnlp-versions></jnlp-versions>";
        String actualXml = readFileContents( this.expectedFile );

        Diff diff = new Diff( expectedXml, actualXml );
        Assert.assertTrue( diff.toString(), diff.similar() );

    }

    public void testWithMultiJarResources()
        throws IOException, SAXException, ParserConfigurationException, MojoExecutionException
    {

        Artifact artifact1 =
            new DefaultArtifact( "groupId", "artifactId1", VersionRange.createFromVersion( "1.0" ), "scope", "jar",
                                 "classifier", null );
        artifact1.setFile( new File( "bogus1.txt" ) );

        Artifact artifact2 =
            new DefaultArtifact( "groupId", "artifactId2", VersionRange.createFromVersion( "1.0" ), "scope", "jar",
                                 "classifier", null );
        artifact2.setFile( new File( "bogus2.txt" ) );

        JarResource jar1 = new JarResource();
        JarResource jar2 = new JarResource();

        jar1.setArtifact( artifact1 );
        jar2.setArtifact( artifact2 );

        List jarResources = new ArrayList( 2 );
        jarResources.add( jar1 );
        jarResources.add( jar2 );

        StringBuffer expectedXml = new StringBuffer();
        expectedXml.append( "<jnlp-versions>" ).append( "  <resource>" ).append( "    <pattern>" ).append(
            "      <name>bogus1.txt</name>" ).append( "      <version-id>1.0</version-id>" ).append(
            "    </pattern>" ).append( "    <file>bogus1.txt</file>" ).append( "  </resource>" ).append(
            "  <resource>" ).append( "    <pattern>" ).append( "      <name>bogus2.txt</name>" ).append(
            "      <version-id>1.0</version-id>" ).append( "    </pattern>" ).append(
            "    <file>bogus2.txt</file>" ).append( "  </resource>" ).append( "</jnlp-versions>" );

        new VersionXmlGenerator( "utf-8" ).generate( this.outputDir, jarResources );

        String actualXml = readFileContents( this.expectedFile );

        Diff diff = new Diff( expectedXml.toString(), actualXml );
        Assert.assertTrue( diff.toString(), diff.similar() );

    }

    private String readFileContents( File file )
        throws IOException
    {

        BufferedReader reader = null;
        StringBuffer fileContents = new StringBuffer();

        try
        {
            reader = new BufferedReader( ReaderFactory.newXmlReader( file ) );

            String line = null;

            while ( ( line = reader.readLine() ) != null )
            {
                fileContents.append( line );
            }

            return fileContents.toString();

        }
        finally
        {
            if ( reader != null )
            {
                reader.close();
            }
        }

    }

}

@@ -1,76 +1,308 @@
-/*%0A * Copyright 2001-2005 The Apache Software Foundation.%0A *%0A * Licensed
+package org.codehaus.mojo.webstart.generator;%0A%0A/*%0A * Licensed to the Apache Software Foundation (ASF) under one%0A * or more contributor license agreements.  See the NOTICE file%0A * distributed with this work for additional information%0A * regarding copyright ownership.  The ASF licenses this file%0A * to you
  und
@@ -340,16 +340,19 @@
 2.0 (the
+%0A *
  %22Licens
@@ -357,14 +357,10 @@
 nse%22
- 
 );
-%0A *
  you
@@ -402,16 +402,19 @@
 mpliance
+%0A *
  with th
@@ -423,19 +423,17 @@
 License.
-%0A *
+ 
  You may
@@ -470,19 +470,16 @@
 at%0A *%0A *
-   
    http:
@@ -580,16 +580,19 @@
 writing,
+%0A *
  softwar
@@ -592,19 +592,16 @@
 software
-%0A *
  distrib
@@ -643,16 +643,19 @@
 ed on an
+%0A *
  %22AS IS%22
@@ -661,19 +661,16 @@
 %22 BASIS,
-%0A *
  WITHOUT
@@ -701,16 +701,19 @@
 S OF ANY
+%0A *
  KIND, e
@@ -737,19 +737,17 @@
 implied.
-%0A *
+ 
  See the
@@ -762,16 +762,19 @@
  for the
+%0A *
  specifi
@@ -809,19 +809,16 @@
 ions and
-%0A *
  limitat
@@ -821,16 +821,19 @@
 itations
+%0A *
  under t
@@ -851,54 +851,8 @@
 %0A */
-%0Apackage org.codehaus.mojo.webstart.generator;
 %0A%0Aim

add and fix some license headers