Skip to content

Commit 7eb2b44

Browse files
Merge pull request #46 from jkschneider/junit5
Upgrade to JUnit Jupiter
2 parents 702d2c8 + 1c6cbe8 commit 7eb2b44

File tree

15 files changed

+219
-189
lines changed

15 files changed

+219
-189
lines changed

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@ There is still much to be done. Your feedback and collaboration would be greatl
6767
If you find this plugin useful, please star this repository and share with your friends & colleagues and on social media.
6868

6969
## Future Plans
70-
* Move from JUnit 4 to Junit 5. Junit 5 is lacking the support for temporary files that JUnit 4 provides.
7170
* Add a Gradle plugin.
7271
* Incorporate Unit Test coverage metrics to quickly identify the safety of refactoring a God class.
7372
* Incorporate bug counts per God class to the Impact (Y-Axis) calculation.

change-proneness-ranker/src/test/java/org/hjug/git/ChangePronenessRankerTest.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,24 @@
77
import java.io.IOException;
88
import java.util.*;
99
import org.eclipse.jgit.api.errors.GitAPIException;
10-
import org.junit.Assert;
11-
import org.junit.Before;
12-
import org.junit.Test;
10+
import org.junit.jupiter.api.Assertions;
11+
import org.junit.jupiter.api.BeforeEach;
12+
import org.junit.jupiter.api.Test;
1313

