Skip to content

Commit f035898

Browse files
committed
First public version
0 parents  commit f035898

File tree

247 files changed

+14737
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

247 files changed

+14737
-0
lines changed

README.md

+492

app/PID_LIST.txt

Whitespace-only changes.

app/bin/main/log4j.properties

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
log4j.rootLogger=info, console
2+
3+
log4j.appender.console=org.apache.log4j.ConsoleAppender
4+
log4j.appender.console.layout=org.apache.log4j.PatternLayout
5+
log4j.appender.console.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1} - %m%n
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
11.1 KB
Binary file not shown.
Binary file not shown.
Binary file not shown.
6.24 KB
Binary file not shown.
212 Bytes
Binary file not shown.
Binary file not shown.
Binary file not shown.
458 Bytes
Binary file not shown.
2.14 KB
Binary file not shown.
8.63 KB
Binary file not shown.
8.42 KB
Binary file not shown.
2.11 KB
Binary file not shown.
Binary file not shown.
4.94 KB
Binary file not shown.
Binary file not shown.
7.55 KB
Binary file not shown.
881 Bytes
Binary file not shown.
5.52 KB
Binary file not shown.
689 Bytes
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
2.49 KB
Binary file not shown.
4.34 KB
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
3.39 KB
Binary file not shown.
Binary file not shown.
Binary file not shown.
9.83 KB
Binary file not shown.
Binary file not shown.
7.44 KB
Binary file not shown.
5.07 KB
Binary file not shown.
Binary file not shown.
5.18 KB
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
773 Bytes
Binary file not shown.
Binary file not shown.
1.81 KB
Binary file not shown.
Binary file not shown.
Binary file not shown.

app/bin/test/utb/fai/AssertTest.class

1.67 KB
Binary file not shown.
Binary file not shown.
3.71 KB
Binary file not shown.
Binary file not shown.
4.16 KB
Binary file not shown.
2.7 KB
Binary file not shown.
2.75 KB
Binary file not shown.
4.28 KB
Binary file not shown.
6.74 KB
Binary file not shown.
3.07 KB
Binary file not shown.
3.66 KB
Binary file not shown.
3.72 KB
Binary file not shown.
3.05 KB
Binary file not shown.
1.3 KB
Binary file not shown.
Binary file not shown.
3.53 KB
Binary file not shown.
2.19 KB
Binary file not shown.

