Package: FileUtils

FileUtils

nameinstructionbranchcomplexitylinemethod
FileUtils()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
readFile(String)
M: 55 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 10 C: 0
0%
M: 1 C: 0
0%
readTextFromJar(String)
M: 48 C: 0
0%
M: 6 C: 0
0%
M: 4 C: 0
0%
M: 17 C: 0
0%
M: 1 C: 0
0%
readTextFromSource(String)
M: 45 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 8 C: 0
0%
M: 1 C: 0
0%
writeFile(String, File)
M: 5 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
writeFile(String, File, boolean)
M: 32 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 14 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*
2: * #%~
3: * Test Framework for Overture
4: * %%
5: * Copyright (C) 2008 - 2014 Overture
6: * %%
7: * This program is free software: you can redistribute it and/or modify
8: * it under the terms of the GNU General Public License as
9: * published by the Free Software Foundation, either version 3 of the
10: * License, or (at your option) any later version.
11: *
12: * This program is distributed in the hope that it will be useful,
13: * but WITHOUT ANY WARRANTY; without even the implied warranty of
14: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15: * GNU General Public License for more details.
16: *
17: * You should have received a copy of the GNU General Public
18: * License along with this program. If not, see
19: * <http://www.gnu.org/licenses/gpl-3.0.html>.
20: * #~%
21: */
22: package org.overture.test.util;
23:
24: import java.io.BufferedReader;
25: import java.io.BufferedWriter;
26: import java.io.File;
27: import java.io.FileReader;
28: import java.io.FileWriter;
29: import java.io.IOException;
30: import java.io.InputStream;
31: import java.io.InputStreamReader;
32: import java.util.ArrayList;
33: import java.util.List;
34:
35: public class FileUtils
36: {
37:
38:         public static List<String> readTextFromJar(String s) throws Exception
39:         {
40:                 InputStream is = null;
41:                 BufferedReader br = null;
42:                 String line;
43:                 List<String> list = new ArrayList<String>();
44:
45:                 try
46:                 {
47:                         is = FileUtils.class.getResourceAsStream(s);
48:                         br = new BufferedReader(new InputStreamReader(is));
49:•                        while (null != (line = br.readLine()))
50:                         {
51:                                 list.add(line);
52:                         }
53:                 } catch (Exception e)
54:                 {
55:                         // System.err.println("Faild to read file from jar: \"" + s + "\"");
56:                         // e.printStackTrace();
57:                         throw e;
58:                 } finally
59:                 {
60:                         try
61:                         {
62:•                                if (br != null)
63:                                 {
64:                                         br.close();
65:                                 }
66:•                                if (is != null)
67:                                 {
68:                                         is.close();
69:                                 }
70:                         } catch (IOException e)
71:                         {
72:                                 e.printStackTrace();
73:                         }
74:                 }
75:                 return list;
76:         }
77:
78:         public static String readFile(String relativePath) throws IOException
79:         {
80:                 StringBuilder sb = new StringBuilder();
81:
82:                 try
83:                 {
84:•                        for (String s : FileUtils.readTextFromJar(relativePath))
85:                         {
86:                                 sb.append("\n" + s);
87:                         }
88:                 } catch (Exception e)
89:                 {
90:                         // ok maybe we are running in debug mode
91:•                        for (String s : FileUtils.readTextFromSource(relativePath))
92:                         {
93:                                 sb.append("\n" + s);
94:                         }
95:                 }
96:                 return sb.toString();
97:         }
98:
99:         private static List<String> readTextFromSource(String relativePath)
100:         {
101:                 List<String> list = new ArrayList<String>();
102:                 try
103:                 {
104:                         BufferedReader in = new BufferedReader(new FileReader(new File(new File("."), ("src/test/resources/" + relativePath).replace('/', File.separatorChar))));
105:                         String str;
106:•                        while ((str = in.readLine()) != null)
107:                         {
108:                                 list.add(str);
109:                         }
110:                         in.close();
111:                 } catch (IOException e)
112:                 {
113:                 }
114:
115:                 return list;
116:         }
117:
118:         // public static void writeFile(File outputFolder, String fileName, String content)
119:         // throws IOException
120:         // {
121:         // FileWriter outputFileReader = new FileWriter(new File(outputFolder,
122:         // fileName), false);
123:         // BufferedWriter outputStream = new BufferedWriter(outputFileReader);
124:         // outputStream.write(content);
125:         // outputStream.close();
126:         //
127:         // }
128:         //
129:         // public static void writeFile(File file, String content) throws IOException
130:         // {
131:         // FileWriter outputFileWriter = new FileWriter(file);
132:         // BufferedWriter outputStream = new BufferedWriter(outputFileWriter);
133:         // outputStream.write(content);
134:         // outputStream.close();
135:         // outputFileWriter.close();
136:         // }
137:
138:         public static void writeFile(String data, File file)
139:         {
140:                 writeFile(data, file, false);
141:         }
142:
143:         public static void writeFile(String data, File file, boolean append)
144:         {
145:                 BufferedWriter outputStream = null;
146:                 try
147:                 {
148:                         FileWriter outputFileReader = new FileWriter(file, append);
149:                         outputStream = new BufferedWriter(outputFileReader);
150:
151:                         outputStream.write(data);
152:
153:                         outputStream.flush();
154:                         outputStream.close();
155:
156:                 } catch (IOException e)
157:                 {
158:                         // TODO Auto-generated catch block
159:                         e.printStackTrace();
160:                 } finally
161:                 {
162:•                        if (outputStream != null)
163:                         {
164:                                 try
165:                                 {
166:                                         outputStream.close();
167:                                 } catch (IOException e)
168:                                 {
169:                                         // TODO Auto-generated catch block
170:                                         e.printStackTrace();
171:                                 }
172:                         }
173:                 }
174:
175:         }
176: }