ActionPerformer.java

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
Location : visit
Killed by : com.github.dakusui.actionunit.scenarios.CompatActionSupportTest.simpleTest(com.github.dakusui.actionunit.scenarios.CompatActionSupportTest)
removed call to java/lang/Runnable::run → KILLED

33

1.1
Location : visit
Killed by : com.github.dakusui.actionunit.scenarios.CompatActionSupportTest.givenWhenAction$whenPerform$thenObjectAdded(com.github.dakusui.actionunit.scenarios.CompatActionSupportTest)
removed call to com/github/dakusui/actionunit/visitors/ActionPerformer::callAccept → KILLED

37

1.1
Location : visit
Killed by : com.github.dakusui.actionunit.scenarios.CompatActionSupportTest.sequentialTest(com.github.dakusui.actionunit.scenarios.CompatActionSupportTest)
negated conditional → KILLED

40

1.1
Location : lambda$visit$0
Killed by : none
negated conditional → TIMED_OUT

2.2
Location : lambda$visit$0
Killed by : com.github.dakusui.actionunit.scenarios.CompatActionSupportTest.sequentialTest(com.github.dakusui.actionunit.scenarios.CompatActionSupportTest)
removed call to com/github/dakusui/actionunit/visitors/ActionPerformer::callAccept → KILLED

3.3
Location : visit
Killed by : com.github.dakusui.actionunit.scenarios.CompatActionSupportTest.sequentialTest(com.github.dakusui.actionunit.scenarios.CompatActionSupportTest)
removed call to java/util/stream/Stream::forEach → KILLED

49

1.1
Location : lambda$visit$1
Killed by : com.github.dakusui.actionunit.ut.VariationTest.forEachAndPipedAction(com.github.dakusui.actionunit.ut.VariationTest)
replaced return value with null for com/github/dakusui/actionunit/visitors/ActionPerformer::lambda$visit$1 → KILLED

51

1.1
Location : visit
Killed by : com.github.dakusui.actionunit.ut.VariationTest.doubleLoop(com.github.dakusui.actionunit.ut.VariationTest)
negated conditional → KILLED

53

1.1
Location : lambda$visit$2
Killed by : com.github.dakusui.actionunit.scenarios.ActionSupportTest.giveExampleScenarioThatThrowsError$whenPerform$thenExceptionThrown(com.github.dakusui.actionunit.scenarios.ActionSupportTest)
replaced return value with null for com/github/dakusui/actionunit/visitors/ActionPerformer::lambda$visit$2 → KILLED

56

1.1
Location : lambda$visit$3
Killed by : com.github.dakusui.actionunit.ut.VariationTest.doubleLoop(com.github.dakusui.actionunit.ut.VariationTest)
removed call to com/github/dakusui/actionunit/visitors/ActionPerformer::callAccept → KILLED

2.2
Location : visit
Killed by : com.github.dakusui.actionunit.ut.VariationTest.doubleLoop(com.github.dakusui.actionunit.ut.VariationTest)
removed call to java/util/stream/Stream::forEach → KILLED

63

1.1
Location : visit
Killed by : com.github.dakusui.actionunit.ut.actions.WhileTest.givenWhileActionWithConditionNeverMet_whenPerform_thenNothingHappens(com.github.dakusui.actionunit.ut.actions.WhileTest)
negated conditional → KILLED

64

1.1
Location : visit
Killed by : none
removed call to com/github/dakusui/actionunit/visitors/ActionPerformer::callAccept → TIMED_OUT

70

1.1
Location : visit
Killed by : com.github.dakusui.actionunit.scenarios.CompatActionSupportTest.givenWhenAction$whenPerform$thenObjectAdded(com.github.dakusui.actionunit.scenarios.CompatActionSupportTest)
negated conditional → KILLED

71

1.1
Location : visit
Killed by : com.github.dakusui.actionunit.scenarios.CompatActionSupportTest.givenWhenAction$whenPerform$thenObjectAdded(com.github.dakusui.actionunit.scenarios.CompatActionSupportTest)
removed call to com/github/dakusui/actionunit/visitors/ActionPerformer::callAccept → KILLED

73

1.1
Location : visit
Killed by : com.github.dakusui.actionunit.ut.actions.WhenTest.givenOneValue$when_NotMatchingWhen_$thenWorksFine(com.github.dakusui.actionunit.ut.actions.WhenTest)
removed call to com/github/dakusui/actionunit/visitors/ActionPerformer::callAccept → KILLED

83

1.1
Location : visit
Killed by : com.github.dakusui.actionunit.ut.actions.WithTest.givenWithAction_whenPerform2(com.github.dakusui.actionunit.ut.actions.WithTest)
removed call to com/github/dakusui/actionunit/visitors/ActionPerformer::callAccept → KILLED

85

1.1
Location : lambda$visit$4
Killed by : none
removed call to com/github/dakusui/actionunit/visitors/ActionPerformer::callAccept → SURVIVED

