Use Files.writeString() and Files.readString() where possible

See gh-31459
This commit is contained in:
dreis2211
2022-06-20 07:03:59 +02:00
committed by Stephane Nicoll
parent d9d1bdf0f6
commit fb45fc4819
9 changed files with 38 additions and 57 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2021 the original author or authors.
* Copyright 2012-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -19,7 +19,6 @@ package org.springframework.boot.gradle.tasks.buildinfo;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
@@ -101,11 +100,11 @@ class BuildInfoIntegrationTests {
@TestTemplate
void notUpToDateWhenExecutedTwiceWithFixedTimeAndChangedGradlePropertiesProjectVersion() throws IOException {
Path gradleProperties = new File(this.gradleBuild.getProjectDir(), "gradle.properties").toPath();
Files.write(gradleProperties, "version=0.1.0".getBytes(StandardCharsets.UTF_8), StandardOpenOption.CREATE,
StandardOpenOption.WRITE, StandardOpenOption.TRUNCATE_EXISTING);
Files.writeString(gradleProperties, "version=0.1.0", StandardOpenOption.CREATE, StandardOpenOption.WRITE,
StandardOpenOption.TRUNCATE_EXISTING);
assertThat(this.gradleBuild.build("buildInfo").task(":buildInfo").getOutcome()).isEqualTo(TaskOutcome.SUCCESS);
Files.write(gradleProperties, "version=0.2.0".getBytes(StandardCharsets.UTF_8), StandardOpenOption.CREATE,
StandardOpenOption.WRITE, StandardOpenOption.TRUNCATE_EXISTING);
Files.writeString(gradleProperties, "version=0.2.0", StandardOpenOption.CREATE, StandardOpenOption.WRITE,
StandardOpenOption.TRUNCATE_EXISTING);
assertThat(this.gradleBuild.build("buildInfo").task(":buildInfo").getOutcome()).isEqualTo(TaskOutcome.SUCCESS);
}

View File

@@ -22,11 +22,12 @@ import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.nio.file.attribute.PosixFilePermission;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.LinkedHashSet;
@@ -294,11 +295,11 @@ abstract class AbstractBootArchiveTests<T extends Jar & BootArchive> {
void customLaunchScriptCanBePrepended() throws IOException {
this.task.getMainClass().set("com.example.Main");
File customScript = new File(this.temp, "custom.script");
Files.write(customScript.toPath(), Arrays.asList("custom script"), StandardOpenOption.CREATE);
Files.writeString(customScript.toPath(), "custom script", StandardOpenOption.CREATE);
this.task.launchScript((configuration) -> configuration.setScript(customScript));
executeTask();
assertThat(Files.readAllBytes(this.task.getArchiveFile().get().getAsFile().toPath()))
.startsWith("custom script".getBytes());
Path path = this.task.getArchiveFile().get().getAsFile().toPath();
assertThat(Files.readString(path, StandardCharsets.ISO_8859_1)).startsWith("custom script");
}
@Test
@@ -310,10 +311,11 @@ abstract class AbstractBootArchiveTests<T extends Jar & BootArchive> {
configuration.getProperties().put("initInfoDescription", "description");
});
executeTask();
byte[] bytes = Files.readAllBytes(this.task.getArchiveFile().get().getAsFile().toPath());
assertThat(bytes).containsSequence("Provides: provides".getBytes());
assertThat(bytes).containsSequence("Short-Description: short description".getBytes());
assertThat(bytes).containsSequence("Description: description".getBytes());
Path path = this.task.getArchiveFile().get().getAsFile().toPath();
String content = Files.readString(path, StandardCharsets.ISO_8859_1);
assertThat(content).containsSequence("Provides: provides");
assertThat(content).containsSequence("Short-Description: short description");
assertThat(content).containsSequence("Description: description");
}
@Test