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