mirror of
https://gitee.com/dcren/initializr.git
synced 2025-07-15 23:13:30 +08:00
Migrate ProjectAssert to java.nio.file.Path
This commit is contained in:
parent
b8cac0407b
commit
62d0a94364
@ -102,7 +102,7 @@ public abstract class AbstractComplianceTests {
|
|||||||
ProjectStructure projectStructure = projectTester
|
ProjectStructure projectStructure = projectTester
|
||||||
.generate(new ProjectDescription());
|
.generate(new ProjectDescription());
|
||||||
Path resolve = projectStructure.resolve("");
|
Path resolve = projectStructure.resolve("");
|
||||||
return new ProjectAssert(resolve.toFile());
|
return new ProjectAssert(resolve);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void setupProjectGenerationContext(InitializrMetadata metadata,
|
private void setupProjectGenerationContext(InitializrMetadata metadata,
|
||||||
|
@ -16,20 +16,19 @@
|
|||||||
|
|
||||||
package io.spring.initializr.generator.spring.test;
|
package io.spring.initializr.generator.spring.test;
|
||||||
|
|
||||||
import java.io.File;
|
import java.nio.file.Files;
|
||||||
import java.io.FileInputStream;
|
import java.nio.file.Path;
|
||||||
import java.io.IOException;
|
|
||||||
import java.nio.charset.Charset;
|
|
||||||
import java.util.Properties;
|
import java.util.Properties;
|
||||||
|
|
||||||
import io.spring.initializr.generator.spring.test.build.GradleBuildAssert;
|
import io.spring.initializr.generator.spring.test.build.GradleBuildAssert;
|
||||||
import io.spring.initializr.generator.spring.test.build.GradleSettingsAssert;
|
import io.spring.initializr.generator.spring.test.build.GradleSettingsAssert;
|
||||||
import io.spring.initializr.generator.spring.test.build.PomAssert;
|
import io.spring.initializr.generator.spring.test.build.PomAssert;
|
||||||
import io.spring.initializr.generator.spring.test.code.SourceCodeAssert;
|
import io.spring.initializr.generator.spring.test.code.SourceCodeAssert;
|
||||||
|
import io.spring.initializr.generator.test.io.TextTestUtils;
|
||||||
|
import io.spring.initializr.generator.test.project.ProjectStructure;
|
||||||
|
|
||||||
import org.springframework.core.io.FileSystemResource;
|
import org.springframework.core.io.FileSystemResource;
|
||||||
import org.springframework.core.io.support.PropertiesLoaderUtils;
|
import org.springframework.core.io.support.PropertiesLoaderUtils;
|
||||||
import org.springframework.util.StreamUtils;
|
|
||||||
import org.springframework.util.StringUtils;
|
import org.springframework.util.StringUtils;
|
||||||
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
@ -45,24 +44,18 @@ public class ProjectAssert {
|
|||||||
|
|
||||||
public static final String DEFAULT_APPLICATION_NAME = "DemoApplication";
|
public static final String DEFAULT_APPLICATION_NAME = "DemoApplication";
|
||||||
|
|
||||||
private final File dir;
|
private final ProjectStructure projectStructure;
|
||||||
|
|
||||||
private Boolean mavenProject;
|
|
||||||
|
|
||||||
public File getDir() {
|
|
||||||
return this.dir;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Boolean getMavenProject() {
|
|
||||||
return this.mavenProject;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a new instance with the directory holding the generated project.
|
* Create a new instance with the directory holding the generated project.
|
||||||
* @param dir the directory of the project
|
* @param dir the directory of the project
|
||||||
*/
|
*/
|
||||||
public ProjectAssert(File dir) {
|
public ProjectAssert(Path dir) {
|
||||||
this.dir = dir;
|
this.projectStructure = new ProjectStructure(dir);
|
||||||
|
}
|
||||||
|
|
||||||
|
public ProjectStructure getProjectStructure() {
|
||||||
|
return this.projectStructure;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -75,9 +68,9 @@ public class ProjectAssert {
|
|||||||
* @return an updated project assert on that base directory
|
* @return an updated project assert on that base directory
|
||||||
*/
|
*/
|
||||||
public ProjectAssert hasBaseDir(String name) {
|
public ProjectAssert hasBaseDir(String name) {
|
||||||
File projectDir = file(name);
|
Path projectDir = this.projectStructure.resolve(name);
|
||||||
assertThat(projectDir).describedAs("No directory %s found in %s", name,
|
assertThat(projectDir).describedAs("No directory %s found in %s", name,
|
||||||
this.dir.getAbsolutePath()).exists();
|
this.projectStructure.getProjectDirectory()).exists();
|
||||||
assertThat(projectDir).isDirectory();
|
assertThat(projectDir).isDirectory();
|
||||||
// Replacing the root dir so that other assertions match the root
|
// Replacing the root dir so that other assertions match the root
|
||||||
return new ProjectAssert(projectDir);
|
return new ProjectAssert(projectDir);
|
||||||
@ -88,13 +81,8 @@ public class ProjectAssert {
|
|||||||
* @return a POM assert
|
* @return a POM assert
|
||||||
*/
|
*/
|
||||||
public PomAssert pomAssert() {
|
public PomAssert pomAssert() {
|
||||||
try {
|
return new PomAssert(
|
||||||
return new PomAssert(StreamUtils.copyToString(
|
TextTestUtils.readContent(this.projectStructure.resolve("pom.xml")));
|
||||||
new FileInputStream(file("pom.xml")), Charset.forName("UTF-8")));
|
|
||||||
}
|
|
||||||
catch (IOException ex) {
|
|
||||||
throw new IllegalArgumentException("Cannot resolve pom.xml", ex);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -102,13 +90,8 @@ public class ProjectAssert {
|
|||||||
* @return a gradle assert
|
* @return a gradle assert
|
||||||
*/
|
*/
|
||||||
public GradleBuildAssert gradleBuildAssert() {
|
public GradleBuildAssert gradleBuildAssert() {
|
||||||
try {
|
return new GradleBuildAssert(
|
||||||
return new GradleBuildAssert(StreamUtils.copyToString(
|
TextTestUtils.readContent(this.projectStructure.resolve("build.gradle")));
|
||||||
new FileInputStream(file("build.gradle")), Charset.forName("UTF-8")));
|
|
||||||
}
|
|
||||||
catch (IOException ex) {
|
|
||||||
throw new IllegalArgumentException("Cannot resolve build.gradle", ex);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -116,14 +99,8 @@ public class ProjectAssert {
|
|||||||
* @return A gradle settings assert
|
* @return A gradle settings assert
|
||||||
*/
|
*/
|
||||||
public GradleSettingsAssert gradleSettingsAssert() {
|
public GradleSettingsAssert gradleSettingsAssert() {
|
||||||
try {
|
return new GradleSettingsAssert(TextTestUtils
|
||||||
return new GradleSettingsAssert(
|
.readContent(this.projectStructure.resolve("settings.gradle")));
|
||||||
StreamUtils.copyToString(new FileInputStream(file("settings.gradle")),
|
|
||||||
Charset.forName("UTF-8")));
|
|
||||||
}
|
|
||||||
catch (IOException ex) {
|
|
||||||
throw new IllegalArgumentException("Cannot resolve settings.gradle", ex);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -133,21 +110,14 @@ public class ProjectAssert {
|
|||||||
*/
|
*/
|
||||||
public SourceCodeAssert sourceCodeAssert(String sourceCodePath) {
|
public SourceCodeAssert sourceCodeAssert(String sourceCodePath) {
|
||||||
hasFile(sourceCodePath);
|
hasFile(sourceCodePath);
|
||||||
try {
|
return new SourceCodeAssert(sourceCodePath,
|
||||||
return new SourceCodeAssert(sourceCodePath, StreamUtils.copyToString(
|
TextTestUtils.readContent(this.projectStructure.resolve(sourceCodePath)));
|
||||||
new FileInputStream(file(sourceCodePath)), Charset.forName("UTF-8")));
|
|
||||||
}
|
|
||||||
catch (IOException ex) {
|
|
||||||
throw new IllegalArgumentException("Cannot resolve path: " + sourceCodePath,
|
|
||||||
ex);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public ProjectAssert isMavenProject() {
|
public ProjectAssert isMavenProject() {
|
||||||
hasFile("pom.xml").hasNoFile("build.gradle");
|
hasFile("pom.xml").hasNoFile("build.gradle");
|
||||||
hasFile("mvnw", "mvnw.cmd", ".mvn/wrapper/maven-wrapper.properties",
|
hasFile("mvnw", "mvnw.cmd", ".mvn/wrapper/maven-wrapper.properties",
|
||||||
".mvn/wrapper/maven-wrapper.jar");
|
".mvn/wrapper/maven-wrapper.jar");
|
||||||
this.mavenProject = true;
|
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -155,7 +125,6 @@ public class ProjectAssert {
|
|||||||
hasFile("build.gradle").hasNoFile("pom.xml");
|
hasFile("build.gradle").hasNoFile("pom.xml");
|
||||||
hasFile("gradlew", "gradlew.bat", "gradle/wrapper/gradle-wrapper.properties",
|
hasFile("gradlew", "gradlew.bat", "gradle/wrapper/gradle-wrapper.properties",
|
||||||
"gradle/wrapper/gradle-wrapper.jar");
|
"gradle/wrapper/gradle-wrapper.jar");
|
||||||
this.mavenProject = false;
|
|
||||||
if (StringUtils.hasText(version)) {
|
if (StringUtils.hasText(version)) {
|
||||||
Properties properties = properties(
|
Properties properties = properties(
|
||||||
"gradle/wrapper/gradle-wrapper.properties");
|
"gradle/wrapper/gradle-wrapper.properties");
|
||||||
@ -234,9 +203,8 @@ public class ProjectAssert {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public ProjectAssert hasFile(String... localPaths) {
|
public ProjectAssert hasFile(String... localPaths) {
|
||||||
for (String localPath : localPaths) {
|
assertThat(this.projectStructure.getRelativePathsOfProjectFiles())
|
||||||
assertFile(localPath, true);
|
.contains(localPaths);
|
||||||
}
|
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -248,28 +216,23 @@ public class ProjectAssert {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public ProjectAssert hasNoFile(String... localPaths) {
|
public ProjectAssert hasNoFile(String... localPaths) {
|
||||||
for (String localPath : localPaths) {
|
assertThat(this.projectStructure.getRelativePathsOfProjectFiles())
|
||||||
assertFile(localPath, false);
|
.doesNotContain(localPaths);
|
||||||
}
|
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public ProjectAssert assertFile(String localPath, boolean exist) {
|
public ProjectAssert assertFile(String localPath, boolean exist) {
|
||||||
File candidate = file(localPath);
|
Path candidate = this.projectStructure.resolve(localPath);
|
||||||
assertThat(candidate.exists())
|
assertThat(Files.exists(candidate))
|
||||||
.describedAs("Invalid presence (%s) exist for %s", exist, localPath)
|
.describedAs("Invalid presence (%s) exist for %s", exist, localPath)
|
||||||
.isEqualTo(exist);
|
.isEqualTo(exist);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
private File file(String localPath) {
|
|
||||||
return new File(this.dir, localPath);
|
|
||||||
}
|
|
||||||
|
|
||||||
private Properties properties(String localPath) {
|
private Properties properties(String localPath) {
|
||||||
File f = file(localPath);
|
Path file = this.projectStructure.resolve(localPath);
|
||||||
try {
|
try {
|
||||||
return PropertiesLoaderUtils.loadProperties(new FileSystemResource(f));
|
return PropertiesLoaderUtils.loadProperties(new FileSystemResource(file));
|
||||||
}
|
}
|
||||||
catch (Exception ex) {
|
catch (Exception ex) {
|
||||||
throw new IllegalStateException("Cannot load Properties", ex);
|
throw new IllegalStateException("Cannot load Properties", ex);
|
||||||
|
@ -51,15 +51,25 @@ public final class TextTestUtils {
|
|||||||
* @return all lines from the file
|
* @return all lines from the file
|
||||||
*/
|
*/
|
||||||
public static List<String> readAllLines(Path source) {
|
public static List<String> readAllLines(Path source) {
|
||||||
|
String content = readContent(source);
|
||||||
|
assertThat(content).endsWith(System.lineSeparator());
|
||||||
|
return readAllLines(content);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Read the content from the specified {@link Path source}. Check the given
|
||||||
|
* {@code source} is a regular file.
|
||||||
|
* @param source a text file
|
||||||
|
* @return the content of the file
|
||||||
|
*/
|
||||||
|
public static String readContent(Path source) {
|
||||||
assertThat(source).isRegularFile();
|
assertThat(source).isRegularFile();
|
||||||
try {
|
try {
|
||||||
BufferedReader reader = Files.newBufferedReader(source,
|
BufferedReader reader = Files.newBufferedReader(source,
|
||||||
StandardCharsets.UTF_8);
|
StandardCharsets.UTF_8);
|
||||||
StringWriter writer = new StringWriter();
|
StringWriter writer = new StringWriter();
|
||||||
FileCopyUtils.copy(reader, writer);
|
FileCopyUtils.copy(reader, writer);
|
||||||
String content = writer.toString();
|
return writer.toString();
|
||||||
assertThat(content).endsWith(System.lineSeparator());
|
|
||||||
return readAllLines(content);
|
|
||||||
}
|
}
|
||||||
catch (IOException ex) {
|
catch (IOException ex) {
|
||||||
throw new IllegalStateException(ex);
|
throw new IllegalStateException(ex);
|
||||||
|
@ -16,11 +16,10 @@
|
|||||||
|
|
||||||
package io.spring.initializr.web;
|
package io.spring.initializr.web;
|
||||||
|
|
||||||
import java.io.File;
|
|
||||||
import java.io.FileOutputStream;
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.nio.charset.Charset;
|
import java.nio.charset.Charset;
|
||||||
|
import java.nio.file.Files;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
@ -69,7 +68,7 @@ public abstract class AbstractInitializrIntegrationTests {
|
|||||||
|
|
||||||
private static final ObjectMapper objectMapper = new ObjectMapper();
|
private static final ObjectMapper objectMapper = new ObjectMapper();
|
||||||
|
|
||||||
public File folder;
|
public Path folder;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private RestTemplateBuilder restTemplateBuilder;
|
private RestTemplateBuilder restTemplateBuilder;
|
||||||
@ -79,7 +78,7 @@ public abstract class AbstractInitializrIntegrationTests {
|
|||||||
@BeforeEach
|
@BeforeEach
|
||||||
public void before(@TempDir Path folder) {
|
public void before(@TempDir Path folder) {
|
||||||
this.restTemplate = this.restTemplateBuilder.build();
|
this.restTemplate = this.restTemplateBuilder.build();
|
||||||
this.folder = folder.toFile();
|
this.folder = folder;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected abstract String createUrl(String context);
|
protected abstract String createUrl(String context);
|
||||||
@ -199,9 +198,8 @@ public abstract class AbstractInitializrIntegrationTests {
|
|||||||
|
|
||||||
protected ProjectAssert projectAssert(byte[] content, ArchiveType archiveType) {
|
protected ProjectAssert projectAssert(byte[] content, ArchiveType archiveType) {
|
||||||
try {
|
try {
|
||||||
File archiveFile = writeArchive(content);
|
Path archiveFile = writeArchive(content);
|
||||||
|
Path project = this.folder.resolve("project");
|
||||||
File project = new File(this.folder, "project");
|
|
||||||
switch (archiveType) {
|
switch (archiveType) {
|
||||||
case ZIP:
|
case ZIP:
|
||||||
unzip(archiveFile, project);
|
unzip(archiveFile, project);
|
||||||
@ -217,30 +215,28 @@ public abstract class AbstractInitializrIntegrationTests {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void untar(File archiveFile, File project) {
|
private void untar(Path archiveFile, Path project) {
|
||||||
Untar expand = new Untar();
|
Untar expand = new Untar();
|
||||||
expand.setProject(new Project());
|
expand.setProject(new Project());
|
||||||
expand.setDest(project);
|
expand.setDest(project.toFile());
|
||||||
expand.setSrc(archiveFile);
|
expand.setSrc(archiveFile.toFile());
|
||||||
Untar.UntarCompressionMethod method = new Untar.UntarCompressionMethod();
|
Untar.UntarCompressionMethod method = new Untar.UntarCompressionMethod();
|
||||||
method.setValue("gzip");
|
method.setValue("gzip");
|
||||||
expand.setCompression(method);
|
expand.setCompression(method);
|
||||||
expand.execute();
|
expand.execute();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void unzip(File archiveFile, File project) {
|
private void unzip(Path archiveFile, Path project) {
|
||||||
Expand expand = new Expand();
|
Expand expand = new Expand();
|
||||||
expand.setProject(new Project());
|
expand.setProject(new Project());
|
||||||
expand.setDest(project);
|
expand.setDest(project.toFile());
|
||||||
expand.setSrc(archiveFile);
|
expand.setSrc(archiveFile.toFile());
|
||||||
expand.execute();
|
expand.execute();
|
||||||
}
|
}
|
||||||
|
|
||||||
protected File writeArchive(byte[] body) throws IOException {
|
protected Path writeArchive(byte[] body) throws IOException {
|
||||||
File archiveFile = new File(this.folder, "archive");
|
Path archiveFile = this.folder.resolve("archive");
|
||||||
try (FileOutputStream stream = new FileOutputStream(archiveFile)) {
|
Files.write(archiveFile, body);
|
||||||
stream.write(body);
|
|
||||||
}
|
|
||||||
return archiveFile;
|
return archiveFile;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -91,8 +91,8 @@ public class ProjectGenerationInvokerTests {
|
|||||||
request.initialize(metadata);
|
request.initialize(metadata);
|
||||||
ProjectGenerationResult result = this.invoker
|
ProjectGenerationResult result = this.invoker
|
||||||
.invokeProjectStructureGeneration(request);
|
.invokeProjectStructureGeneration(request);
|
||||||
|
new ProjectAssert(result.getRootDirectory()).isJavaProject();
|
||||||
File file = result.getRootDirectory().toFile();
|
File file = result.getRootDirectory().toFile();
|
||||||
new ProjectAssert(file).isJavaProject();
|
|
||||||
Map<String, List<File>> tempFiles = (Map<String, List<File>>) ReflectionTestUtils
|
Map<String, List<File>> tempFiles = (Map<String, List<File>>) ReflectionTestUtils
|
||||||
.getField(this.invoker, "temporaryFiles");
|
.getField(this.invoker, "temporaryFiles");
|
||||||
assertThat(tempFiles.get(file.getName())).contains(file);
|
assertThat(tempFiles.get(file.getName())).contains(file);
|
||||||
|
Loading…
Reference in New Issue
Block a user