Search in sources :

Example 1 with TestNGFramework

use of com.theoryinpractice.testng.TestNGFramework in project intellij-community by JetBrains.

the class TestNGDataProviderInspection method createMethodFix.

private static CreateMethodQuickFix createMethodFix(PsiAnnotationMemberValue provider, @NotNull PsiClass providerClass, PsiClass topLevelClass) {
    final String name = StringUtil.unquoteString(provider.getText());
    FileTemplateDescriptor templateDesc = new TestNGFramework().getParametersMethodFileTemplateDescriptor();
    assert templateDesc != null;
    final FileTemplate fileTemplate = FileTemplateManager.getInstance(provider.getProject()).getCodeTemplate(templateDesc.getFileName());
    String body = "";
    try {
        final Properties attributes = new Properties();
        attributes.put(FileTemplate.ATTRIBUTE_NAME, name);
        body = fileTemplate.getText(attributes);
        body = body.replace("${BODY}\n", "");
        final PsiMethod methodFromTemplate = JavaPsiFacade.getElementFactory(providerClass.getProject()).createMethodFromText(body, providerClass);
        final PsiCodeBlock methodBody = methodFromTemplate.getBody();
        if (methodBody != null) {
            body = StringUtil.trimEnd(StringUtil.trimStart(methodBody.getText(), "{"), "}");
        } else {
            body = "";
        }
    } catch (Exception ignored) {
    }
    if (StringUtil.isEmptyOrSpaces(body)) {
        body = "return new Object[][]{};";
    }
    String signature = "@" + DataProvider.class.getName() + " public ";
    if (providerClass == topLevelClass) {
        signature += "static ";
    }
    signature += "Object[][] " + name + "()";
    return CreateMethodQuickFix.createFix(providerClass, signature, body);
}
Also used : TestNGFramework(com.theoryinpractice.testng.TestNGFramework) FileTemplateDescriptor(com.intellij.ide.fileTemplates.FileTemplateDescriptor) FileTemplate(com.intellij.ide.fileTemplates.FileTemplate) Properties(java.util.Properties) IOException(java.io.IOException)

Aggregations

FileTemplate (com.intellij.ide.fileTemplates.FileTemplate)1 FileTemplateDescriptor (com.intellij.ide.fileTemplates.FileTemplateDescriptor)1 TestNGFramework (com.theoryinpractice.testng.TestNGFramework)1 IOException (java.io.IOException)1 Properties (java.util.Properties)1