2.2
Location : visit
Killed by : none
removed call to java/util/Optional::ifPresent → SURVIVED

94

1.1
Location : visit
Killed by : com.github.dakusui.actionunit.ut.actions.AttemptTest.givenAttemptAction$whenPerform$thenWorksFine(com.github.dakusui.actionunit.ut.actions.AttemptTest)
removed call to com/github/dakusui/actionunit/visitors/ActionPerformer::callAccept → KILLED

96

1.1
Location : visit
Killed by : com.github.dakusui.actionunit.ut.actions.AttemptTest.givenAttemptAction$whenPerform$thenWorksFine(com.github.dakusui.actionunit.ut.actions.AttemptTest)
negated conditional → KILLED

98

1.1
Location : visit
Killed by : com.github.dakusui.actionunit.ut.actions.AttemptTest.givenAttemptAction$whenPerform$thenWorksFine(com.github.dakusui.actionunit.ut.actions.AttemptTest)
removed call to com/github/dakusui/actionunit/visitors/ActionPerformer::callAccept → KILLED

104

1.1
Location : visit
Killed by : com.github.dakusui.actionunit.ut.actions.AttemptTest.givenAttemptAction$whenPerform$thenWorksFine(com.github.dakusui.actionunit.ut.actions.AttemptTest)
removed call to com/github/dakusui/actionunit/visitors/ActionPerformer::callAccept → KILLED

113

1.1
Location : visit
Killed by : com.github.dakusui.actionunit.scenarios.CompatActionSupportTest.retryTest(com.github.dakusui.actionunit.scenarios.CompatActionSupportTest)
changed conditional boundary → KILLED

2.2
Location : visit
Killed by : none
Changed increment from 1 to -1 → TIMED_OUT

3.3
Location : visit
Killed by : com.github.dakusui.actionunit.scenarios.ActionSupportTest.givenRetryWithPassingAction$whenPerform$thenNoRetry(com.github.dakusui.actionunit.scenarios.ActionSupportTest)
negated conditional → KILLED

115

1.1
Location : visit
Killed by : com.github.dakusui.actionunit.scenarios.CompatActionSupportTest.retryTest(com.github.dakusui.actionunit.scenarios.CompatActionSupportTest)
removed call to com/github/dakusui/actionunit/visitors/ActionPerformer::callAccept → KILLED

119

1.1
Location : visit
Killed by : com.github.dakusui.actionunit.scenarios.CompatActionSupportTest.retryTest$failOnce(com.github.dakusui.actionunit.scenarios.CompatActionSupportTest)
negated conditional → KILLED

121

1.1
Location : visit
Killed by : none
removed call to com/github/dakusui/actionunit/visitors/ActionPerformer::registerLastExceptionFor → SURVIVED

122

1.1
Location : visit
Killed by : none
removed call to com/github/dakusui/actionunit/utils/InternalUtils::sleep → SURVIVED

128

1.1
Location : visit
Killed by : com.github.dakusui.actionunit.scenarios.ActionSupportTest.givenRetryWithPassingAction$whenPerform$thenNoRetry(com.github.dakusui.actionunit.scenarios.ActionSupportTest)
negated conditional → KILLED

129

1.1
Location : visit
Killed by : none
removed call to com/github/dakusui/actionunit/visitors/ActionPerformer::unregisterLastExceptionFor → SURVIVED

138

1.1
Location : lambda$visit$5
Killed by : com.github.dakusui.actionunit.scenarios.CompatActionSupportTest.givenTimeOutAtTopLevel$whenErrorThrownFromInside$thenError(com.github.dakusui.actionunit.scenarios.CompatActionSupportTest)
removed call to com/github/dakusui/actionunit/visitors/ActionPerformer::callAccept → KILLED

139

1.1
Location : lambda$visit$5
Killed by : none
replaced Boolean return with False for com/github/dakusui/actionunit/visitors/ActionPerformer::lambda$visit$5 → SURVIVED

141

1.1
Location : lambda$visit$6
Killed by : none
replaced return value with "" for com/github/dakusui/actionunit/visitors/ActionPerformer::lambda$visit$6 → SURVIVED

142

1.1
Location : lambda$visit$7
Killed by : none
replaced return value with "" for com/github/dakusui/actionunit/visitors/ActionPerformer::lambda$visit$7 → SURVIVED

161

1.1
Location : ongoingExceptionsTable
Killed by : com.github.dakusui.actionunit.scenarios.CompatActionSupportTest.retryTest$failForever(com.github.dakusui.actionunit.scenarios.CompatActionSupportTest)
replaced return value with Collections.emptyMap for com/github/dakusui/actionunit/visitors/ActionPerformer::ongoingExceptionsTable → KILLED

177

1.1
Location : formatOngoingExceptions
Killed by : none
replaced return value with "" for com/github/dakusui/actionunit/visitors/ActionPerformer::formatOngoingExceptions → SURVIVED

Active mutators

Tests examined


Report generated by PIT 1.7.3