Skip to content

Commit 86c9ff5

Browse files
committed
Use enhanced switch expressions
1 parent 1f5d97e commit 86c9ff5

File tree

15 files changed

+115
-184
lines changed

15 files changed

+115
-184
lines changed

junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/descriptor/AbstractExtensionContext.java

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -221,16 +221,11 @@ public Store getStore(Namespace namespace) {
221221

222222
@Override
223223
public Store getStore(StoreScope scope, Namespace namespace) {
224-
// TODO [#4246] Use switch expression
225-
switch (scope) {
226-
case LAUNCHER_SESSION:
227-
return launcherStoreFacade.getSessionLevelStore(namespace);
228-
case EXECUTION_REQUEST:
229-
return launcherStoreFacade.getRequestLevelStore(namespace);
230-
case EXTENSION_CONTEXT:
231-
return getStore(namespace);
232-
}
233-
throw new JUnitException("Unknown StoreScope: " + scope);
224+
return switch (scope) {
225+
case LAUNCHER_SESSION -> launcherStoreFacade.getSessionLevelStore(namespace);
226+
case EXECUTION_REQUEST -> launcherStoreFacade.getRequestLevelStore(namespace);
227+
case EXTENSION_CONTEXT -> getStore(namespace);
228+
};
234229
}
235230

236231
@Override
@@ -267,12 +262,9 @@ public <E extends Extension> List<E> getExtensions(Class<E> extensionType) {
267262
protected abstract Node.ExecutionMode getPlatformExecutionMode();
268263

269264
private ExecutionMode toJupiterExecutionMode(Node.ExecutionMode mode) {
270-
switch (mode) {
271-
case CONCURRENT:
272-
return ExecutionMode.CONCURRENT;
273-
case SAME_THREAD:
274-
return ExecutionMode.SAME_THREAD;
275-
}
276-
throw new JUnitException("Unknown ExecutionMode: " + mode);
265+
return switch (mode) {
266+
case CONCURRENT -> ExecutionMode.CONCURRENT;
267+
case SAME_THREAD -> ExecutionMode.SAME_THREAD;
268+
};
277269
}
278270
}

junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/descriptor/ExclusiveResourceCollector.java

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
import org.junit.jupiter.api.parallel.ResourceLock;
2727
import org.junit.jupiter.api.parallel.ResourceLockTarget;
2828
import org.junit.jupiter.api.parallel.ResourceLocksProvider;
29-
import org.junit.platform.commons.JUnitException;
3029
import org.junit.platform.commons.util.ReflectionUtils;
3130
import org.junit.platform.commons.util.StringUtils;
3231
import org.junit.platform.engine.support.hierarchical.ExclusiveResource;
@@ -113,13 +112,10 @@ private List<ResourceLocksProvider> getProviders() {
113112
}
114113

115114
private static ExclusiveResource.LockMode toLockMode(ResourceAccessMode mode) {
116-
switch (mode) {
117-
case READ:
118-
return ExclusiveResource.LockMode.READ;
119-
case READ_WRITE:
120-
return ExclusiveResource.LockMode.READ_WRITE;
121-
}
122-
throw new JUnitException("Unknown ResourceAccessMode: " + mode);
115+
return switch (mode) {
116+
case READ -> ExclusiveResource.LockMode.READ;
117+
case READ_WRITE -> ExclusiveResource.LockMode.READ_WRITE;
118+
};
123119
}
124120
}
125121
}

junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/descriptor/JupiterTestDescriptor.java

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@
4141
import org.junit.jupiter.engine.execution.ConditionEvaluator;
4242
import org.junit.jupiter.engine.execution.JupiterEngineExecutionContext;
4343
import org.junit.jupiter.engine.extension.ExtensionRegistry;
44-
import org.junit.platform.commons.JUnitException;
4544
import org.junit.platform.commons.util.ExceptionUtils;
4645
import org.junit.platform.commons.util.UnrecoverableExceptions;
4746
import org.junit.platform.engine.DiscoveryIssue;
@@ -171,13 +170,10 @@ Optional<ExecutionMode> getExecutionModeFromAnnotation(AnnotatedElement element)
171170
}
172171

