Skip to content

Commit 2fe724c

Browse files
committed
GH-1438 - Refactorings to ease implementation.
The creation of a Changes instances is now implemented in a type solely depending on an Environment to avoid a dependency to the JUnit API which – ironically – seems a bit cumbersome to test against.
1 parent 3169f8b commit 2fe724c

File tree

3 files changed

+116
-24
lines changed

3 files changed

+116
-24
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* Copyright 2025 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.modulith.junit;
17+
18+
import org.slf4j.Logger;
19+
import org.slf4j.LoggerFactory;
20+
import org.springframework.core.env.Environment;
21+
import org.springframework.modulith.junit.diff.FileModificationDetector;
22+
import org.springframework.util.Assert;
23+
24+
/**
25+
* Dedicated factory to create a {@link Changes} instance from an {@link Environment}.
26+
*
27+
* @author Oliver Drotbohm
28+
* @since 2.1
29+
*/
30+
class ChangesFactory {
31+
32+
private static final Logger log = LoggerFactory.getLogger(StateStore.class);
33+
34+
/**
35+
* Creates a new {@link Changes} instance from an {@link Environment}.
36+
*
37+
* @param environment will never be {@literal null}.
38+
* @return will never be {@literal null}.
39+
*/
40+
static Changes getChanges(Environment environment) {
41+
42+
Assert.notNull(environment, "Environment must not be null!");
43+
44+
if (Boolean.TRUE == environment.getProperty("spring.modulith.test.skip-optimizations", Boolean.class)) {
45+
return Changes.NONE;
46+
}
47+
48+
// Determine detector
49+
var detector = FileModificationDetector.getDetector(environment);
50+
var result = Changes.of(detector.getModifiedFiles());
51+
52+
if (log.isInfoEnabled()) {
53+
54+
log.trace("Detected changes:");
55+
log.trace("-----------------");
56+
57+
result.forEach(it -> log.info(it.toString()));
58+
}
59+
60+
// Obtain changes
61+
return result;
62+
}
63+
}

spring-modulith-junit/src/main/java/org/springframework/modulith/junit/StateStore.java

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,8 @@
1818
import org.junit.jupiter.api.extension.ExtensionContext;
1919
import org.junit.jupiter.api.extension.ExtensionContext.Namespace;
2020
import org.junit.jupiter.api.extension.ExtensionContext.Store;
21-
import org.slf4j.Logger;
22-
import org.slf4j.LoggerFactory;
2321
import org.springframework.boot.context.config.ConfigDataEnvironmentPostProcessor;
2422
import org.springframework.core.env.StandardEnvironment;
25-
import org.springframework.modulith.junit.diff.FileModificationDetector;
2623
import org.springframework.util.Assert;
2724

2825
/**
@@ -32,8 +29,6 @@
3229
*/
3330
class StateStore {
3431

35-
private static final Logger log = LoggerFactory.getLogger(StateStore.class);
36-
3732
private final Store store;
3833

3934
/**
@@ -55,30 +50,13 @@ class StateStore {
5550
*/
5651
Changes getChanges() {
5752

58-
return (Changes) store.getOrComputeIfAbsent("changed-files", s -> {
53+
return (Changes) store.computeIfAbsent("changed-files", __ -> {
5954

6055
// Lookup configuration
6156
var environment = new StandardEnvironment();
6257
ConfigDataEnvironmentPostProcessor.applyTo(environment);
6358

64-
if (Boolean.TRUE == environment.getProperty("spring.modulith.test.skip-optimizations", Boolean.class)) {
65-
return Changes.NONE;
66-
}
67-
68-
// Determine detector
69-
var detector = FileModificationDetector.getDetector(environment);
70-
var result = Changes.of(detector.getModifiedFiles());
71-
72-
if (log.isInfoEnabled()) {
73-
74-
log.trace("Detected changes:");
75-
log.trace("-----------------");
76-
77-
result.forEach(it -> log.info(it.toString()));
78-
}
79-
80-
// Obtain changes
81-
return Changes.of(detector.getModifiedFiles());
59+
return ChangesFactory.getChanges(environment);
8260
});
8361
}
8462
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Copyright 2025 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.modulith.junit;
17+
18+
import static org.assertj.core.api.Assertions.*;
19+
20+
import java.util.Map;
21+
22+
import org.junit.jupiter.api.Test;
23+
import org.springframework.core.env.Environment;
24+
import org.springframework.core.env.MapPropertySource;
25+
import org.springframework.core.env.StandardEnvironment;
26+
27+
/**
28+
* Unit tests for {@link ChangesFactory}.
29+
*
30+
* @author Oliver Drotbohm
31+
*/
32+
class ChangesFactoryUnitTests {
33+
34+
@Test // GH-31
35+
void returnsNoChangesIfDisabledByProperty() {
36+
37+
var environment = getEnvironment(Map.of("spring.modulith.test.skip-optimizations", "true"));
38+
39+
assertThat(ChangesFactory.getChanges(environment)).isEqualTo(Changes.NONE);
40+
}
41+
42+
private Environment getEnvironment(Map<String, Object> properties) {
43+
44+
var propertySource = new MapPropertySource("properties", properties);
45+
46+
var environment = new StandardEnvironment();
47+
environment.getPropertySources().addFirst(propertySource);
48+
49+
return environment;
50+
}
51+
}

0 commit comments

Comments
 (0)