Skip to content

Commit b9db41d

Browse files
authored
Merge pull request #426 from magento/updated-to-junit-4-fixed-generator-tests
Updated test framework to Junit 4 and fixed ignored tests
2 parents ddabf9c + 6e06aaa commit b9db41d

17 files changed

+204
-144
lines changed

build.gradle

+10
Original file line numberDiff line numberDiff line change
@@ -103,3 +103,13 @@ idea {
103103
generatedSourceDirs += file('gen')
104104
}
105105
}
106+
107+
dependencies {
108+
testImplementation 'junit:junit:4.13'
109+
}
110+
111+
test {
112+
useJUnit()
113+
114+
maxHeapSize = '1G'
115+
}

src/com/magento/idea/magento2plugin/actions/generation/generator/CLICommandDiXmlGenerator.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
import com.intellij.psi.xml.XmlTag;
1818
import com.magento.idea.magento2plugin.actions.generation.data.CLICommandXmlData;
1919
import com.magento.idea.magento2plugin.actions.generation.generator.util.FindOrCreateDiXml;
20-
import com.magento.idea.magento2plugin.actions.generation.generator.util.GetCodeTemplate;
20+
import com.magento.idea.magento2plugin.actions.generation.generator.util.GetCodeTemplateUtil;
2121
import com.magento.idea.magento2plugin.actions.generation.generator.util.XmlFilePositionUtil;
2222
import com.magento.idea.magento2plugin.magento.files.ModuleDiXml;
2323
import com.magento.idea.magento2plugin.magento.packages.Areas;
@@ -31,7 +31,7 @@
3131
"PMD.UselessParentheses"
3232
})
3333
public class CLICommandDiXmlGenerator extends FileGenerator {
34-
private final GetCodeTemplate getCodeTemplate;
34+
private final GetCodeTemplateUtil getCodeTemplateUtil;
3535
private final FindOrCreateDiXml findOrCreateDiXml;
3636
private final XmlFilePositionUtil positionUtil;
3737
private final CLICommandXmlData cliCommandXmlData;
@@ -50,7 +50,7 @@ public CLICommandDiXmlGenerator(
5050
super(project);
5151
this.cliCommandXmlData = cliCommandXmlData;
5252
this.project = project;
53-
this.getCodeTemplate = GetCodeTemplate.getInstance(project);
53+
this.getCodeTemplateUtil = new GetCodeTemplateUtil(project);
5454
this.findOrCreateDiXml = new FindOrCreateDiXml(project);
5555
this.positionUtil = XmlFilePositionUtil.getInstance();
5656
}
@@ -69,7 +69,7 @@ public PsiFile generate(final String actionName) {
6969
WriteCommandAction.runWriteCommandAction(project, () -> {
7070
final StringBuffer textBuf = new StringBuffer();
7171
try {
72-
final String template = getCodeTemplate.execute(
72+
final String template = getCodeTemplateUtil.execute(
7373
ModuleDiXml.TEMPLATE_CLI_COMMAND,
7474
getAttributes()
7575
);

src/com/magento/idea/magento2plugin/actions/generation/generator/CrontabXmlGenerator.java

+81-59
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
* Copyright © Magento, Inc. All rights reserved.
33
* See COPYING.txt for license details.
44
*/
5+
56
package com.magento.idea.magento2plugin.actions.generation.generator;
67

78
import com.intellij.openapi.command.WriteCommandAction;
@@ -17,57 +18,68 @@
1718
import com.intellij.psi.xml.XmlTag;
1819
import com.magento.idea.magento2plugin.actions.generation.data.CrontabXmlData;
1920
import com.magento.idea.magento2plugin.actions.generation.generator.util.FindOrCreateCrontabXml;
20-
import com.magento.idea.magento2plugin.actions.generation.generator.util.GetCodeTemplate;
21+
import com.magento.idea.magento2plugin.actions.generation.generator.util.GetCodeTemplateUtil;
2122
import com.magento.idea.magento2plugin.actions.generation.generator.util.XmlFilePositionUtil;
2223
import com.magento.idea.magento2plugin.magento.files.CrontabXmlTemplate;
2324
import com.magento.idea.magento2plugin.util.xml.XmlPsiTreeUtil;
24-
import org.jetbrains.annotations.NotNull;
2525
import java.io.IOException;
2626
import java.util.Collection;
2727
import java.util.Properties;
28+
import org.jetbrains.annotations.NotNull;
2829

2930
public class CrontabXmlGenerator extends FileGenerator {
30-
private Project project;
31-
private CrontabXmlData crontabXmlData;
31+
private final Project project;
32+
private final CrontabXmlData crontabXmlData;
3233
private boolean isCronGroupDeclared;
3334

34-
private GetCodeTemplate getCodeTemplate;
35-
private CodeStyleManager codeStyleManager;
36-
private FindOrCreateCrontabXml findOrCreateCrontabXml;
37-
private XmlFilePositionUtil positionUtil;
38-
private PsiDocumentManager psiDocumentManager;
35+
private final GetCodeTemplateUtil getCodeTemplateUtil;
36+
private final CodeStyleManager codeStyleManager;
37+
private final FindOrCreateCrontabXml findOrCreateCrontabXml;
38+
private final XmlFilePositionUtil positionUtil;
39+
private final PsiDocumentManager psiDocumentManager;
3940

40-
public CrontabXmlGenerator(Project project, @NotNull CrontabXmlData crontabXmlData) {
41+
/**
42+
* Generator for crontab.xml.
43+
*
44+
* @param project Project
45+
* @param crontabXmlData CrontabXmlData
46+
*/
47+
public CrontabXmlGenerator(
48+
final Project project,
49+
final @NotNull CrontabXmlData crontabXmlData
50+
) {
4151
super(project);
4252

4353
this.project = project;
4454
this.crontabXmlData = crontabXmlData;
4555

4656
this.findOrCreateCrontabXml = new FindOrCreateCrontabXml(project);
47-
this.getCodeTemplate = GetCodeTemplate.getInstance(project);
57+
this.getCodeTemplateUtil = new GetCodeTemplateUtil(project);
4858
this.psiDocumentManager = PsiDocumentManager.getInstance(project);
4959
this.codeStyleManager = CodeStyleManager.getInstance(project);
5060
this.positionUtil = XmlFilePositionUtil.getInstance();
5161
}
5262

5363
/**
54-
* Register newly created cronjob in the crontab.xml. If there is not crontab.xml, it will create one.
64+
* Register newly created cronjob in the crontab.xml.
65+
* If there is not crontab.xml, it will create one.
5566
*
56-
* @param actionName
67+
* @param actionName String
5768
*
5869
* @return PsiFile
5970
*/
60-
public PsiFile generate(String actionName) {
61-
String moduleName = this.crontabXmlData.getModuleName();
71+
@Override
72+
public PsiFile generate(final String actionName) {
73+
final String moduleName = this.crontabXmlData.getModuleName();
6274

63-
XmlFile crontabXmlFile = (XmlFile) this.findOrCreateCrontabXml.execute(
64-
actionName,
65-
moduleName
75+
final XmlFile crontabXmlFile = (XmlFile) this.findOrCreateCrontabXml.execute(
76+
actionName,
77+
moduleName
6678
);
6779

68-
String cronjobGroup = this.crontabXmlData.getCronGroup();
69-
String cronjobName = this.crontabXmlData.getCronjobName();
70-
XmlTag cronGroupTag = this.getCronGroupTag(crontabXmlFile, cronjobGroup);
80+
final String cronjobGroup = this.crontabXmlData.getCronGroup();
81+
final String cronjobName = this.crontabXmlData.getCronjobName();
82+
final XmlTag cronGroupTag = this.getCronGroupTag(crontabXmlFile, cronjobGroup);
7183

7284
this.isCronGroupDeclared = false;
7385
boolean isCronjobDeclared = false;
@@ -78,46 +90,49 @@ public PsiFile generate(String actionName) {
7890
}
7991

8092
if (isCronjobDeclared) {
81-
throw new RuntimeException(cronjobName +" cronjob is already declared in the " + moduleName + " module");
93+
throw new RuntimeException(cronjobName//NOPMD
94+
+ " cronjob is already declared in the " + moduleName + " module");
8295
}
8396

8497
WriteCommandAction.runWriteCommandAction(project, () -> {
85-
StringBuffer textBuf = new StringBuffer();
98+
final StringBuffer textBuf = new StringBuffer();
8699

87100
try {
88-
String cronjobRegistrationTemplate = this.getCodeTemplate.execute(
89-
CrontabXmlTemplate.TEMPLATE_CRONJOB_REGISTRATION,
90-
getAttributes()
101+
final String cronjobRegistrationTemplate = this.getCodeTemplateUtil.execute(
102+
CrontabXmlTemplate.TEMPLATE_CRONJOB_REGISTRATION,
103+
getAttributes()
91104
);
92105

93106
textBuf.append(cronjobRegistrationTemplate);
94107
} catch (IOException e) {
95-
e.printStackTrace();
108+
e.printStackTrace();//NOPMD
96109
return;
97110
}
98111

99-
int insertPos = this.isCronGroupDeclared
112+
final int insertPos = this.isCronGroupDeclared
100113
? this.positionUtil.getEndPositionOfTag(cronGroupTag)
101114
: this.positionUtil.getRootInsertPosition(crontabXmlFile);
102115

103116
if (textBuf.length() > 0 && insertPos >= 0) {
104-
Document document = this.psiDocumentManager.getDocument(crontabXmlFile);
117+
final Document document = this.psiDocumentManager.getDocument(crontabXmlFile);
105118

106119
if (document == null) {
107120
// practically this should not be possible as we tell to edit XML file
108-
throw new RuntimeException(
109-
crontabXmlFile.getVirtualFile().getPath() + " file is binary or has no document associations"
121+
throw new RuntimeException(//NOPMD
122+
crontabXmlFile.getVirtualFile().getPath()
123+
+ " file is binary or has no document associations"
110124
);
111125
}
112126

113127
if (!document.isWritable()) {
114-
throw new RuntimeException(
115-
crontabXmlFile.getVirtualFile().getPath() + " file is not writable. Please check file permission"
128+
throw new RuntimeException(//NOPMD
129+
crontabXmlFile.getVirtualFile().getPath()
130+
+ " file is not writable. Please check file permission"
116131
);
117132
}
118133

119134
document.insertString(insertPos, textBuf);
120-
int endPos = insertPos + textBuf.length() + 1;
135+
final int endPos = insertPos + textBuf.length() + 1;
121136

122137
this.codeStyleManager.reformatText(crontabXmlFile, insertPos, endPos);
123138
this.psiDocumentManager.commitDocument(document);
@@ -127,12 +142,11 @@ public PsiFile generate(String actionName) {
127142
return crontabXmlFile;
128143
}
129144

130-
protected void fillAttributes(Properties attributes) {
131-
String cronjobName = this.crontabXmlData.getCronjobName();
132-
String cronjobGroup = this.crontabXmlData.getCronGroup();
133-
String cronjobInstance = this.crontabXmlData.getCronjobInstance();
134-
String cronjobSchedule = this.crontabXmlData.getCronjobSchedule();
135-
String cronjobScheduleConfigPath = this.crontabXmlData.getCronjobScheduleConfigPath();
145+
@Override
146+
protected void fillAttributes(final Properties attributes) {
147+
final String cronjobGroup = this.crontabXmlData.getCronGroup();
148+
final String cronjobSchedule = this.crontabXmlData.getCronjobSchedule();
149+
final String cronjobScheduleConfigPath = this.crontabXmlData.getCronjobScheduleConfigPath();
136150

137151
if (!this.isCronGroupDeclared) {
138152
attributes.setProperty("CRON_GROUP", cronjobGroup);
@@ -146,36 +160,43 @@ protected void fillAttributes(Properties attributes) {
146160
attributes.setProperty("CRONJOB_SCHEDULE_CONFIG_PATH", cronjobScheduleConfigPath);
147161
}
148162

163+
final String cronjobName = this.crontabXmlData.getCronjobName();
149164
attributes.setProperty("CRONJOB_NAME", cronjobName);
165+
166+
final String cronjobInstance = this.crontabXmlData.getCronjobInstance();
150167
attributes.setProperty("CRONJOB_INSTANCE", cronjobInstance);
151168
}
152169

153170
/**
154-
* Check whenever cronjob with cronjobName is declared under cronGroupTag
171+
* Check whenever cronjob with cronjobName is declared under cronGroupTag.
155172
*
156-
* @param cronGroupTag
157-
* @param cronjobName
173+
* @param cronGroupTag XmlTag
174+
* @param cronjobName String
158175
*
159176
* @return boolean
160177
*/
161-
private boolean isCronjobDeclared(XmlTag cronGroupTag, String cronjobName) {
162-
XmlTag[] cronjobTags = PsiTreeUtil.getChildrenOfType(cronGroupTag, XmlTag.class);
178+
private boolean isCronjobDeclared(final XmlTag cronGroupTag, final String cronjobName) {
179+
final XmlTag[] cronjobTags = PsiTreeUtil.getChildrenOfType(cronGroupTag, XmlTag.class);
163180

164181
if (cronjobTags == null) {
165182
return false;
166183
}
167184

168-
for (XmlTag cronjobTag: cronjobTags) {
185+
for (final XmlTag cronjobTag: cronjobTags) {
169186
if (!cronjobTag.getName().equals(CrontabXmlTemplate.CRON_JOB_TAG)) {
170187
continue;
171188
}
172189

173-
XmlAttribute[] cronjobAttributes = PsiTreeUtil.getChildrenOfType(cronjobTag, XmlAttribute.class);
190+
final XmlAttribute[] cronjobAttributes = PsiTreeUtil.getChildrenOfType(
191+
cronjobTag,
192+
XmlAttribute.class
193+
);
174194

175195
// todo: handle null pointer
176196

177-
for (XmlAttribute cronjobAttribute: cronjobAttributes) {
178-
if (!cronjobAttribute.getName().equals(CrontabXmlTemplate.CRON_JOB_NAME_ATTRIBUTE)) {
197+
for (final XmlAttribute cronjobAttribute: cronjobAttributes) {
198+
if (!cronjobAttribute.getName()
199+
.equals(CrontabXmlTemplate.CRON_JOB_NAME_ATTRIBUTE)) {
179200
continue;
180201
}
181202

@@ -189,26 +210,27 @@ private boolean isCronjobDeclared(XmlTag cronGroupTag, String cronjobName) {
189210
}
190211

191212
/**
192-
* Retrieve cronGroup tag with cronjobGroup name if it registered in crontabXmlFile
213+
* Retrieve cronGroup tag with cronjobGroup name if it registered in crontabXmlFile.
193214
*
194-
* @param crontabXmlFile
195-
* @param cronjobGroup
215+
* @param crontabXmlFile XmlFile
216+
* @param cronjobGroup String
196217
*
197218
* @return XmlTag
198219
*/
199-
private XmlTag getCronGroupTag(XmlFile crontabXmlFile, String cronjobGroup) {
200-
Collection<XmlAttributeValue> cronGroupIdAttributes = XmlPsiTreeUtil.findAttributeValueElements(
201-
crontabXmlFile,
202-
CrontabXmlTemplate.CRON_GROUP_TAG,
203-
CrontabXmlTemplate.CRON_GROUP_NAME_ATTRIBUTE
220+
private XmlTag getCronGroupTag(final XmlFile crontabXmlFile, final String cronjobGroup) {
221+
final Collection<XmlAttributeValue> cronGroupIdAttributes
222+
= XmlPsiTreeUtil.findAttributeValueElements(
223+
crontabXmlFile,
224+
CrontabXmlTemplate.CRON_GROUP_TAG,
225+
CrontabXmlTemplate.CRON_GROUP_NAME_ATTRIBUTE
204226
);
205227

206-
for (XmlAttributeValue cronGroupIdAttribute: cronGroupIdAttributes) {
228+
for (final XmlAttributeValue cronGroupIdAttribute: cronGroupIdAttributes) {
207229
if (!cronGroupIdAttribute.getValue().equals(cronjobGroup)) {
208230
continue;
209231
}
210232

211-
return PsiTreeUtil.getParentOfType(cronGroupIdAttribute, XmlTag.class);
233+
return PsiTreeUtil.getParentOfType(cronGroupIdAttribute, XmlTag.class);//NOPMD
212234
}
213235

214236
return null;

src/com/magento/idea/magento2plugin/actions/generation/generator/ModuleCronGroupXmlGenerator.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
import com.intellij.psi.xml.XmlTag;
1818
import com.magento.idea.magento2plugin.actions.generation.data.CronGroupXmlData;
1919
import com.magento.idea.magento2plugin.actions.generation.generator.util.FindOrCreateCronGroupXml;
20-
import com.magento.idea.magento2plugin.actions.generation.generator.util.GetCodeTemplate;
20+
import com.magento.idea.magento2plugin.actions.generation.generator.util.GetCodeTemplateUtil;
2121
import com.magento.idea.magento2plugin.actions.generation.generator.util.XmlFilePositionUtil;
2222
import com.magento.idea.magento2plugin.bundles.CommonBundle;
2323
import com.magento.idea.magento2plugin.bundles.ValidatorBundle;
@@ -33,7 +33,7 @@
3333
public class ModuleCronGroupXmlGenerator extends FileGenerator {
3434
private final CronGroupXmlData cronGroupXmlData;
3535
private final Project project;
36-
private final GetCodeTemplate getCodeTemplate;
36+
private final GetCodeTemplateUtil getCodeTemplateUtil;
3737
private final XmlFilePositionUtil positionUtil;
3838
private final PsiDocumentManager psiDocumentManager;
3939
private final FindOrCreateCronGroupXml findOrCreateCronGroupsXml;
@@ -54,7 +54,7 @@ public ModuleCronGroupXmlGenerator(
5454
super(project);
5555
this.project = project;
5656
this.cronGroupXmlData = cronGroupXmlData;
57-
this.getCodeTemplate = GetCodeTemplate.getInstance(project);
57+
this.getCodeTemplateUtil = new GetCodeTemplateUtil(project);
5858
this.findOrCreateCronGroupsXml = new FindOrCreateCronGroupXml(project);
5959
this.positionUtil = XmlFilePositionUtil.getInstance();
6060
this.psiDocumentManager = PsiDocumentManager.getInstance(project);
@@ -96,7 +96,7 @@ public PsiFile generate(final String actionName) {
9696
final StringBuffer textBuf = new StringBuffer();
9797

9898
try {
99-
final String cronjobRegistrationTemplate = this.getCodeTemplate.execute(
99+
final String cronjobRegistrationTemplate = this.getCodeTemplateUtil.execute(
100100
CronGroupXmlTemplate.TEMPLATE_CRON_GROUP_REGISTRATION,
101101
getAttributes()
102102
);

src/com/magento/idea/magento2plugin/actions/generation/generator/ObserverEventsXmlGenerator.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
import com.intellij.psi.xml.XmlFile;
1515
import com.magento.idea.magento2plugin.actions.generation.data.ObserverEventsXmlData;
1616
import com.magento.idea.magento2plugin.actions.generation.generator.util.FindOrCreateEventsXml;
17-
import com.magento.idea.magento2plugin.actions.generation.generator.util.GetCodeTemplate;
17+
import com.magento.idea.magento2plugin.actions.generation.generator.util.GetCodeTemplateUtil;
1818
import com.magento.idea.magento2plugin.actions.generation.generator.util.XmlFilePositionUtil;
1919
import com.magento.idea.magento2plugin.magento.files.ModuleEventsXml;
2020
import java.io.IOException;
@@ -23,7 +23,7 @@
2323
public class ObserverEventsXmlGenerator extends FileGenerator {
2424
private final FindOrCreateEventsXml findOrCreateEventsXml;
2525
private final XmlFilePositionUtil positionUtil;
26-
private final GetCodeTemplate getCodeTemplate;
26+
private final GetCodeTemplateUtil getCodeTemplateUtil;
2727
private final ObserverEventsXmlData observerEventsXmlData;
2828
private final Project project;
2929

@@ -42,7 +42,7 @@ public ObserverEventsXmlGenerator(
4242
this.project = project;
4343
this.findOrCreateEventsXml = new FindOrCreateEventsXml(project);
4444
this.positionUtil = XmlFilePositionUtil.getInstance();
45-
this.getCodeTemplate = GetCodeTemplate.getInstance(project);
45+
this.getCodeTemplateUtil = new GetCodeTemplateUtil(project);
4646
}
4747

4848
/**
@@ -63,7 +63,7 @@ public PsiFile generate(final String actionName) {
6363
WriteCommandAction.runWriteCommandAction(project, () -> {
6464
final StringBuffer textBuf = new StringBuffer();
6565
try {
66-
textBuf.append(getCodeTemplate.execute(
66+
textBuf.append(getCodeTemplateUtil.execute(
6767
ModuleEventsXml.TEMPLATE_OBSERVER,
6868
getAttributes())
6969
);

0 commit comments

Comments
 (0)