Skip to content

Commit

Permalink
Lower severity of m2e's 'Project configuration is not up-to-date...' …
Browse files Browse the repository at this point in the history
…diagnostics

Signed-off-by: Snjezana Peco <[email protected]>
  • Loading branch information
snjeza authored and fbricon committed Aug 29, 2018
1 parent 44d351e commit 29dc698
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import java.net.PasswordAuthentication;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Hashtable;
import java.util.concurrent.Executors;

import org.apache.commons.lang3.StringUtils;
Expand All @@ -38,7 +37,6 @@
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.preferences.DefaultScope;
import org.eclipse.core.runtime.preferences.IEclipsePreferences;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.WorkingCopyOwner;
import org.eclipse.jdt.core.manipulation.JavaManipulation;
import org.eclipse.jdt.internal.core.manipulation.JavaManipulationPlugin;
Expand Down Expand Up @@ -137,7 +135,6 @@ public void start(BundleContext bundleContext) throws Exception {
preferenceCache.install();

preferenceManager = new PreferenceManager();
initializeJDTOptions();
digestStore = new DigestStore(getStateLocation().toFile());
projectsManager = new ProjectsManager(preferenceManager);
try {
Expand Down Expand Up @@ -327,17 +324,6 @@ static void startLanguageServer(LanguageServer newLanguageServer) throws IOExcep
}
}

/**
* Initialize default preference values of used bundles to match server
* functionality.
*/
private void initializeJDTOptions() {
// Update JavaCore options
Hashtable<String, String> javaCoreOptions = JavaCore.getOptions();
javaCoreOptions.put(JavaCore.CODEASSIST_VISIBILITY_CHECK, JavaCore.ENABLED);
JavaCore.setOptions(javaCoreOptions);
}

