use of org.junit.Assert.assertNotNull in project eclipse.jdt.ls by eclipse.
the class CodeActionResolveHandlerTest method testAssignAllParamsToFields.
@Test
public void testAssignAllParamsToFields() throws Exception {
when(preferenceManager.getClientPreferences().isResolveCodeActionSupported()).thenReturn(true);
StringBuilder buf = new StringBuilder();
buf.append("public class App {\n");
buf.append(" private String s;\n");
buf.append("\n");
buf.append(" public App(String s, String s3, String s2) {\n");
buf.append(" }\n");
buf.append("}\n");
ICompilationUnit unit = defaultPackage.createCompilationUnit("App.java", buf.toString(), false, null);
CodeActionParams params = new CodeActionParams();
params.setTextDocument(new TextDocumentIdentifier(JDTUtils.toURI(unit)));
Range range = CodeActionUtil.getRange(unit, "s3");
params.setRange(range);
CodeActionContext context = new CodeActionContext(Collections.emptyList(), Collections.singletonList(JavaCodeActionKind.QUICK_ASSIST));
params.setContext(context);
List<Either<Command, CodeAction>> codeActions = server.codeAction(params).join();
Assert.assertNotNull(codeActions);
Assert.assertFalse("No quickassist actions were found", codeActions.isEmpty());
Optional<Either<Command, CodeAction>> assignAllToNewFieldsResponse = codeActions.stream().filter(codeAction -> {
return "Assign all parameters to new fields".equals(codeAction.getRight().getTitle());
}).findFirst();
Assert.assertTrue("Should return the quick assist 'Assign all parameters to new fields'", assignAllToNewFieldsResponse.isPresent());
CodeAction unresolvedCodeAction = assignAllToNewFieldsResponse.get().getRight();
CodeAction resolvedCodeAction = server.resolveCodeAction(unresolvedCodeAction).join();
Assert.assertNotNull("Should resolve the edit property in the resolveCodeAction request", resolvedCodeAction.getEdit());
String actual = AbstractQuickFixTest.evaluateWorkspaceEdit(resolvedCodeAction.getEdit());
buf = new StringBuilder();
buf.append("public class App {\n");
buf.append(" private String s;\n");
buf.append(" private String s4;\n");
buf.append(" private String s3;\n");
buf.append(" private String s2;\n");
buf.append("\n");
buf.append(" public App(String s, String s3, String s2) {\n");
buf.append(" s4 = s;\n");
buf.append(" this.s3 = s3;\n");
buf.append(" this.s2 = s2;\n");
buf.append(" }\n");
buf.append("}\n");
Assert.assertEquals(buf.toString(), actual);
}
use of org.junit.Assert.assertNotNull in project eclipse.jdt.ls by eclipse.
the class CodeActionResolveHandlerTest method testResolveCodeAction_SourceActions.
@Test
public void testResolveCodeAction_SourceActions() throws Exception {
when(preferenceManager.getClientPreferences().isResolveCodeActionSupported()).thenReturn(true);
StringBuilder buf = new StringBuilder();
buf.append("public class E {\n");
buf.append(" private void hello() {\n");
buf.append(" }\n");
buf.append("}\n");
ICompilationUnit unit = defaultPackage.createCompilationUnit("E.java", buf.toString(), false, null);
CodeActionParams params = new CodeActionParams();
params.setTextDocument(new TextDocumentIdentifier(JDTUtils.toURI(unit)));
final Range range = CodeActionUtil.getRange(unit, "hello");
params.setRange(range);
CodeActionContext context = new CodeActionContext(Collections.emptyList(), Collections.singletonList(CodeActionKind.Source));
params.setContext(context);
List<Either<Command, CodeAction>> codeActions = server.codeAction(params).join();
Assert.assertNotNull(codeActions);
Assert.assertFalse("No source actions were found", codeActions.isEmpty());
for (Either<Command, CodeAction> codeAction : codeActions) {
Assert.assertNull("Should defer the edit property to the resolveCodeAction request", codeAction.getRight().getEdit());
}
Optional<Either<Command, CodeAction>> generateConstructorResponse = codeActions.stream().filter(codeAction -> {
return "Generate Constructors".equals(codeAction.getRight().getTitle());
}).findFirst();
Assert.assertTrue("Should return the quick assist 'Convert to lambda expression'", generateConstructorResponse.isPresent());
CodeAction unresolvedCodeAction = generateConstructorResponse.get().getRight();
Assert.assertNotNull("Should preserve the data property for the unresolved code action", unresolvedCodeAction.getData());
CodeAction resolvedCodeAction = server.resolveCodeAction(unresolvedCodeAction).join();
Assert.assertNotNull("Should resolve the edit property in the resolveCodeAction request", resolvedCodeAction.getEdit());
String actual = AbstractQuickFixTest.evaluateWorkspaceEdit(resolvedCodeAction.getEdit());
buf = new StringBuilder();
buf.append("public class E {\n");
buf.append(" private void hello() {\n");
buf.append(" }\n");
buf.append("\n");
buf.append(" /**\n");
buf.append(" * \n");
buf.append(" */\n");
buf.append(" public E() {\n");
buf.append(" }\n");
buf.append("}\n");
Assert.assertEquals(buf.toString(), actual);
}
use of org.junit.Assert.assertNotNull in project eclipse.jdt.ls by eclipse.
the class CodeActionHandlerTest method testCodeAction_allKindsOfActions.
@Test
public void testCodeAction_allKindsOfActions() throws Exception {
ICompilationUnit unit = getWorkingCopy("src/java/Foo.java", "public class Foo {\n" + " void foo() {\n" + " String bar = \"astring\";" + " }\n" + "}\n");
CodeActionParams params = new CodeActionParams();
params.setTextDocument(new TextDocumentIdentifier(JDTUtils.toURI(unit)));
final Range range = CodeActionUtil.getRange(unit, "bar");
params.setRange(range);
CodeActionContext context = new CodeActionContext(Arrays.asList(getDiagnostic(Integer.toString(IProblem.LocalVariableIsNeverUsed), range)));
params.setContext(context);
List<Either<Command, CodeAction>> codeActions = getCodeActions(params);
Assert.assertNotNull(codeActions);
Assert.assertFalse("No code actions were found", codeActions.isEmpty());
boolean hasQuickFix = codeActions.stream().anyMatch(codeAction -> codeAction.getRight().getKind().startsWith(CodeActionKind.QuickFix));
assertTrue("No quickfix actions were found", hasQuickFix);
boolean hasRefactor = codeActions.stream().anyMatch(codeAction -> codeAction.getRight().getKind().startsWith(CodeActionKind.Refactor));
assertTrue("No refactor actions were found", hasRefactor);
boolean hasSource = codeActions.stream().anyMatch(codeAction -> codeAction.getRight().getKind().startsWith(CodeActionKind.Source));
assertTrue("No source actions were found", hasSource);
List<String> baseKinds = codeActions.stream().map(codeAction -> getBaseKind(codeAction.getRight().getKind())).collect(Collectors.toList());
assertTrue("quickfix actions should be ahead of refactor actions", baseKinds.lastIndexOf(CodeActionKind.QuickFix) < baseKinds.indexOf(CodeActionKind.Refactor));
assertTrue("refactor actions should be ahead of source actions", baseKinds.lastIndexOf(CodeActionKind.Refactor) < baseKinds.indexOf(CodeActionKind.Source));
}
use of org.junit.Assert.assertNotNull in project eclipse.jdt.ls by eclipse.
the class CodeActionHandlerTest method testCodeAction_customFileFormattingOptions.
@Test
public void testCodeAction_customFileFormattingOptions() throws Exception {
when(clientPreferences.isWorkspaceConfigurationSupported()).thenReturn(true);
when(connection.configuration(Mockito.any())).thenReturn(Arrays.asList(4, true));
server.setClientConnection(connection);
JavaLanguageServerPlugin.getInstance().setProtocol(server);
IJavaProject javaProject = ProjectUtils.getJavaProject(project);
Map<String, String> projectOptions = javaProject.getOptions(false);
// Indent using Tabs
projectOptions.put(DefaultCodeFormatterConstants.FORMATTER_TAB_CHAR, JavaCore.TAB);
projectOptions.put(DefaultCodeFormatterConstants.FORMATTER_TAB_SIZE, "4");
javaProject.setOptions(projectOptions);
IPackageFragmentRoot sourceFolder = javaProject.getPackageFragmentRoot(project.getFolder("src"));
IPackageFragment pack1 = sourceFolder.createPackageFragment("test1", false, null);
StringBuilder builder = new StringBuilder();
builder.append("package test1;\n");
builder.append("interface I {\n");
builder.append(" void method();\n");
builder.append("}\n");
builder.append("public class E {\n");
builder.append(" void bar(I i) {\n");
builder.append(" }\n");
builder.append(" void foo() {\n");
builder.append(" bar(() /*[*//*]*/-> {\n");
builder.append(" });\n");
builder.append(" }\n");
builder.append("}\n");
ICompilationUnit cu = pack1.createCompilationUnit("E.java", builder.toString(), false, null);
CodeActionParams params = new CodeActionParams();
params.setTextDocument(new TextDocumentIdentifier(JDTUtils.toURI(cu)));
final Range range = CodeActionUtil.getRange(cu, "/*[*//*]*/");
params.setRange(range);
params.setContext(new CodeActionContext(Collections.emptyList(), Arrays.asList(CodeActionKind.Refactor)));
List<Either<Command, CodeAction>> codeActions = getCodeActions(params);
Assert.assertNotNull(codeActions);
Optional<Either<Command, CodeAction>> found = codeActions.stream().filter((codeAction) -> {
return codeAction.isRight() && Objects.equals("Convert to anonymous class creation", codeAction.getRight().getTitle());
}).findAny();
Assert.assertTrue(found.isPresent());
Either<Command, CodeAction> codeAction = found.get();
Command c = codeAction.isLeft() ? codeAction.getLeft() : codeAction.getRight().getCommand();
Assert.assertEquals(CodeActionHandler.COMMAND_ID_APPLY_EDIT, c.getCommand());
Assert.assertNotNull(c.getArguments());
Assert.assertTrue(c.getArguments().get(0) instanceof WorkspaceEdit);
WorkspaceEdit edit = (WorkspaceEdit) c.getArguments().get(0);
String actual = AbstractQuickFixTest.evaluateWorkspaceEdit(edit);
builder = new StringBuilder();
builder.append("package test1;\n");
builder.append("interface I {\n");
builder.append(" void method();\n");
builder.append("}\n");
builder.append("public class E {\n");
builder.append(" void bar(I i) {\n");
builder.append(" }\n");
builder.append(" void foo() {\n");
builder.append(" bar(new I() {\n");
builder.append(" @Override\n");
builder.append(" public void method() {\n");
builder.append(" }\n");
builder.append(" });\n");
builder.append(" }\n");
builder.append("}\n");
AbstractSourceTestCase.compareSource(builder.toString(), actual);
}
use of org.junit.Assert.assertNotNull in project graal by oracle.
the class MultiThreadedLanguageTest method testAccessTruffleContextFromExclusivePolyglotThread.
@Test
public void testAccessTruffleContextFromExclusivePolyglotThread() throws Throwable {
// don't allow multi-threading in this test as every context
// is used exclusively by one thread.
MultiThreadedLanguage.isThreadAccessAllowed = (req) -> {
return req.singleThreaded;
};
final int iterations = 10;
final int innerIterations = 10;
AtomicReference<Throwable> lastError = new AtomicReference<>();
UncaughtExceptionHandler uncaughtHandler = (run, e) -> lastError.set(e);
Context polyglotContext = Context.newBuilder().allowCreateThread(true).build();
ConcurrentHashMap<LanguageContext, String> seenContexts = new ConcurrentHashMap<>();
eval(polyglotContext, new Function<Env, Object>() {
@SuppressWarnings("hiding")
public Object apply(Env env) {
List<Thread> threads = new ArrayList<>();
List<TruffleContext> contexts = new ArrayList<>();
for (int i = 0; i < iterations; i++) {
TruffleContext context = env.newContextBuilder().build();
Thread thread = env.createThread(() -> {
assertUniqueContext();
List<Thread> innerThreads = new ArrayList<>();
List<TruffleContext> innerContexts = new ArrayList<>();
for (int j = 0; j < innerIterations; j++) {
TruffleContext innerContext = env.newContextBuilder().build();
Thread innerThread = env.createThread(() -> {
assertUniqueContext();
}, innerContext);
innerThread.setUncaughtExceptionHandler(uncaughtHandler);
innerThread.start();
innerThreads.add(innerThread);
innerContexts.add(innerContext);
}
for (Thread innerThread : innerThreads) {
try {
innerThread.join();
} catch (InterruptedException e) {
}
}
for (TruffleContext innerContext : innerContexts) {
innerContext.close();
}
}, context);
thread.setUncaughtExceptionHandler(uncaughtHandler);
thread.start();
threads.add(thread);
contexts.add(context);
}
for (Thread thread : threads) {
try {
thread.join();
} catch (InterruptedException e) {
}
}
for (TruffleContext context : contexts) {
context.close();
}
return null;
}
private LanguageContext assertUniqueContext() {
LanguageContext languageContext = MultiThreadedLanguage.getContext();
Assert.assertNotNull(languageContext);
Assert.assertFalse(seenContexts.containsKey(languageContext));
seenContexts.put(languageContext, "");
return languageContext;
}
});
Assert.assertEquals(221, initializeCount.get());
Assert.assertEquals(initializeCount.get() - 1, disposeCount.get());
Assert.assertEquals(0, initializeMultiThreadingCount.get());
// Test that the same context is available in threads when created with Env.getContext()
MultiThreadedLanguage.isThreadAccessAllowed = (req) -> {
return true;
};
eval(polyglotContext, new Function<Env, Object>() {
@SuppressWarnings("hiding")
public Object apply(Env env) {
List<Thread> threads = new ArrayList<>();
LanguageContext languageContext = MultiThreadedLanguage.getContext();
for (int i = 0; i < iterations; i++) {
Thread thread = env.createThread(() -> {
LanguageContext threadContext = MultiThreadedLanguage.getContext();
assertSame(languageContext, threadContext);
List<Thread> innerThreads = new ArrayList<>();
List<TruffleContext> innerContexts = new ArrayList<>();
for (int j = 0; j < innerIterations; j++) {
Thread innerThread = env.createThread(() -> {
LanguageContext innerThreadContext = MultiThreadedLanguage.getContext();
assertSame(languageContext, innerThreadContext);
}, env.getContext());
innerThread.setUncaughtExceptionHandler(uncaughtHandler);
innerThread.start();
innerThreads.add(innerThread);
}
for (Thread innerThread : innerThreads) {
try {
innerThread.join();
} catch (InterruptedException e) {
}
}
for (TruffleContext innerContext : innerContexts) {
innerContext.close();
}
}, env.getContext());
thread.setUncaughtExceptionHandler(uncaughtHandler);
thread.start();
threads.add(thread);
}
for (Thread thread : threads) {
try {
thread.join();
} catch (InterruptedException e) {
}
}
return null;
}
});
if (lastError.get() != null) {
throw lastError.get();
}
polyglotContext.close();
Assert.assertEquals(331, initializeCount.get());
Assert.assertEquals(initializeCount.get(), disposeCount.get());
Assert.assertEquals(1, initializeMultiThreadingCount.get());
}
Aggregations