1 package org.apache.maven.plugin.assembly.testutils;
2
3 /*
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
19 * under the License.
20 */
21
22 import java.io.BufferedReader;
23 import java.io.File;
24 import java.io.FileReader;
25 import java.io.FileWriter;
26 import java.io.IOException;
27 import java.io.PrintWriter;
28 import java.io.StringWriter;
29
30 import org.codehaus.plexus.util.IOUtil;
31
32 public final class TestUtils
33 {
34
35 private TestUtils()
36 {
37 }
38
39 /**
40 * Write a text to a file using platform encoding.
41 */
42 public static void writeToFile( File file, String testStr )
43 throws IOException
44 {
45 FileWriter fw = null;
46 try
47 {
48 fw = new FileWriter( file ); // platform encoding
49 fw.write( testStr );
50 }
51 finally
52 {
53 IOUtil.close( fw );
54 }
55 }
56
57 /**
58 * Read file content using platform encoding and converting line endings to \\n.
59 */
60 public static String readFile( File file ) throws IOException
61 {
62 StringBuffer buffer = new StringBuffer();
63
64 BufferedReader reader = new BufferedReader( new FileReader( file ) ); // platform encoding
65
66 String line = null;
67
68 while( ( line = reader.readLine() ) != null )
69 {
70 if ( buffer.length() > 0 )
71 {
72 buffer.append( '\n' );
73 }
74
75 buffer.append( line );
76 }
77
78 return buffer.toString();
79 }
80
81 public static String toString( Throwable error )
82 {
83 StringWriter sw = new StringWriter();
84 PrintWriter pw = new PrintWriter( sw );
85
86 error.printStackTrace( pw );
87
88 return sw.toString();
89 }
90 }