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