|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (c) 2018 Red Hat Inc. and others. |
| 3 | + * All rights reserved. This program and the accompanying materials |
| 4 | + * are made available under the terms of the Eclipse Public License v1.0 |
| 5 | + * which accompanies this distribution, and is available at |
| 6 | + * http://www.eclipse.org/legal/epl-v10.html |
| 7 | + * |
| 8 | + * Contributors: |
| 9 | + * Red Hat Inc. - initial API and implementation |
| 10 | + *******************************************************************************/ |
| 11 | +package org.eclipse.jdt.ls.core.internal; |
| 12 | + |
| 13 | +import static org.junit.Assert.assertEquals; |
| 14 | +import static org.junit.Assert.assertNotNull; |
| 15 | + |
| 16 | +import java.util.Collections; |
| 17 | +import java.util.List; |
| 18 | + |
| 19 | +import org.eclipse.jdt.core.ElementChangedEvent; |
| 20 | +import org.eclipse.jdt.core.ICompilationUnit; |
| 21 | +import org.eclipse.jdt.core.IElementChangedListener; |
| 22 | +import org.eclipse.jdt.core.IJavaProject; |
| 23 | +import org.eclipse.jdt.core.IPackageFragment; |
| 24 | +import org.eclipse.jdt.core.IPackageFragmentRoot; |
| 25 | +import org.eclipse.jdt.core.JavaCore; |
| 26 | +import org.eclipse.jdt.ls.core.internal.managers.AbstractProjectsManagerBasedTest; |
| 27 | +import org.eclipse.jdt.ls.core.internal.preferences.Preferences; |
| 28 | +import org.eclipse.jdt.ls.core.internal.preferences.Preferences.Severity; |
| 29 | +import org.junit.After; |
| 30 | +import org.junit.Before; |
| 31 | +import org.junit.Test; |
| 32 | +import org.junit.runner.RunWith; |
| 33 | +import org.mockito.Mockito; |
| 34 | +import org.mockito.runners.MockitoJUnitRunner; |
| 35 | + |
| 36 | +@RunWith(MockitoJUnitRunner.class) |
| 37 | +public class ServerToClientNotificationTest extends AbstractProjectsManagerBasedTest { |
| 38 | + private JavaClientConnection javaClient; |
| 39 | + |
| 40 | + @Before |
| 41 | + public void setup() throws Exception { |
| 42 | + mockPreferences(); |
| 43 | + |
| 44 | + javaClient = new JavaClientConnection(client); |
| 45 | + } |
| 46 | + |
| 47 | + @After |
| 48 | + public void tearDown() throws Exception { |
| 49 | + javaClient.disconnect(); |
| 50 | + for (ICompilationUnit cu : JavaCore.getWorkingCopies(null)) { |
| 51 | + cu.discardWorkingCopy(); |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + private Preferences mockPreferences() { |
| 56 | + Preferences mockPreferences = Mockito.mock(Preferences.class); |
| 57 | + Mockito.when(preferenceManager.getPreferences()).thenReturn(mockPreferences); |
| 58 | + Mockito.when(preferenceManager.getPreferences(Mockito.any())).thenReturn(mockPreferences); |
| 59 | + Mockito.when(mockPreferences.getIncompleteClasspathSeverity()).thenReturn(Severity.ignore); |
| 60 | + return mockPreferences; |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + public void testSendNotification() throws Exception { |
| 65 | + IElementChangedListener listener = new IElementChangedListener() { |
| 66 | + |
| 67 | + @Override |
| 68 | + public void elementChanged(ElementChangedEvent event) { |
| 69 | + // This should sent Java Model events to a client |
| 70 | + javaClient.notify("notify", event); |
| 71 | + } |
| 72 | + }; |
| 73 | + try { |
| 74 | + JavaCore.addElementChangedListener(listener); |
| 75 | + IJavaProject javaProject = newDefaultProject(); |
| 76 | + IPackageFragmentRoot sourceFolder = javaProject.getPackageFragmentRoot(javaProject.getProject().getFolder("src")); |
| 77 | + IPackageFragment pack1 = sourceFolder.createPackageFragment("java", false, null); |
| 78 | + |
| 79 | + // @formatter:off |
| 80 | + String standaloneFileContent = |
| 81 | + "package java;\n"+ |
| 82 | + "public class Foo extends UnknownType {"+ |
| 83 | + " public void method1(){\n"+ |
| 84 | + " super.whatever();"+ |
| 85 | + " }\n"+ |
| 86 | + "}"; |
| 87 | + // @formatter:on |
| 88 | + ICompilationUnit cu1 = pack1.createCompilationUnit("Foo.java", standaloneFileContent, false, null); |
| 89 | + |
| 90 | + // Check if all the events are received and events aren't nulls |
| 91 | + List<ElementChangedEvent> eventReports = getClientRequests("notify"); |
| 92 | + assertEquals(7, eventReports.size()); |
| 93 | + |
| 94 | + for (ElementChangedEvent notification : eventReports) { |
| 95 | + assertNotNull(notification); |
| 96 | + } |
| 97 | + } finally { |
| 98 | + JavaCore.removeElementChangedListener(listener); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + @SuppressWarnings("unchecked") |
| 103 | + private <T> List<T> getClientRequests(String name) { |
| 104 | + List<?> requests = clientRequests.get(name); |
| 105 | + return requests != null ? (List<T>) requests : Collections.emptyList(); |
| 106 | + } |
| 107 | +} |
0 commit comments