forked from AutoGeneral/IridiumApplicationTesting
/
Main.java
148 lines (123 loc) · 3.85 KB
/
Main.java
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
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
package au.com.agic.apptesting;
import au.com.agic.apptesting.constants.Constants;
import au.com.agic.apptesting.utils.SystemPropertyUtils;
import au.com.agic.apptesting.utils.impl.SystemPropertyUtilsImpl;
import javaslang.control.Try;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import static com.google.common.base.Preconditions.checkState;
public final class Main {
private static final Logger LOGGER = LoggerFactory.getLogger(Main.class);
private static final SystemPropertyUtils SYSTEM_PROPERTY_UTILS = new SystemPropertyUtilsImpl();
/**
* Used to name threads that might be reused
*/
public static final AtomicInteger THREAD_COUNT = new AtomicInteger(0);
private static ScheduledExecutorService terminator;
private static Future<?> terminatorFuture;
private Main() {
}
public static void main(final String... args) {
try {
System.exit(run());
} catch (final Exception ex) {
LOGGER.error(
"WEBAPPTESTER-BUG-0007: "
+ "An exception was raised while attempting to run the Cucumber test scripts", ex);
System.exit(-1);
}
}
/**
* Run the test script
* @return The number of failures returned by the test script
*/
public static int run() {
final List<File> globalTempFiles = new ArrayList<>();
try {
/*
This is required to run ZAP from webstart
*/
System.setSecurityManager(null);
createShutdownTimer();
/*
Get the retry count
*/
final int retryCount = NumberUtils.toInt(
SYSTEM_PROPERTY_UTILS.getProperty(Constants.TEST_RETRY_COUNT),
1
);
/*
Get the delay between retries
*/
final int retryDelay = NumberUtils.toInt(
SYSTEM_PROPERTY_UTILS.getProperty(Constants.DELAY_BETWEEN_RETRY),
0
);
/*
Execute the tests
*/
int lastFailures = 0;
for (int retry = 0; retry < retryCount; ++retry) {
LOGGER.info("WEBAPPTESTER-INFO-0009: Running test attempt {} of {}", retry + 1, retryCount);
lastFailures = new TestRunner().run(globalTempFiles);
/*
Write some output to let the caller know how many failures there were
*/
LOGGER.info("WEBAPPTESTER-INFO-0008: TestRunner experienced {} failures", lastFailures);
if (lastFailures == 0) {
break;
}
Try.run(() -> Thread.sleep(retryDelay * 1000));
}
return lastFailures;
} finally {
THREAD_COUNT.set(0);
globalTempFiles.forEach(FileUtils::deleteQuietly);
cancelShutdownTimer();
}
}
private static void cancelShutdownTimer() {
if (terminatorFuture != null) {
terminatorFuture.cancel(false);
}
if (terminator != null) {
terminator.shutdown();
}
terminatorFuture = null;
terminator = null;
}
/**
* Creates a thread that will shutdown the application after a certain amount of time has passed.
*/
private static void createShutdownTimer() {
checkState(terminatorFuture == null);
checkState(terminator == null);
final int maxExecutionTime = NumberUtils.toInt(
SYSTEM_PROPERTY_UTILS.getProperty(Constants.MAX_EXECUTION_TIME)
);
if (maxExecutionTime > 0) {
terminator = Executors.newSingleThreadScheduledExecutor();
terminatorFuture = terminator.schedule(() -> {
try {
LOGGER.error(
"WEBAPPTESTER-INFO-0011: "
+ "Iridium was shut down because it ran longer than the maximum execution time of " + maxExecutionTime + " seconds");
System.exit(-2);
} catch (final Exception ex) {
LOGGER.error(
"WEBAPPTESTER-BUG-0009: The shutdown timer threw an exception", ex);
}
}, maxExecutionTime, TimeUnit.SECONDS);
}
}
}