Skip to content

Commit 6c1bf0f

Browse files
committed
Use var
1 parent e015542 commit 6c1bf0f

File tree

87 files changed

+1392
-1503
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

87 files changed

+1392
-1503
lines changed

platform-tests/src/test/java/org/junit/jupiter/extensions/Heavyweight.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
import org.junit.jupiter.api.extension.BeforeEachCallback;
2020
import org.junit.jupiter.api.extension.ExtensionContext;
21-
import org.junit.jupiter.api.extension.ExtensionContext.Store;
2221
import org.junit.jupiter.api.extension.ExtensionContext.Store.CloseableResource;
2322
import org.junit.jupiter.api.extension.ParameterContext;
2423
import org.junit.jupiter.api.extension.ParameterResolver;
@@ -37,9 +36,9 @@ public boolean supportsParameter(ParameterContext parameterContext, ExtensionCon
3736

3837
@Override
3938
public Object resolveParameter(ParameterContext parameterContext, ExtensionContext context) {
40-
ExtensionContext engineContext = context.getRoot();
41-
Store store = engineContext.getStore(ExtensionContext.Namespace.GLOBAL);
42-
ResourceValue resource = store.getOrComputeIfAbsent(ResourceValue.class);
39+
var engineContext = context.getRoot();
40+
var store = engineContext.getStore(ExtensionContext.Namespace.GLOBAL);
41+
var resource = store.getOrComputeIfAbsent(ResourceValue.class);
4342
resource.usages.incrementAndGet();
4443
return resource;
4544
}

platform-tests/src/test/java/org/junit/platform/commons/function/TryTests.java

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -24,73 +24,73 @@ public class TryTests {
2424

2525
@Test
2626
void successfulTriesCanBeTransformed() throws Exception {
27-
Try<String> t = Try.success("foo");
27+
var success = Try.success("foo");
2828

29-
assertThat(t.get()).isEqualTo("foo");
30-
assertThat(t.getOrThrow(RuntimeException::new)).isEqualTo("foo");
31-
assertThat(t.toOptional()).contains("foo");
29+
assertThat(success.get()).isEqualTo("foo");
30+
assertThat(success.getOrThrow(RuntimeException::new)).isEqualTo("foo");
31+
assertThat(success.toOptional()).contains("foo");
3232

33-
assertThat(t.andThen(v -> {
33+
assertThat(success.andThen(v -> {
3434
assertThat(v).isEqualTo("foo");
3535
return Try.success("bar");
3636
}).get()).isEqualTo("bar");
37-
assertThat(t.andThenTry(v -> {
37+
assertThat(success.andThenTry(v -> {
3838
assertThat(v).isEqualTo("foo");
3939
return "bar";
4040
}).get()).isEqualTo("bar");
4141

42-
assertThat(t.orElse(() -> fail("should not be called"))).isSameAs(t);
43-
assertThat(t.orElseTry(() -> fail("should not be called"))).isSameAs(t);
42+
assertThat(success.orElse(() -> fail("should not be called"))).isSameAs(success);
43+
assertThat(success.orElseTry(() -> fail("should not be called"))).isSameAs(success);
4444

45-
AtomicReference<String> value = new AtomicReference<>();
46-
assertThat(t.ifSuccess(value::set)).isSameAs(t);
45+
var value = new AtomicReference<String>();
46+
assertThat(success.ifSuccess(value::set)).isSameAs(success);
4747
assertThat(value.get()).isEqualTo("foo");
48-
assertThat(t.ifFailure(cause -> fail("should not be called"))).isSameAs(t);
48+
assertThat(success.ifFailure(cause -> fail("should not be called"))).isSameAs(success);
4949
}
5050

5151
@Test
5252
void failedTriesCanBeTransformed() throws Exception {
53-
JUnitException cause = new JUnitException("foo");
54-
Try<String> t = Try.failure(cause);
53+
var cause = new JUnitException("foo");
54+
var failure = Try.failure(cause);
5555

56-
assertThat(assertThrows(JUnitException.class, t::get)).isSameAs(cause);
57-
assertThat(assertThrows(RuntimeException.class, () -> t.getOrThrow(RuntimeException::new))).isInstanceOf(
56+
assertThat(assertThrows(JUnitException.class, failure::get)).isSameAs(cause);
57+
assertThat(assertThrows(RuntimeException.class, () -> failure.getOrThrow(RuntimeException::new))).isInstanceOf(
5858
RuntimeException.class).hasCause(cause);
59-
assertThat(t.toOptional()).isEmpty();
59+
assertThat(failure.toOptional()).isEmpty();
6060

61-
assertThat(t.andThen(v -> fail("should not be called"))).isSameAs(t);
62-
assertThat(t.andThenTry(v -> fail("should not be called"))).isSameAs(t);
61+
assertThat(failure.andThen(v -> fail("should not be called"))).isSameAs(failure);
62+
assertThat(failure.andThenTry(v -> fail("should not be called"))).isSameAs(failure);
6363

64-
assertThat(t.orElse(() -> Try.success("bar")).get()).isEqualTo("bar");
65-
assertThat(t.orElseTry(() -> "bar").get()).isEqualTo("bar");
64+
assertThat(failure.orElse(() -> Try.success("bar")).get()).isEqualTo("bar");
65+
assertThat(failure.orElseTry(() -> "bar").get()).isEqualTo("bar");
6666

67-
assertThat(t.ifSuccess(v -> fail("should not be called"))).isSameAs(t);
68-
AtomicReference<Exception> exception = new AtomicReference<>();
69-
assertThat(t.ifFailure(exception::set)).isSameAs(t);
67+
assertThat(failure.ifSuccess(v -> fail("should not be called"))).isSameAs(failure);
68+
var exception = new AtomicReference<Exception>();
69+
assertThat(failure.ifFailure(exception::set)).isSameAs(failure);
7070
assertThat(exception.get()).isSameAs(cause);
7171
}
7272

7373
@Test
7474
void successfulTriesCanStoreNull() throws Exception {
75-
Try<String> t = Try.success(null);
76-
assertThat(t.get()).isNull();
77-
assertThat(t.getOrThrow(RuntimeException::new)).isNull();
78-
assertThat(t.toOptional()).isEmpty();
75+
var success = Try.success(null);
76+
assertThat(success.get()).isNull();
77+
assertThat(success.getOrThrow(RuntimeException::new)).isNull();
78+
assertThat(success.toOptional()).isEmpty();
7979
}
8080

8181
@Test
8282
void triesWithSameContentAreEqual() {
83-
Exception cause = new Exception();
83+
var cause = new Exception();
8484
Callable<Object> failingCallable = () -> {
8585
throw cause;
8686
};
8787

88-
Try<String> success = Try.call(() -> "foo");
88+
var success = Try.call(() -> "foo");
8989
assertThat(success).isEqualTo(success).hasSameHashCodeAs(success);
9090
assertThat(success).isEqualTo(Try.success("foo"));
9191
assertThat(success).isNotEqualTo(Try.failure(cause));
9292

93-
Try<Object> failure = Try.call(failingCallable);
93+
var failure = Try.call(failingCallable);
9494
assertThat(failure).isEqualTo(failure).hasSameHashCodeAs(failure);
9595
assertThat(failure).isNotEqualTo(Try.success("foo"));
9696
assertThat(failure).isEqualTo(Try.failure(cause));
@@ -100,12 +100,12 @@ void triesWithSameContentAreEqual() {
100100
void methodPreconditionsAreChecked() {
101101
assertThrows(JUnitException.class, () -> Try.call(null));
102102

103-
Try<String> success = Try.success("foo");
103+
var success = Try.success("foo");
104104
assertThrows(JUnitException.class, () -> success.andThen(null));
105105
assertThrows(JUnitException.class, () -> success.andThenTry(null));
106106
assertThrows(JUnitException.class, () -> success.ifSuccess(null));
107107

108-
Try<String> failure = Try.failure(new Exception());
108+
var failure = Try.failure(new Exception());
109109
assertThrows(JUnitException.class, () -> failure.orElse(null));
110110
assertThrows(JUnitException.class, () -> failure.orElseTry(null));
111111
assertThrows(JUnitException.class, () -> failure.ifFailure(null));

platform-tests/src/test/java/org/junit/platform/commons/support/AnnotationSupportTests.java

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import java.lang.annotation.Retention;
2121
import java.lang.annotation.RetentionPolicy;
2222
import java.lang.annotation.Target;
23-
import java.lang.reflect.Method;
2423
import java.util.Optional;
2524

2625
import org.junit.jupiter.api.Tag;
@@ -36,15 +35,15 @@ class AnnotationSupportTests {
3635

3736
@Test
3837
void isAnnotatedPreconditions() {
39-
Optional<Class<Probe>> optional = Optional.of(Probe.class);
38+
var optional = Optional.of(Probe.class);
4039
assertPreconditionViolationException("annotationType", () -> AnnotationSupport.isAnnotated(optional, null));
4140
assertPreconditionViolationException("annotationType", () -> AnnotationSupport.isAnnotated(Probe.class, null));
4241
}
4342

4443
@Test
4544
void isAnnotatedDelegates() {
46-
Class<Probe> element = Probe.class;
47-
Optional<Class<Probe>> optional = Optional.of(element);
45+
var element = Probe.class;
46+
var optional = Optional.of(element);
4847

4948
assertEquals(AnnotationUtils.isAnnotated(optional, Tag.class),
5049
AnnotationSupport.isAnnotated(optional, Tag.class));
@@ -59,16 +58,16 @@ void isAnnotatedDelegates() {
5958

6059
@Test
6160
void findAnnotationPreconditions() {
62-
Optional<Class<Probe>> optional = Optional.of(Probe.class);
61+
var optional = Optional.of(Probe.class);
6362
assertPreconditionViolationException("annotationType", () -> AnnotationSupport.findAnnotation(optional, null));
6463
assertPreconditionViolationException("annotationType",
6564
() -> AnnotationSupport.findAnnotation(Probe.class, null));
6665
}
6766

6867
@Test
6968
void findAnnotationDelegates() {
70-
Class<Probe> element = Probe.class;
71-
Optional<Class<Probe>> optional = Optional.of(element);
69+
var element = Probe.class;
70+
var optional = Optional.of(element);
7271

7372
assertEquals(AnnotationUtils.findAnnotation(optional, Tag.class),
7473
AnnotationSupport.findAnnotation(optional, Tag.class));
@@ -132,7 +131,7 @@ void findAnnotatedMethodsDelegates() {
132131

133132
@Test
134133
void findRepeatableAnnotationsDelegates() throws Throwable {
135-
Method bMethod = Probe.class.getDeclaredMethod("bMethod");
134+
var bMethod = Probe.class.getDeclaredMethod("bMethod");
136135
assertEquals(AnnotationUtils.findRepeatableAnnotations(bMethod, Tag.class),
137136
AnnotationSupport.findRepeatableAnnotations(bMethod, Tag.class));
138137
Object expected = assertThrows(PreconditionViolationException.class,
@@ -185,7 +184,7 @@ void findAnnotatedFieldsPreconditions() {
185184

186185
@Test
187186
void findAnnotatedFieldValuesForNonStaticFields() {
188-
Fields instance = new Fields();
187+
var instance = new Fields();
189188

190189
assertThat(AnnotationSupport.findAnnotatedFieldValues(instance, Tag.class)).isEmpty();
191190

@@ -203,7 +202,7 @@ void findAnnotatedFieldValuesForStaticFields() {
203202

204203
@Test
205204
void findAnnotatedFieldValuesForNonStaticFieldsByType() {
206-
Fields instance = new Fields();
205+
var instance = new Fields();
207206

208207
assertThat(AnnotationSupport.findAnnotatedFieldValues(instance, FieldMarker.class, Number.class)).isEmpty();
209208

platform-tests/src/test/java/org/junit/platform/commons/support/PreconditionAssertions.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@
2222
class PreconditionAssertions {
2323

2424
static void assertPreconditionViolationException(String name, Executable executable) {
25-
PreconditionViolationException exception = assertThrows(PreconditionViolationException.class, executable);
25+
var exception = assertThrows(PreconditionViolationException.class, executable);
2626
assertEquals(name + " must not be null", exception.getMessage());
2727
}
2828

2929
static void assertPreconditionViolationExceptionForString(String name, Executable executable) {
30-
PreconditionViolationException exception = assertThrows(PreconditionViolationException.class, executable);
30+
var exception = assertThrows(PreconditionViolationException.class, executable);
3131
assertEquals(name + " must not be null or blank", exception.getMessage());
3232
}
3333

platform-tests/src/test/java/org/junit/platform/commons/support/ReflectionSupportTests.java

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919

2020
import java.lang.reflect.Field;
2121
import java.lang.reflect.Method;
22-
import java.net.URI;
2322
import java.nio.file.Path;
2423
import java.util.ArrayList;
2524
import java.util.List;
@@ -80,9 +79,9 @@ List<DynamicTest> findAllClassesInClasspathRootDelegates() throws Throwable {
8079
List<Path> paths = new ArrayList<>();
8180
paths.add(Path.of(".").toRealPath());
8281
paths.addAll(ReflectionUtils.getAllClasspathRootDirectories());
83-
for (Path path : paths) {
84-
URI root = path.toUri();
85-
String displayName = root.getPath();
82+
for (var path : paths) {
83+
var root = path.toUri();
84+
var displayName = root.getPath();
8685
if (displayName.length() > 42) {
8786
displayName = "..." + displayName.substring(displayName.length() - 42);
8887
}
@@ -95,7 +94,7 @@ List<DynamicTest> findAllClassesInClasspathRootDelegates() throws Throwable {
9594

9695
@Test
9796
void findAllClassesInClasspathRootPreconditions() {
98-
URI path = Path.of(".").toUri();
97+
var path = Path.of(".").toUri();
9998
assertPreconditionViolationException("root",
10099
() -> ReflectionSupport.findAllClassesInClasspathRoot(null, allTypes, allNames));
101100
assertPreconditionViolationException("class predicate",
@@ -133,7 +132,7 @@ void findAllClassesInModuleDelegates() {
133132

134133
@Test
135134
void findAllClassesInModulePreconditions() {
136-
PreconditionViolationException exception = assertThrows(PreconditionViolationException.class,
135+
var exception = assertThrows(PreconditionViolationException.class,
137136
() -> ReflectionSupport.findAllClassesInModule(null, allTypes, allNames));
138137
assertEquals("Module name must not be null or empty", exception.getMessage());
139138
assertPreconditionViolationException("class predicate",
@@ -159,7 +158,7 @@ void newInstancePreconditions() {
159158

160159
@Test
161160
void invokeMethodDelegates() throws Exception {
162-
Method method = Boolean.class.getMethod("valueOf", String.class);
161+
var method = Boolean.class.getMethod("valueOf", String.class);
163162
assertEquals(ReflectionUtils.invokeMethod(method, null, "true"),
164163
ReflectionSupport.invokeMethod(method, null, "true"));
165164
}
@@ -168,8 +167,8 @@ void invokeMethodDelegates() throws Exception {
168167
void invokeMethodPreconditions() throws Exception {
169168
assertPreconditionViolationException("Method", () -> ReflectionSupport.invokeMethod(null, null, "true"));
170169

171-
Method method = Boolean.class.getMethod("toString");
172-
PreconditionViolationException exception = assertThrows(PreconditionViolationException.class,
170+
var method = Boolean.class.getMethod("toString");
171+
var exception = assertThrows(PreconditionViolationException.class,
173172
() -> ReflectionSupport.invokeMethod(method, null));
174173
assertEquals("Cannot invoke non-static method [" + method.toGenericString() + "] on a null target.",
175174
exception.getMessage());
@@ -203,11 +202,11 @@ void findFieldsPreconditions() {
203202

204203
@Test
205204
void tryToReadFieldValueDelegates() throws Exception {
206-
Field staticField = getClass().getDeclaredField("staticField");
205+
var staticField = getClass().getDeclaredField("staticField");
207206
assertEquals(ReflectionUtils.tryToReadFieldValue(staticField, null),
208207
ReflectionSupport.tryToReadFieldValue(staticField, null));
209208

210-
Field instanceField = getClass().getDeclaredField("instanceField");
209+
var instanceField = getClass().getDeclaredField("instanceField");
211210
assertEquals(ReflectionUtils.tryToReadFieldValue(instanceField, this),
212211
ReflectionSupport.tryToReadFieldValue(instanceField, this));
213212
}
@@ -216,7 +215,7 @@ void tryToReadFieldValueDelegates() throws Exception {
216215
void tryToReadFieldValuePreconditions() throws Exception {
217216
assertPreconditionViolationException("Field", () -> ReflectionSupport.tryToReadFieldValue(null, this));
218217

219-
Field instanceField = getClass().getDeclaredField("instanceField");
218+
var instanceField = getClass().getDeclaredField("instanceField");
220219
Exception exception = assertThrows(PreconditionViolationException.class,
221220
() -> ReflectionSupport.tryToReadFieldValue(instanceField, null));
222221
assertThat(exception)//

0 commit comments

Comments
 (0)