Skip to content

Commit

Permalink
Reduce footprint of GenerationPathHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
scordio committed Oct 1, 2024
1 parent 3cfdb78 commit 59f366d
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,21 @@
import java.io.IOException;
import java.nio.file.Paths;

import static org.assertj.core.api.Assertions.assertThat;

public class AssertionGeneratorOverrideTemplateTest {

private BaseAssertionGenerator assertionGenerator;
private ClassToClassDescriptionConverter converter;

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

@Before
public void before() throws IOException {
assertionGenerator = genHandle.buildAssertionGenerator();
assertionGenerator = new BaseAssertionGenerator();
assertionGenerator.setDirectoryWhereAssertionFilesAreGenerated(genHandle.getRoot());
converter = new ClassToClassDescriptionConverter();
}

Expand All @@ -58,6 +62,12 @@ public void should_generate_assertion_with_custom_template() throws IOException
"custom_has_assertion_template_for_whole_number.txt")));

assertionGenerator.generateCustomAssertionFor(converter.convertToClassDescription(Car.class));
genHandle.assertGeneratedAssertClass(Car.class, "CarAssert.expected.txt", true);
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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.assertj.assertions.generator.description.ClassDescription;
import org.assertj.assertions.generator.description.converter.AnnotationConfiguration;
import org.assertj.assertions.generator.description.converter.ClassToClassDescriptionConverter;
import org.assertj.core.api.Assertions;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
Expand All @@ -47,24 +48,27 @@
import static org.apache.commons.lang3.StringUtils.replace;
import static org.assertj.assertions.generator.util.ClassUtil.collectClasses;
import static org.assertj.core.api.Assertions.*;
import static org.assertj.core.api.Assertions.assertThat;

@RunWith(Theories.class)
public class AssertionGeneratorTest extends BeanWithExceptionsTest {

private static final String LINE_SEPARATOR = System.lineSeparator();
private static final Set<TypeToken<?>> EMPTY_HIERARCHY = new HashSet<>();
private static final Logger logger = LoggerFactory.getLogger(AssertionGeneratorTest.class);

private ClassToClassDescriptionConverter converter;
private BaseAssertionGenerator assertionGenerator;

@Rule
public final GenerationPathHandler generationPathHandler = new GenerationPathHandler(AssertionGeneratorTest.class,
Paths.get("src/test/resources"));
private static final Set<TypeToken<?>> EMPTY_HIERARCHY = new HashSet<>();
public final GenerationPathHandler generationPathHandler = new GenerationPathHandler(
Paths.get("src/test/resources"));

@Before
public void beforeEachTest() throws IOException {
converter = new ClassToClassDescriptionConverter();
assertionGenerator = generationPathHandler.buildAssertionGenerator();
assertionGenerator = new BaseAssertionGenerator();
assertionGenerator.setDirectoryWhereAssertionFilesAreGenerated(generationPathHandler.getRoot());
}

@Test
Expand Down Expand Up @@ -336,15 +340,25 @@ private void verifyFlatAssertionGenerationFor(Class<?> clazz) throws IOException
ClassDescription classDescription = converter.convertToClassDescription(clazz);
assertionGenerator.generateCustomAssertionFor(classDescription);
String expectedAssertFile = clazz.getSimpleName() + "Assert.flat.expected.txt";
generationPathHandler.assertGeneratedAssertClass(clazz, expectedAssertFile, true);
File expectedFile = generationPathHandler.getResourcesDir().resolve(expectedAssertFile).toAbsolutePath().toFile();
File actualFile = generationPathHandler.fileGeneratedFor(clazz);
// compile it!
generationPathHandler.compileGeneratedFilesFor(clazz);

assertThat(actualFile).hasSameTextualContentAs(expectedFile);
}

private void verifyFlatAssertionGenerationFor(Class<?> clazz, String generatedAssertionPackage) throws IOException {
String expectedAssertFile = clazz.getSimpleName() + "Assert.generated.in.custom.package.flat.expected.txt";
logger.info("Generating flat assertions for {} in package {}", clazz, generatedAssertionPackage);
ClassDescription classDescription = converter.convertToClassDescription(clazz);
assertionGenerator.generateCustomAssertionFor(classDescription);
generationPathHandler.assertGeneratedAssertClass(clazz, expectedAssertFile, true, generatedAssertionPackage);
File expectedFile = generationPathHandler.getResourcesDir().resolve(expectedAssertFile).toAbsolutePath().toFile();
File actualFile = generationPathHandler.fileGeneratedFor(clazz, generatedAssertionPackage);
// compile it!
generationPathHandler.compileGeneratedFilesFor(generatedAssertionPackage, clazz);

assertThat(actualFile).hasSameTextualContentAs(expectedFile);
}

