2 * Copyright 2000-2009 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package com
.theoryinpractice
.testng
.inspection
;
18 import com
.intellij
.codeInsight
.daemon
.HighlightDisplayKey
;
19 import com
.intellij
.codeInspection
.*;
20 import com
.intellij
.codeInspection
.ex
.InspectionProfileImpl
;
21 import com
.intellij
.openapi
.diagnostic
.Logger
;
22 import com
.intellij
.openapi
.project
.Project
;
23 import com
.intellij
.openapi
.ui
.LabeledComponent
;
24 import com
.intellij
.openapi
.util
.JDOMExternalizableStringList
;
25 import com
.intellij
.openapi
.util
.text
.StringUtil
;
26 import com
.intellij
.profile
.codeInspection
.InspectionProfileManager
;
27 import com
.intellij
.profile
.codeInspection
.InspectionProjectProfileManager
;
28 import com
.intellij
.psi
.PsiAnnotation
;
29 import com
.intellij
.psi
.PsiClass
;
30 import com
.intellij
.psi
.PsiNameValuePair
;
31 import com
.intellij
.ui
.DocumentAdapter
;
32 import com
.theoryinpractice
.testng
.util
.TestNGUtil
;
33 import org
.jetbrains
.annotations
.NonNls
;
34 import org
.jetbrains
.annotations
.NotNull
;
35 import org
.jetbrains
.annotations
.Nullable
;
38 import javax
.swing
.event
.DocumentEvent
;
40 import java
.util
.ArrayList
;
41 import java
.util
.Arrays
;
42 import java
.util
.List
;
43 import java
.util
.regex
.Matcher
;
44 import java
.util
.regex
.Pattern
;
47 * @author Hani Suleiman Date: Aug 3, 2005 Time: 3:34:56 AM
49 public class DependsOnGroupsInspection
extends BaseJavaLocalInspectionTool
{
50 private static final Logger LOGGER
= Logger
.getInstance("TestNG Runner");
51 private static final Pattern PATTERN
= Pattern
.compile("\"([a-zA-Z1-9_\\(\\)]*)\"");
52 private static final ProblemDescriptor
[] EMPTY
= new ProblemDescriptor
[0];
54 public JDOMExternalizableStringList groups
= new JDOMExternalizableStringList();
55 @NonNls public static String SHORT_NAME
= "groupsTestNG";
59 public String
getGroupDisplayName() {
65 public String
getDisplayName() {
66 return "Groups problem";
71 public String
getShortName() {
75 public boolean isEnabledByDefault() {
80 public JComponent
createOptionsPanel() {
81 final LabeledComponent
<JTextField
> definedGroups
= new LabeledComponent
<JTextField
>();
82 definedGroups
.setText("&Defined Groups");
83 final JTextField textField
= new JTextField(StringUtil
.join(groups
.toArray(new String
[groups
.size()]), ","));
84 textField
.getDocument().addDocumentListener(new DocumentAdapter() {
85 protected void textChanged(final DocumentEvent e
) {
87 final String
[] groupsFromString
= textField
.getText().split("[, ]");
88 groups
.addAll(Arrays
.asList(groupsFromString
));
91 definedGroups
.setComponent(textField
);
92 final JPanel optionsPanel
= new JPanel(new BorderLayout());
93 optionsPanel
.add(definedGroups
, BorderLayout
.NORTH
);
99 public ProblemDescriptor
[] checkClass(@NotNull PsiClass psiClass
, @NotNull InspectionManager manager
, boolean isOnTheFly
) {
101 if (!psiClass
.getContainingFile().isWritable()) return null;
103 PsiAnnotation
[] annotations
= TestNGUtil
.getTestNGAnnotations(psiClass
);
104 if (annotations
.length
== 0) return EMPTY
;
106 List
<ProblemDescriptor
> problemDescriptors
= new ArrayList
<ProblemDescriptor
>();
107 for (PsiAnnotation annotation
: annotations
) {
109 PsiNameValuePair dep
= null;
110 PsiNameValuePair
[] params
= annotation
.getParameterList().getAttributes();
111 for (PsiNameValuePair param
: params
) {
112 if (param
.getName() != null && param
.getName().matches("(groups|dependsOnGroups)")) {
119 if (dep
.getValue() != null) {
120 LOGGER
.info("Found " + dep
.getName() + " with: " + dep
.getValue().getText());
121 Matcher matcher
= PATTERN
.matcher(dep
.getValue().getText());
122 while (matcher
.find()) {
123 String methodName
= matcher
.group(1);
124 if (!groups
.contains(methodName
)) {
125 LOGGER
.info("group doesn't exist:" + methodName
);
126 ProblemDescriptor descriptor
= manager
.createProblemDescriptor(annotation
, "Group '" + methodName
+ "' is undefined.",
127 new GroupNameQuickFix(methodName
),
128 ProblemHighlightType
.GENERIC_ERROR_OR_WARNING
, isOnTheFly
);
129 problemDescriptors
.add(descriptor
);
136 return problemDescriptors
.toArray(new ProblemDescriptor
[]{});
139 private class GroupNameQuickFix
implements LocalQuickFix
{
143 public GroupNameQuickFix(@NotNull String groupName
) {
144 myGroupName
= groupName
;
148 public String
getName() {
149 return "Add '" + myGroupName
+ "' as a defined test group.";
153 public String
getFamilyName() {
157 public void applyFix(@NotNull Project project
, @NotNull ProblemDescriptor problemDescriptor
) {
158 groups
.add(myGroupName
);
159 final InspectionProfile inspectionProfile
=
160 InspectionProjectProfileManager
.getInstance(project
).getInspectionProfile();
161 //correct save settings
162 ((InspectionProfileImpl
)inspectionProfile
).isProperSetting(HighlightDisplayKey
.find(SHORT_NAME
));
163 InspectionProfileManager
.getInstance().fireProfileChanged(inspectionProfile
);
167 inspectionProfile.save();
169 catch (IOException e) {
170 Messages.showErrorDialog(project, e.getMessage(), CommonBundle.getErrorTitle());