Merge pull request #15793 from dreis2211

* pr/15793:
  Use Assertions.contentOf() where possible
pull/15799/head
Stephane Nicoll 6 years ago
commit e2532e1ef5

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2018 the original author or authors. * Copyright 2012-2019 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -48,9 +48,9 @@ import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary; import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.util.ReflectionTestUtils; import org.springframework.test.util.ReflectionTestUtils;
import org.springframework.util.FileCopyUtils;
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 LiquibaseAutoConfiguration}. * Tests for {@link LiquibaseAutoConfiguration}.
@ -286,8 +286,7 @@ public class LiquibaseAutoConfigurationTests {
File actualFile = (File) ReflectionTestUtils.getField(liquibase, File actualFile = (File) ReflectionTestUtils.getField(liquibase,
"rollbackFile"); "rollbackFile");
assertThat(actualFile).isEqualTo(file).exists(); assertThat(actualFile).isEqualTo(file).exists();
String content = new String(FileCopyUtils.copyToByteArray(file)); assertThat(contentOf(file)).contains("DROP TABLE PUBLIC.customer;");
assertThat(content).contains("DROP TABLE PUBLIC.customer;");
}); });
} }

@ -38,7 +38,6 @@ import org.springframework.context.event.ContextClosedEvent;
import org.springframework.scheduling.annotation.EnableScheduling; import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled; import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import org.springframework.util.FileCopyUtils;
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;
@ -125,7 +124,7 @@ public class RestarterTests {
} }
@Test @Test
public void addClassLoaderFiles() throws Exception { public void addClassLoaderFiles() {
ClassLoaderFiles classLoaderFiles = new ClassLoaderFiles(); ClassLoaderFiles classLoaderFiles = new ClassLoaderFiles();
classLoaderFiles.addFile("f", new ClassLoaderFile(Kind.ADDED, "abc".getBytes())); classLoaderFiles.addFile("f", new ClassLoaderFile(Kind.ADDED, "abc".getBytes()));
Restarter restarter = Restarter.getInstance(); Restarter restarter = Restarter.getInstance();
@ -133,8 +132,7 @@ public class RestarterTests {
restarter.restart(); restarter.restart();
ClassLoader classLoader = ((TestableRestarter) restarter) ClassLoader classLoader = ((TestableRestarter) restarter)
.getRelaunchClassLoader(); .getRelaunchClassLoader();
assertThat(FileCopyUtils.copyToByteArray(classLoader.getResourceAsStream("f"))) assertThat(classLoader.getResourceAsStream("f")).hasContent("abc");
.isEqualTo("abc".getBytes());
} }
@Test @Test

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2018 the original author or authors. * Copyright 2012-2019 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -29,9 +29,9 @@ import java.util.zip.ZipFile;
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.FileCopyUtils;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.contentOf;
/** /**
* Verification utility for use with maven-invoker-plugin verification scripts. * Verification utility for use with maven-invoker-plugin verification scripts.
@ -182,7 +182,7 @@ public final class Verify {
assertThat(this.file).exists().isFile(); assertThat(this.file).exists().isFile();
if (scriptContents.length > 0 && executable) { if (scriptContents.length > 0 && executable) {
String contents = new String(FileCopyUtils.copyToByteArray(this.file)); String contents = contentOf(this.file);
contents = contents.substring(0, contents contents = contents.substring(0, contents
.indexOf(new String(new byte[] { 0x50, 0x4b, 0x03, 0x04 }))); .indexOf(new String(new byte[] { 0x50, 0x4b, 0x03, 0x04 })));
for (String content : scriptContents) { for (String content : scriptContents) {
@ -191,7 +191,7 @@ public final class Verify {
} }
if (!executable) { if (!executable) {
String contents = new String(FileCopyUtils.copyToByteArray(this.file)); String contents = contentOf(this.file);
assertThat(contents).as("Is executable") assertThat(contents).as("Is executable")
.startsWith(new String(new byte[] { 0x50, 0x4b, 0x03, 0x04 })); .startsWith(new String(new byte[] { 0x50, 0x4b, 0x03, 0x04 }));
} }

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2018 the original author or authors. * Copyright 2012-2019 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -17,7 +17,6 @@
package org.springframework.boot.context; package org.springframework.boot.context;
import java.io.File; import java.io.File;
import java.io.FileReader;
import org.junit.After; import org.junit.After;
import org.junit.Before; import org.junit.Before;
@ -35,10 +34,10 @@ import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.ConfigurableEnvironment; import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.StandardEnvironment; import org.springframework.core.env.StandardEnvironment;
import org.springframework.mock.env.MockPropertySource; import org.springframework.mock.env.MockPropertySource;
import org.springframework.util.FileCopyUtils;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException; import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
import static org.assertj.core.api.Assertions.contentOf;
import static org.mockito.BDDMockito.given; import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
@ -72,8 +71,7 @@ public class ApplicationPidFileWriterTests {
File file = this.temporaryFolder.newFile(); File file = this.temporaryFolder.newFile();
ApplicationPidFileWriter listener = new ApplicationPidFileWriter(file); ApplicationPidFileWriter listener = new ApplicationPidFileWriter(file);
listener.onApplicationEvent(EVENT); listener.onApplicationEvent(EVENT);
FileReader reader = new FileReader(file); assertThat(contentOf(file)).isNotEmpty();
assertThat(FileCopyUtils.copyToString(reader)).isNotEmpty();
} }
@Test @Test
@ -82,8 +80,7 @@ public class ApplicationPidFileWriterTests {
System.setProperty("PIDFILE", this.temporaryFolder.newFile().getAbsolutePath()); System.setProperty("PIDFILE", this.temporaryFolder.newFile().getAbsolutePath());
ApplicationPidFileWriter listener = new ApplicationPidFileWriter(file); ApplicationPidFileWriter listener = new ApplicationPidFileWriter(file);
listener.onApplicationEvent(EVENT); listener.onApplicationEvent(EVENT);
FileReader reader = new FileReader(System.getProperty("PIDFILE")); assertThat(contentOf(new File(System.getProperty("PIDFILE")))).isNotEmpty();
assertThat(FileCopyUtils.copyToString(reader)).isNotEmpty();
} }
@Test @Test
@ -93,7 +90,7 @@ public class ApplicationPidFileWriterTests {
file.getAbsolutePath()); file.getAbsolutePath());
ApplicationPidFileWriter listener = new ApplicationPidFileWriter(); ApplicationPidFileWriter listener = new ApplicationPidFileWriter();
listener.onApplicationEvent(event); listener.onApplicationEvent(event);
assertThat(FileCopyUtils.copyToString(new FileReader(file))).isNotEmpty(); assertThat(contentOf(file)).isNotEmpty();
} }
@Test @Test
@ -103,10 +100,10 @@ public class ApplicationPidFileWriterTests {
file.getAbsolutePath()); file.getAbsolutePath());
ApplicationPidFileWriter listener = new ApplicationPidFileWriter(); ApplicationPidFileWriter listener = new ApplicationPidFileWriter();
listener.onApplicationEvent(event); listener.onApplicationEvent(event);
assertThat(FileCopyUtils.copyToString(new FileReader(file))).isEmpty(); assertThat(contentOf(file)).isEmpty();
listener.setTriggerEventType(ApplicationEnvironmentPreparedEvent.class); listener.setTriggerEventType(ApplicationEnvironmentPreparedEvent.class);
listener.onApplicationEvent(event); listener.onApplicationEvent(event);
assertThat(FileCopyUtils.copyToString(new FileReader(file))).isNotEmpty(); assertThat(contentOf(file)).isNotEmpty();
} }
@Test @Test
@ -116,10 +113,10 @@ public class ApplicationPidFileWriterTests {
file.getAbsolutePath()); file.getAbsolutePath());
ApplicationPidFileWriter listener = new ApplicationPidFileWriter(); ApplicationPidFileWriter listener = new ApplicationPidFileWriter();
listener.onApplicationEvent(event); listener.onApplicationEvent(event);
assertThat(FileCopyUtils.copyToString(new FileReader(file))).isEmpty(); assertThat(contentOf(file)).isEmpty();
listener.setTriggerEventType(ApplicationReadyEvent.class); listener.setTriggerEventType(ApplicationReadyEvent.class);
listener.onApplicationEvent(event); listener.onApplicationEvent(event);
assertThat(FileCopyUtils.copyToString(new FileReader(file))).isNotEmpty(); assertThat(contentOf(file)).isNotEmpty();
} }
@Test @Test
@ -129,7 +126,7 @@ public class ApplicationPidFileWriterTests {
listener.setTriggerEventType(ApplicationStartingEvent.class); listener.setTriggerEventType(ApplicationStartingEvent.class);
listener.onApplicationEvent( listener.onApplicationEvent(
new ApplicationStartingEvent(new SpringApplication(), new String[] {})); new ApplicationStartingEvent(new SpringApplication(), new String[] {}));
assertThat(FileCopyUtils.copyToString(new FileReader(file))).isNotEmpty(); assertThat(contentOf(file)).isNotEmpty();
} }
@Test @Test
@ -138,7 +135,7 @@ public class ApplicationPidFileWriterTests {
file.setReadOnly(); file.setReadOnly();
ApplicationPidFileWriter listener = new ApplicationPidFileWriter(file); ApplicationPidFileWriter listener = new ApplicationPidFileWriter(file);
listener.onApplicationEvent(EVENT); listener.onApplicationEvent(EVENT);
assertThat(FileCopyUtils.copyToString(new FileReader(file))).isEmpty(); assertThat(contentOf(file)).isEmpty();
} }
@Test @Test

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2018 the original author or authors. * Copyright 2012-2019 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -19,7 +19,6 @@ package org.springframework.boot.logging.log4j2;
import java.beans.PropertyChangeEvent; import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener; import java.beans.PropertyChangeListener;
import java.io.File; import java.io.File;
import java.io.FileReader;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.EnumSet; import java.util.EnumSet;
@ -44,10 +43,10 @@ import org.springframework.boot.logging.LoggingSystem;
import org.springframework.boot.logging.LoggingSystemProperties; import org.springframework.boot.logging.LoggingSystemProperties;
import org.springframework.boot.testsupport.assertj.Matched; import org.springframework.boot.testsupport.assertj.Matched;
import org.springframework.boot.testsupport.rule.OutputCapture; import org.springframework.boot.testsupport.rule.OutputCapture;
import org.springframework.util.FileCopyUtils;
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;
import static org.assertj.core.api.Assertions.contentOf;
import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.not; import static org.hamcrest.Matchers.not;
import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.any;
@ -243,14 +242,13 @@ public class Log4J2LoggingSystemTests extends AbstractLoggingSystemTests {
} }
@Test @Test
public void exceptionsIncludeClassPackaging() throws Exception { public void exceptionsIncludeClassPackaging() {
this.loggingSystem.beforeInitialize(); this.loggingSystem.beforeInitialize();
this.loggingSystem.initialize(null, null, getLogFile(null, tmpDir())); this.loggingSystem.initialize(null, null, getLogFile(null, tmpDir()));
Matcher<String> expectedOutput = containsString("[junit-"); Matcher<String> expectedOutput = containsString("[junit-");
this.output.expect(expectedOutput); this.output.expect(expectedOutput);
this.logger.warn("Expected exception", new RuntimeException("Expected")); this.logger.warn("Expected exception", new RuntimeException("Expected"));
String fileContents = FileCopyUtils String fileContents = contentOf(new File(tmpDir() + "/spring.log"));
.copyToString(new FileReader(new File(tmpDir() + "/spring.log")));
assertThat(fileContents).is(Matched.by(expectedOutput)); assertThat(fileContents).is(Matched.by(expectedOutput));
} }
@ -262,7 +260,7 @@ public class Log4J2LoggingSystemTests extends AbstractLoggingSystemTests {
} }
@Test @Test
public void customExceptionConversionWord() throws Exception { public void customExceptionConversionWord() {
System.setProperty(LoggingSystemProperties.EXCEPTION_CONVERSION_WORD, "%ex"); System.setProperty(LoggingSystemProperties.EXCEPTION_CONVERSION_WORD, "%ex");
try { try {
this.loggingSystem.beforeInitialize(); this.loggingSystem.beforeInitialize();
@ -274,8 +272,7 @@ public class Log4J2LoggingSystemTests extends AbstractLoggingSystemTests {
this.output.expect(expectedOutput); this.output.expect(expectedOutput);
this.logger.warn("Expected exception", this.logger.warn("Expected exception",
new RuntimeException("Expected", new RuntimeException("Cause"))); new RuntimeException("Expected", new RuntimeException("Cause")));
String fileContents = FileCopyUtils String fileContents = contentOf(new File(tmpDir() + "/spring.log"));
.copyToString(new FileReader(new File(tmpDir() + "/spring.log")));
assertThat(fileContents).is(Matched.by(expectedOutput)); assertThat(fileContents).is(Matched.by(expectedOutput));
} }
finally { finally {

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2018 the original author or authors. * Copyright 2012-2019 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -17,7 +17,6 @@
package org.springframework.boot.logging.logback; package org.springframework.boot.logging.logback;
import java.io.File; import java.io.File;
import java.io.FileReader;
import java.util.EnumSet; import java.util.EnumSet;
import java.util.List; import java.util.List;
import java.util.logging.Handler; import java.util.logging.Handler;
@ -55,10 +54,10 @@ import org.springframework.boot.testsupport.runner.classpath.ClassPathExclusions
import org.springframework.boot.testsupport.runner.classpath.ModifiedClassPathRunner; import org.springframework.boot.testsupport.runner.classpath.ModifiedClassPathRunner;
import org.springframework.mock.env.MockEnvironment; import org.springframework.mock.env.MockEnvironment;
import org.springframework.test.util.ReflectionTestUtils; import org.springframework.test.util.ReflectionTestUtils;
import org.springframework.util.FileCopyUtils;
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;
import static org.assertj.core.api.Assertions.contentOf;
import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.not; import static org.hamcrest.Matchers.not;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
@ -89,15 +88,13 @@ public class LogbackLoggingSystemTests extends AbstractLoggingSystemTests {
private LoggingInitializationContext initializationContext; private LoggingInitializationContext initializationContext;
private MockEnvironment environment;
@Before @Before
public void setup() { public void setup() {
this.loggingSystem.cleanUp(); this.loggingSystem.cleanUp();
this.logger = ((LoggerContext) StaticLoggerBinder.getSingleton() this.logger = ((LoggerContext) StaticLoggerBinder.getSingleton()
.getLoggerFactory()).getLogger(getClass()); .getLoggerFactory()).getLogger(getClass());
this.environment = new MockEnvironment(); MockEnvironment environment = new MockEnvironment();
this.initializationContext = new LoggingInitializationContext(this.environment); this.initializationContext = new LoggingInitializationContext(environment);
} }
@Override @Override
@ -120,7 +117,7 @@ public class LogbackLoggingSystemTests extends AbstractLoggingSystemTests {
} }
@Test @Test
public void withFile() throws Exception { public void withFile() {
this.loggingSystem.beforeInitialize(); this.loggingSystem.beforeInitialize();
this.logger.info("Hidden"); this.logger.info("Hidden");
this.loggingSystem.initialize(this.initializationContext, null, this.loggingSystem.initialize(this.initializationContext, null,
@ -340,7 +337,7 @@ public class LogbackLoggingSystemTests extends AbstractLoggingSystemTests {
} }
@Test @Test
public void testFilePatternProperty() throws Exception { public void testFilePatternProperty() {
MockEnvironment environment = new MockEnvironment(); MockEnvironment environment = new MockEnvironment();
environment.setProperty("logging.pattern.file", "%logger %msg"); environment.setProperty("logging.pattern.file", "%logger %msg");
LoggingInitializationContext loggingInitializationContext = new LoggingInitializationContext( LoggingInitializationContext loggingInitializationContext = new LoggingInitializationContext(
@ -355,7 +352,7 @@ public class LogbackLoggingSystemTests extends AbstractLoggingSystemTests {
} }
@Test @Test
public void testMaxFileSizeProperty() throws Exception { public void testMaxFileSizeProperty() {
MockEnvironment environment = new MockEnvironment(); MockEnvironment environment = new MockEnvironment();
environment.setProperty("logging.file.max-size", "100MB"); environment.setProperty("logging.file.max-size", "100MB");
LoggingInitializationContext loggingInitializationContext = new LoggingInitializationContext( LoggingInitializationContext loggingInitializationContext = new LoggingInitializationContext(
@ -370,7 +367,7 @@ public class LogbackLoggingSystemTests extends AbstractLoggingSystemTests {
} }
@Test @Test
public void testMaxFileSizePropertyWithXmlConfiguration() throws Exception { public void testMaxFileSizePropertyWithXmlConfiguration() {
MockEnvironment environment = new MockEnvironment(); MockEnvironment environment = new MockEnvironment();
environment.setProperty("logging.file.max-size", "100MB"); environment.setProperty("logging.file.max-size", "100MB");
LoggingInitializationContext loggingInitializationContext = new LoggingInitializationContext( LoggingInitializationContext loggingInitializationContext = new LoggingInitializationContext(
@ -386,7 +383,7 @@ public class LogbackLoggingSystemTests extends AbstractLoggingSystemTests {
} }
@Test @Test
public void testMaxHistoryProperty() throws Exception { public void testMaxHistoryProperty() {
MockEnvironment environment = new MockEnvironment(); MockEnvironment environment = new MockEnvironment();
environment.setProperty("logging.file.max-history", "30"); environment.setProperty("logging.file.max-history", "30");
LoggingInitializationContext loggingInitializationContext = new LoggingInitializationContext( LoggingInitializationContext loggingInitializationContext = new LoggingInitializationContext(
@ -415,20 +412,19 @@ public class LogbackLoggingSystemTests extends AbstractLoggingSystemTests {
} }
@Test @Test
public void exceptionsIncludeClassPackaging() throws Exception { public void exceptionsIncludeClassPackaging() {
this.loggingSystem.beforeInitialize(); this.loggingSystem.beforeInitialize();
this.loggingSystem.initialize(this.initializationContext, null, this.loggingSystem.initialize(this.initializationContext, null,
getLogFile(null, tmpDir())); getLogFile(null, tmpDir()));
Matcher<String> expectedOutput = containsString("[junit-"); Matcher<String> expectedOutput = containsString("[junit-");
this.output.expect(expectedOutput); this.output.expect(expectedOutput);
this.logger.warn("Expected exception", new RuntimeException("Expected")); this.logger.warn("Expected exception", new RuntimeException("Expected"));
String fileContents = FileCopyUtils String fileContents = contentOf(new File(tmpDir() + "/spring.log"));
.copyToString(new FileReader(new File(tmpDir() + "/spring.log")));
assertThat(fileContents).is(Matched.by(expectedOutput)); assertThat(fileContents).is(Matched.by(expectedOutput));
} }
@Test @Test
public void customExceptionConversionWord() throws Exception { public void customExceptionConversionWord() {
System.setProperty(LoggingSystemProperties.EXCEPTION_CONVERSION_WORD, "%ex"); System.setProperty(LoggingSystemProperties.EXCEPTION_CONVERSION_WORD, "%ex");
try { try {
this.loggingSystem.beforeInitialize(); this.loggingSystem.beforeInitialize();
@ -441,8 +437,7 @@ public class LogbackLoggingSystemTests extends AbstractLoggingSystemTests {
this.output.expect(expectedOutput); this.output.expect(expectedOutput);
this.logger.warn("Expected exception", this.logger.warn("Expected exception",
new RuntimeException("Expected", new RuntimeException("Cause"))); new RuntimeException("Expected", new RuntimeException("Cause")));
String fileContents = FileCopyUtils String fileContents = contentOf(new File(tmpDir() + "/spring.log"));
.copyToString(new FileReader(new File(tmpDir() + "/spring.log")));
assertThat(fileContents).is(Matched.by(expectedOutput)); assertThat(fileContents).is(Matched.by(expectedOutput));
} }
finally { finally {
@ -512,9 +507,8 @@ public class LogbackLoggingSystemTests extends AbstractLoggingSystemTests {
return (SizeAndTimeBasedRollingPolicy<?>) getFileAppender().getRollingPolicy(); return (SizeAndTimeBasedRollingPolicy<?>) getFileAppender().getRollingPolicy();
} }
private String getLineWithText(File file, String outputSearch) throws Exception { private String getLineWithText(File file, String outputSearch) {
return getLineWithText(FileCopyUtils.copyToString(new FileReader(file)), return getLineWithText(contentOf(file), outputSearch);
outputSearch);
} }
private String getLineWithText(String output, String outputSearch) { private String getLineWithText(String output, String outputSearch) {

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2018 the original author or authors. * Copyright 2012-2019 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -17,16 +17,14 @@
package org.springframework.boot.system; package org.springframework.boot.system;
import java.io.File; import java.io.File;
import java.io.FileReader;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.junit.rules.TemporaryFolder; import org.junit.rules.TemporaryFolder;
import org.springframework.util.FileCopyUtils;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException; import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
import static org.assertj.core.api.Assertions.contentOf;
/** /**
* Tests for {@link ApplicationPid}. * Tests for {@link ApplicationPid}.
@ -49,7 +47,7 @@ public class ApplicationPidTests {
} }
@Test @Test
public void throwIllegalStateWritingMissingPid() throws Exception { public void throwIllegalStateWritingMissingPid() {
ApplicationPid pid = new ApplicationPid(null); ApplicationPid pid = new ApplicationPid(null);
assertThatIllegalStateException() assertThatIllegalStateException()
.isThrownBy(() -> pid.write(this.temporaryFolder.newFile())) .isThrownBy(() -> pid.write(this.temporaryFolder.newFile()))
@ -61,8 +59,7 @@ public class ApplicationPidTests {
ApplicationPid pid = new ApplicationPid("123"); ApplicationPid pid = new ApplicationPid("123");
File file = this.temporaryFolder.newFile(); File file = this.temporaryFolder.newFile();
pid.write(file); pid.write(file);
String actual = FileCopyUtils.copyToString(new FileReader(file)); assertThat(contentOf(file)).isEqualTo("123");
assertThat(actual).isEqualTo("123");
} }
@Test @Test
@ -72,8 +69,7 @@ public class ApplicationPidTests {
File file = this.temporaryFolder.newFile(); File file = this.temporaryFolder.newFile();
file.delete(); file.delete();
pid.write(file); pid.write(file);
String actual = FileCopyUtils.copyToString(new FileReader(file)); assertThat(contentOf(file)).isEqualTo("123");
assertThat(actual).isEqualTo("123");
} }
@Test @Test

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2018 the original author or authors. * Copyright 2012-2019 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -17,7 +17,6 @@
package org.springframework.boot.web.context; package org.springframework.boot.web.context;
import java.io.File; import java.io.File;
import java.io.FileReader;
import java.util.HashSet; import java.util.HashSet;
import java.util.Locale; import java.util.Locale;
import java.util.Set; import java.util.Set;
@ -29,10 +28,10 @@ import org.junit.Test;
import org.junit.rules.TemporaryFolder; import org.junit.rules.TemporaryFolder;
import org.springframework.boot.web.server.WebServer; import org.springframework.boot.web.server.WebServer;
import org.springframework.util.FileCopyUtils;
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;
import static org.assertj.core.api.Assertions.contentOf;
import static org.mockito.BDDMockito.given; import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
@ -59,7 +58,7 @@ public class WebServerPortFileWriterTest {
File file = this.temporaryFolder.newFile(); File file = this.temporaryFolder.newFile();
WebServerPortFileWriter listener = new WebServerPortFileWriter(file); WebServerPortFileWriter listener = new WebServerPortFileWriter(file);
listener.onApplicationEvent(mockEvent("", 8080)); listener.onApplicationEvent(mockEvent("", 8080));
assertThat(FileCopyUtils.copyToString(new FileReader(file))).isEqualTo("8080"); assertThat(contentOf(file)).isEqualTo("8080");
} }
@Test @Test
@ -67,8 +66,8 @@ public class WebServerPortFileWriterTest {
System.setProperty("PORTFILE", this.temporaryFolder.newFile().getAbsolutePath()); System.setProperty("PORTFILE", this.temporaryFolder.newFile().getAbsolutePath());
WebServerPortFileWriter listener = new WebServerPortFileWriter(); WebServerPortFileWriter listener = new WebServerPortFileWriter();
listener.onApplicationEvent(mockEvent("", 8080)); listener.onApplicationEvent(mockEvent("", 8080));
FileReader reader = new FileReader(System.getProperty("PORTFILE")); String content = contentOf(new File(System.getProperty("PORTFILE")));
assertThat(FileCopyUtils.copyToString(reader)).isEqualTo("8080"); assertThat(content).isEqualTo("8080");
} }
@Test @Test
@ -77,8 +76,8 @@ public class WebServerPortFileWriterTest {
System.setProperty("PORTFILE", this.temporaryFolder.newFile().getAbsolutePath()); System.setProperty("PORTFILE", this.temporaryFolder.newFile().getAbsolutePath());
WebServerPortFileWriter listener = new WebServerPortFileWriter(file); WebServerPortFileWriter listener = new WebServerPortFileWriter(file);
listener.onApplicationEvent(mockEvent("", 8080)); listener.onApplicationEvent(mockEvent("", 8080));
FileReader reader = new FileReader(System.getProperty("PORTFILE")); String content = contentOf(new File(System.getProperty("PORTFILE")));
assertThat(FileCopyUtils.copyToString(reader)).isEqualTo("8080"); assertThat(content).isEqualTo("8080");
} }
@Test @Test
@ -87,15 +86,14 @@ public class WebServerPortFileWriterTest {
WebServerPortFileWriter listener = new WebServerPortFileWriter(file); WebServerPortFileWriter listener = new WebServerPortFileWriter(file);
listener.onApplicationEvent(mockEvent("", 8080)); listener.onApplicationEvent(mockEvent("", 8080));
listener.onApplicationEvent(mockEvent("management", 9090)); listener.onApplicationEvent(mockEvent("management", 9090));
assertThat(FileCopyUtils.copyToString(new FileReader(file))).isEqualTo("8080"); assertThat(contentOf(file)).isEqualTo("8080");
String managementFile = file.getName(); String managementFile = file.getName();
managementFile = managementFile.substring(0, managementFile.length() managementFile = managementFile.substring(0, managementFile.length()
- StringUtils.getFilenameExtension(managementFile).length() - 1); - StringUtils.getFilenameExtension(managementFile).length() - 1);
managementFile = managementFile + "-management." managementFile = managementFile + "-management."
+ StringUtils.getFilenameExtension(file.getName()); + StringUtils.getFilenameExtension(file.getName());
FileReader reader = new FileReader( String content = contentOf(new File(file.getParentFile(), managementFile));
new File(file.getParentFile(), managementFile)); assertThat(content).isEqualTo("9090");
assertThat(FileCopyUtils.copyToString(reader)).isEqualTo("9090");
assertThat(collectFileNames(file.getParentFile())).contains(managementFile); assertThat(collectFileNames(file.getParentFile())).contains(managementFile);
} }
@ -110,9 +108,8 @@ public class WebServerPortFileWriterTest {
- StringUtils.getFilenameExtension(managementFile).length() - 1); - StringUtils.getFilenameExtension(managementFile).length() - 1);
managementFile = managementFile + "-MANAGEMENT." managementFile = managementFile + "-MANAGEMENT."
+ StringUtils.getFilenameExtension(file.getName()); + StringUtils.getFilenameExtension(file.getName());
FileReader reader = new FileReader( String content = contentOf(new File(file.getParentFile(), managementFile));
new File(file.getParentFile(), managementFile)); assertThat(content).isEqualTo("9090");
assertThat(FileCopyUtils.copyToString(reader)).isEqualTo("9090");
assertThat(collectFileNames(file.getParentFile())).contains(managementFile); assertThat(collectFileNames(file.getParentFile())).contains(managementFile);
} }

Loading…
Cancel
Save