private void verifyHierarchicalAssertionGenerationFor(Class<?> clazz) throws IOException {
Expand All @@ -363,10 +377,14 @@ private void verifyHierarchicalAssertionGenerationFor(Class<?> aClass,
generatedAssertFiles.addAll(asList(assertionGenerator.generateHierarchicalCustomAssertionFor(classDescription, typeHierarchy)));

String expectedConcreteAssertFile = clazz.getSimpleName() + "Assert.expected.txt";
generationPathHandler.assertGeneratedAssertClass(clazz, expectedConcreteAssertFile, false);
File expectedFile1 = generationPathHandler.getResourcesDir().resolve(expectedConcreteAssertFile).toAbsolutePath().toFile();
File actualFile = generationPathHandler.fileGeneratedFor(clazz);

assertThat(actualFile).hasSameTextualContentAs(expectedFile1);

String expectedAbstractAssertFile = "Abstract" + clazz.getSimpleName() + "Assert.expected.txt";
generationPathHandler.assertAbstractGeneratedAssertClass(clazz, expectedAbstractAssertFile);
File expectedFile = generationPathHandler.getResourcesDir().resolve(expectedAbstractAssertFile).toAbsolutePath().toFile();
assertThat(generationPathHandler.abstractFileGeneratedFor(clazz)).hasSameTextualContentAs(expectedFile);
}
generationPathHandler.compileGeneratedFiles(generatedAssertFiles);
}
Expand All @@ -382,10 +400,14 @@ private void verifyHierarchicalAssertionGenerationFor(Class<?> aClass, String ge
generatedAssertFiles.addAll(asList(assertionGenerator.generateHierarchicalCustomAssertionFor(classDescription, EMPTY_HIERARCHY)));

String expectedConcreteAssertFile = clazz.getSimpleName() + "Assert.generated.in.custom.package.expected.txt";
generationPathHandler.assertGeneratedAssertClass(clazz, expectedConcreteAssertFile, false, generatedAssertionPackage);
File expectedFile1 = generationPathHandler.getResourcesDir().resolve(expectedConcreteAssertFile).toAbsolutePath().toFile();
File actualFile = generationPathHandler.fileGeneratedFor(clazz, generatedAssertionPackage);

assertThat(actualFile).hasSameTextualContentAs(expectedFile1);

String expectedAbstractAssertFile = "Abstract" + clazz.getSimpleName() + "Assert.generated.in.custom.package.expected.txt";
generationPathHandler.assertAbstractGeneratedAssertClass(clazz, expectedAbstractAssertFile, generatedAssertionPackage);
File expectedFile = generationPathHandler.getResourcesDir().resolve(expectedAbstractAssertFile).toAbsolutePath().toFile();
assertThat(generationPathHandler.abstractFileGeneratedFor(clazz, generatedAssertionPackage)).hasSameTextualContentAs(expectedFile);
}
generationPathHandler.compileGeneratedFiles(generatedAssertFiles);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,17 @@
import static org.assertj.core.api.Assertions.contentOf;

