Skip to content

Commit

Permalink
feat(#314): first naive implementation of RuleInherianceInTests
Browse files Browse the repository at this point in the history
  • Loading branch information
volodya-lombrozo committed Jan 20, 2024
1 parent bf917b2 commit 8411be2
Show file tree
Hide file tree
Showing 8 changed files with 202 additions and 6 deletions.
6 changes: 6 additions & 0 deletions docs/rules/inheritance-in-tests.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Inheritance in tests

@todo #314:90min Add documentation for the RuleInheritenceInTests.java.
The documentation should be added to the `docs/rules/inheritance-in-tests.md` file.
The documentation should contain the description of the rule and the
examples of the correct and incorrect code.
2 changes: 1 addition & 1 deletion src/it/all-incorrect/verify.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,6 @@ String log = new File(basedir, 'build.log').text;
"Test name 'testAnother' doesn't follow naming rules, because test name doesn't have to contain the word 'test'",
"Test name 'testAnother' doesn't follow naming rules, because the test name has to be written using present tense",
"Method 'containsLineHitter' contains line hitter anti-pattern",
"The class 'UsesInheritenceTest' uses inheritance in tests, which is dangerous",
"The test class 'UsesInheritenceTest' uses inheritance, which is dangerous for maintainability",
].each { assert log.contains(it): "Log doesn't contain ['$it']" }
true
17 changes: 15 additions & 2 deletions src/main/java/com/github/lombrozo/testnames/TestClass.java
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ public Fake(final String name, final TestCase... all) {
* @param suppressed Suppressed rules
*/
public Fake(final List<String> suppressed) {
this("FakeClassTest", Collections.emptyList(), suppressed);
this(Fake.DEFAULT_NAME, Collections.emptyList(), suppressed);
}

/**
Expand All @@ -155,7 +155,7 @@ public Fake(final List<String> suppressed) {
* @param all All cases
*/
public Fake(final List<String> suppressed, final TestCase... all) {
this("FakeClassTest", Arrays.asList(all), suppressed);
this(Fake.DEFAULT_NAME, Arrays.asList(all), suppressed);
}

/**
Expand Down Expand Up @@ -185,6 +185,19 @@ public Fake(final TestClassCharacteristics props) {
this(name, all, suppressed, new TestClassCharacteristics.Fake(false));
}

/**
* Constructor.
* @param parent The parent class name
*/
Fake(final String parent) {
this(
Fake.DEFAULT_NAME,
Collections.emptyList(),
Collections.emptyList(),
new TestClassCharacteristics.Fake(parent)
);
}

/**
* Main ctor.
* @param name The name of test class
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ public interface TestClassCharacteristics {
*/
int numberOfMethods();

/**
* The parent class name.
* @return The parent class name.
*/
String parent();

/**
* Fake implementation for test characteristics.
*
Expand All @@ -66,6 +72,11 @@ public interface TestClassCharacteristics {
@ToString
final class Fake implements TestClassCharacteristics {

/**
* The default parent class name.
*/
private static final String DEFAULT_PARENT = "java.lang.Object";

/**
* Is the test class a JUnit extension?
*/
Expand All @@ -86,12 +97,25 @@ final class Fake implements TestClassCharacteristics {
*/
private final boolean integration;

/**
* The parent class name.
*/
private final String parent;

/**
* Constructor.
* @param extension Is the test class a JUnit extension?
*/
public Fake(final boolean extension) {
this(extension, 0, 0, false);
this(extension, 0, 0, false, Fake.DEFAULT_PARENT);
}

/**
* Constructor.
* @param parent The parent class name.
*/
public Fake(final String parent) {
this(false, 0, 0, false, parent);
}

/**
Expand All @@ -100,7 +124,7 @@ public Fake(final boolean extension) {
* @param nmethods The total number of methods in the class.
*/
public Fake(final int ntests, final int nmethods) {
this(false, ntests, nmethods, false);
this(false, ntests, nmethods, false, Fake.DEFAULT_PARENT);
}

/**
Expand All @@ -115,12 +139,14 @@ public Fake(
final boolean extension,
final int ntests,
final int nmethods,
final boolean integration
final boolean integration,
final String parent
) {
this.junit = extension;
this.tests = ntests;
this.methods = nmethods;
this.integration = integration;
this.parent = parent;
}

@Override
Expand All @@ -142,6 +168,11 @@ public int numberOfTests() {
public int numberOfMethods() {
return this.methods;
}

@Override
public String parent() {
return this.parent;
}
}

/**
Expand Down Expand Up @@ -172,5 +203,10 @@ public int numberOfTests() {
public int numberOfMethods() {
return 0;
}

@Override
public String parent() {
return Fake.DEFAULT_PARENT;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,21 @@ public int numberOfMethods() {
return this.klass.getDeclaredMethods().length;
}

@Override
public String parent() {
try {
return this.klass.getSuperclass().getName();
} catch (final NotFoundException exception) {
throw new IllegalStateException(
String.format(
"Can't get parent of class %s",
this.klass.getName()
),
exception
);
}
}

/**
* Checks whether a method is test-method.
* @param method To check.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,19 @@ public int numberOfTests() {
public int numberOfMethods() {
return (int) this.klass.methods().count();
}

@Override
public String parent() {
return this.klass.parents()
.stream()
.findFirst()
.orElseThrow(
() -> new IllegalStateException(
String.format(
"Can't get parent of class %s",
this.klass
)
)
).getName();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* MIT License
*
* Copyright (c) 2022-2023 Volodya
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package com.github.lombrozo.testnames.rules;

import com.github.lombrozo.testnames.Complaint;
import com.github.lombrozo.testnames.Rule;
import com.github.lombrozo.testnames.TestClass;
import com.github.lombrozo.testnames.complaints.ComplaintLinked;
import java.util.Collection;
import java.util.Collections;

/**
* The rule that checks a test class doesn't use inheritance.
* @since 1.2.0
*/
public final class RuleInheritenceInTests implements Rule {

/**
* Test class.
*/
private final TestClass clazz;

/**
* Constructor.
* @param clazz Test class.
*/
public RuleInheritenceInTests(final TestClass clazz) {
this.clazz = clazz;
}

@Override
public Collection<Complaint> complaints() {
final String parent = this.clazz.characteristics().parent();
if (parent.equals("java.lang.Object")) {
return Collections.emptyList();
} else {
return Collections.singleton(
new ComplaintLinked(
String.format(
"The test class '%s' has parent class '%s', inheritance in tests is dangerous for maintainability",
this.clazz.name(),
parent
),
String.format(
"Please rename remove all the parents classes from the %s test class",
this.clazz.name()
),
this.getClass(),
"inheritance-in-tests.md"
)
);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.github.lombrozo.testnames.rules;

import com.github.lombrozo.testnames.TestClass;
import com.github.lombrozo.testnames.TestClassCharacteristics;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.Test;

class RuleInheritenceInTestsTest {

@Test
void doesNotFoundParents() {
MatcherAssert.assertThat(
"Should not find any complaints if there are no parents",
new RuleInheritenceInTests(new TestClass.Fake()).complaints(),
Matchers.emptyIterable()
);
}

@Test
void hasSomeParent() {
final String parent = "some.parent.Parent";
final TestClass.Fake clazz = new TestClass.Fake(new TestClassCharacteristics.Fake(parent));
MatcherAssert.assertThat(
"Should find complaint if there is a parent",
new RuleInheritenceInTests(clazz).complaints().iterator().next().message(),
Matchers.containsString(
String.format(
"The test class '%s' has parent class '%s', inheritance in tests is dangerous for maintainability",
clazz.name(),
parent
)
)
);
}
}

0 comments on commit 8411be2

Please sign in to comment.