1 package org.apache.maven.shared.tools.easymock;
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 /**
33 * @version $Id$
34 */
35 public final class TestUtils
36 {
37 private TestUtils()
38 {
39 //nop
40 }
41
42 /**
43 * @param file
44 * @param testStr
45 * @throws IOException if any
46 */
47 public static void writeToFile( File file, String testStr )
48 throws IOException
49 {
50 FileWriter fw = null;
51 try
52 {
53 fw = new FileWriter( file );
54 fw.write( testStr );
55 }
56 finally
57 {
58 IOUtil.close( fw );
59 }
60 }
61
62 /**
63 * @param file
64 * @return
65 * @throws IOException if any
66 * @todo maybe used {@link IOUtil#toString(java.io.Reader)}
67 */
68 public static String readFile( File file )
69 throws IOException
70 {
71 StringBuffer buffer = new StringBuffer();
72
73 BufferedReader reader = new BufferedReader( new FileReader( file ) );
74
75 String line = null;
76
77 while ( ( line = reader.readLine() ) != null )
78 {
79 if ( buffer.length() > 0 )
80 {
81 buffer.append( '\n' );
82 }
83
84 buffer.append( line );
85 }
86
87 return buffer.toString();
88 }
89
90 /**
91 * @param error
92 * @return
93 */
94 public static String toString( Throwable error )
95 {
96 StringWriter sw = new StringWriter();
97 PrintWriter pw = new PrintWriter( sw );
98
99 error.printStackTrace( pw );
100
101 return sw.toString();
102 }
103 }