View Javadoc

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