use of org.eclipse.jdt.ls.core.internal.handlers.GenerateConstructorsHandler.CheckConstructorsResponse in project eclipse.jdt.ls by eclipse.
the class GenerateConstructorsHandlerTest method testGenerateConstructors_enum.
@Test
public void testGenerateConstructors_enum() throws ValidateEditException, CoreException, IOException {
// @formatter:off
ICompilationUnit unit = fPackageP.createCompilationUnit("B.java", "package p;\r\n" + "\r\n" + "public enum B {\r\n" + "}", true, null);
// @formatter:on
CodeActionParams params = CodeActionUtil.constructCodeActionParams(unit, "enum B");
CheckConstructorsResponse response = GenerateConstructorsHandler.checkConstructorsStatus(params);
assertNotNull(response.constructors);
assertEquals(1, response.constructors.length);
assertNotNull(response.fields);
assertEquals(0, response.fields.length);
CodeGenerationSettings settings = new CodeGenerationSettings();
settings.createComments = false;
TextEdit edit = GenerateConstructorsHandler.generateConstructors(unit.findPrimaryType(), response.constructors, response.fields, settings, null, new NullProgressMonitor());
assertNotNull(edit);
JavaModelUtil.applyEdit(unit, edit, true, null);
/* @formatter:off */
String expected = "package p;\r\n" + "\r\n" + "public enum B {\r\n" + " ;\r\n" + "\r\n" + " private B() {\r\n" + " }\r\n" + "}";
/* @formatter:on */
compareSource(expected, unit.getSource());
}
use of org.eclipse.jdt.ls.core.internal.handlers.GenerateConstructorsHandler.CheckConstructorsResponse in project eclipse.jdt.ls by eclipse.
the class GenerateConstructorsHandlerTest method testGenerateConstructorsAfterCursorPosition.
@Test
public void testGenerateConstructorsAfterCursorPosition() throws ValidateEditException, CoreException, IOException {
String oldValue = preferences.getCodeGenerationInsertionLocation();
try {
preferences.setCodeGenerationInsertionLocation(CodeGenerationUtils.INSERT_AFTER_CURSOR);
// @formatter:off
fPackageP.createCompilationUnit("B.java", "package p;\r\n" + "\r\n" + "public class B {\r\n" + " public B(String name) {\r\n" + " }\r\n" + " public B(String name, int id) {\r\n" + " }\r\n" + " private B() {\r\n" + " }\r\n" + "}", true, null);
ICompilationUnit unit = fPackageP.createCompilationUnit("C.java", "package p;\r\n" + "\r\n" + "public class C extends B {\r\n" + " private static String logger;\r\n" + " private final String uuid = \"123\";\r\n" + " private final String instance;/*|*/\r\n" + " private String address;\r\n" + "}", true, null);
// @formatter:on
CodeActionParams params = CodeActionUtil.constructCodeActionParams(unit, "String address");
CheckConstructorsResponse response = GenerateConstructorsHandler.checkConstructorsStatus(params);
assertNotNull(response.constructors);
assertEquals(2, response.constructors.length);
assertNotNull(response.fields);
assertEquals(2, response.fields.length);
CodeGenerationSettings settings = new CodeGenerationSettings();
settings.createComments = false;
Range cursor = CodeActionUtil.getRange(unit, "/*|*/");
TextEdit edit = GenerateConstructorsHandler.generateConstructors(unit.findPrimaryType(), response.constructors, response.fields, settings, cursor, new NullProgressMonitor());
assertNotNull(edit);
JavaModelUtil.applyEdit(unit, edit, true, null);
/* @formatter:off */
String expected = "package p;\r\n" + "\r\n" + "public class C extends B {\r\n" + " private static String logger;\r\n" + " private final String uuid = \"123\";\r\n" + " private final String instance;/*|*/\r\n" + " public C(String name, String instance, String address) {\r\n" + " super(name);\r\n" + " this.instance = instance;\r\n" + " this.address = address;\r\n" + " }\r\n" + " public C(String name, int id, String instance, String address) {\r\n" + " super(name, id);\r\n" + " this.instance = instance;\r\n" + " this.address = address;\r\n" + " }\r\n" + " private String address;\r\n" + "}";
/* @formatter:on */
compareSource(expected, unit.getSource());
} finally {
preferences.setCodeGenerationInsertionLocation(oldValue);
}
}
Aggregations