173172
public static ExecutionMode toExecutionMode(org.junit.jupiter.api.parallel.ExecutionMode mode) {
174-
switch (mode) {
175-
case CONCURRENT:
176-
return ExecutionMode.CONCURRENT;
177-
case SAME_THREAD:
178-
return ExecutionMode.SAME_THREAD;
179-
}
180-
throw new JUnitException("Unknown ExecutionMode: " + mode);
173+
return switch (mode) {
174+
case CONCURRENT -> ExecutionMode.CONCURRENT;
175+
case SAME_THREAD -> ExecutionMode.SAME_THREAD;
176+
};
181177
}
182178

183179
@Override

junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/discovery/ClassSelectorResolver.java

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -125,31 +125,29 @@ else if (isInnerClass(nestedClass) && predicates.looksLikeIntendedTestClass(nest
125125
public Resolution resolve(UniqueIdSelector selector, Context context) {
126126
UniqueId uniqueId = selector.getUniqueId();
127127
UniqueId.Segment lastSegment = uniqueId.getLastSegment();
128-
if (ClassTestDescriptor.SEGMENT_TYPE.equals(lastSegment.getType())) {
129-
return resolveStandaloneClassUniqueId(context, lastSegment, __ -> true, this::newClassTestDescriptor);
130-
}
131-
if (ClassTemplateTestDescriptor.STANDALONE_CLASS_SEGMENT_TYPE.equals(lastSegment.getType())) {
132-
return resolveStandaloneClassUniqueId(context, lastSegment, this.predicates.isAnnotatedWithClassTemplate,
133-
this::newClassTemplateTestDescriptor);
134-
}
135-
if (NestedClassTestDescriptor.SEGMENT_TYPE.equals(lastSegment.getType())) {
136-
return resolveNestedClassUniqueId(context, uniqueId, __ -> true, this::newNestedClassTestDescriptor);
137-
}
138-
if (ClassTemplateTestDescriptor.NESTED_CLASS_SEGMENT_TYPE.equals(lastSegment.getType())) {
139-
return resolveNestedClassUniqueId(context, uniqueId, this.predicates.isAnnotatedWithClassTemplate,
140-
this::newNestedClassTemplateTestDescriptor);
141-
}
142-
if (ClassTemplateInvocationTestDescriptor.SEGMENT_TYPE.equals(lastSegment.getType())) {
143-
Optional<ClassTemplateInvocationTestDescriptor> testDescriptor = context.addToParent(
144-
() -> selectUniqueId(uniqueId.removeLastSegment()), parent -> {
145-
int index = Integer.parseInt(lastSegment.getValue().substring(1));
146-
return Optional.of(newDummyClassTemplateInvocationTestDescriptor(parent, index));
147-
});
148-
return toInvocationMatch(testDescriptor) //
149-
.map(Resolution::match) //
150-
.orElse(unresolved());
151-
}
152-
return unresolved();
128+
return switch (lastSegment.getType()) {
129+
case ClassTestDescriptor.SEGMENT_TYPE -> //
130+
resolveStandaloneClassUniqueId(context, lastSegment, __ -> true, this::newClassTestDescriptor);
131+
case ClassTemplateTestDescriptor.STANDALONE_CLASS_SEGMENT_TYPE -> //
132+
resolveStandaloneClassUniqueId(context, lastSegment, this.predicates.isAnnotatedWithClassTemplate,
133+
this::newClassTemplateTestDescriptor);
134+
case NestedClassTestDescriptor.SEGMENT_TYPE -> //
135+
resolveNestedClassUniqueId(context, uniqueId, __ -> true, this::newNestedClassTestDescriptor);
136+
case ClassTemplateTestDescriptor.NESTED_CLASS_SEGMENT_TYPE -> //
137+
resolveNestedClassUniqueId(context, uniqueId, this.predicates.isAnnotatedWithClassTemplate,
138+
this::newNestedClassTemplateTestDescriptor);
139+
case ClassTemplateInvocationTestDescriptor.SEGMENT_TYPE -> {
140+
Optional<ClassTemplateInvocationTestDescriptor> testDescriptor = context.addToParent(
141+
() -> selectUniqueId(uniqueId.removeLastSegment()), parent -> {
142+
int index = Integer.parseInt(lastSegment.getValue().substring(1));
143+
return Optional.of(newDummyClassTemplateInvocationTestDescriptor(parent, index));
144+
});
145+
yield toInvocationMatch(testDescriptor) //
146+
.map(Resolution::match) //
147+
.orElse(unresolved());
148+
}
149+
default -> unresolved();
150+
};
153151
}
154152

155153
@Override

junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/extension/TimeoutDuration.java

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -76,23 +76,15 @@ public Duration toDuration() {
7676
}
7777

7878
private ChronoUnit toChronoUnit() {
79-
switch (unit) {
80-
case NANOSECONDS:
81-
return ChronoUnit.NANOS;
82-
case MICROSECONDS:
83-
return ChronoUnit.MICROS;
84-
case MILLISECONDS:
85-
return ChronoUnit.MILLIS;
86-
case SECONDS:
87-
return ChronoUnit.SECONDS;
88-
case MINUTES:
89-
return ChronoUnit.MINUTES;
90-
case HOURS:
91-
return ChronoUnit.HOURS;
92-
case DAYS:
93-
return ChronoUnit.DAYS;
94-
default:
95-
throw new JUnitException("Could not map TimeUnit " + unit + " to ChronoUnit");
96-
}
79+
return switch (unit) {
80+
case NANOSECONDS -> ChronoUnit.NANOS;
81+
case MICROSECONDS -> ChronoUnit.MICROS;
82+
case MILLISECONDS -> ChronoUnit.MILLIS;
83+
case SECONDS -> ChronoUnit.SECONDS;
84+
case MINUTES -> ChronoUnit.MINUTES;
85+
case HOURS -> ChronoUnit.HOURS;
86+
case DAYS -> ChronoUnit.DAYS;
87+
default -> throw new JUnitException("Could not map TimeUnit " + unit + " to ChronoUnit");
88+
};
9789
}
9890
}

junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/extension/TimeoutExtension.java

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -221,16 +221,12 @@ private boolean isTimeoutDisabled(ExtensionContext extensionContext) {
221221
* Determine if timeouts are disabled for the supplied mode.
222222
*/
223223
private boolean isTimeoutDisabled(String mode) {
224-
switch (mode) {
225-
case ENABLED_MODE_VALUE:
226-
return false;
227-
case DISABLED_MODE_VALUE:
228-
return true;
229-
case DISABLED_ON_DEBUG_MODE_VALUE:
230-
return RuntimeUtils.isDebugMode();
231-
default:
232-
throw new ExtensionConfigurationException("Unsupported timeout mode: " + mode);
233-
}
224+
return switch (mode) {
225+
case ENABLED_MODE_VALUE -> false;
226+
case DISABLED_MODE_VALUE -> true;
227+
case DISABLED_ON_DEBUG_MODE_VALUE -> RuntimeUtils.isDebugMode();
228+
default -> throw new ExtensionConfigurationException("Unsupported timeout mode: " + mode);
229+
};
234230
}
235231

236232
@FunctionalInterface

junit-jupiter-params/src/main/java/org/junit/jupiter/params/ParameterizedClassInvocationContext.java

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import org.junit.jupiter.api.extension.ExtensionContext;
2323
import org.junit.jupiter.params.ParameterizedClassContext.InjectionType;
2424
import org.junit.jupiter.params.provider.Arguments;
25-
import org.junit.platform.commons.JUnitException;
2625
import org.junit.platform.commons.util.Preconditions;
2726

2827
class ParameterizedClassInvocationContext extends ParameterizedInvocationContext<ParameterizedClassContext>
@@ -53,13 +52,10 @@ public void prepareInvocation(ExtensionContext context) {
5352

5453
private Extension createParameterInjector() {
5554
InjectionType injectionType = this.declarationContext.getInjectionType();
56-
switch (injectionType) {
57-
case CONSTRUCTOR:
58-
return createExtensionForConstructorInjection();
59-
case FIELDS:
60-
return createExtensionForFieldInjection();
61-
}
62-
throw new JUnitException("Unsupported injection type: " + injectionType);
55+
return switch (injectionType) {
56+
case CONSTRUCTOR -> createExtensionForConstructorInjection();
57+
case FIELDS -> createExtensionForFieldInjection();
58+
};
6359
}
6460

6561
private ClassTemplateConstructorParameterResolver createExtensionForConstructorInjection() {
@@ -72,15 +68,12 @@ private ClassTemplateConstructorParameterResolver createExtensionForConstructorI
7268
private Extension createExtensionForFieldInjection() {
7369
ResolverFacade resolverFacade = this.declarationContext.getResolverFacade();
7470
TestInstance.Lifecycle lifecycle = this.declarationContext.getTestInstanceLifecycle();
75-
switch (lifecycle) {
76-
case PER_CLASS:
77-
return new BeforeClassTemplateInvocationFieldInjector(resolverFacade, this.arguments,
78-
this.invocationIndex, this.resolutionCache);
79-
case PER_METHOD:
80-
return new InstancePostProcessingClassTemplateFieldInjector(resolverFacade, this.arguments,
81-
this.invocationIndex, this.resolutionCache);
82-
}
83-
throw new JUnitException("Unsupported lifecycle: " + lifecycle);
71+
return switch (lifecycle) {
72+
case PER_CLASS -> new BeforeClassTemplateInvocationFieldInjector(resolverFacade, this.arguments,
73+
this.invocationIndex, this.resolutionCache);
74+
case PER_METHOD -> new InstancePostProcessingClassTemplateFieldInjector(resolverFacade, this.arguments,
75+
this.invocationIndex, this.resolutionCache);
76+
};
8477
}
8578

8679
private Stream<Extension> createLifecycleMethodInvokers() {

junit-platform-console/src/main/java/org/junit/platform/console/options/Theme.java

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -116,16 +116,11 @@ public final String skipped() {
116116
}
117117

118118
public final String status(TestExecutionResult result) {
119-
switch (result.getStatus()) {
120-
case SUCCESSFUL:
121-
return successful();
122-
case ABORTED:
123-
return aborted();
124-
case FAILED:
125-
return failed();
126-
default:
127-
return result.getStatus().name();
128-
}
119+
return switch (result.getStatus()) {
120+
case SUCCESSFUL -> successful();
121+
case ABORTED -> aborted();
122+
case FAILED -> failed();
123+
};
129124
}
130125

131126
/**

junit-platform-console/src/main/java/org/junit/platform/console/tasks/ConsoleTestExecutor.java

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -180,21 +180,16 @@ private SummaryGeneratingListener registerListeners(PrintWriter out, Optional<Pa
180180
private Optional<DetailsPrintingListener> createDetailsPrintingListener(PrintWriter out) {
181181
ColorPalette colorPalette = getColorPalette();
182182
Theme theme = outputOptions.getTheme();
183-
switch (outputOptions.getDetails()) {
184-
case SUMMARY:
185-
// summary listener is always created and registered
186-
return Optional.empty();
187-
case FLAT:
188-
return Optional.of(new FlatPrintingListener(out, colorPalette));
189-
case TREE:
190-
return Optional.of(new TreePrintingListener(out, colorPalette, theme));
191-
case VERBOSE:
192-
return Optional.of(new VerboseTreePrintingListener(out, colorPalette, 16, theme));
193-
case TESTFEED:
194-
return Optional.of(new TestFeedPrintingListener(out, colorPalette));
195-
default:
196-
return Optional.empty();
197-
}
183+
return switch (outputOptions.getDetails()) {
184+
case SUMMARY ->
185+
// summary listener is always created and registered
186+
Optional.empty();
187+
case FLAT -> Optional.of(new FlatPrintingListener(out, colorPalette));
188+
case TREE -> Optional.of(new TreePrintingListener(out, colorPalette, theme));
189+
case VERBOSE -> Optional.of(new VerboseTreePrintingListener(out, colorPalette, 16, theme));
190+
case TESTFEED -> Optional.of(new TestFeedPrintingListener(out, colorPalette));
191+
case NONE -> Optional.empty();
192+
};
198193
}
199194

200195
private ColorPalette getColorPalette() {

junit-platform-console/src/main/java/org/junit/platform/console/tasks/Style.java

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,11 @@ enum Style {
2121
NONE, SUCCESSFUL, ABORTED, FAILED, SKIPPED, CONTAINER, TEST, DYNAMIC, REPORTED;
2222

2323
static Style valueOf(TestExecutionResult result) {
24-
switch (result.getStatus()) {
25-
case SUCCESSFUL:
26-
return Style.SUCCESSFUL;
27-
case ABORTED:
28-
return Style.ABORTED;
29-
case FAILED:
30-
return Style.FAILED;
31-
default:
32-
return Style.NONE;
33-
}
24+
return switch (result.getStatus()) {
25+
case SUCCESSFUL -> Style.SUCCESSFUL;
26+
case ABORTED -> Style.ABORTED;
27+
case FAILED -> Style.FAILED;
28+
};
3429
}
3530

3631
static Style valueOf(TestIdentifier testIdentifier) {

0 commit comments

Comments
 (0)