forked from eclipse-jdtls/eclipse.jdt.ls
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Issue 522 - Allow custom notifications
Fixes eclipse-jdtls#522 Signed-off-by: Victor Rubezhny <[email protected]>
- Loading branch information
Showing
3 changed files
with
218 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
95 changes: 95 additions & 0 deletions
95
org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/JavaModelNotification.java
This file contains 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 |
---|---|---|
@@ -0,0 +1,95 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2018 Red Hat Inc. and others. | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License v1.0 | ||
* which accompanies this distribution, and is available at | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
* | ||
* Contributors: | ||
* Red Hat Inc. - initial API and implementation | ||
*******************************************************************************/ | ||
package org.eclipse.jdt.ls.core.internal; | ||
|
||
import org.eclipse.lsp4j.jsonrpc.json.MessageJsonHandler; | ||
|
||
import com.google.gson.annotations.Expose; | ||
import com.google.gson.annotations.SerializedName; | ||
|
||
/** | ||
* To be used to notify a client on different kinds of changes that might happen | ||
* on Java Model | ||
* | ||
* @author Victor Rubezhny | ||
* | ||
*/ | ||
public class JavaModelNotification { | ||
|
||
/** | ||
* The notification type | ||
* | ||
*/ | ||
@SerializedName("type") | ||
@Expose | ||
String type; | ||
|
||
/** | ||
* The event value | ||
* | ||
*/ | ||
@SerializedName("event") | ||
@Expose | ||
Object event; | ||
|
||
/** | ||
* The notification type. | ||
* | ||
* @return The notification type | ||
*/ | ||
public String getType() { | ||
return type; | ||
} | ||
|
||
/** | ||
* The notification type | ||
* | ||
* @param type | ||
* The notification type | ||
*/ | ||
public void setType(String type) { | ||
this.type = type; | ||
} | ||
|
||
public JavaModelNotification withType(String type) { | ||
this.type = type; | ||
return this; | ||
} | ||
|
||
/** | ||
* The event object. | ||
* | ||
* @return The event | ||
*/ | ||
public Object getEvent() { | ||
return event; | ||
} | ||
|
||
/** | ||
* The event object. | ||
* | ||
* @param event | ||
* The event object | ||
*/ | ||
public void setSeverity(String source) { | ||
this.event = event; | ||
} | ||
|
||
public JavaModelNotification withEvent(Object event) { | ||
this.event = event; | ||
return this; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return MessageJsonHandler.toString(this); | ||
} | ||
} |
103 changes: 103 additions & 0 deletions
103
...eclipse.jdt.ls.tests/src/org/eclipse/jdt/ls/core/internal/JavaModelEventProviderTest.java
This file contains 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 |
---|---|---|
@@ -0,0 +1,103 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2017 Red Hat Inc. and others. | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License v1.0 | ||
* which accompanies this distribution, and is available at | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
* | ||
* Contributors: | ||
* Red Hat Inc. - initial API and implementation | ||
*******************************************************************************/ | ||
package org.eclipse.jdt.ls.core.internal; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertNotNull; | ||
import static org.junit.Assert.assertTrue; | ||
|
||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
import org.eclipse.jdt.core.ElementChangedEvent; | ||
import org.eclipse.jdt.core.ICompilationUnit; | ||
import org.eclipse.jdt.core.IElementChangedListener; | ||
import org.eclipse.jdt.core.IJavaProject; | ||
import org.eclipse.jdt.core.IPackageFragment; | ||
import org.eclipse.jdt.core.IPackageFragmentRoot; | ||
import org.eclipse.jdt.core.JavaCore; | ||
import org.eclipse.jdt.ls.core.internal.managers.AbstractProjectsManagerBasedTest; | ||
import org.eclipse.jdt.ls.core.internal.preferences.Preferences; | ||
import org.eclipse.jdt.ls.core.internal.preferences.Preferences.Severity; | ||
import org.junit.After; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.mockito.Mockito; | ||
import org.mockito.runners.MockitoJUnitRunner; | ||
|
||
@RunWith(MockitoJUnitRunner.class) | ||
public class JavaModelEventProviderTest extends AbstractProjectsManagerBasedTest { | ||
private JavaClientConnection javaClient; | ||
|
||
@Before | ||
public void setup() throws Exception { | ||
mockPreferences(); | ||
|
||
javaClient = new JavaClientConnection(client); | ||
} | ||
|
||
@After | ||
public void tearDown() throws Exception { | ||
javaClient.disconnect(); | ||
for (ICompilationUnit cu : JavaCore.getWorkingCopies(null)) { | ||
cu.discardWorkingCopy(); | ||
} | ||
} | ||
|
||
private Preferences mockPreferences() { | ||
Preferences mockPreferences = Mockito.mock(Preferences.class); | ||
Mockito.when(preferenceManager.getPreferences()).thenReturn(mockPreferences); | ||
Mockito.when(preferenceManager.getPreferences(Mockito.any())).thenReturn(mockPreferences); | ||
Mockito.when(mockPreferences.getIncompleteClasspathSeverity()).thenReturn(Severity.ignore); | ||
return mockPreferences; | ||
} | ||
|
||
@Test | ||
public void testSendNotification() throws Exception { | ||
JavaCore.addElementChangedListener(new IElementChangedListener() { | ||
|
||
@Override | ||
public void elementChanged(ElementChangedEvent event) { | ||
JavaModelNotification notification = new JavaModelNotification().withType("JavaModelEvent").withEvent(event); | ||
client.sendJavaModelNotification(notification); | ||
} | ||
}); | ||
IJavaProject javaProject = newDefaultProject(); | ||
IPackageFragmentRoot sourceFolder = javaProject.getPackageFragmentRoot(javaProject.getProject().getFolder("src")); | ||
IPackageFragment pack1 = sourceFolder.createPackageFragment("java", false, null); | ||
|
||
// @formatter:off | ||
String standaloneFileContent = | ||
"package java;\n"+ | ||
"public class Foo extends UnknownType {"+ | ||
" public void method1(){\n"+ | ||
" super.whatever();"+ | ||
" }\n"+ | ||
"}"; | ||
// @formatter:on | ||
ICompilationUnit cu1 = pack1.createCompilationUnit("Foo.java", standaloneFileContent, false, null); | ||
|
||
List<JavaModelNotification> eventReports = getClientRequests("sendJavaModelNotification"); | ||
assertEquals(7, eventReports.size()); | ||
|
||
for (JavaModelNotification notification : eventReports) { | ||
assertNotNull(notification); | ||
assertTrue(notification.getEvent() instanceof ElementChangedEvent); | ||
} | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
private <T> List<T> getClientRequests(String name) { | ||
List<?> requests = clientRequests.get(name); | ||
return requests != null ? (List<T>) requests : Collections.emptyList(); | ||
} | ||
} |