Merge pull request #17444 from dreis2211

* gh-17444:
  Use Assertions.contentOf() where possible

Closes gh-17444
pull/17390/head
Andy Wilkinson 5 years ago
commit 335f9590be

@ -18,8 +18,6 @@ package org.springframework.boot.actuate.autoconfigure.logging;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path; import java.nio.file.Path;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
@ -30,9 +28,9 @@ import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.test.context.runner.WebApplicationContextRunner; import org.springframework.boot.test.context.runner.WebApplicationContextRunner;
import org.springframework.core.io.Resource; import org.springframework.core.io.Resource;
import org.springframework.util.FileCopyUtils; import org.springframework.util.FileCopyUtils;
import org.springframework.util.StreamUtils;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.contentOf;
/** /**
* Tests for {@link LogFileWebEndpointAutoConfiguration}. * Tests for {@link LogFileWebEndpointAutoConfiguration}.
@ -119,9 +117,7 @@ class LogFileWebEndpointAutoConfigurationTests {
LogFileWebEndpoint endpoint = context.getBean(LogFileWebEndpoint.class); LogFileWebEndpoint endpoint = context.getBean(LogFileWebEndpoint.class);
Resource resource = endpoint.logFile(); Resource resource = endpoint.logFile();
assertThat(resource).isNotNull(); assertThat(resource).isNotNull();
try (InputStream input = resource.getInputStream()) { assertThat(contentOf(resource.getFile())).isEqualTo("--TEST--");
assertThat(StreamUtils.copyToString(input, StandardCharsets.UTF_8)).isEqualTo("--TEST--");
}
}); });
} }

@ -18,8 +18,6 @@ package org.springframework.boot.actuate.logging;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
@ -30,9 +28,9 @@ import org.junit.jupiter.api.io.TempDir;
import org.springframework.core.io.Resource; import org.springframework.core.io.Resource;
import org.springframework.mock.env.MockEnvironment; import org.springframework.mock.env.MockEnvironment;
import org.springframework.util.FileCopyUtils; import org.springframework.util.FileCopyUtils;
import org.springframework.util.StreamUtils;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.contentOf;
/** /**
* Tests for {@link LogFileWebEndpoint}. * Tests for {@link LogFileWebEndpoint}.
@ -71,7 +69,7 @@ class LogFileWebEndpointTests {
this.environment.setProperty("logging.file.name", this.logFile.getAbsolutePath()); this.environment.setProperty("logging.file.name", this.logFile.getAbsolutePath());
Resource resource = this.endpoint.logFile(); Resource resource = this.endpoint.logFile();
assertThat(resource).isNotNull(); assertThat(resource).isNotNull();
assertThat(contentOf(resource)).isEqualTo("--TEST--"); assertThat(contentOf(resource.getFile())).isEqualTo("--TEST--");
} }
@Test @Test
@ -80,7 +78,7 @@ class LogFileWebEndpointTests {
this.environment.setProperty("logging.file", this.logFile.getAbsolutePath()); this.environment.setProperty("logging.file", this.logFile.getAbsolutePath());
Resource resource = this.endpoint.logFile(); Resource resource = this.endpoint.logFile();
assertThat(resource).isNotNull(); assertThat(resource).isNotNull();
assertThat(contentOf(resource)).isEqualTo("--TEST--"); assertThat(contentOf(resource.getFile())).isEqualTo("--TEST--");
} }
@Test @Test
@ -88,13 +86,7 @@ class LogFileWebEndpointTests {
LogFileWebEndpoint endpoint = new LogFileWebEndpoint(this.environment, this.logFile); LogFileWebEndpoint endpoint = new LogFileWebEndpoint(this.environment, this.logFile);
Resource resource = endpoint.logFile(); Resource resource = endpoint.logFile();
assertThat(resource).isNotNull(); assertThat(resource).isNotNull();
assertThat(contentOf(resource)).isEqualTo("--TEST--"); assertThat(contentOf(resource.getFile())).isEqualTo("--TEST--");
}
private String contentOf(Resource resource) throws IOException {
try (InputStream input = resource.getInputStream()) {
return StreamUtils.copyToString(input, StandardCharsets.UTF_8);
}
} }
} }

Loading…
Cancel
Save