You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by hb...@apache.org on 2007/11/17 15:30:10 UTC
svn commit: r595953 - in /maven/shared/trunk/maven-shared-io: ./
src/test/java/org/apache/maven/shared/io/
src/test/java/org/apache/maven/shared/io/location/
Author: hboutemy
Date: Sat Nov 17 06:30:09 2007
New Revision: 595953
URL: http://svn.apache.org/viewvc?rev=595953&view=rev
Log:
make encoding handling more explicit
Modified:
maven/shared/trunk/maven-shared-io/pom.xml
maven/shared/trunk/maven-shared-io/src/test/java/org/apache/maven/shared/io/TestUtils.java
maven/shared/trunk/maven-shared-io/src/test/java/org/apache/maven/shared/io/location/ArtifactLocationTest.java
maven/shared/trunk/maven-shared-io/src/test/java/org/apache/maven/shared/io/location/FileLocationTest.java
maven/shared/trunk/maven-shared-io/src/test/java/org/apache/maven/shared/io/location/URLLocationTest.java
maven/shared/trunk/maven-shared-io/src/test/java/org/apache/maven/shared/io/location/URLLocatorStrategyTest.java
Modified: maven/shared/trunk/maven-shared-io/pom.xml
URL: http://svn.apache.org/viewvc/maven/shared/trunk/maven-shared-io/pom.xml?rev=595953&r1=595952&r2=595953&view=diff
==============================================================================
--- maven/shared/trunk/maven-shared-io/pom.xml (original)
+++ maven/shared/trunk/maven-shared-io/pom.xml Sat Nov 17 06:30:09 2007
@@ -67,7 +67,7 @@
<dependency>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-utils</artifactId>
- <version>1.2</version>
+ <version>1.4.6</version>
</dependency>
<dependency>
<groupId>org.codehaus.plexus</groupId>
Modified: maven/shared/trunk/maven-shared-io/src/test/java/org/apache/maven/shared/io/TestUtils.java
URL: http://svn.apache.org/viewvc/maven/shared/trunk/maven-shared-io/src/test/java/org/apache/maven/shared/io/TestUtils.java?rev=595953&r1=595952&r2=595953&view=diff
==============================================================================
--- maven/shared/trunk/maven-shared-io/src/test/java/org/apache/maven/shared/io/TestUtils.java (original)
+++ maven/shared/trunk/maven-shared-io/src/test/java/org/apache/maven/shared/io/TestUtils.java Sat Nov 17 06:30:09 2007
@@ -21,13 +21,16 @@
import java.io.BufferedReader;
import java.io.File;
-import java.io.FileReader;
-import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
+import java.io.Reader;
+import java.io.StringReader;
import java.io.StringWriter;
+import java.io.Writer;
import org.codehaus.plexus.util.IOUtil;
+import org.codehaus.plexus.util.ReaderFactory;
+import org.codehaus.plexus.util.WriterFactory;
public final class TestUtils
{
@@ -36,37 +39,105 @@
{
}
- public static void writeToFile( File file, String testStr )
+ private static void write( Writer writer, String content )
throws IOException
{
- FileWriter fw = null;
try
{
- fw = new FileWriter( file );
- fw.write( testStr );
+ writer.write( content );
}
finally
{
- IOUtil.close( fw );
+ IOUtil.close( writer );
}
}
+ public static void writePlatformFile( File file, String content )
+ throws IOException
+ {
+ write( WriterFactory.newPlatformWriter( file ), content );
+ }
+
+ public static void writeFileWithEncoding( File file, String content, String encoding )
+ throws IOException
+ {
+ write( WriterFactory.newWriter( file, encoding ), content );
+ }
+
+ public static void writeXmlFile( File file, String content )
+ throws IOException
+ {
+ write( WriterFactory.newXmlWriter( file ), content );
+ }
+
+ /**
+ * writes content to a file, using platform encoding.
+ * @deprecated this API isn't explicit about encoding, use writePlatformFile() or writeXmlFile()
+ * depending on your need
+ */
+ public static void writeToFile( File file, String testStr )
+ throws IOException
+ {
+ writePlatformFile( file, testStr );
+ }
+
+ /**
+ * reads content from a file and normalize EOLs to simple line feed (\\n), using platform encoding.
+ * @deprecated this API isn't explicit about encoding nor EOL normalization, use readPlatformFile() or
+ * readXmlFile() depending on your need, in conjunction with normalizeEndOfLine()
+ */
public static String readFile( File file ) throws IOException
{
+ return normalizeEndOfLine( readPlatformFile( file ) );
+ }
+
+ public static String readPlatformFile( File file ) throws IOException
+ {
+ StringWriter buffer = new StringWriter();
+
+ Reader reader = ReaderFactory.newPlatformReader( file );
+
+ IOUtil.copy( reader, buffer );
+
+ return buffer.toString();
+ }
+
+ public static String readXmlFile( File file ) throws IOException
+ {
+ StringWriter buffer = new StringWriter();
+
+ Reader reader = ReaderFactory.newXmlReader( file );
+
+ IOUtil.copy( reader, buffer );
+
+ return buffer.toString();
+ }
+
+ /**
+ * normalize EOLs to simple line feed (\\n).
+ */
+ public static String normalizeEndOfLine( String content )
+ {
StringBuffer buffer = new StringBuffer();
- BufferedReader reader = new BufferedReader( new FileReader( file ) );
+ BufferedReader reader = new BufferedReader( new StringReader( content ) );
String line = null;
- while( ( line = reader.readLine() ) != null )
- {
- if ( buffer.length() > 0 )
+ try {
+ while( ( line = reader.readLine() ) != null )
{
- buffer.append( '\n' );
- }
+ if ( buffer.length() > 0 )
+ {
+ buffer.append( '\n' );
+ }
- buffer.append( line );
+ buffer.append( line );
+ }
+ }
+ catch ( IOException ioe )
+ {
+ // should not occur since everything happens in-memory
}
return buffer.toString();
Modified: maven/shared/trunk/maven-shared-io/src/test/java/org/apache/maven/shared/io/location/ArtifactLocationTest.java
URL: http://svn.apache.org/viewvc/maven/shared/trunk/maven-shared-io/src/test/java/org/apache/maven/shared/io/location/ArtifactLocationTest.java?rev=595953&r1=595952&r2=595953&view=diff
==============================================================================
--- maven/shared/trunk/maven-shared-io/src/test/java/org/apache/maven/shared/io/location/ArtifactLocationTest.java (original)
+++ maven/shared/trunk/maven-shared-io/src/test/java/org/apache/maven/shared/io/location/ArtifactLocationTest.java Sat Nov 17 06:30:09 2007
@@ -58,7 +58,7 @@
String testStr = "This is a test";
- TestUtils.writeToFile( f, testStr );
+ TestUtils.writeFileWithEncoding( f, testStr, "US-ASCII" );
Artifact a = new DefaultArtifact( "group", "artifact", VersionRange.createFromVersion( "1" ), null, "jar",
null, new DefaultArtifactHandler() );
@@ -75,7 +75,7 @@
assertEquals( testStr.length(), read );
- assertEquals( testStr, new String( buffer ) );
+ assertEquals( testStr, new String( buffer, "US-ASCII" ) );
}
}
Modified: maven/shared/trunk/maven-shared-io/src/test/java/org/apache/maven/shared/io/location/FileLocationTest.java
URL: http://svn.apache.org/viewvc/maven/shared/trunk/maven-shared-io/src/test/java/org/apache/maven/shared/io/location/FileLocationTest.java?rev=595953&r1=595952&r2=595953&view=diff
==============================================================================
--- maven/shared/trunk/maven-shared-io/src/test/java/org/apache/maven/shared/io/location/FileLocationTest.java (original)
+++ maven/shared/trunk/maven-shared-io/src/test/java/org/apache/maven/shared/io/location/FileLocationTest.java Sat Nov 17 06:30:09 2007
@@ -22,6 +22,7 @@
import org.apache.maven.shared.io.TestUtils;
import org.codehaus.plexus.util.IOUtil;
+import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
@@ -52,7 +53,7 @@
String testStr = "This is a test";
- TestUtils.writeToFile( file, testStr );
+ TestUtils.writeFileWithEncoding( file, testStr, "US-ASCII" );
FileLocation location = new FileLocation( file, file.getAbsolutePath() );
@@ -61,7 +62,7 @@
ByteBuffer buffer = ByteBuffer.allocate( testStr.length() );
location.read( buffer );
- assertEquals( testStr, new String( buffer.array() ) );
+ assertEquals( testStr, new String( buffer.array(), "US-ASCII" ) );
}
public void testShouldReadFileContentsUsingStream() throws IOException
@@ -71,17 +72,17 @@
String testStr = "This is a test";
- TestUtils.writeToFile( file, testStr );
+ TestUtils.writeFileWithEncoding( file, testStr, "US-ASCII" );
FileLocation location = new FileLocation( file, file.getAbsolutePath() );
location.open();
InputStream stream = location.getInputStream();
- StringWriter writer = new StringWriter();
- IOUtil.copy( stream, writer );
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
+ IOUtil.copy( stream, out );
- assertEquals( testStr, writer.toString() );
+ assertEquals( testStr, new String(out.toByteArray(), "US-ASCII" ) );
}
public void testShouldReadFileContentsUsingByteArray() throws IOException
@@ -91,7 +92,7 @@
String testStr = "This is a test";
- TestUtils.writeToFile( file, testStr );
+ TestUtils.writeFileWithEncoding( file, testStr, "US-ASCII" );
FileLocation location = new FileLocation( file, file.getAbsolutePath() );
@@ -100,7 +101,7 @@
byte[] buffer = new byte[ testStr.length() ];
location.read( buffer );
- assertEquals( testStr, new String( buffer ) );
+ assertEquals( testStr, new String( buffer, "US-ASCII" ) );
}
public void testShouldReadThenClose() throws IOException
@@ -110,7 +111,7 @@
String testStr = "This is a test";
- TestUtils.writeToFile( file, testStr );
+ TestUtils.writeFileWithEncoding( file, testStr, "US-ASCII" );
FileLocation location = new FileLocation( file, file.getAbsolutePath() );
@@ -119,7 +120,7 @@
byte[] buffer = new byte[ testStr.length() ];
location.read( buffer );
- assertEquals( testStr, new String( buffer ) );
+ assertEquals( testStr, new String( buffer, "US-ASCII" ) );
location.close();
}
Modified: maven/shared/trunk/maven-shared-io/src/test/java/org/apache/maven/shared/io/location/URLLocationTest.java
URL: http://svn.apache.org/viewvc/maven/shared/trunk/maven-shared-io/src/test/java/org/apache/maven/shared/io/location/URLLocationTest.java?rev=595953&r1=595952&r2=595953&view=diff
==============================================================================
--- maven/shared/trunk/maven-shared-io/src/test/java/org/apache/maven/shared/io/location/URLLocationTest.java (original)
+++ maven/shared/trunk/maven-shared-io/src/test/java/org/apache/maven/shared/io/location/URLLocationTest.java Sat Nov 17 06:30:09 2007
@@ -61,7 +61,7 @@
String testStr = "This is a test";
- TestUtils.writeToFile( f, testStr );
+ TestUtils.writeFileWithEncoding( f, testStr, "US-ASCII" );
URL url = f.toURL();
@@ -75,7 +75,7 @@
assertEquals( testStr.length(), read );
- assertEquals( testStr, new String( buffer ) );
+ assertEquals( testStr, new String( buffer, "US-ASCII" ) );
}
}
Modified: maven/shared/trunk/maven-shared-io/src/test/java/org/apache/maven/shared/io/location/URLLocatorStrategyTest.java
URL: http://svn.apache.org/viewvc/maven/shared/trunk/maven-shared-io/src/test/java/org/apache/maven/shared/io/location/URLLocatorStrategyTest.java?rev=595953&r1=595952&r2=595953&view=diff
==============================================================================
--- maven/shared/trunk/maven-shared-io/src/test/java/org/apache/maven/shared/io/location/URLLocatorStrategyTest.java (original)
+++ maven/shared/trunk/maven-shared-io/src/test/java/org/apache/maven/shared/io/location/URLLocatorStrategyTest.java Sat Nov 17 06:30:09 2007
@@ -59,7 +59,7 @@
String testStr = "This is a test.";
- TestUtils.writeToFile( tempFile, testStr );
+ TestUtils.writeFileWithEncoding( tempFile, testStr, "US-ASCII" );
MessageHolder mh = new DefaultMessageHolder();
@@ -73,7 +73,7 @@
byte[] buffer = new byte[testStr.length()];
location.read( buffer );
- assertEquals( testStr, new String( buffer ) );
+ assertEquals( testStr, new String( buffer, "US-ASCII" ) );
}
}