app/build.gradle

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* This file was generated by the Gradle 'init' task.
3+
*
4+
* This generated file contains a sample Java application project to get you started.
5+
* For more details on building Java & JVM projects, please refer to https://docs.gradle.org/8.7/userguide/building_java_projects.html in the Gradle documentation.
6+
*/
7+
8+
plugins {
9+
// Apply the application plugin to add support for building a CLI application in Java.
10+
id 'application'
11+
id 'com.github.johnrengelman.shadow' version '8.1.1'
12+
}
13+
14+
repositories {
15+
// Use Maven Central for resolving dependencies.
16+
mavenCentral()
17+
}
18+
19+
dependencies {
20+
// Use JUnit test framework.
21+
testImplementation libs.junit
22+
23+
// This dependency is used by the application.
24+
implementation libs.guava
25+
26+
implementation 'commons-cli:commons-cli:1.4'
27+
implementation 'com.aventstack:extentreports:5.0.5'
28+
implementation 'org.yaml:snakeyaml:1.29'
29+
implementation 'com.icegreen:greenmail:1.6.1'
30+
implementation 'com.sun.mail:javax.mail:1.6.2'
31+
implementation 'org.apache.httpcomponents:httpclient:4.5.13'
32+
implementation 'org.jsoup:jsoup:1.14.3'
33+
implementation 'org.eclipse.paho:org.eclipse.paho.client.mqttv3:1.2.5'
34+
implementation 'com.fasterxml.jackson.core:jackson-databind:2.13.1'
35+
implementation 'com.fasterxml.jackson.dataformat:jackson-dataformat-xml:2.13.1'
36+
implementation 'io.moquette:moquette-broker:0.15'
37+
}
38+
39+
// Apply a specific Java toolchain to ease working on different environments.
40+
java {
41+
toolchain {
42+
languageVersion = JavaLanguageVersion.of(17)
43+
}
44+
}
45+
46+
application {
47+
// Define the main class for the application.
48+
mainClass = 'utb.fai.NetworkAppTestingTool'
49+
}
50+
51+
jar {
52+
manifest {
53+
attributes 'Main-Class': 'utb.fai.NetworkAppTestingTool'
54+
}
55+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,273 @@
1+
package utb.fai.Core;
2+
3+
import java.io.BufferedReader;
4+
import java.io.IOException;
5+
import java.io.InputStream;
6+
import java.io.InputStreamReader;
7+
import java.io.OutputStream;
8+
import java.util.ArrayList;
9+
import java.util.List;
10+
11+
import utb.fai.Exception.InternalErrorException;
12+
import utb.fai.Exception.NonUniqueModuleNamesException;
13+
import utb.fai.Exception.TestedAppFailedToRunException;
14+
15+
/**
16+
* Trida urcena pro spousteni externich aplikaci. Umoznuje spusteni, ukonceni,
17+
* odeslani a prijimani zprav na standartni stream teto aplikace.
18+
*/
19+
@NATTAnnotation.Module("app.std.out")
20+
public class ExternalProgramRunner extends NATTModule {
21+
22+
public static final String NAME = "app-std-out";
23+
24+
private NATTLogger logger = new NATTLogger(ExternalProgramRunner.class);
25+
26+
private Process currentProcess;
27+
private long currentPID;
28+
private OutputStream outputStream;
29+
30+
public ExternalProgramRunner() throws NonUniqueModuleNamesException, InternalErrorException {
31+
super(ExternalProgramRunner.NAME);
32+
this.currentProcess = null;
33+
super.setRunning(true);
34+
String currentDirectory = System.getProperty("user.dir");
35+
logger.info("External program runner init done. Working directory path: " + currentDirectory);
36+
}
37+
38+
@Override
39+
public void runModule() throws InternalErrorException {
40+
// ignorovano ...
41+
}
42+
43+
@Override
44+
public boolean terminateModule() {
45+
return false;
46+
}
47+
48+
@Override
49+
public boolean sendMessage(String message) throws InternalErrorException {
50+
return this.sendMessageToExternalProgram(message, true);
51+
}
52+
53+
/**
54+
* Prevede prikaz zapsany v retezci na pole argumenut. Napriklad: "java -jar
55+
* app.jar 11" rozzdeli na [java, jar, app.jar, 11]
56+
*
57+
* @param commandString Prikaz zapsany v retezci
58+
* @return Pole prikazu
59+
*/
60+
public static String[] commandStringToCommandList(String commandString) {
61+
if (commandString == null) {
62+
String[] empty = new String[0];
63+
return empty;
64+
}
65+
if (commandString.isEmpty()) {
66+
String[] empty = new String[0];
67+
return empty;
68+
}
69+
70+
String[] parts = commandString.split("(?=(?:[^']*'[^']*')*[^']*$)\\s+");
71+
72+
List<String> commandList = new ArrayList<>();
73+
74+
for (String part : parts) {
75+
if (part.startsWith("'") && part.endsWith("'")) {
76+
part = part.substring(1, part.length() - 1);
77+
}
78+
commandList.add(part);
79+
}
80+
81+
String[] command = commandList.toArray(new String[0]);
82+
return command;
83+
}
84+
85+
/**
86+
* Spusti aplikace pro testovani
87+
*
88+
* @param command Prikaz pro spusteni aplikace
89+
*/
90+
public void runExternalProgram(String command) throws TestedAppFailedToRunException {
91+
if (this.currentProcess != null) {
92+
logger.warning("Failed to run. External application is currently running!");
93+
return;
94+
}
95+
96+
logger.info("Run external application with command: " + command);
97+
98+
try {
99+
currentProcess = Runtime.getRuntime().exec(ExternalProgramRunner.commandStringToCommandList(command));
100+
if (currentProcess == null) {
101+
throw new TestedAppFailedToRunException(command);
102+
}
103+
this.outputStream = currentProcess.getOutputStream();
104+
InputStream inputStream = currentProcess.getInputStream();
105+
106+
if (inputStream == null) {
107+
currentProcess.destroy();
108+
throw new TestedAppFailedToRunException(command);
109+
}
110+
if (this.outputStream == null) {
111+
currentProcess.destroy();
112+
throw new TestedAppFailedToRunException(command);
113+
}
114+
115+
ProgramOutputHandler outputHandler = new ProgramOutputHandler(this.currentProcess, inputStream,
116+
this.getName());
117+
outputHandler.start();
118+
119+
// prida pid procesu do souboru pro priadne pozdejsi ukonceni (pokud by doslo k
120+
// necekanemu ukonceni nastroje)
121+
try {
122+
this.currentPID = currentProcess.pid();
123+
ProcessManager.addPID(ProcessManager.DEFAULT_FILE, String.valueOf(this.currentPID));
124+
} catch (Exception ex) {
125+
}
126+
127+
} catch (IOException e) {
128+
throw new TestedAppFailedToRunException(command);
129+
}
130+
}
131+
132+
/**
133+
* Odesle zpravu na System.in spustene aplikace
134+
*
135+
* @param message Obsah odesilane zpravy
136+
* @param endLine Pokud je true bude za konec zpravy vlozen symbol ukonceni
137+
* radku
138+
*
139+
* @return True v pripade uspesneho odeslani zpravy
140+
*/
141+
public boolean sendMessageToExternalProgram(String message, boolean endLine) {
142+
if (currentProcess != null) {
143+
logger.info("Sending message to external application on System.in. Message content: " + message);
144+
try {
145+
146+
outputStream.write(message.getBytes());
147+
if (endLine) {
148+
outputStream.write('\n');
149+
}
150+
outputStream.flush();
151+
152+
return true;
153+
154+
} catch (IOException e) {
155+
logger.warning("Failed to send data. Stream is closed!");
156+
}
157+
} else {
158+
logger.warning("Failed to send data. External application is not running now!");
159+
}
160+
161+
return false;
162+
}
163+
164+
/**
165+
* Overi zda je proces spusteny
166+
*
167+
* @return True v pripade ze proces je stale sputeny
168+
*/
169+
public boolean isProcessRunning() {
170+
if (this.currentProcess == null) {
171+
return false;
172+
}
173+
try {
174+
this.currentProcess.exitValue();
175+
return false; // Proces byl ukoncen
176+
} catch (IllegalThreadStateException e) {
177+
return true; // proces je stale aktivni
178+
}
179+
}
180+
181+
/**
182+
* Ukonci aplikaci
183+
*/
184+
public boolean stopExternalProgram() {
185+
if (currentProcess != null) {
186+
logger.info("Termiting external application");
187+
currentProcess.destroy();
188+
currentProcess = null;
189+
190+
try {
191+
outputStream.close();
192+
} catch (IOException e) {
193+
}
194+
195+
try {
196+
ProcessManager.removePID(ProcessManager.DEFAULT_FILE, String.valueOf(this.currentPID));
197+
} catch (Exception ex) {
198+
}
199+
200+
return true;
201+
}
202+
203+
return false;
204+
}
205+
206+
/**
207+
* Trida pro zpracovani prichozich zprav od externe spustene aplikace. Zpravy
208+
* jsou prijimany pres standartni stream.
209+
*/
210+
private class ProgramOutputHandler extends Thread {
211+
212+
private Process targetProcess;
213+
214+
private BufferedReader reader;
215+
216+
private String msgBufferName;
217+
218+
public ProgramOutputHandler(Process targetProcess, InputStream inputStream, String msgBufferName) {
219+
this.targetProcess = targetProcess;
220+
this.reader = new BufferedReader(new InputStreamReader(inputStream));
221+
this.msgBufferName = msgBufferName;
222+
}
223+
224+
@Override
225+
public void run() {
226+
String line;
227+
try {
228+
while ((line = reader.readLine()) != null) {
229+
// prijatou zpravu vlozi na buffer a zalogguje
230+
NATTContext.instance().getMessageBuffer().addMessage(msgBufferName, "", line);
231+
notifyMessageListeners("", line);
232+
}
233+
} catch (IOException e) {
234+
logger.info("External application termited");
235+
}
236+
237+
// odstraneni PID z listu jelikoz aplikace jiz byla ukoncena
238+
try {
239+
ProcessManager.removePID(ProcessManager.DEFAULT_FILE, String.valueOf(currentPID));
240+
} catch (Exception ex) {
241+
}
242+
243+
// pokusi se ziskat exit kod aplikace a vypsat pripadne chyby
244+
int exitCode = 0;
245+
try {
246+
exitCode = this.targetProcess.waitFor();
247+
if (exitCode != 0) {
248+
logger.warning("External application ends with error: " + exitCode);
249+
BufferedReader errorReader = new BufferedReader(
250+
new InputStreamReader(targetProcess.getErrorStream()));
251+
String errorLine;
252+
StringBuilder errorMessage = new StringBuilder();
253+
try {
254+
while ((errorLine = errorReader.readLine()) != null) {
255+
errorMessage.append(errorLine).append("\n");
256+
}
257+
} catch (IOException e) {
258+
}
259+
if (!errorMessage.isEmpty()) {
260+
logger.warning("Error message from external application: " + errorMessage.toString());
261+
}
262+
} else {
263+
logger.info("External application ends with exit code 0");
264+
}
265+
} catch (InterruptedException e) {
266+
logger.warning("Failed to get exit code of external application. Message: " + e.getMessage());
267+
}
268+
currentProcess = null;
269+
}
270+
271+
}
272+
273+
}

0 commit comments

Comments
 (0)