1 | package com.github.dakusui.actionunit.visitors; | |
2 | ||
3 | import com.github.dakusui.actionunit.actions.*; | |
4 | import com.github.dakusui.actionunit.core.Action; | |
5 | import com.github.dakusui.actionunit.core.Context; | |
6 | import com.github.dakusui.actionunit.exceptions.ActionException; | |
7 | import com.github.dakusui.actionunit.utils.InternalUtils; | |
8 | ||
9 | import java.util.HashMap; | |
10 | import java.util.Map; | |
11 | import java.util.concurrent.ConcurrentHashMap; | |
12 | import java.util.function.Function; | |
13 | import java.util.stream.Stream; | |
14 | ||
15 | import static java.util.Objects.requireNonNull; | |
16 | import static java.util.concurrent.TimeUnit.NANOSECONDS; | |
17 | ||
18 | public abstract class ActionPerformer implements Action.Visitor { | |
19 | public static final String ONGOING_EXCEPTIONS_TABLE_NAME = "ONGOING_EXCEPTIONS"; | |
20 | protected Context context; | |
21 | ||
22 | protected ActionPerformer(Context context) { | |
23 | this.context = requireNonNull(context); | |
24 | Map<Action, Throwable> ongoingExceptions = new ConcurrentHashMap<>(); | |
25 | this.context.assignTo(ONGOING_EXCEPTIONS_TABLE_NAME, ongoingExceptions); | |
26 | } | |
27 | ||
28 | public void visit(Leaf action) { | |
29 |
1
1. visit : removed call to java/lang/Runnable::run → KILLED |
action.runnable(context).run(); |
30 | } | |
31 | ||
32 | public void visit(Named action) { | |
33 |
1
1. visit : removed call to com/github/dakusui/actionunit/visitors/ActionPerformer::callAccept → KILLED |
callAccept(action.action(), this); |
34 | } | |
35 | ||
36 | public void visit(Composite action) { | |
37 |
1
1. visit : negated conditional → KILLED |
Stream<Action> actionStream = action.isParallel() |
38 | ? action.children().parallelStream() | |
39 | : action.children().stream(); | |
40 |
3
1. lambda$visit$0 : negated conditional → TIMED_OUT 2. lambda$visit$0 : removed call to com/github/dakusui/actionunit/visitors/ActionPerformer::callAccept → KILLED 3. visit : removed call to java/util/stream/Stream::forEach → KILLED |
actionStream.forEach(a -> callAccept(a, action.isParallel() ? |
41 | newInstance(this.context.createChild()) : | |
42 | this)); | |
43 | } | |
44 | ||
45 | public <E> void visit(ForEach<E> action) { | |
46 | Stream<E> data = action.valueSource().apply(this.context); | |
47 | Function<E, Action.Visitor> visitorFactory = v -> { | |
48 | this.context.assignTo(action.internalVariableName(), v); | |
49 |
1
1. lambda$visit$1 : replaced return value with null for com/github/dakusui/actionunit/visitors/ActionPerformer::lambda$visit$1 → KILLED |
return this; |
50 | }; | |
51 |
1
1. visit : negated conditional → KILLED |
if (action.isParallel()) { |
52 | data = data.parallel(); | |
53 |
1
1. lambda$visit$2 : replaced return value with null for com/github/dakusui/actionunit/visitors/ActionPerformer::lambda$visit$2 → KILLED |
visitorFactory = v -> newInstance(this.context.createChild().assignTo(action.internalVariableName(), v)); |
54 | } | |
55 | Function<E, Action.Visitor> finalVisitorFactory = visitorFactory; | |
56 |
2
1. lambda$visit$3 : removed call to com/github/dakusui/actionunit/visitors/ActionPerformer::callAccept → KILLED 2. visit : removed call to java/util/stream/Stream::forEach → KILLED |
data.forEach(each -> callAccept( |
57 | action.action(), | |
58 | finalVisitorFactory.apply(each))); | |
59 | } | |
60 | ||
61 | @Override | |
62 | public void visit(While action) { | |
63 |
1
1. visit : negated conditional → KILLED |
while (action.condition().test(this.context)) { |
64 |
1
1. visit : removed call to com/github/dakusui/actionunit/visitors/ActionPerformer::callAccept → TIMED_OUT |
callAccept(action.perform(), this); |
65 | } | |
66 | } | |
67 | ||
68 | @Override | |
69 | public void visit(When action) { | |
70 |
1
1. visit : negated conditional → KILLED |
if (action.cond().test(this.context)) { |
71 |
1
1. visit : removed call to com/github/dakusui/actionunit/visitors/ActionPerformer::callAccept → KILLED |
callAccept(action.perform(), this); |
72 | } else { | |
73 |
1
1. visit : removed call to com/github/dakusui/actionunit/visitors/ActionPerformer::callAccept → KILLED |
callAccept(action.otherwise(), this); |
74 | } | |
75 | } | |
76 | ||
77 | @Override | |
78 | public <V> void visit(With<V> action) { | |
79 | Context originalContext = this.context; | |
80 | this.context = this.context.createChild(); | |
81 | context.assignTo(action.internalVariableName(), action.valueSource().apply(context)); | |
82 | try { | |
83 |
1
1. visit : removed call to com/github/dakusui/actionunit/visitors/ActionPerformer::callAccept → KILLED |
callAccept(action.action(), this); |
84 | } finally { | |
85 |
2
1. lambda$visit$4 : removed call to com/github/dakusui/actionunit/visitors/ActionPerformer::callAccept → SURVIVED 2. visit : removed call to java/util/Optional::ifPresent → SURVIVED |
action.close().ifPresent(a -> callAccept(a, this)); |
86 | this.context = originalContext; | |
87 | } | |
88 | } | |
89 | ||
90 | @Override | |
91 | public void visit(Attempt action) { | |
92 | Context originalContext = this.context; | |
93 | try { | |
94 |
1
1. visit : removed call to com/github/dakusui/actionunit/visitors/ActionPerformer::callAccept → KILLED |
callAccept(action.perform(), this); |
95 | } catch (Throwable t) { | |
96 |
1
1. visit : negated conditional → KILLED |
if (action.targetExceptionClass().isAssignableFrom(t.getClass())) { |
97 | this.context = this.context.createChild(); | |
98 |
1
1. visit : removed call to com/github/dakusui/actionunit/visitors/ActionPerformer::callAccept → KILLED |
callAccept(action.recover(), newInstance( |
99 | originalContext.assignTo(Context.Impl.ONGOING_EXCEPTION, t) | |
100 | )); | |
101 | } else | |
102 | throw ActionException.wrap(t); | |
103 | } finally { | |
104 |
1
1. visit : removed call to com/github/dakusui/actionunit/visitors/ActionPerformer::callAccept → KILLED |
callAccept(action.ensure(), this); |
105 | this.context = originalContext; | |
106 | } | |
107 | } | |
108 | ||
109 | public void visit(Retry action) { | |
110 | boolean succeeded = false; | |
111 | Action targetAction = action.perform(); | |
112 | Throwable lastException = null; | |
113 |
3
1. visit : Changed increment from 1 to -1 → TIMED_OUT 2. visit : changed conditional boundary → KILLED 3. visit : negated conditional → KILLED |
for (int i = 0; i <= action.times(); i++) { |
114 | try { | |
115 |
1
1. visit : removed call to com/github/dakusui/actionunit/visitors/ActionPerformer::callAccept → KILLED |
callAccept(targetAction, this); |
116 | succeeded = true; | |
117 | break; | |
118 | } catch (Throwable t) { | |
119 |
1
1. visit : negated conditional → KILLED |
if (action.targetExceptionClass().isAssignableFrom(t.getClass())) { |
120 | lastException = t; | |
121 |
1
1. visit : removed call to com/github/dakusui/actionunit/visitors/ActionPerformer::registerLastExceptionFor → SURVIVED |
registerLastExceptionFor(targetAction, lastException); |
122 |
1
1. visit : removed call to com/github/dakusui/actionunit/utils/InternalUtils::sleep → SURVIVED |
InternalUtils.sleep(action.intervalInNanoseconds(), NANOSECONDS); |
123 | } else { | |
124 | throw ActionException.wrap(t); | |
125 | } | |
126 | } | |
127 | } | |
128 |
1
1. visit : negated conditional → KILLED |
if (succeeded) { |
129 |
1
1. visit : removed call to com/github/dakusui/actionunit/visitors/ActionPerformer::unregisterLastExceptionFor → SURVIVED |
unregisterLastExceptionFor(targetAction); |
130 | } else { | |
131 | throw ActionException.wrap(lastException); | |
132 | } | |
133 | } | |
134 | ||
135 | public void visit(TimeOut action) { | |
136 | InternalUtils.runWithTimeout( | |
137 | () -> { | |
138 |
1
1. lambda$visit$5 : removed call to com/github/dakusui/actionunit/visitors/ActionPerformer::callAccept → KILLED |
callAccept(action.perform(), ActionPerformer.this); |
139 |
1
1. lambda$visit$5 : replaced Boolean return with False for com/github/dakusui/actionunit/visitors/ActionPerformer::lambda$visit$5 → SURVIVED |
return true; |
140 | }, | |
141 |
1
1. lambda$visit$6 : replaced return value with "" for com/github/dakusui/actionunit/visitors/ActionPerformer::lambda$visit$6 → SURVIVED |
() -> String.format("%s", action), |
142 |
1
1. lambda$visit$7 : replaced return value with "" for com/github/dakusui/actionunit/visitors/ActionPerformer::lambda$visit$7 → SURVIVED |
() -> formatOngoingExceptions(action.perform()), |
143 | action.durationInNanos(), | |
144 | NANOSECONDS | |
145 | ); | |
146 | } | |
147 | ||
148 | protected abstract Action.Visitor newInstance(Context context); | |
149 | ||
150 | protected abstract void callAccept(Action action, Action.Visitor visitor); | |
151 | ||
152 | private void registerLastExceptionFor(Action action, Throwable e) { | |
153 | ongoingExceptionsTable().put(action, e); | |
154 | } | |
155 | ||
156 | private void unregisterLastExceptionFor(Action action) { | |
157 | ongoingExceptionsTable().remove(action); | |
158 | } | |
159 | ||
160 | private Map<Action, Throwable> ongoingExceptionsTable() { | |
161 |
1
1. ongoingExceptionsTable : replaced return value with Collections.emptyMap for com/github/dakusui/actionunit/visitors/ActionPerformer::ongoingExceptionsTable → KILLED |
return this.context.valueOf(ONGOING_EXCEPTIONS_TABLE_NAME); |
162 | } | |
163 | ||
164 | private String formatOngoingExceptions(Action action) { | |
165 | StringBuilder b = new StringBuilder(); | |
166 | for (Action ongoingAction : ongoingExceptionsTable().keySet()) { | |
167 | b.append(String.format("%n%s%n----%n", ongoingAction)); | |
168 | Throwable e = ongoingExceptionsTable().get(ongoingAction); | |
169 | b.append(e.getMessage()); | |
170 | b.append(String.format("%n")); | |
171 | for (StackTraceElement element : e.getStackTrace()) { | |
172 | b.append("\t"); | |
173 | b.append(element); | |
174 | b.append(String.format("%n")); | |
175 | } | |
176 | } | |
177 |
1
1. formatOngoingExceptions : replaced return value with "" for com/github/dakusui/actionunit/visitors/ActionPerformer::formatOngoingExceptions → SURVIVED |
return b.toString(); |
178 | } | |
179 | } | |
Mutations | ||
29 |
1.1 |
|
33 |
1.1 |
|
37 |
1.1 |
|
40 |
1.1 2.2 3.3 |
|
49 |
1.1 |
|
51 |
1.1 |
|
53 |
1.1 |
|
56 |
1.1 2.2 |
|
63 |
1.1 |
|
64 |
1.1 |
|
70 |
1.1 |
|
71 |
1.1 |
|
73 |
1.1 |
|
83 |
1.1 |
|
85 |
1.1 2.2 |
|
94 |
1.1 |
|
96 |
1.1 |
|
98 |
1.1 |
|
104 |
1.1 |
|
113 |
1.1 2.2 3.3 |
|
115 |
1.1 |
|
119 |
1.1 |
|
121 |
1.1 |
|
122 |
1.1 |
|
128 |
1.1 |
|
129 |
1.1 |
|
138 |
1.1 |
|
139 |
1.1 |
|
141 |
1.1 |
|
142 |
1.1 |
|
161 |
1.1 |
|
177 |
1.1 |