/**
* @return
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,13 @@ private static Diagnostic toDiagnostic(Range range, IMarker marker) {
}
Diagnostic d = new Diagnostic();
d.setSource(JavaLanguageServerPlugin.SERVER_SOURCE_ID);
d.setMessage(marker.getAttribute(IMarker.MESSAGE, ""));
d.setCode(String.valueOf(marker.getAttribute(IJavaModelMarker.ID, 0)));
String message = marker.getAttribute(IMarker.MESSAGE, "");
if (message != null && message.startsWith("Project configuration is not up-to-date with pom.xml.")) {
message = "Project configuration is not up-to-date with pom.xml, requires an update.";
}
d.setMessage(message);
d.setSeverity(convertSeverity(marker.getAttribute(IMarker.SEVERITY, -1)));
d.setCode(String.valueOf(marker.getAttribute(IJavaModelMarker.ID, 0)));
d.setRange(range);
return d;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@
import org.eclipse.lsp4j.ClientCapabilities;
import org.eclipse.m2e.core.MavenPlugin;
import org.eclipse.m2e.core.embedder.IMavenConfiguration;
import org.eclipse.m2e.core.internal.IMavenConstants;
import org.eclipse.m2e.core.internal.preferences.MavenPreferenceConstants;
import org.eclipse.m2e.core.internal.preferences.ProblemSeverity;

/**
* Preference manager
Expand Down Expand Up @@ -83,6 +86,12 @@ public static void initialize() {
if (m2eAptPrefs != null) {
m2eAptPrefs.put(M2E_APT_ID + ".mode", "jdt_apt");
}
initializeMavenPreferences();
}

private static void initializeMavenPreferences() {
IEclipsePreferences store = DefaultScope.INSTANCE.getNode(IMavenConstants.PLUGIN_ID);
store.put(MavenPreferenceConstants.P_OUT_OF_DATE_PROJECT_CONFIG_PB, ProblemSeverity.warning.toString());
}

public void update(Preferences preferences) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,10 @@
import java.util.List;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.resources.IWorkspaceDescription;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.OperationCanceledException;

/**
* @author Fred Bricon
Expand Down Expand Up @@ -61,7 +58,7 @@ public static void delete(IProject project) {
try {
project.delete(true, new NullProgressMonitor());
} catch (CoreException e) {
e.printStackTrace();
JavaLanguageServerPlugin.logException(e.getMessage(), e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,21 @@
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;

import org.apache.commons.io.IOUtils;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IMarker;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IncrementalProjectBuilder;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.jdt.ls.core.internal.JavaClientConnection;
import org.eclipse.jdt.ls.core.internal.managers.AbstractProjectsManagerBasedTest;
import org.eclipse.jface.text.IDocument;
Expand Down Expand Up @@ -107,7 +116,6 @@ public void testTaskMarkers() throws Exception {
initHandler.addWorkspaceDiagnosticsHandler();
//import project
importProjects("eclipse/hello");

ArgumentCaptor<PublishDiagnosticsParams> captor = ArgumentCaptor.forClass(PublishDiagnosticsParams.class);
verify(connection, atLeastOnce()).publishDiagnostics(captor.capture());

Expand Down Expand Up @@ -236,6 +244,47 @@ public void testProjectLevelMarkers() throws Exception {
assertTrue(diags.get(4).getMessage().startsWith("The compiler compliance specified is 1.7 but a JRE 1.8 is used"));
}

@Test
public void testProjectConfigurationIsNotUpToDate() throws Exception {
InitHandler initHandler = new InitHandler(projectsManager, preferenceManager, connection);
initHandler.addWorkspaceDiagnosticsHandler();
//import project
importProjects("maven/salut");
IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject("salut");
IFile pom = project.getFile("/pom.xml");
assertTrue(pom.exists());
try (InputStream is = pom.getContents(); InputStream nis = new ByteArrayInputStream(change(is).getBytes())) {
pom.setContents(nis, IResource.FORCE, null);
}
ResourcesPlugin.getWorkspace().build(IncrementalProjectBuilder.FULL_BUILD, monitor);
ArgumentCaptor<PublishDiagnosticsParams> captor = ArgumentCaptor.forClass(PublishDiagnosticsParams.class);
verify(connection, atLeastOnce()).publishDiagnostics(captor.capture());
List<PublishDiagnosticsParams> allCalls = captor.getAllValues();
Collections.reverse(allCalls);
projectsManager.setConnection(client);
Optional<PublishDiagnosticsParams> projectDiags = allCalls.stream().filter(p -> p.getUri().endsWith("maven/salut")).findFirst();
assertTrue("No maven/salut errors were found", projectDiags.isPresent());
List<Diagnostic> diags = projectDiags.get().getDiagnostics();
Comparator<Diagnostic> comparator = (Diagnostic d1, Diagnostic d2) -> {
int diff = d1.getRange().getStart().getLine() - d2.getRange().getStart().getLine();
if (diff == 0) {
diff = d1.getMessage().compareTo(d2.getMessage());
}
return diff;
};
Collections.sort(diags, comparator);
assertEquals(diags.toString(), 3, diags.size());
Diagnostic diag = diags.get(1);
assertTrue(diag.getMessage().startsWith("Project configuration is not up-to-date with pom.xml, requires an update."));
assertEquals(diag.getSeverity(), DiagnosticSeverity.Warning);
}

private String change(InputStream is) throws IOException {
String str = IOUtils.toString(is);
String newStr = str.replaceAll("1.7", "1.8");
return newStr;
}

private IMarker createMarker(int severity, String msg, int line, int start, int end) {
IMarker m = mock(IMarker.class);
when(m.exists()).thenReturn(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ public void cleanUp() throws Exception {
WorkspaceHelper.deleteAllProjects();
FileUtils.forceDelete(getWorkingProjectDirectory());
Job.getJobManager().setProgressProvider(null);
JobHelpers.waitForJobsToComplete();
}

protected void assertIsJavaProject(IProject project) {
Expand Down

0 comments on commit 29dc698

Please sign in to comment.