/
NamedThreadHasStatus.java
106 lines (96 loc) · 3.31 KB
/
NamedThreadHasStatus.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
/*******************************************************************************
* Copyright (c) 2017 Red Hat, Inc and others.
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Red Hat, Inc - initial API and implementation
*******************************************************************************/
package org.eclipse.reddeer.core.condition;
import java.util.Iterator;
import java.util.Set;
import org.hamcrest.Matcher;
import org.eclipse.reddeer.common.condition.AbstractWaitCondition;
/**
* Condition is met when thread with matching name has required state
*
* @author vlado pakan
* @author jkopriva@redhat.com
*
*/
public class NamedThreadHasStatus extends AbstractWaitCondition {
private Matcher<String> nameMatcher;
private Thread.State state;
private Thread.State resultStatus;
private boolean returnTrueIfDoesNotExist;
private Set<Thread> currentThreads = Thread.getAllStackTraces().keySet();
/**
* Condition is met when thread with name matching nameMatcher has state equals to state
* In case returnTrueIfDoesNotExist parameter is set to true condition is fulfilled also
* when matching thread does not exists
* @param nameMatcher name matcher
* @param state state
* @param returnTrueIfDoesNotExist should return true if does not exist
*
*/
public NamedThreadHasStatus(Matcher<String> nameMatcher , Thread.State state, boolean returnTrueIfDoesNotExist) {
this.nameMatcher = nameMatcher;
this.state = state;
this.returnTrueIfDoesNotExist = returnTrueIfDoesNotExist;
}
@Override
public boolean test() {
currentThreads = Thread.getAllStackTraces().keySet();
Iterator<Thread> itThread = currentThreads.iterator();
boolean threadNotFound = true;
boolean hasState = false;
while(threadNotFound && itThread.hasNext()){
Thread thread = itThread.next();
if (nameMatcher.matches(thread.getName())){
threadNotFound = false;
hasState = thread.getState().equals(state);
if (hasState) {
resultStatus = thread.getState();
}
}
}
return hasState || (threadNotFound && returnTrueIfDoesNotExist);
}
@Override
public String description() {
return "thread with name matching" + this.nameMatcher + " has state " + this.state;
}
@Override
public String errorMessageWhile() {
return createErrorMessageWithThreadList("The following threads are still available:\n");
}
@Override
public String errorMessageUntil() {
return createErrorMessageWithThreadList("The threads have not been found.The following threads are available:\n");
}
/**
* Creates error message for methods errorMessageWhile() and errorMessageUntil() with job names.
*
* @param messageStart start of the error message with job list
*/
private String createErrorMessageWithThreadList(String messageStart){
StringBuilder message = new StringBuilder(messageStart);
for (Thread thread : currentThreads){
if (thread != null){
message.append(thread.getName());
message.append(":");
message.append(thread.getState());
message.append("\n");
}
}
return message.toString();
}
@SuppressWarnings("unchecked")
@Override
public Thread.State getResult() {
return this.resultStatus;
}
}