Debian Codegraph @

junixsocket-rmi/src/test/java/org/newsclub/net/unix/rmi/AFSocketCapabilityCondition.java

raw
1 /*
2 * junixsocket
3 *
4 * Copyright 2009-2022 Christian Kohlschütter
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18 package org.newsclub.net.unix.rmi;
19
20 import java.lang.reflect.AnnotatedElement;
21 import java.util.ArrayList;
22 import java.util.Arrays;
23 import java.util.List;
24 import java.util.Optional;
25
26 import org.junit.jupiter.api.extension.ConditionEvaluationResult;
27 import org.junit.jupiter.api.extension.ExecutionCondition;
28 import org.junit.jupiter.api.extension.ExtensionContext;
29 import org.newsclub.net.unix.AFSocket;
30 import org.newsclub.net.unix.AFSocketCapability;
31
32 // FIXME: Move this class into a unit-testing helper artifact.
33 // This is a deliberate copy of the same class from junixsocket-common's tests.
34 // CPD-OFF
35 public class AFSocketCapabilityCondition implements ExecutionCondition {
36 @SuppressWarnings("exports")
37 @Override
38 public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext context) {
39 AFSocketCapability[] requiredCapabilities = {};
40
41 Optional<AnnotatedElement> element = context.getElement();
42 if (element.isPresent()) {
43 @SuppressWarnings("null")
44 AFSocketCapabilityRequirement requirement = element.get().getAnnotation(
45 AFSocketCapabilityRequirement.class);
46 if (requirement != null) {
47 requiredCapabilities = requirement.value();
48 }
49 }
50
51 List<AFSocketCapability> unsupported = new ArrayList<>(requiredCapabilities.length);
52 for (AFSocketCapability capability : requiredCapabilities) {
53 if (!AFSocket.supports(capability)) {
54 unsupported.add(capability);
55 }
56 }
57
58 ConditionEvaluationResult result;
59 if (unsupported.isEmpty()) {
60 result = ConditionEvaluationResult.enabled("All capability requirements met: " + Arrays
61 .toString(requiredCapabilities));
62 } else {
63 result = ConditionEvaluationResult.disabled("Missing capabilities: " + unsupported);
64 }
65
66 return result;
67 }
68 }