Repository navigation
fix the bug: Cannot run from src/test anymore bug #413 #232
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
1320d76
fix the bug: Cannot run from src/test anymore bug #375
andxu 00aa99b
revert unexpected changes.
andxu e69547e
revert unexpected changes.
andxu 1de4046
refactor code to jdt utils
andxu 7ff03a7
Merge branch 'master' into andy_test_cp
andxu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,8 +22,6 @@ | |
| import org.eclipse.core.runtime.CoreException; | ||
| import org.eclipse.core.runtime.IStatus; | ||
| import org.eclipse.core.runtime.Status; | ||
| import org.eclipse.jdt.core.IClasspathAttribute; | ||
| import org.eclipse.jdt.core.IClasspathEntry; | ||
| import org.eclipse.jdt.core.IJavaElement; | ||
| import org.eclipse.jdt.core.IJavaProject; | ||
| import org.eclipse.jdt.core.search.IJavaSearchConstants; | ||
|
|
@@ -40,7 +38,7 @@ | |
|
|
||
| public class ResolveClasspathsHandler { | ||
| private static final Logger logger = Logger.getLogger(Configuration.LOGGER_NAME); | ||
| private static final String SCOPE_ATTRIBUTE = "maven.scope"; | ||
|
|
||
|
|
||
| /** | ||
| * Resolves class path for a java project. | ||
|
|
@@ -150,7 +148,8 @@ private static String[][] computeClassPath(String mainClass, String projectName) | |
| } | ||
| project = projects.get(0); | ||
| } | ||
| return computeClassPath(project); | ||
|
|
||
| return computeClassPath(project, isMainClassInTestFolder(project, mainClass)); | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -162,31 +161,34 @@ private static String[][] computeClassPath(String mainClass, String projectName) | |
| * @throws CoreException | ||
| * CoreException | ||
| */ | ||
| private static String[][] computeClassPath(IJavaProject javaProject) throws CoreException { | ||
| private static String[][] computeClassPath(IJavaProject javaProject, boolean includeTestScope) | ||
| throws CoreException { | ||
| if (javaProject == null) { | ||
| throw new IllegalArgumentException("javaProject is null"); | ||
| } | ||
| String[][] result = new String[2][]; | ||
| if (JavaRuntime.isModularProject(javaProject)) { | ||
| result[0] = computeDefaultRuntimeClassPath(javaProject); | ||
| result[0] = computeDefaultRuntimeClassPath(javaProject, includeTestScope); | ||
| result[1] = new String[0]; | ||
| } else { | ||
| result[0] = new String[0]; | ||
| result[1] = computeDefaultRuntimeClassPath(javaProject); | ||
| result[1] = computeDefaultRuntimeClassPath(javaProject, includeTestScope); | ||
| } | ||
| return result; | ||
| } | ||
|
|
||
| private static String[] computeDefaultRuntimeClassPath(IJavaProject jproject) throws CoreException { | ||
| private static String[] computeDefaultRuntimeClassPath(IJavaProject jproject, boolean includeTestScope) | ||
| throws CoreException { | ||
| IRuntimeClasspathEntry[] unresolved = JavaRuntime.computeUnresolvedRuntimeClasspath(jproject); | ||
| Set<String> resolved = new LinkedHashSet<String>(); | ||
| for (int i = 0; i < unresolved.length; i++) { | ||
| IRuntimeClasspathEntry entry = unresolved[i]; | ||
| if (entry.getClasspathProperty() == IRuntimeClasspathEntry.USER_CLASSES) { | ||
| IRuntimeClasspathEntry[] entries = JavaRuntime.resolveRuntimeClasspathEntry(entry, jproject, true); | ||
| IRuntimeClasspathEntry[] entries = JavaRuntime.resolveRuntimeClasspathEntry(entry, jproject, | ||
| !includeTestScope); | ||
| for (int j = 0; j < entries.length; j++) { | ||
|
|
||
| if (entries[j].getClasspathEntry().isTest() && !isRuntime(entries[j].getClasspathEntry())) { | ||
| if (!includeTestScope && JdtUtils.isTest(entries[j].getClasspathEntry())) { | ||
| continue; | ||
| } | ||
| String location = entries[j].getLocation(); | ||
|
|
@@ -200,10 +202,43 @@ private static String[] computeDefaultRuntimeClassPath(IJavaProject jproject) th | |
| return resolved.toArray(new String[resolved.size()]); | ||
| } | ||
|
|
||
| private static boolean isRuntime(final IClasspathEntry classpathEntry) { | ||
| for (IClasspathAttribute attribute : classpathEntry.getExtraAttributes()) { | ||
| if (SCOPE_ATTRIBUTE.equals(attribute.getName()) && "runtime".equals(attribute.getValue())) { | ||
| return true; | ||
|
|
||
| /** | ||
| * Test whether the main class is located in test folders. | ||
| * @param project the java project containing the main class | ||
| * @param mainClass the main class name | ||
| * @return whether the main class is located in test folders | ||
| */ | ||
| private static boolean isMainClassInTestFolder(IJavaProject project, String mainClass) { | ||
| // get a list of test folders and check whether main class is here | ||
| int constraints = IJavaSearchScope.SOURCES; | ||
| IJavaElement[] testFolders = JdtUtils.getTestPackageFragmentRoots(project); | ||
| if (testFolders.length > 0) { | ||
| try { | ||
|
|
||
| List<Object> mainClassesInTestFolder = new ArrayList<>(); | ||
| SearchPattern pattern = SearchPattern.createPattern(mainClass, IJavaSearchConstants.CLASS, | ||
| IJavaSearchConstants.DECLARATIONS, | ||
| SearchPattern.R_CASE_SENSITIVE | SearchPattern.R_EXACT_MATCH); | ||
| SearchEngine searchEngine = new SearchEngine(); | ||
| IJavaSearchScope scope = SearchEngine.createJavaSearchScope(testFolders, constraints); | ||
| SearchRequestor requestor = new SearchRequestor() { | ||
| @Override | ||
| public void acceptSearchMatch(SearchMatch match) { | ||
| Object element = match.getElement(); | ||
| if (element instanceof IJavaElement) { | ||
| mainClassesInTestFolder.add(element); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. isFound = true; |
||
| } | ||
| } | ||
| }; | ||
|
|
||
| searchEngine.search(pattern, new SearchParticipant[] { | ||
| SearchEngine.getDefaultSearchParticipant() | ||
| }, scope, requestor, null /* progress monitor */); | ||
|
|
||
| return !mainClassesInTestFolder.isEmpty(); | ||
| } catch (Exception e) { | ||
| logger.log(Level.SEVERE, String.format("Searching the main class failure: %s", e.toString()), e); | ||
| } | ||
| } | ||
| return false; | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
boolean isFound = false;
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I cannot change the value isFound at the callback