Skip to content

Commit d13c17e

Browse files
authored
Fix NPE when customizing context (#206)
If you do not provide a MavenSystemHome when creating a Runtime, attempts to customize the context on that runtime will fail with an NPE because the customize method attempts to invoke `derive` without a null guard. Aside from customizing the context, MIMA works as expected with a null maven system home, so the simplest fix is to add a null guard. I also included a test case.
1 parent 1dfa839 commit d13c17e

File tree

4 files changed

+86
-1
lines changed

4 files changed

+86
-1
lines changed

context/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,5 +39,10 @@
3939
<artifactId>junit-jupiter-api</artifactId>
4040
<scope>test</scope>
4141
</dependency>
42+
<dependency>
43+
<groupId>org.mockito</groupId>
44+
<artifactId>mockito-core</artifactId>
45+
<scope>test</scope>
46+
</dependency>
4247
</dependencies>
4348
</project>

context/src/main/java/eu/maveniverse/maven/mima/context/internal/RuntimeSupport.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,9 @@ protected Context customizeContext(
131131
overrides,
132132
overrides.getBasedirOverride() != null ? overrides.getBasedirOverride() : context.basedir(),
133133
((MavenUserHomeImpl) context.mavenUserHome()).derive(overrides),
134-
((MavenSystemHomeImpl) context.mavenSystemHome()).derive(overrides),
134+
context.mavenSystemHome() != null
135+
? ((MavenSystemHomeImpl) context.mavenSystemHome()).derive(overrides)
136+
: null,
135137
context.repositorySystem(),
136138
session,
137139
Collections.unmodifiableList(
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package eu.maveniverse.maven.mima.context.internal;
2+
3+
import eu.maveniverse.maven.mima.context.Context;
4+
import eu.maveniverse.maven.mima.context.ContextOverrides;
5+
import eu.maveniverse.maven.mima.context.Lookup;
6+
import java.nio.file.Paths;
7+
import java.util.ArrayList;
8+
import java.util.List;
9+
import java.util.Optional;
10+
import org.eclipse.aether.DefaultRepositorySystemSession;
11+
import org.eclipse.aether.RepositorySystem;
12+
import org.eclipse.aether.RepositorySystemSession;
13+
import org.eclipse.aether.repository.RemoteRepository;
14+
import org.junit.jupiter.api.Assertions;
15+
import org.junit.jupiter.api.Test;
16+
import org.mockito.Mockito;
17+
18+
class RuntimeSupportTest {
19+
@Test
20+
void itPropagatesNullMavenSystemHome() {
21+
RuntimeSupport runtimeSupport = new RuntimeSupport("test", "123", 999, "123") {
22+
@Override
23+
public boolean managedRepositorySystem() {
24+
return false;
25+
}
26+
27+
@Override
28+
public Context create(ContextOverrides overrides) {
29+
return null;
30+
}
31+
32+
@Override
33+
protected void customizeLocalRepositoryManager(Context context, DefaultRepositorySystemSession session) {
34+
// Intentionally skipped
35+
}
36+
37+
@Override
38+
protected List<RemoteRepository> customizeRemoteRepositories(
39+
ContextOverrides contextOverrides, List<RemoteRepository> remoteRepositories) {
40+
// Intentionally skipped
41+
return remoteRepositories;
42+
}
43+
};
44+
45+
Context context = new Context(
46+
runtimeSupport,
47+
ContextOverrides.create().build(),
48+
Paths.get("/test"),
49+
runtimeSupport.defaultMavenUserHome(),
50+
null,
51+
Mockito.mock(RepositorySystem.class),
52+
Mockito.mock(RepositorySystemSession.class),
53+
new ArrayList<>(),
54+
null,
55+
new Lookup() {
56+
@Override
57+
public <T> Optional<T> lookup(Class<T> type) {
58+
return Optional.empty();
59+
}
60+
61+
@Override
62+
public <T> Optional<T> lookup(Class<T> type, String name) {
63+
return Optional.empty();
64+
}
65+
},
66+
null);
67+
68+
ContextOverrides overrides =
69+
ContextOverrides.create().offline(Boolean.TRUE).build();
70+
71+
Assertions.assertDoesNotThrow(() -> runtimeSupport.customizeContext(overrides, context, false));
72+
}
73+
}

pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,11 @@
282282
<artifactId>junit-jupiter-params</artifactId>
283283
<version>${version.junit}</version>
284284
</dependency>
285+
<dependency>
286+
<groupId>org.mockito</groupId>
287+
<artifactId>mockito-core</artifactId>
288+
<version>5.18.0</version>
289+
</dependency>
285290
</dependencies>
286291
</dependencyManagement>
287292

0 commit comments

Comments
 (0)