1414
public class ChangePronenessRankerTest {
1515

1616
private ChangePronenessRanker changePronenessRanker;
1717
private RepositoryLogReader repositoryLogReader;
1818

19-
@Before
19+
@BeforeEach
2020
public void setUp() {
2121
repositoryLogReader = mock(RepositoryLogReader.class);
2222
changePronenessRanker = new ChangePronenessRanker(null, repositoryLogReader);
2323
}
2424

2525
// TODO: this should probably be a cucumber test
2626
@Test
27-
public void testChangePronenessCalculation() throws IOException, GitAPIException {
27+
void testChangePronenessCalculation() throws IOException, GitAPIException {
2828
ScmLogInfo scmLogInfo = new ScmLogInfo("path", 1595275997, 0, 1);
2929

3030
TreeMap<Integer, Integer> commitsWithChangeCounts = new TreeMap<>();
@@ -39,11 +39,11 @@ public void testChangePronenessCalculation() throws IOException, GitAPIException
3939
changePronenessRanker.rankChangeProneness(scmLogInfos);
4040

4141
// 1 commit of a class we're interested in, 6 commits of other files after it
42-
Assert.assertEquals((float) 1 / 7, scmLogInfo.getChangeProneness(), 0.1);
42+
Assertions.assertEquals((float) 1 / 7, scmLogInfo.getChangeProneness(), 0.1);
4343
}
4444

4545
@Test
46-
public void testRankChangeProneness() throws IOException, GitAPIException {
46+
void testRankChangeProneness() throws IOException, GitAPIException {
4747
ScmLogInfo scmLogInfo = new ScmLogInfo("file1", 1595275997, 0, 1);
4848

4949
TreeMap<Integer, Integer> commitsWithChangeCounts = new TreeMap<>();
@@ -65,8 +65,8 @@ public void testRankChangeProneness() throws IOException, GitAPIException {
6565
changePronenessRanker.rankChangeProneness(scmLogInfos);
6666

6767
// ranks higher since fewer commits since initial commit
68-
Assert.assertEquals(2, scmLogInfo.getChangePronenessRank());
68+
Assertions.assertEquals(2, scmLogInfo.getChangePronenessRank());
6969
// ranks lower since there have been more commits since initial commit
70-
Assert.assertEquals(1, scmLogInfo2.getChangePronenessRank());
70+
Assertions.assertEquals(1, scmLogInfo2.getChangePronenessRank());
7171
}
7272
}

change-proneness-ranker/src/test/java/org/hjug/git/GitLogReaderTest.java

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,32 +8,35 @@
88
import org.eclipse.jgit.api.errors.GitAPIException;
99
import org.eclipse.jgit.lib.Repository;
1010
import org.eclipse.jgit.revwalk.RevCommit;
11-
import org.junit.*;
12-
import org.junit.rules.TemporaryFolder;
11+
import org.junit.jupiter.api.AfterEach;
12+
import org.junit.jupiter.api.Assertions;
13+
import org.junit.jupiter.api.BeforeEach;
14+
import org.junit.jupiter.api.Test;
15+
import org.junit.jupiter.api.io.TempDir;
1316

1417
public class GitLogReaderTest {
1518
// Borrowed bits and pieces from
1619
// https://gist.github.com/rherrmann/0c682ea327862cb6847704acf90b1d5d
1720

18-
@Rule
19-
public TemporaryFolder tempFolder = new TemporaryFolder();
21+
@TempDir
22+
public File tempFolder;
2023

2124
private Git git;
2225
private Repository repository;
2326

24-
@Before
27+
@BeforeEach
2528
public void setUp() throws GitAPIException {
26-
git = Git.init().setDirectory(tempFolder.getRoot()).call();
29+
git = Git.init().setDirectory(tempFolder).call();
2730
repository = git.getRepository();
2831
}
2932

30-
@After
33+
@AfterEach
3134
public void tearDown() {
3235
repository.close();
3336
}
3437

3538
@Test
36-
public void testFileLog() throws IOException, GitAPIException, InterruptedException {
39+
void testFileLog() throws IOException, GitAPIException, InterruptedException {
3740
// This path works when referencing the full Tobago repository
3841
// String filePath = "tobago-core/src/main/java/org/apache/myfaces/tobago/facelets/AttributeHandler.java";
3942

@@ -58,13 +61,13 @@ public void testFileLog() throws IOException, GitAPIException, InterruptedExcept
5861

5962
ScmLogInfo scmLogInfo = gitLogReader.fileLog(repository, attributeHandler);
6063

61-
Assert.assertEquals(2, scmLogInfo.getCommitCount());
62-
Assert.assertEquals(firstCommit.getCommitTime(), scmLogInfo.getEarliestCommit());
63-
Assert.assertEquals(secondCommit.getCommitTime(), scmLogInfo.getMostRecentCommit());
64+
Assertions.assertEquals(2, scmLogInfo.getCommitCount());
65+
Assertions.assertEquals(firstCommit.getCommitTime(), scmLogInfo.getEarliestCommit());
66+
Assertions.assertEquals(secondCommit.getCommitTime(), scmLogInfo.getMostRecentCommit());
6467
}
6568

6669
@Test
67-
public void testWalkFirstCommit() throws IOException, GitAPIException {
70+
void testWalkFirstCommit() throws IOException, GitAPIException {
6871
GitLogReader gitLogReader = new GitLogReader();
6972

7073
String attributeHandler = "AttributeHandler.java";
@@ -75,12 +78,12 @@ public void testWalkFirstCommit() throws IOException, GitAPIException {
7578

7679
Map<Integer, Integer> result = gitLogReader.walkFirstCommit(repository, commit);
7780

78-
Assert.assertTrue(result.containsKey(commit.getCommitTime()));
79-
Assert.assertEquals(1, result.get(commit.getCommitTime()).intValue());
81+
Assertions.assertTrue(result.containsKey(commit.getCommitTime()));
82+
Assertions.assertEquals(1, result.get(commit.getCommitTime()).intValue());
8083
}
8184

8285
@Test
83-
public void testCaptureChangCountByCommitTimestamp() throws Exception {
86+
void testCaptureChangCountByCommitTimestamp() throws Exception {
8487
GitLogReader gitLogReader = new GitLogReader();
8588

8689
String attributeHandler = "AttributeHandler.java";
@@ -105,8 +108,9 @@ public void testCaptureChangCountByCommitTimestamp() throws Exception {
105108

106109
Map<Integer, Integer> commitCounts = gitLogReader.captureChangeCountByCommitTimestamp(repository);
107110

108-
Assert.assertEquals(1, commitCounts.get(firstCommit.getCommitTime()).intValue());
109-
Assert.assertEquals(2, commitCounts.get(secondCommit.getCommitTime()).intValue());
111+
Assertions.assertEquals(1, commitCounts.get(firstCommit.getCommitTime()).intValue());
112+
Assertions.assertEquals(
113+
2, commitCounts.get(secondCommit.getCommitTime()).intValue());
110114
}
111115

112116
private void writeFile(String name, String content) throws IOException {

cost-benefit-calculator/src/test/java/org/hjug/cbc/CostBenefitCalculatorTest.java

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,30 +11,33 @@
1111
import org.hjug.git.GitLogReader;
1212
import org.hjug.metrics.GodClass;
1313
import org.hjug.metrics.PMDGodClassRuleRunner;
14-
import org.junit.*;
15-
import org.junit.rules.TemporaryFolder;
14+
import org.junit.jupiter.api.AfterEach;
15+
import org.junit.jupiter.api.Assertions;
16+
import org.junit.jupiter.api.BeforeEach;
17+
import org.junit.jupiter.api.Test;
18+
import org.junit.jupiter.api.io.TempDir;
1619

1720
public class CostBenefitCalculatorTest {
1821

19-
@Rule
20-
public TemporaryFolder tempFolder = new TemporaryFolder();
22+
@TempDir
23+
public File tempFolder;
2124

2225
private Git git;
2326
private Repository repository;
2427

25-
@Before
28+
@BeforeEach
2629
public void setUp() throws GitAPIException {
27-
git = Git.init().setDirectory(tempFolder.getRoot()).call();
30+
git = Git.init().setDirectory(tempFolder).call();
2831
repository = git.getRepository();
2932
}
3033

31-
@After
34+
@AfterEach
3235
public void tearDown() {
3336
repository.close();
3437
}
3538

3639
@Test
37-
public void testCostBenefitCalculation() throws IOException, GitAPIException, InterruptedException {
40+
void testCostBenefitCalculation() throws IOException, GitAPIException, InterruptedException {
3841
String attributeHandler = "AttributeHandler.java";
3942
InputStream resourceAsStream = getClass().getClassLoader().getResourceAsStream(attributeHandler);
4043
writeFile(attributeHandler, convertInputStreamToString(resourceAsStream));
@@ -60,12 +63,12 @@ public void testCostBenefitCalculation() throws IOException, GitAPIException, In
6063
List<RankedDisharmony> disharmonies = costBenefitCalculator.calculateCostBenefitValues(
6164
git.getRepository().getDirectory().getPath());
6265

63-
Assert.assertEquals(0, disharmonies.get(0).getPriority().intValue());
64-
Assert.assertEquals(0, disharmonies.get(1).getPriority().intValue());
66+
Assertions.assertEquals(0, disharmonies.get(0).getPriority().intValue());
67+
Assertions.assertEquals(0, disharmonies.get(1).getPriority().intValue());
6568
}
6669

6770
@Test
68-
public void scanClassesInRepo2() throws IOException, GitAPIException {
71+
void scanClassesInRepo2() throws IOException, GitAPIException {
6972
String attributeHandler = "AttributeHandler.java";
7073
InputStream resourceAsStream = getClass().getClassLoader().getResourceAsStream(attributeHandler);
7174
writeFile(attributeHandler, convertInputStreamToString(resourceAsStream));
@@ -86,11 +89,11 @@ public void scanClassesInRepo2() throws IOException, GitAPIException {
8689
godClassOptional.ifPresent(godClass -> godClasses.put(filePath, godClass));
8790
}
8891

89-
Assert.assertFalse(godClasses.isEmpty());
92+
Assertions.assertFalse(godClasses.isEmpty());
9093
}
9194

9295
@Test
93-
public void scanClassesInRepo() throws IOException, GitAPIException {
96+
void scanClassesInRepo() throws IOException, GitAPIException {
9497
String attributeHandler = "AttributeHandler.java";
9598
InputStream resourceAsStream = getClass().getClassLoader().getResourceAsStream(attributeHandler);
9699
writeFile(attributeHandler, convertInputStreamToString(resourceAsStream));
@@ -111,7 +114,7 @@ public void scanClassesInRepo() throws IOException, GitAPIException {
111114
godClassOptional.ifPresent(godClass -> godClasses.put(filePath, godClass));
112115
}
113116

114-
Assert.assertFalse(godClasses.isEmpty());
117+
Assertions.assertFalse(godClasses.isEmpty());
115118
}
116119

117120
private void writeFile(String name, String content) throws IOException {

effort-ranker/src/main/java/org/hjug/metrics/CBOClass.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
package org.hjug.metrics;
22

3-
import lombok.Data;
4-
53
import java.util.Scanner;
4+
import lombok.Data;
65

76
/**
87
* Created by Jim on 11/16/2016.

effort-ranker/src/main/java/org/hjug/metrics/CBORuleRunner.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
11
package org.hjug.metrics;
22

3+
import java.io.File;
4+
import java.io.FileInputStream;
5+
import java.io.FileNotFoundException;
6+
import java.io.InputStream;
7+
import java.util.Optional;
38
import lombok.extern.slf4j.Slf4j;
49
import net.sourceforge.pmd.*;
510
import net.sourceforge.pmd.lang.Language;
611
import net.sourceforge.pmd.lang.LanguageRegistry;
712
import net.sourceforge.pmd.lang.java.JavaLanguageModule;
813
import org.hjug.metrics.rules.CBORule;
914

10-
import java.io.File;
11-
import java.io.FileInputStream;
12-
import java.io.FileNotFoundException;
13-
import java.io.InputStream;
14-
import java.util.Optional;
15-
1615
// based on http://sdoulger.blogspot.com/2010/12/call-pmd-from-your-code-with-you-custom.html
1716
@Slf4j
1817
public class CBORuleRunner {

effort-ranker/src/main/java/org/hjug/metrics/rules/CBORule.java

Lines changed: 42 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package org.hjug.metrics.rules;
22

3+
import java.util.HashSet;
4+
import java.util.List;
5+
import java.util.Set;
36
import net.sourceforge.pmd.lang.ast.Node;
47
import net.sourceforge.pmd.lang.java.ast.*;
58
import net.sourceforge.pmd.lang.java.rule.AbstractJavaRule;
@@ -9,18 +12,20 @@
912
import net.sourceforge.pmd.properties.PropertyFactory;
1013
import net.sourceforge.pmd.properties.constraints.NumericConstraints;
1114

12-
import java.util.HashSet;
13-
import java.util.List;
14-
import java.util.Set;
15-
1615
/**
1716
* Copy of PMD's CouplingBetweenObjectsRule
1817
* but generates the originally intended message containing coupling count
1918
*/
2019
public class CBORule extends AbstractJavaRule {
2120
private int couplingCount;
2221
private Set<String> typesFoundSoFar;
23-
private static final PropertyDescriptor<Integer> THRESHOLD_DESCRIPTOR = ((PropertyBuilder.GenericPropertyBuilder)((PropertyBuilder.GenericPropertyBuilder)((PropertyBuilder.GenericPropertyBuilder) PropertyFactory.intProperty("threshold").desc("Unique type reporting threshold")).require(NumericConstraints.positive())).defaultValue(20)).build();
22+
private static final PropertyDescriptor<Integer> THRESHOLD_DESCRIPTOR = ((PropertyBuilder.GenericPropertyBuilder)
23+
((PropertyBuilder.GenericPropertyBuilder)
24+
((PropertyBuilder.GenericPropertyBuilder) PropertyFactory.intProperty("threshold")
25+
.desc("Unique type reporting threshold"))
26+
.require(NumericConstraints.positive()))
27+
.defaultValue(20))
28+
.build();
2429

2530
public CBORule() {
2631
this.definePropertyDescriptor(THRESHOLD_DESCRIPTOR);
@@ -30,16 +35,19 @@ public Object visit(ASTCompilationUnit cu, Object data) {
3035
this.typesFoundSoFar = new HashSet();
3136
this.couplingCount = 0;
3237
Object returnObj = super.visit(cu, data);
33-
if (this.couplingCount > (Integer)this.getProperty(THRESHOLD_DESCRIPTOR)) {
34-
//only the line below is different from PMD's CouplingBetweenObjectsRule class
35-
this.addViolationWithMessage(data, cu, "A value of " + this.couplingCount + " may denote a high amount of coupling within the class");
38+
if (this.couplingCount > (Integer) this.getProperty(THRESHOLD_DESCRIPTOR)) {
39+
// only the line below is different from PMD's CouplingBetweenObjectsRule class
40+
this.addViolationWithMessage(
41+
data,
42+
cu,
43+
"A value of " + this.couplingCount + " may denote a high amount of coupling within the class");
3644
}
3745

3846
return returnObj;
3947
}
4048

4149
public Object visit(ASTResultType node, Object data) {
42-
for(int x = 0; x < node.getNumChildren(); ++x) {
50+
for (int x = 0; x < node.getNumChildren(); ++x) {
4351
Node tNode = node.getChild(x);
4452
if (tNode instanceof ASTType) {
4553
Node reftypeNode = tNode.getChild(0);
@@ -66,10 +74,10 @@ public Object visit(ASTFormalParameter node, Object data) {
6674
}
6775

6876
public Object visit(ASTFieldDeclaration node, Object data) {
69-
for(int x = 0; x < node.getNumChildren(); ++x) {
77+
for (int x = 0; x < node.getNumChildren(); ++x) {
7078
Node firstStmt = node.getChild(x);
7179
if (firstStmt instanceof ASTType) {
72-
ASTType tp = (ASTType)firstStmt;
80+
ASTType tp = (ASTType) firstStmt;
7381
Node nd = tp.getChild(0);
7482
this.checkVariableType(nd, nd.getImage());
7583
}
@@ -79,35 +87,49 @@ public Object visit(ASTFieldDeclaration node, Object data) {
7987
}
8088

8189
private void handleASTTypeChildren(Node node) {
82-
for(int x = 0; x < node.getNumChildren(); ++x) {
90+
for (int x = 0; x < node.getNumChildren(); ++x) {
8391
Node sNode = node.getChild(x);
8492
if (sNode instanceof ASTType) {
8593
Node nameNode = sNode.getChild(0);
8694
this.checkVariableType(nameNode, nameNode.getImage());
8795
}
8896
}
89-
9097
}
9198

9299
private void checkVariableType(Node nameNode, String variableType) {
93-
List<ASTClassOrInterfaceDeclaration> parentTypes = nameNode.getParentsOfType(ASTClassOrInterfaceDeclaration.class);
100+
List<ASTClassOrInterfaceDeclaration> parentTypes =
101+
nameNode.getParentsOfType(ASTClassOrInterfaceDeclaration.class);
94102
if (!parentTypes.isEmpty()) {
95-
if (!((ASTClassOrInterfaceDeclaration)parentTypes.get(0)).isInterface()) {
96-
ClassScope clzScope = (ClassScope)((JavaNode)nameNode).getScope().getEnclosingScope(ClassScope.class);
97-
if (!clzScope.getClassName().equals(variableType) && !this.filterTypes(variableType) && !this.typesFoundSoFar.contains(variableType)) {
103+
if (!((ASTClassOrInterfaceDeclaration) parentTypes.get(0)).isInterface()) {
104+
ClassScope clzScope =
105+
(ClassScope) ((JavaNode) nameNode).getScope().getEnclosingScope(ClassScope.class);
106+
if (!clzScope.getClassName().equals(variableType)
107+
&& !this.filterTypes(variableType)
108+
&& !this.typesFoundSoFar.contains(variableType)) {
98109
++this.couplingCount;
99110
this.typesFoundSoFar.add(variableType);
100111
}
101-
102112
}
103113
}
104114
}
105115

106116
private boolean filterTypes(String variableType) {
107-
return variableType != null && (variableType.startsWith("java.lang.") || "String".equals(variableType) || this.filterPrimitivesAndWrappers(variableType));
117+
return variableType != null
118+
&& (variableType.startsWith("java.lang.")
119+
|| "String".equals(variableType)
120+
|| this.filterPrimitivesAndWrappers(variableType));
108121
}
109122

110123
private boolean filterPrimitivesAndWrappers(String variableType) {
111-
return "int".equals(variableType) || "Integer".equals(variableType) || "char".equals(variableType) || "Character".equals(variableType) || "double".equals(variableType) || "long".equals(variableType) || "short".equals(variableType) || "float".equals(variableType) || "byte".equals(variableType) || "boolean".equals(variableType);
124+
return "int".equals(variableType)
125+
|| "Integer".equals(variableType)
126+
|| "char".equals(variableType)
127+
|| "Character".equals(variableType)
128+
|| "double".equals(variableType)
129+
|| "long".equals(variableType)
130+
|| "short".equals(variableType)
131+
|| "float".equals(variableType)
132+
|| "byte".equals(variableType)
133+
|| "boolean".equals(variableType);
112134
}
113135
}

0 commit comments

Comments
 (0)