package org.codehaus.mojo.versions.api;

import junit.framework.TestCase;
import org.codehaus.mojo.versions.rewriting.ModifiedPomXMLEventReader;
import org.codehaus.stax2.XMLInputFactory2;

import javax.xml.stream.XMLInputFactory;
import java.io.File;
import java.io.IOException;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.net.URL;

/**
 * Created by IntelliJ IDEA.
 * User: user
 * Date: 17-Jun-2009
 * Time: 18:40:49
 * To change this template use File | Settings | File Templates.
 */
public class PomHelperTest
    extends TestCase
{
    public void testLongProperties() throws Exception {
        URL url = getClass().getResource( "PomHelperTest.testLongProperties.pom.xml" );        
        StringBuffer input = PomHelper.readFile( new File(url.getPath()) );
        
        XMLInputFactory inputFactory = XMLInputFactory2.newInstance();
        inputFactory.setProperty( XMLInputFactory2.P_PRESERVE_LOCATION, Boolean.TRUE );
        
        ModifiedPomXMLEventReader pom = new ModifiedPomXMLEventReader( input, inputFactory );

        PomHelper.setProjectVersion( pom, "1" );
        PomHelper.setDependencyVersion( pom, "localhost", "it-set-004", "${V1234567890123456789012}", "1" );
        
        System.out.println(pom.asStringBuffer().toString());
    }
    
    /**
     * Reads a file into a StringBuffer.
     *
     * @param outFile The file to read.
     * @return StringBuffer The contents of the file.
     * @throws java.io.IOException when things go wrong.
     */
    protected final StringBuffer readFile( File outFile )
        throws IOException
    {
        StringBuffer input;
        BufferedInputStream reader;
        reader = new BufferedInputStream( new FileInputStream( outFile ) );

        byte[] content = new byte[(int) outFile.length()];
        input = new StringBuffer( content.length );
        try
        {
            int length = reader.read( content, 0, content.length );
            input.append( new String( content, 0, length, PomHelper.POM_ENCODING ) );
            return input;
        }
        finally
        {
            reader.close();
        }
    }

}

@@ -261,318 +261,346 @@
 ava.
-io.IOException;%0Aimport java.io.BufferedInputStream;%0Aimport java.io.FileInputStream;%0Aimport java.net.URL;%0A%0A/**%0A * Created by IntelliJ IDEA.%0A * User: user%0A * Date: 17-Jun-2009%0A * Time: 18:40:49%0A * To change this template use File %7C Settings %7C File Templates.%0A */%0Apublic class PomHelperTest%0A    extends TestCase%0A%7B
+net.URL;%0A%0A/**%0A * Tets the methods of %7B@link PomHelper%7D.%0A */%0Apublic class PomHelperTest%0A    extends TestCase%0A%7B%0A    /**%0A     * Tests what happens when changing a long property substitution pattern, e.g.%0A     * %3Ca href=%22http://jira.codehaus.org/browse/MVERSIONS-44%22%3EMVERSIONS-44%3C/a%3E%0A     *%0A     * @throws Exception if the test fails.%0A     */
 %0A   
@@ -632,16 +632,24 @@
 erties()
+%0A       
  throws 
@@ -657,16 +657,20 @@
 xception
+%0A   
  %7B%0A     
@@ -751,24 +751,16 @@
 .xml%22 );
-        
 %0A       
@@ -810,16 +810,17 @@
 ew File(
+ 
 url.getP
@@ -824,29 +824,22 @@
 etPath()
+ 
 ) );%0A
-        
 %0A       
@@ -986,32 +986,24 @@
 ean.TRUE );%0A
-        
 %0A        Mod
@@ -1127,32 +1127,95 @@
 on( pom, %221%22 );%0A
+        System.out.println( pom.asStringBuffer().toString() );%0A
         PomHelpe
@@ -1307,24 +1307,16 @@
  %221%22 );%0A
-        
 %0A       
@@ -1335,16 +1335,17 @@
 println(
+ 
 pom.asSt
@@ -1371,855 +1371,11 @@
 ng()
+ 
 );
-%0A    %7D%0A    %0A    /**%0A     * Reads a file into a StringBuffer.%0A     *%0A     * @param outFile The file to read.%0A     * @return StringBuffer The contents of the file.%0A     * @throws java.io.IOException when things go wrong.%0A     */%0A    protected final StringBuffer readFile( File outFile )%0A        throws IOException%0A    %7B%0A        StringBuffer input;%0A        BufferedInputStream reader;%0A        reader = new BufferedInputStream( new FileInputStream( outFile ) );%0A%0A        byte%5B%5D content = new byte%5B(int) outFile.length()%5D;%0A        input = new StringBuffer( content.length );%0A        try%0A        %7B%0A            int length = reader.read( content, 0, content.length );%0A            input.append( new String( content, 0, length, PomHelper.POM_ENCODING ) );%0A            return input;%0A        %7D%0A        finally%0A        %7B%0A            reader.close();%0A        %7D
 %0A   

improve test documentation