Guard compilation unit from null attributes

Closes gh-975
This commit is contained in:
Stephane Nicoll 2019-08-07 15:18:05 +02:00
parent 9964bb222c
commit 98bdd29356
4 changed files with 56 additions and 0 deletions

View File

@ -20,6 +20,8 @@ import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.springframework.util.Assert;
/**
* A compilation unit that represents an individual source file.
*
@ -34,15 +36,30 @@ public abstract class CompilationUnit<T extends TypeDeclaration> {
private final List<T> typeDeclarations = new ArrayList<>();
/**
* Create a new instance with the package to use and the name of the type.
* @param packageName the package in which the source file should be located
* @param name the name of the file
*/
public CompilationUnit(String packageName, String name) {
Assert.hasText(packageName, "'packageName' must not be null");
Assert.hasText(name, "'name' must not be null");
this.packageName = packageName;
this.name = name;
}
/**
* Return the package name in which the file should reside.
* @return the package name
*/
public String getPackageName() {
return this.packageName;
}
/**
* Return the name of the source file.
* @return the name of the source file
*/
public String getName() {
return this.name;
}

View File

@ -31,6 +31,7 @@ import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
/**
* Tests for {@link GroovySourceCodeWriter}.
@ -46,6 +47,18 @@ class GroovySourceCodeWriterTests {
private final GroovySourceCodeWriter writer = new GroovySourceCodeWriter(
IndentingWriterFactory.withDefaultSettings());
@Test
void nullPackageInvalidCompilationUnit() {
GroovySourceCode sourceCode = new GroovySourceCode();
assertThatIllegalArgumentException().isThrownBy(() -> sourceCode.createCompilationUnit(null, "Test"));
}
@Test
void nullNameInvalidCompilationUnit() {
GroovySourceCode sourceCode = new GroovySourceCode();
assertThatIllegalArgumentException().isThrownBy(() -> sourceCode.createCompilationUnit("com.example", null));
}
@Test
void emptyCompilationUnit() throws IOException {
GroovySourceCode sourceCode = new GroovySourceCode();

View File

@ -31,6 +31,7 @@ import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
/**
* Tests for {@link JavaSourceCodeWriter}.
@ -45,6 +46,18 @@ class JavaSourceCodeWriterTests {
private final JavaSourceCodeWriter writer = new JavaSourceCodeWriter(IndentingWriterFactory.withDefaultSettings());
@Test
void nullPackageInvalidCompilationUnit() {
JavaSourceCode sourceCode = new JavaSourceCode();
assertThatIllegalArgumentException().isThrownBy(() -> sourceCode.createCompilationUnit(null, "Test"));
}
@Test
void nullNameInvalidCompilationUnit() {
JavaSourceCode sourceCode = new JavaSourceCode();
assertThatIllegalArgumentException().isThrownBy(() -> sourceCode.createCompilationUnit("com.example", null));
}
@Test
void emptyCompilationUnit() throws IOException {
JavaSourceCode sourceCode = new JavaSourceCode();

View File

@ -30,6 +30,7 @@ import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
/**
* Tests for {@link KotlinSourceCodeWriter}.
@ -45,6 +46,18 @@ class KotlinSourceCodeWriterTests {
private final KotlinSourceCodeWriter writer = new KotlinSourceCodeWriter(
IndentingWriterFactory.withDefaultSettings());
@Test
void nullPackageInvalidCompilationUnit() {
KotlinSourceCode sourceCode = new KotlinSourceCode();
assertThatIllegalArgumentException().isThrownBy(() -> sourceCode.createCompilationUnit(null, "Test"));
}
@Test
void nullNameInvalidCompilationUnit() {
KotlinSourceCode sourceCode = new KotlinSourceCode();
assertThatIllegalArgumentException().isThrownBy(() -> sourceCode.createCompilationUnit("com.example", null));
}
@Test
void emptyCompilationUnit() throws IOException {
KotlinSourceCode sourceCode = new KotlinSourceCode();