summaryrefslogtreecommitdiff
path: root/java/java-impl/src/com/intellij/testIntegration/createTest/MissedTestsDialog.java
blob: ee8bcf0d1c1c48fba4393ea7985e2915870d47f1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/*
 * Copyright 2000-2014 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.intellij.testIntegration.createTest;

import com.intellij.codeInsight.CodeInsightBundle;
import com.intellij.codeInsight.template.Template;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.DialogWrapper;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.psi.*;
import com.intellij.refactoring.ui.MemberSelectionTable;
import com.intellij.refactoring.util.classMembers.MemberInfo;
import com.intellij.testIntegration.TestFramework;
import com.intellij.testIntegration.TestIntegrationUtils;
import com.intellij.ui.ScrollPaneFactory;
import com.intellij.ui.components.JBCheckBox;
import com.intellij.util.Function;
import com.intellij.util.IncorrectOperationException;
import com.intellij.util.containers.ContainerUtil;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class MissedTestsDialog extends DialogWrapper {
  private final PsiClass mySourceClass;
  private final PsiClass myTestClass;
  private TestFramework myDescriptor;
  private MemberSelectionTable myTable;
  private JBCheckBox myIncludeInheritedCb = new JBCheckBox(CodeInsightBundle.message("intention.create.test.dialog.show.inherited"));

  public MissedTestsDialog(@Nullable Project project, PsiClass sourceClass, PsiClass testClass, TestFramework descriptor) {
    super(project, true);
    mySourceClass = sourceClass;
    myTestClass = testClass;
    myDescriptor = descriptor;
    setTitle("Create Missed Tests");
    init();
  }

  public Collection<MemberInfo> getSelectedMethods() {
    return myTable.getSelectedMemberInfos();
  }

  @Nullable
  @Override
  protected JComponent createCenterPanel() {
    final List<MemberInfo> info = TestIntegrationUtils.extractClassMethods(mySourceClass, false);

    disableMethodsWithTests(info);

    final JPanel panel = new JPanel(new BorderLayout());
    myTable = new MemberSelectionTable(info, null);
    panel.add(ScrollPaneFactory.createScrollPane(myTable), BorderLayout.CENTER);
    panel.add(myIncludeInheritedCb, BorderLayout.NORTH);
    myIncludeInheritedCb.addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent e) {
        updateMethodsTable();
      }
    });
    return panel;
  }

  private void disableMethodsWithTests(List<MemberInfo> info) {
    final Set<String> existingNames = new HashSet<String>();
    final String prefix = getTestPrefix(existingNames);

    existingNames.addAll(ContainerUtil.map(myTestClass.getMethods(), new Function<PsiMethod, String>() {
      @Override
      public String fun(PsiMethod method) {
        return StringUtil.decapitalize(StringUtil.trimStart(method.getName(), prefix));
      }
    }));


    for (MemberInfo memberInfo : info) {
      final PsiMember member = memberInfo.getMember();
      memberInfo.setChecked(!(member instanceof PsiMethod && existingNames.contains(member.getName())));
    }
  }

  private String getTestPrefix(Set<String> existingNames) {
    final Template template = TestIntegrationUtils.createTestMethodTemplate(TestIntegrationUtils.MethodKind.TEST, myDescriptor, myTestClass, null, true, existingNames);
    try {
      return JavaPsiFacade.getElementFactory(myTestClass.getProject()).createMethodFromText(template.getTemplateText(), myTestClass).getName();
    }
    catch (IncorrectOperationException e) {
      return "";
    }
  }

  private void updateMethodsTable() {
    List<MemberInfo> infos = TestIntegrationUtils.extractClassMethods(mySourceClass, myIncludeInheritedCb.isSelected());

    Set<PsiMember> selectedMethods = new HashSet<PsiMember>();
    for (MemberInfo each : myTable.getSelectedMemberInfos()) {
      selectedMethods.add(each.getMember());
    }

    for (MemberInfo each : infos) {
      each.setChecked(selectedMethods.contains(each.getMember()));
    }

    myTable.setMemberInfos(infos);
  }
}