public class AssertionsEntryPointGeneratorTest {

private BaseAssertionGenerator generator;

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

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

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@

import javax.tools.JavaFileObject;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
Expand All @@ -39,8 +38,8 @@

/**
* Test {@link org.junit.Rule} used to generate temporary folders per test case so there is no concern of
* tests interacting with each other. This utilizes the built-in {@link TemporaryFolder} to accomplish this and
* contains generator specific methods.
* tests interacting with each other. This uses the built-in {@link TemporaryFolder} to accomplish this and
* contains generator-specific methods.
*/
public class GenerationPathHandler extends TemporaryFolder {

Expand All @@ -49,9 +48,9 @@ public class GenerationPathHandler extends TemporaryFolder {
private static final ClassToClassDescriptionConverter CLASS_DESCRIPTION_CONVERTER = new ClassToClassDescriptionConverter();

private final Compiler compiler;
private Path resourcesDir;
private final Path resourcesDir;

public GenerationPathHandler(final Class<?> owningClass, Path resourcesDir) {
GenerationPathHandler(Path resourcesDir) {
super(DEFAULT_GENERATION_ROOT.toFile());

//noinspection ResultOfMethodCallIgnored
Expand All @@ -64,7 +63,7 @@ public GenerationPathHandler(final Class<?> owningClass, Path resourcesDir) {
.withOptions("-classpath", currentClasspath);
}

public Path getResourcesDir() {
Path getResourcesDir() {
return resourcesDir;
}

Expand All @@ -87,32 +86,26 @@ public void evaluate() throws Throwable {
};
}

public BaseAssertionGenerator buildAssertionGenerator() throws IOException {
BaseAssertionGenerator assertionGenerator = new BaseAssertionGenerator();
assertionGenerator.setDirectoryWhereAssertionFilesAreGenerated(getRoot());
return assertionGenerator;
}

public Path packagePathFor(Class<?> clazz) {
private Path packagePathFor(Class<?> clazz) {
return pathFromRoot(clazz.getPackage().getName().replace('.', File.separatorChar));
}

public File fileGeneratedFor(Class<?> clazz) {
File fileGeneratedFor(Class<?> clazz) {
String generatedFileName = CLASS_DESCRIPTION_CONVERTER.convertToClassDescription(clazz).getAssertClassFilename();
return packagePathFor(clazz).resolve(generatedFileName).toFile();
}

public File fileGeneratedFor(Class<?> clazz, String generatedAssertionPackage) {
File fileGeneratedFor(Class<?> clazz, String generatedAssertionPackage) {
String generatedFileName = CLASS_DESCRIPTION_CONVERTER.convertToClassDescription(clazz).getAssertClassFilename();
return pathFromRoot(generatedAssertionPackage).resolve(generatedFileName).toFile();
}

public File abstractFileGeneratedFor(Class<?> clazz) {
File abstractFileGeneratedFor(Class<?> clazz) {
String generatedFileName = CLASS_DESCRIPTION_CONVERTER.convertToClassDescription(clazz).getAbstractAssertClassFilename();
return packagePathFor(clazz).resolve(generatedFileName).toFile();
}

public File abstractFileGeneratedFor(Class<?> clazz, String generatedAssertionPackage) {
File abstractFileGeneratedFor(Class<?> clazz, String generatedAssertionPackage) {
String generatedFileName = CLASS_DESCRIPTION_CONVERTER.convertToClassDescription(clazz).getAbstractAssertClassFilename();
return pathFromRoot(generatedAssertionPackage).resolve(generatedFileName).toFile();
}
Expand All @@ -121,7 +114,7 @@ private Path pathFromRoot(String generatedAssertionPackage) {
return getRoot().toPath().resolve(generatedAssertionPackage.replace('.', File.separatorChar));
}

public void compileGeneratedFiles(Iterable<? extends File> files) {
void compileGeneratedFiles(Iterable<? extends File> files) {
List<JavaFileObject> javaFileObjects = toJavaFileObjects(files);
Compilation compilation = compiler.compile(javaFileObjects);
try {
Expand All @@ -144,7 +137,7 @@ private List<JavaFileObject> toJavaFileObjects(Iterable<? extends File> files) {
return javaFileObjects;
}

public void compileGeneratedFilesFor(Class<?>... classes) {
void compileGeneratedFilesFor(Class<?>... classes) {
List<File> files = new ArrayList<>(classes.length);
for (Class<?> clazz : classes) {
files.add(fileGeneratedFor(clazz));
Expand All @@ -157,7 +150,7 @@ public void compileGeneratedFilesFor(Class<?>... classes) {
compileGeneratedFiles(files);
}

public void compileGeneratedFilesFor(String generatedAssertionPackage, Class<?>... classes) {
void compileGeneratedFilesFor(String generatedAssertionPackage, Class<?>... classes) {
List<File> files = new ArrayList<>(classes.length);
for (Class<?> clazz : classes) {
files.add(fileGeneratedFor(clazz, generatedAssertionPackage));
Expand All @@ -170,35 +163,6 @@ public void compileGeneratedFilesFor(String generatedAssertionPackage, Class<?>.
compileGeneratedFiles(files);
}

public void assertGeneratedAssertClass(Class<?> clazz, String expectedAssertFile, final boolean compileGenerated) throws IOException {
File expectedFile = resourcesDir.resolve(expectedAssertFile).toAbsolutePath().toFile();
File actualFile = fileGeneratedFor(clazz);
// compile it!
if (compileGenerated) compileGeneratedFilesFor(clazz);

assertThat(actualFile).hasSameContentAs(expectedFile);
}

public void assertGeneratedAssertClass(Class<?> clazz, String expectedAssertFile, final boolean compileGenerated,
String generatedAssertionPackage) throws IOException {
File expectedFile = resourcesDir.resolve(expectedAssertFile).toAbsolutePath().toFile();
File actualFile = fileGeneratedFor(clazz, generatedAssertionPackage);
// compile it!
if (compileGenerated) compileGeneratedFilesFor(generatedAssertionPackage, clazz);

assertThat(actualFile).hasSameContentAs(expectedFile);
}

public void assertAbstractGeneratedAssertClass(Class<?> clazz, String expectedAssertFile) {
File expectedFile = resourcesDir.resolve(expectedAssertFile).toAbsolutePath().toFile();
assertThat(abstractFileGeneratedFor(clazz)).hasSameContentAs(expectedFile);
}

public void assertAbstractGeneratedAssertClass(Class<?> clazz, String expectedAssertFile, String generatedAssertionPackage) {
File expectedFile = resourcesDir.resolve(expectedAssertFile).toAbsolutePath().toFile();
assertThat(abstractFileGeneratedFor(clazz, generatedAssertionPackage)).hasSameContentAs(expectedFile);
}

/**
* Returns the current classpaths of the given classloader including its parents.
*
Expand Down Expand Up @@ -229,4 +193,5 @@ private static String getClasspathFromClassloader(ClassLoader currentClassloader

return Joiner.on(File.pathSeparatorChar).join(classpaths);
}

}

0 comments on commit 59f366d

Please sign in to comment.