Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.channels.FileChannel;
import java.util.ArrayList;
import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class FileUtils {
public static void visitAllFiles(File f, ArrayList<File> files) {
if (f.isDirectory()) {
String[] children = f.list();
for (int i = 0; i < children.length; i++) {
visitAllFiles(new File(f, children[i]), files);
}
} else {
if (f.toString().endsWith(".ttcn")) {
files.add(f);
}
}
}
// -----------------------------------------------------------------------------------------------
public static void loadConfig(Properties config) throws IOException {
FileInputStream fis = new FileInputStream(
"resources/TestCastTester.properties");
config.load(fis);
fis.close();
if ((config.get("TestCastCommandLineTool") == null)
|| (config.get("LoopbackAdapterPath") == null)
|| (config.get("TempPath") == null)
|| (config.get("LogPath") == null)
|| (config.get("ATSPath") == null)) {
throw new IOException();
}
}
// -----------------------------------------------------------------------------------------------
public static void copyFile(File in, File out) throws IOException {
FileChannel inChannel = new FileInputStream(in).getChannel();
FileChannel outChannel = new FileOutputStream(out).getChannel();
try {
inChannel.transferTo(0, inChannel.size(), outChannel);
} catch (IOException e) {
throw e;
} finally {
if (inChannel != null)
inChannel.close();
if (outChannel != null)
outChannel.close();
}
}
// -----------------------------------------------------------------------------------------------
public static int countT3Modules(File f) {
int moduleCount = 0;
try {
FileInputStream fs = new FileInputStream(f.getAbsolutePath());
DataInputStream in = new DataInputStream(fs);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine = "";
while ((strLine = br.readLine()) != null) {
if (strLine.matches("^\\s*module\\s+\\w+\\s*.*")) {
moduleCount++;
}
}
in.close();
fs.close();
} catch (IOException e) {
e.printStackTrace();
}
return moduleCount;
}
// -----------------------------------------------------------------------------------------------
private static Pattern s_verdictPassMatch = Pattern.compile("^^\\s*\\*\\*\\s*@verdict\\s*pass\\s*(.*)");
private static Pattern s_verdictMatch = Pattern.compile("^\\s*\\*\\*\\s*@verdict\\s*.*");
private static Pattern s_configMatch = Pattern.compile("^\\s*\\*\\*\\s*@configuration\\s*(.*)");
public static T3ExpectedOutput extractExpectedOutput(File f) {
T3ExpectedOutput t3ExpectedOutput = new T3ExpectedOutput();
try {
FileInputStream fs = new FileInputStream(f.getAbsolutePath());
DataInputStream in = new DataInputStream(fs);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine = "";
while ((strLine = br.readLine()) != null) {
if (s_verdictMatch.matcher(strLine).matches()) {
Matcher matcher = s_verdictPassMatch.matcher(strLine);
if (matcher.find()) {
String result = matcher.group(1);
String[] split = result.split(",");
t3ExpectedOutput.setExpectedOutput(split[0].trim());
if (split.length > 1)
t3ExpectedOutput.setExecutionResult(split[1].trim());
}
// System.out.println(strLine);
} else if ((configMatch = s_configMatch.matcher(strLine)).matches()) {
t3ExpectedOutput.setConfiguration(configMatch.group(1));
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
}
}
in.close();
fs.close();
} catch (IOException e) {
e.printStackTrace();
}
return t3ExpectedOutput;
}
// -----------------------------------------------------------------------------------------------
public static ArrayList<File> splitT3ModuleFiles(File f) {
ArrayList<File> files = new ArrayList<File>();
StringBuffer current = new StringBuffer();
File newFile = new File(f.getParent() + "//temp.ttcn");
if (f.exists()) {
f.renameTo(newFile);
}
try {
FileInputStream fs = new FileInputStream(newFile.getAbsolutePath());
DataInputStream in = new DataInputStream(fs);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine = "";
int moduleCounter = 0;
String currentModuleName = "";
while ((strLine = br.readLine()) != null) {
if (strLine.matches("^\\s*module\\s+\\w+\\s*.*")) {
moduleCounter++;
if (moduleCounter > 1) { // write file
String targetFilename = f.getParent() + "//" + currentModuleName + ".ttcn";
FileWriter fw = new FileWriter(targetFilename);
fw.write(current.toString());
fw.close();
current = new StringBuffer();
files.add(new File(targetFilename));
}
Matcher matcher = Pattern.compile("^\\s*module\\s+(\\w+)\\s*.*").matcher(strLine);
if (matcher.find()) {
currentModuleName = matcher.group(1);
}
}
current.append(strLine + "\n");
}
// write file
String targetFilename = f.getParent() + "//" + currentModuleName + ".ttcn";
FileWriter fw = new FileWriter(targetFilename);
fw.write(current.toString());
fw.close();
in.close();
fs.close();
files.add(new File(targetFilename));
newFile.delete();
} catch (IOException e) {
e.printStackTrace();
}
return files;
}
public static ArrayList<String> readExclusionList(File f) {
ArrayList<String> files = new ArrayList<String>();
try {
FileInputStream fs = new FileInputStream(f.getAbsolutePath());
DataInputStream in = new DataInputStream(fs);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine = "";
while ((strLine = br.readLine()) != null) {
if (strLine.trim().length() > 0)
files.add(strLine);
}
return files;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}