SubscriberInteraction.java

1
package com.jsql.view.interaction;
2
3
import com.jsql.model.bean.util.Interaction;
4
import com.jsql.model.bean.util.Request;
5
import com.jsql.util.LogLevelUtil;
6
import org.apache.logging.log4j.LogManager;
7
import org.apache.logging.log4j.Logger;
8
9
import javax.swing.*;
10
import java.lang.reflect.Constructor;
11
import java.lang.reflect.InvocationTargetException;
12
import java.util.concurrent.Flow.Subscriber;
13
import java.util.concurrent.Flow.Subscription;
14
15
public class SubscriberInteraction implements Subscriber<Request> {
16
17
    private static final Logger LOGGER = LogManager.getRootLogger();
18
    
19
    private final String packageInteraction;
20
21
    /**
22
     * Observer pattern.<br>
23
     * Receive an update order from the model:<br>
24
     * - Use the Request message to get the Interaction class,<br>
25
     * - Pass the parameters to that class.
26
     */
27
    private Subscription subscription;
28
    
29
    public SubscriberInteraction(String packageInteraction) {
30
        this.packageInteraction = packageInteraction;
31
    }
32
    
33
    @Override
34
    public void onSubscribe(Subscription subscription) {
35
        this.subscription = subscription;
36 1 1. onSubscribe : removed call to java/util/concurrent/Flow$Subscription::request → NO_COVERAGE
        subscription.request(1);
37
    }
38
39
    @Override
40
    public void onNext(Request request) {
41 1 1. onNext : removed call to java/util/concurrent/Flow$Subscription::request → NO_COVERAGE
        this.subscription.request(1);
42 1 1. onNext : negated conditional → NO_COVERAGE
        if (Interaction.UNSUBSCRIBE.equals(request.getMessage())) {
43 1 1. onNext : removed call to java/util/concurrent/Flow$Subscription::cancel → NO_COVERAGE
            this.subscription.cancel();
44
            return;
45
        }
46
        
47
        // Display model thread name in logs instead of the observer name
48
        String nameThread = Thread.currentThread().getName();
49
        
50 1 1. onNext : removed call to javax/swing/SwingUtilities::invokeLater → NO_COVERAGE
        SwingUtilities.invokeLater(() -> {
51 1 1. lambda$onNext$0 : removed call to java/lang/Thread::setName → NO_COVERAGE
            Thread.currentThread().setName("from " + nameThread);
52
            try {
53
                Class<?> cl = Class.forName(this.packageInteraction +"."+ request.getMessage());
54
                var types = new Class[]{ Object[].class };
55
                Constructor<?> constructor = cl.getConstructor(types);
56
                @SuppressWarnings({"java:S1905", "java:S3878"})  // rules opposite by intellij and sonar
57
                InteractionCommand interactionCommand = (InteractionCommand) constructor.newInstance(
58
                    new Object[] {  request.getParameters() }
59
                );
60 1 1. lambda$onNext$0 : removed call to com/jsql/view/interaction/InteractionCommand::execute → NO_COVERAGE
                interactionCommand.execute();
61
            } catch (ClassNotFoundException e) {
62
                LOGGER.log(LogLevelUtil.IGNORE, e);  // Ignore unused interaction message
63
            } catch (
64
                InstantiationException
65
                | IllegalAccessException
66
                | NoSuchMethodException
67
                | SecurityException
68
                | IllegalArgumentException
69
                | InvocationTargetException
70
                e
71
            ) {
72
                LOGGER.log(LogLevelUtil.CONSOLE_JAVA, e, e);
73
            }
74
        });
75
    }
76
77
    @Override
78
    public void onError(Throwable e) {
79
        LOGGER.log(LogLevelUtil.CONSOLE_JAVA, e, e);
80
    }
81
82
    @Override
83
    public void onComplete() {
84
        // Nothing
85
    }
86
}

Mutations

36

1.1
Location : onSubscribe
Killed by : none
removed call to java/util/concurrent/Flow$Subscription::request → NO_COVERAGE

41

1.1
Location : onNext
Killed by : none
removed call to java/util/concurrent/Flow$Subscription::request → NO_COVERAGE

42

1.1
Location : onNext
Killed by : none
negated conditional → NO_COVERAGE

43

1.1
Location : onNext
Killed by : none
removed call to java/util/concurrent/Flow$Subscription::cancel → NO_COVERAGE

50

1.1
Location : onNext
Killed by : none
removed call to javax/swing/SwingUtilities::invokeLater → NO_COVERAGE

51

1.1
Location : lambda$onNext$0
Killed by : none
removed call to java/lang/Thread::setName → NO_COVERAGE

60

1.1
Location : lambda$onNext$0
Killed by : none
removed call to com/jsql/view/interaction/InteractionCommand::execute → NO_COVERAGE

Active mutators

Tests examined


Report generated by PIT 1.19.1