mirror of
https://gitee.com/dcren/initializr.git
synced 2025-04-05 17:38:06 +08:00
Guard compilation unit from null
attributes
Closes gh-975
This commit is contained in:
parent
9964bb222c
commit
98bdd29356
@ -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;
|
||||
}
|
||||
|
@ -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();
|
||||
|
@ -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();
|
||||
|
@ -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();
|
||||
|
Loading…
Reference in New Issue
Block a user