Skip to content

Commit

Permalink
Leverage JUnit built-in temporary directory
Browse files Browse the repository at this point in the history
  • Loading branch information
scordio committed Oct 2, 2024
1 parent 53232c4 commit 6d82670
Show file tree
Hide file tree
Showing 7 changed files with 251 additions and 67 deletions.
19 changes: 19 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# EditorConfig: https://editorconfig.org/

# top-most EditorConfig file
root = true

# Unix-style newlines with a newline ending every file
[*]
end_of_line = lf
insert_final_newline = true
charset = utf-8

# 2 space indentation for java, xml and yml files
[*.{java,xml,yml,sh}]
indent_style = space
indent_size = 2

# Maven POM code convention
[pom.xml]
max_line_length = 205
8 changes: 2 additions & 6 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<!-- Needed to properly bring in the Maven dependencies, see http://maven.apache.org/surefire/maven-surefire-plugin/examples/class-loading.html -->
<surefire.useSystemClassLoader>false</surefire.useSystemClassLoader>
<!-- Dependency versions overriding -->
<junit-jupiter.version>5.11.1</junit-jupiter.version>
</properties>

<dependencyManagement>
Expand All @@ -35,12 +37,6 @@
<artifactId>guava</artifactId>
<version>33.3.1-jre</version>
</dependency>
<dependency>
<groupId>org.junit</groupId>
<artifactId>junit-bom</artifactId>
<version>5.11.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,9 +192,8 @@ public BaseAssertionGenerator() throws IOException {
* Creates a new <code>{@link BaseAssertionGenerator}</code> with the templates from the given directory.
*
* @param templatesDirectory path where to find templates
* @throws IOException if some template file could not be found or read
*/
public BaseAssertionGenerator(String templatesDirectory) throws IOException {
public BaseAssertionGenerator(String templatesDirectory) {
templateRegistry = DefaultTemplateRegistryProducer.create(templatesDirectory);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,60 +14,62 @@

import org.assertj.assertions.generator.data.cars.Car;
import org.assertj.assertions.generator.description.converter.ClassToClassDescriptionConverter;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;

import static org.assertj.assertions.generator.Template.Type.ABSTRACT_ASSERT_CLASS;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatNullPointerException;
import static org.assertj.core.api.Assertions.assertThatRuntimeException;

public class AssertionGeneratorOverrideTemplateTest {
class AssertionGeneratorOverrideTemplateTest {

private BaseAssertionGenerator assertionGenerator;
private ClassToClassDescriptionConverter converter;
private GenerationHandler genHandle;

@Rule
public final GenerationPathHandler genHandle = new GenerationPathHandler(
Paths.get("src/test/resources"));

@Before
public void before() throws IOException {
@BeforeEach
void before(@TempDir Path tempDir) throws IOException {
assertionGenerator = new BaseAssertionGenerator();
assertionGenerator.setDirectoryWhereAssertionFilesAreGenerated(genHandle.getRoot());
assertionGenerator.setDirectoryWhereAssertionFilesAreGenerated(tempDir.toFile());
converter = new ClassToClassDescriptionConverter();
genHandle = new GenerationHandler(tempDir, Paths.get("src/test/resources"));
}

@Test(expected = NullPointerException.class)
public void should_fail_if_custom_template_is_null() {
assertionGenerator.register(null);
@Test
void should_fail_if_custom_template_is_null() {
assertThatNullPointerException().isThrownBy(() -> assertionGenerator.register(null));
}

@Test(expected = NullPointerException.class)
public void should_fail_if_custom_template_content_is_null() {
assertionGenerator.register(new Template(Template.Type.ABSTRACT_ASSERT_CLASS, (File) null));
@Test
void should_fail_if_custom_template_content_is_null() {
assertThatNullPointerException().isThrownBy(() -> assertionGenerator.register(new Template(ABSTRACT_ASSERT_CLASS, (File) null)));
}

@Test(expected = RuntimeException.class)
public void should_fail_if_custom_template_content_cant_be_read() {
assertionGenerator.register(new Template(Template.Type.ABSTRACT_ASSERT_CLASS, new File("not_existing.template")));
@Test
void should_fail_if_custom_template_content_cant_be_read() {
assertThatRuntimeException().isThrownBy(() -> assertionGenerator.register(new Template(ABSTRACT_ASSERT_CLASS, new File("not_existing.template"))));
}

@Test
public void should_generate_assertion_with_custom_template() throws IOException {
void should_generate_assertion_with_custom_template() throws IOException {
assertionGenerator.register(new Template(Template.Type.HAS_FOR_WHOLE_NUMBER,
new File("customtemplates" + File.separator,
"custom_has_assertion_template_for_whole_number.txt")));
new File("customtemplates" + File.separator,
"custom_has_assertion_template_for_whole_number.txt")));

assertionGenerator.generateCustomAssertionFor(converter.convertToClassDescription(Car.class));
File expectedFile = genHandle.getResourcesDir().resolve("CarAssert.expected.txt").toAbsolutePath().toFile();
File actualFile = genHandle.fileGeneratedFor(Car.class);
// compile it!
genHandle.compileGeneratedFilesFor(Car.class);

assertThat(actualFile).hasSameContentAs(expectedFile);
assertThat(actualFile).hasSameTextualContentAs(expectedFile);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,38 +25,44 @@
import org.assertj.assertions.generator.data.nba.team.Team;
import org.assertj.assertions.generator.description.ClassDescription;
import org.assertj.assertions.generator.description.converter.ClassToClassDescriptionConverter;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

import java.io.File;
import java.io.IOException;
import java.nio.file.Paths;
import java.nio.file.Path;
import java.util.LinkedHashSet;
import java.util.Set;

import static com.google.common.collect.Sets.newLinkedHashSet;
import static org.assertj.assertions.generator.AssertionsEntryPointType.*;
import static org.assertj.assertions.generator.AssertionsEntryPointType.AUTO_CLOSEABLE_BDD_SOFT;
import static org.assertj.assertions.generator.AssertionsEntryPointType.AUTO_CLOSEABLE_SOFT;
import static org.assertj.assertions.generator.AssertionsEntryPointType.BDD;
import static org.assertj.assertions.generator.AssertionsEntryPointType.BDD_SOFT;
import static org.assertj.assertions.generator.AssertionsEntryPointType.JUNIT_BDD_SOFT;
import static org.assertj.assertions.generator.AssertionsEntryPointType.JUNIT_SOFT;
import static org.assertj.assertions.generator.AssertionsEntryPointType.SOFT;
import static org.assertj.assertions.generator.AssertionsEntryPointType.STANDARD;
import static org.assertj.assertions.generator.util.ClassUtil.collectClasses;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.contentOf;

public class AssertionsEntryPointGeneratorTest {
class AssertionsEntryPointGeneratorTest {

private BaseAssertionGenerator generator;
@TempDir
private Path tempDir;

@Rule
public final GenerationPathHandler genHandle = new GenerationPathHandler(
Paths.get("src/test/resources"));
private BaseAssertionGenerator generator;

@Before
public void beforeEachTest() throws IOException {
@BeforeEach
void before() throws IOException {
generator = new BaseAssertionGenerator();
generator.setDirectoryWhereAssertionFilesAreGenerated(genHandle.getRoot());
generator.setDirectoryWhereAssertionFilesAreGenerated(tempDir.toFile());
}

@Test
public void should_generate_standard_assertions_entry_point_class_file() throws Exception {
void should_generate_standard_assertions_entry_point_class_file() throws Exception {
// GIVEN : classes we want to have entry point assertions for
Set<ClassDescription> classDescriptionSet = getClassDescriptionsOf(Ring.class, Race.class, ArtWork.class,
Name.class, Player.class, Movie.class,
Expand All @@ -72,7 +78,7 @@ public void should_generate_standard_assertions_entry_point_class_file() throws
}

@Test
public void should_generate_correctly_standard_assertions_entry_point_class_for_classes_with_same_name()
void should_generate_correctly_standard_assertions_entry_point_class_for_classes_with_same_name()
throws Exception {
// GIVEN : classes we want to have entry point assertions for
Set<ClassDescription> classDescriptionSet = getClassDescriptionsOf(Team.class,
Expand All @@ -86,7 +92,7 @@ public void should_generate_correctly_standard_assertions_entry_point_class_for_
}

@Test
public void should_generate_assertion_entry_point_class_file_with_custom_package() throws Exception {
void should_generate_assertion_entry_point_class_file_with_custom_package() throws Exception {
// GIVEN : classes we want to have entry point assertions for
Set<ClassDescription> classDescriptionSet = getClassDescriptionsOf(Ring.class, Race.class, ArtWork.class,
Name.class, Player.class, Movie.class,
Expand All @@ -99,11 +105,11 @@ public void should_generate_assertion_entry_point_class_file_with_custom_package
String expectedContent = readExpectedContentFromFile("AssertionsWithCustomPackage.expected.txt");
assertThat(assertionsEntryPointFile).as("check entry point class content")
.hasContent(expectedContent)
.hasParent(genHandle.getRoot().toPath().resolve("my/custom/package").toFile());
.hasParent(tempDir.resolve("my/custom/package").toFile());
}

@Test
public void should_generate_bdd_assertion_entry_point_class_file() throws Exception {
void should_generate_bdd_assertion_entry_point_class_file() throws Exception {
// GIVEN : classes we want to have entry point assertions for
Set<ClassDescription> classDescriptionSet = getClassDescriptionsOf(Ring.class, Race.class, ArtWork.class,
Name.class, Player.class, Movie.class,
Expand All @@ -117,7 +123,7 @@ public void should_generate_bdd_assertion_entry_point_class_file() throws Except
}

@Test
public void should_generate_soft_assertions_entry_point_class_file() throws Exception {
void should_generate_soft_assertions_entry_point_class_file() throws Exception {
// GIVEN : classes we want to have entry point assertions for
Set<ClassDescription> classDescriptionSet = getClassDescriptionsOf(Ring.class, Race.class, ArtWork.class,
Name.class, Player.class, Movie.class,
Expand All @@ -132,7 +138,7 @@ public void should_generate_soft_assertions_entry_point_class_file() throws Exce
}

@Test
public void should_generate_junit_soft_assertions_entry_point_class_file() throws Exception {
void should_generate_junit_soft_assertions_entry_point_class_file() throws Exception {
// GIVEN : classes we want to have entry point assertions for
Set<ClassDescription> classDescriptionSet = getClassDescriptionsOf(Ring.class, Race.class, ArtWork.class,
Name.class, Player.class, Movie.class,
Expand All @@ -149,7 +155,7 @@ public void should_generate_junit_soft_assertions_entry_point_class_file() throw
}

@Test
public void should_generate_bdd_soft_assertions_entry_point_class_file() throws Exception {
void should_generate_bdd_soft_assertions_entry_point_class_file() throws Exception {
// GIVEN : classes we want to have entry point assertions for
Set<ClassDescription> classDescriptionSet = getClassDescriptionsOf(Ring.class, Race.class, ArtWork.class,
Name.class, Player.class, Movie.class,
Expand All @@ -165,7 +171,7 @@ public void should_generate_bdd_soft_assertions_entry_point_class_file() throws
}

@Test
public void should_generate_junit_bdd_soft_assertions_entry_point_class_file() throws Exception {
void should_generate_junit_bdd_soft_assertions_entry_point_class_file() throws Exception {
// GIVEN : classes we want to have entry point assertions for
Set<ClassDescription> classDescriptionSet = getClassDescriptionsOf(Ring.class, Race.class, ArtWork.class,
Name.class, Player.class, Movie.class,
Expand All @@ -181,7 +187,7 @@ public void should_generate_junit_bdd_soft_assertions_entry_point_class_file() t
}

@Test
public void should_generate_auto_closeable_soft_assertions_entry_point_class_file() throws Exception {
void should_generate_auto_closeable_soft_assertions_entry_point_class_file() throws Exception {
// GIVEN : classes we want to have entry point assertions for
Set<ClassDescription> classDescriptionSet = getClassDescriptionsOf(Ring.class, Race.class, ArtWork.class,
Name.class, Player.class, Movie.class,
Expand All @@ -198,7 +204,7 @@ public void should_generate_auto_closeable_soft_assertions_entry_point_class_fil
}

@Test
public void should_generate_auto_closeable_bdd_soft_assertions_entry_point_class_file() throws Exception {
void should_generate_auto_closeable_bdd_soft_assertions_entry_point_class_file() throws Exception {
// GIVEN : classes we want to have entry point assertions for
Set<ClassDescription> classDescriptionSet = getClassDescriptionsOf(Ring.class, Race.class, ArtWork.class,
Name.class, Player.class, Movie.class,
Expand All @@ -215,7 +221,7 @@ public void should_generate_auto_closeable_bdd_soft_assertions_entry_point_class
}

@Test
public void should_generate_an_assertions_entry_point_class_file_that_matches_given_class_name() throws Exception {
void should_generate_an_assertions_entry_point_class_file_that_matches_given_class_name() throws Exception {
// GIVEN : custom entry point class template changing the class name.
Set<ClassDescription> classDescriptionSet = getClassDescriptionsOf(Ring.class);
generator.register(new Template(Template.Type.ASSERTIONS_ENTRY_POINT_CLASS,
Expand All @@ -232,7 +238,7 @@ public void should_generate_an_assertions_entry_point_class_file_that_matches_gi
}

@Test
public void should_return_null_assertion_entry_point_file_if_no_classes_description_are_given() throws Exception {
void should_return_null_assertion_entry_point_file_if_no_classes_description_are_given() throws Exception {
// GIVEN no ClassDescription
Set<ClassDescription> classDescriptionSet = newLinkedHashSet();
// THEN generated entry points file are null
Expand All @@ -243,7 +249,7 @@ public void should_return_null_assertion_entry_point_file_if_no_classes_descript
}

@Test
public void should_return_empty_assertion_entry_point_class_content_if_no_classes_description_are_given()
void should_return_empty_assertion_entry_point_class_content_if_no_classes_description_are_given()
throws Exception {
// GIVEN no ClassDescription
Set<ClassDescription> emptySet = newLinkedHashSet();
Expand All @@ -255,7 +261,7 @@ public void should_return_empty_assertion_entry_point_class_content_if_no_classe
}

@Test
public void should_return_null_assertion_entry_point_file_if_null_classes_description_are_given() throws Exception {
void should_return_null_assertion_entry_point_file_if_null_classes_description_are_given() throws Exception {
// GIVEN no ClassDescription
// THEN generated entry points file are null
for (AssertionsEntryPointType assertionsEntryPointType : AssertionsEntryPointType.values()) {
Expand All @@ -264,7 +270,7 @@ public void should_return_null_assertion_entry_point_file_if_null_classes_descri
}

@Test
public void should_return_empty_assertion_entry_point_class_if_null_classes_description_are_given() throws Exception {
void should_return_empty_assertion_entry_point_class_if_null_classes_description_are_given() throws Exception {
// GIVEN no ClassDescription
// THEN generated entry points file are null
for (AssertionsEntryPointType assertionsEntryPointType : AssertionsEntryPointType.values()) {
Expand All @@ -273,7 +279,7 @@ public void should_return_empty_assertion_entry_point_class_if_null_classes_desc
}

@Test
public void should_not_generate_assertion_entry_point_for_non_public_class_in_package() throws Exception {
void should_not_generate_assertion_entry_point_for_non_public_class_in_package() throws Exception {
// GIVEN package including a package private class
Set<ClassDescription> classDescriptions = getClassDescriptionsOf(collectClasses("org.assertj.assertions.generator.data"));
// WHEN
Expand All @@ -285,7 +291,7 @@ public void should_not_generate_assertion_entry_point_for_non_public_class_in_pa
}

@Test
public void should_not_generate_assertion_entry_point_for_non_public_class() throws Exception {
void should_not_generate_assertion_entry_point_for_non_public_class() throws Exception {
// GIVEN package including a package private class
Set<ClassDescription> classDescriptions = getClassDescriptionsOf(collectClasses("org.assertj.assertions.generator.data.inner.PackagePrivate"));
// WHEN
Expand Down
Loading

0 comments on commit 6d82670

Please sign in to comment.