Skip to content

Commit

Permalink
#15 some fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
l3r8yJ committed Jan 16, 2023
1 parent fec2264 commit 5135c5f
Show file tree
Hide file tree
Showing 27 changed files with 851 additions and 42 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class UserTest {
}

@Test
public void createsUserWithotName() {
public void createsUserWithoutName() {
// correct
}

Expand Down
31 changes: 27 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.github.volodya-lombrozo</groupId>
<artifactId>test-naming-conventions</artifactId>
Expand Down Expand Up @@ -42,7 +40,6 @@
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>

<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
Expand Down Expand Up @@ -123,6 +120,32 @@
</pluginManagement>
</build>
<profiles>
<profile>
<id>qulice</id>
<build>
<plugins>
<plugin>
<groupId>com.qulice</groupId>
<artifactId>qulice-maven-plugin</artifactId>
<version>0.22.0</version>
<configuration>
<license>file:${basedir}/LICENSE</license>
<excludes>
<exclude>duplicatefinder:.*</exclude>
<exclude>checkstyle: /src/test/java/resources/.*</exclude>
</excludes>
</configuration>
<executions>
<execution>
<goals>
<goal>check</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>release</id>
<activation>
Expand Down
46 changes: 45 additions & 1 deletion src/main/java/com/github/lombrozo/testnames/Cases.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,65 @@
/*
* MIT License
*
* Copyright (c) 2022 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;

import java.util.Arrays;
import java.util.Collection;

/**
* The bulk of test cases.
*
* @since 0.1.0
*/
@FunctionalInterface
public interface Cases {

/**
* All cases.
*
* @return All cases as collection
*/
Collection<TestCase> all();

/**
* Correct cases.
*
* @return Correct bulk as list
*/
static Cases correct() {
return () -> Arrays.asList(
new TestCase.FakeCase("removes"),
new TestCase.FakeCase("creates")
);
}

static Cases wrong(){
/**
* Wrong cases.
*
* @return Wrong bulk as list
*/
static Cases wrong() {
return () -> Arrays.asList(
new TestCase.FakeCase("remove"),
new TestCase.FakeCase("test")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,27 @@
/*
* MIT License
*
* Copyright (c) 2022 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;

import java.io.IOException;
Expand All @@ -8,10 +32,21 @@
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
* The rule for composite test.
*
* @since 0.1.0
*/
final class CompositeTestPathRule implements Rule {

/**
* The start.
*/
private final Path start;

/**
* @param path The start
*/
CompositeTestPathRule(final Path path) {
this.start = path;
}
Expand Down
49 changes: 48 additions & 1 deletion src/main/java/com/github/lombrozo/testnames/JavaParserCase.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,62 @@
/*
* MIT License
*
* Copyright (c) 2022 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;

import java.nio.file.Path;
import lombok.Data;

/**
* Parser for test case.
*
* @since 0.1.0
*/
@Data
class JavaParserCase implements TestCase {
final class JavaParserCase implements TestCase {

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

/**
* The name of test case.
*/
private final String name;

/**
* The path.
*/
private final Path path;

/**
* Ctor.
*
* @param className The class name
* @param name The test case name
* @param path The path
*/
JavaParserCase(final String className, final String name, final Path path) {
this.className = className;
this.name = name;
Expand Down
87 changes: 76 additions & 11 deletions src/main/java/com/github/lombrozo/testnames/JavaTestCode.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,53 @@
/*
* MIT License
*
* Copyright (c) 2022 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;

import com.github.javaparser.ParseProblemException;
import com.github.javaparser.StaticJavaParser;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.Node;
import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration;
import com.github.javaparser.ast.body.MethodDeclaration;
import java.io.IOException;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

public final class JavaTestCode implements Cases {

/**
* Path to java class.
*/
private final Path path;

/**
* Ctor.
*
* @param path Path to the class
*/
public JavaTestCode(final Path path) {
this.path = path;
}
Expand All @@ -26,24 +60,55 @@ public Collection<TestCase> all() {
final List<TestCase> names = new ArrayList<>();
for (final Node childNode : childNodes) {
if (childNode instanceof ClassOrInterfaceDeclaration) {
final ClassOrInterfaceDeclaration clazz = (ClassOrInterfaceDeclaration) childNode;
clazz.getMethods().forEach(m -> {
if (!m.isPrivate() && (
m.isAnnotationPresent("Test")
|| m.isAnnotationPresent("ParameterizedTest"))) {

final String name = m.getNameAsString();
names.add(new JavaParserCase(clazz.getNameAsString(), name, this.path));
}
});
this.checkTestMethods(
names,
(ClassOrInterfaceDeclaration) childNode
);
}
}
return names;
} catch (IOException | ParseProblemException ex) {
} catch (final IOException | ParseProblemException ex) {
throw new IllegalStateException(
String.format("Failed to parse Java class by path %s", this.path),
ex
);
}
}

/**
* Checks methods in class.
*
* @param names List of test cases
* @param childNode list child nodes
*/
private void checkTestMethods(
final List<TestCase> names,
final ClassOrInterfaceDeclaration childNode
) {
for (final MethodDeclaration method : childNode.getMethods()) {
if (JavaTestCode.isTest(method)) {
names.add(
new JavaParserCase(
childNode.getNameAsString(),
method.getNameAsString(),
this.path
)
);
}
}
}

/**
* Check if method test or parameterized test.
*
* @param method To check
* @return Result as boolean
*/
private static boolean isTest(final MethodDeclaration method) {
return !method.isPrivate()
&& (
method.isAnnotationPresent("Test")
|| method.isAnnotationPresent("ParameterizedTest")
);
}
}
Loading

0 comments on commit 5135c5f

Please sign in to comment.