Skip to content

Commit 69aaa5c

Browse files
authored
fix: NPE when get test source paths (#1622)
Signed-off-by: Sheng Chen <sheche@microsoft.com>
1 parent 26188a9 commit 69aaa5c

File tree

2 files changed

+16
-7
lines changed

2 files changed

+16
-7
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ hs_err_pid*
3131
.DS_Store
3232
target/
3333
.settings
34+
test/**/.classpath
35+
test/**/.project
3436
**/.checkstyle
3537
target/
3638
bin/

java-extension/com.microsoft.java.test.plugin/src/main/java/com/microsoft/java/test/plugin/util/ProjectTestUtils.java

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2017-2021 Microsoft Corporation and others.
2+
* Copyright (c) 2017-2023 Microsoft Corporation and others.
33
* All rights reserved. This program and the accompanying materials
44
* are made available under the terms of the Eclipse Public License v1.0
55
* which accompanies this distribution, and is available at
@@ -11,6 +11,7 @@
1111

1212
package com.microsoft.java.test.plugin.util;
1313

14+
import org.eclipse.core.resources.IProject;
1415
import org.eclipse.core.runtime.IPath;
1516
import org.eclipse.core.runtime.IProgressMonitor;
1617
import org.eclipse.jdt.core.IClasspathAttribute;
@@ -111,13 +112,13 @@ public static List<TestSourcePath> getTestSourcePaths(IJavaProject project) thro
111112
}
112113

113114
if (isTestEntry(entry)) {
114-
paths.add(new TestSourcePath(parseTestSourcePathString(entry, project), true));
115+
addTestSourcePath(paths, project.getProject(), entry, true);
115116
continue;
116117
}
117118

118119
// Always return true Eclipse & invisible project
119120
if (ProjectUtils.isGeneralJavaProject(project.getProject())) {
120-
paths.add(new TestSourcePath(parseTestSourcePathString(entry, project), false));
121+
addTestSourcePath(paths, project.getProject(), entry, false);
121122
}
122123
}
123124
return paths;
@@ -134,14 +135,20 @@ public static Set<IJavaProject> parseProjects(String uriStr) {
134135
.collect(Collectors.toSet());
135136
}
136137

137-
private static String parseTestSourcePathString(IClasspathEntry entry, IJavaProject project) {
138-
final IPath relativePath = entry.getPath().makeRelativeTo(project.getPath());
138+
private static void addTestSourcePath(List<TestSourcePath> paths, IProject project,
139+
IClasspathEntry entry, boolean isStrict) {
140+
final IPath relativePath = entry.getPath().makeRelativeTo(project.getFullPath());
141+
final IPath testSourcePath;
139142
// Eclipse project that source files stored directly at project root
140143
if (relativePath.isEmpty()) {
141-
return project.getProject().getLocation().toOSString();
144+
testSourcePath = project.getLocation();
145+
} else {
146+
testSourcePath = project.getFolder(relativePath).getLocation();
142147
}
143148

144-
return project.getProject().getFolder(relativePath).getLocation().toOSString();
149+
if (testSourcePath != null) {
150+
paths.add(new TestSourcePath(testSourcePath.toOSString(), isStrict));
151+
}
145152
}
146153

147154
public static boolean isTest(IJavaProject project, IPath path, boolean containsGeneral) {

0 commit comments

Comments
 (0)