Skip to content

Commit e2532e1

Browse files
committed
Merge pull request #15793 from dreis2211
* pr/15793: Use Assertions.contentOf() where possible
2 parents 342212b + d4ab101 commit e2532e1

File tree

8 files changed

+58
-80
lines changed

8 files changed

+58
-80
lines changed

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/liquibase/LiquibaseAutoConfigurationTests.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2018 the original author or authors.
2+
* Copyright 2012-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -48,9 +48,9 @@
4848
import org.springframework.context.annotation.Primary;
4949
import org.springframework.jdbc.core.JdbcTemplate;
5050
import org.springframework.test.util.ReflectionTestUtils;
51-
import org.springframework.util.FileCopyUtils;
5251

5352
import static org.assertj.core.api.Assertions.assertThat;
53+
import static org.assertj.core.api.Assertions.contentOf;
5454

5555
/**
5656
* Tests for {@link LiquibaseAutoConfiguration}.
@@ -286,8 +286,7 @@ public void rollbackFile() throws IOException {
286286
File actualFile = (File) ReflectionTestUtils.getField(liquibase,
287287
"rollbackFile");
288288
assertThat(actualFile).isEqualTo(file).exists();
289-
String content = new String(FileCopyUtils.copyToByteArray(file));
290-
assertThat(content).contains("DROP TABLE PUBLIC.customer;");
289+
assertThat(contentOf(file)).contains("DROP TABLE PUBLIC.customer;");
291290
});
292291
}
293292

spring-boot-project/spring-boot-devtools/src/test/java/org/springframework/boot/devtools/restart/RestarterTests.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@
3838
import org.springframework.scheduling.annotation.EnableScheduling;
3939
import org.springframework.scheduling.annotation.Scheduled;
4040
import org.springframework.stereotype.Component;
41-
import org.springframework.util.FileCopyUtils;
4241
import org.springframework.util.StringUtils;
4342

4443
import static org.assertj.core.api.Assertions.assertThat;
@@ -125,16 +124,15 @@ public void addClassLoaderFilesMustNotBeNull() {
125124
}
126125

127126
@Test
128-
public void addClassLoaderFiles() throws Exception {
127+
public void addClassLoaderFiles() {
129128
ClassLoaderFiles classLoaderFiles = new ClassLoaderFiles();
130129
classLoaderFiles.addFile("f", new ClassLoaderFile(Kind.ADDED, "abc".getBytes()));
131130
Restarter restarter = Restarter.getInstance();
132131
restarter.addClassLoaderFiles(classLoaderFiles);
133132
restarter.restart();
134133
ClassLoader classLoader = ((TestableRestarter) restarter)
135134
.getRelaunchClassLoader();
136-
assertThat(FileCopyUtils.copyToByteArray(classLoader.getResourceAsStream("f")))
137-
.isEqualTo("abc".getBytes());
135+
assertThat(classLoader.getResourceAsStream("f")).hasContent("abc");
138136
}
139137

140138
@Test

spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/test/java/org/springframework/boot/maven/Verify.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2018 the original author or authors.
2+
* Copyright 2012-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -29,9 +29,9 @@
2929

3030
import org.springframework.core.io.FileSystemResource;
3131
import org.springframework.core.io.support.PropertiesLoaderUtils;
32-
import org.springframework.util.FileCopyUtils;
3332

3433
import static org.assertj.core.api.Assertions.assertThat;
34+
import static org.assertj.core.api.Assertions.contentOf;
3535

3636
/**
3737
* Verification utility for use with maven-invoker-plugin verification scripts.
@@ -182,7 +182,7 @@ public void verify(boolean executable, String... scriptContents)
182182
assertThat(this.file).exists().isFile();
183183

184184
if (scriptContents.length > 0 && executable) {
185-
String contents = new String(FileCopyUtils.copyToByteArray(this.file));
185+
String contents = contentOf(this.file);
186186
contents = contents.substring(0, contents
187187
.indexOf(new String(new byte[] { 0x50, 0x4b, 0x03, 0x04 })));
188188
for (String content : scriptContents) {
@@ -191,7 +191,7 @@ public void verify(boolean executable, String... scriptContents)
191191
}
192192

193193
if (!executable) {
194-
String contents = new String(FileCopyUtils.copyToByteArray(this.file));
194+
String contents = contentOf(this.file);
195195
assertThat(contents).as("Is executable")
196196
.startsWith(new String(new byte[] { 0x50, 0x4b, 0x03, 0x04 }));
197197
}

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/ApplicationPidFileWriterTests.java

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2018 the original author or authors.
2+
* Copyright 2012-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,7 +17,6 @@
1717
package org.springframework.boot.context;
1818

1919
import java.io.File;
20-
import java.io.FileReader;
2120

2221
import org.junit.After;
2322
import org.junit.Before;
@@ -35,10 +34,10 @@
3534
import org.springframework.core.env.ConfigurableEnvironment;
3635
import org.springframework.core.env.StandardEnvironment;
3736
import org.springframework.mock.env.MockPropertySource;
38-
import org.springframework.util.FileCopyUtils;
3937

4038
import static org.assertj.core.api.Assertions.assertThat;
4139
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
40+
import static org.assertj.core.api.Assertions.contentOf;
4241
import static org.mockito.BDDMockito.given;
4342
import static org.mockito.Mockito.mock;
4443

@@ -72,8 +71,7 @@ public void createPidFile() throws Exception {
7271
File file = this.temporaryFolder.newFile();
7372
ApplicationPidFileWriter listener = new ApplicationPidFileWriter(file);
7473
listener.onApplicationEvent(EVENT);
75-
FileReader reader = new FileReader(file);
76-
assertThat(FileCopyUtils.copyToString(reader)).isNotEmpty();
74+
assertThat(contentOf(file)).isNotEmpty();
7775
}
7876

7977
@Test
@@ -82,8 +80,7 @@ public void overridePidFile() throws Exception {
8280
System.setProperty("PIDFILE", this.temporaryFolder.newFile().getAbsolutePath());
8381
ApplicationPidFileWriter listener = new ApplicationPidFileWriter(file);
8482
listener.onApplicationEvent(EVENT);
85-
FileReader reader = new FileReader(System.getProperty("PIDFILE"));
86-
assertThat(FileCopyUtils.copyToString(reader)).isNotEmpty();
83+
assertThat(contentOf(new File(System.getProperty("PIDFILE")))).isNotEmpty();
8784
}
8885

8986
@Test
@@ -93,7 +90,7 @@ public void overridePidFileWithSpring() throws Exception {
9390
file.getAbsolutePath());
9491
ApplicationPidFileWriter listener = new ApplicationPidFileWriter();
9592
listener.onApplicationEvent(event);
96-
assertThat(FileCopyUtils.copyToString(new FileReader(file))).isNotEmpty();
93+
assertThat(contentOf(file)).isNotEmpty();
9794
}
9895

9996
@Test
@@ -103,10 +100,10 @@ public void tryEnvironmentPreparedEvent() throws Exception {
103100
file.getAbsolutePath());
104101
ApplicationPidFileWriter listener = new ApplicationPidFileWriter();
105102
listener.onApplicationEvent(event);
106-
assertThat(FileCopyUtils.copyToString(new FileReader(file))).isEmpty();
103+
assertThat(contentOf(file)).isEmpty();
107104
listener.setTriggerEventType(ApplicationEnvironmentPreparedEvent.class);
108105
listener.onApplicationEvent(event);
109-
assertThat(FileCopyUtils.copyToString(new FileReader(file))).isNotEmpty();
106+
assertThat(contentOf(file)).isNotEmpty();
110107
}
111108

112109
@Test
@@ -116,10 +113,10 @@ public void tryReadyEvent() throws Exception {
116113
file.getAbsolutePath());
117114
ApplicationPidFileWriter listener = new ApplicationPidFileWriter();
118115
listener.onApplicationEvent(event);
119-
assertThat(FileCopyUtils.copyToString(new FileReader(file))).isEmpty();
116+
assertThat(contentOf(file)).isEmpty();
120117
listener.setTriggerEventType(ApplicationReadyEvent.class);
121118
listener.onApplicationEvent(event);
122-
assertThat(FileCopyUtils.copyToString(new FileReader(file))).isNotEmpty();
119+
assertThat(contentOf(file)).isNotEmpty();
123120
}
124121

125122
@Test
@@ -129,7 +126,7 @@ public void withNoEnvironment() throws Exception {
129126
listener.setTriggerEventType(ApplicationStartingEvent.class);
130127
listener.onApplicationEvent(
131128
new ApplicationStartingEvent(new SpringApplication(), new String[] {}));
132-
assertThat(FileCopyUtils.copyToString(new FileReader(file))).isNotEmpty();
129+
assertThat(contentOf(file)).isNotEmpty();
133130
}
134131

135132
@Test
@@ -138,7 +135,7 @@ public void continueWhenPidFileIsReadOnly() throws Exception {
138135
file.setReadOnly();
139136
ApplicationPidFileWriter listener = new ApplicationPidFileWriter(file);
140137
listener.onApplicationEvent(EVENT);
141-
assertThat(FileCopyUtils.copyToString(new FileReader(file))).isEmpty();
138+
assertThat(contentOf(file)).isEmpty();
142139
}
143140

144141
@Test

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/log4j2/Log4J2LoggingSystemTests.java

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2018 the original author or authors.
2+
* Copyright 2012-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -19,7 +19,6 @@
1919
import java.beans.PropertyChangeEvent;
2020
import java.beans.PropertyChangeListener;
2121
import java.io.File;
22-
import java.io.FileReader;
2322
import java.util.ArrayList;
2423
import java.util.Collections;
2524
import java.util.EnumSet;
@@ -44,10 +43,10 @@
4443
import org.springframework.boot.logging.LoggingSystemProperties;
4544
import org.springframework.boot.testsupport.assertj.Matched;
4645
import org.springframework.boot.testsupport.rule.OutputCapture;
47-
import org.springframework.util.FileCopyUtils;
4846
import org.springframework.util.StringUtils;
4947

5048
import static org.assertj.core.api.Assertions.assertThat;
49+
import static org.assertj.core.api.Assertions.contentOf;
5150
import static org.hamcrest.Matchers.containsString;
5251
import static org.hamcrest.Matchers.not;
5352
import static org.mockito.ArgumentMatchers.any;
@@ -243,14 +242,13 @@ public void springConfigLocations() {
243242
}
244243

245244
@Test
246-
public void exceptionsIncludeClassPackaging() throws Exception {
245+
public void exceptionsIncludeClassPackaging() {
247246
this.loggingSystem.beforeInitialize();
248247
this.loggingSystem.initialize(null, null, getLogFile(null, tmpDir()));
249248
Matcher<String> expectedOutput = containsString("[junit-");
250249
this.output.expect(expectedOutput);
251250
this.logger.warn("Expected exception", new RuntimeException("Expected"));
252-
String fileContents = FileCopyUtils
253-
.copyToString(new FileReader(new File(tmpDir() + "/spring.log")));
251+
String fileContents = contentOf(new File(tmpDir() + "/spring.log"));
254252
assertThat(fileContents).is(Matched.by(expectedOutput));
255253
}
256254

@@ -262,7 +260,7 @@ public void beforeInitializeFilterDisablesErrorLogging() {
262260
}
263261

264262
@Test
265-
public void customExceptionConversionWord() throws Exception {
263+
public void customExceptionConversionWord() {
266264
System.setProperty(LoggingSystemProperties.EXCEPTION_CONVERSION_WORD, "%ex");
267265
try {
268266
this.loggingSystem.beforeInitialize();
@@ -274,8 +272,7 @@ public void customExceptionConversionWord() throws Exception {
274272
this.output.expect(expectedOutput);
275273
this.logger.warn("Expected exception",
276274
new RuntimeException("Expected", new RuntimeException("Cause")));
277-
String fileContents = FileCopyUtils
278-
.copyToString(new FileReader(new File(tmpDir() + "/spring.log")));
275+
String fileContents = contentOf(new File(tmpDir() + "/spring.log"));
279276
assertThat(fileContents).is(Matched.by(expectedOutput));
280277
}
281278
finally {

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/logback/LogbackLoggingSystemTests.java

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2018 the original author or authors.
2+
* Copyright 2012-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,7 +17,6 @@
1717
package org.springframework.boot.logging.logback;
1818

1919
import java.io.File;
20-
import java.io.FileReader;
2120
import java.util.EnumSet;
2221
import java.util.List;
2322
import java.util.logging.Handler;
@@ -55,10 +54,10 @@
5554
import org.springframework.boot.testsupport.runner.classpath.ModifiedClassPathRunner;
5655
import org.springframework.mock.env.MockEnvironment;
5756
import org.springframework.test.util.ReflectionTestUtils;
58-
import org.springframework.util.FileCopyUtils;
5957
import org.springframework.util.StringUtils;
6058

6159
import static org.assertj.core.api.Assertions.assertThat;
60+
import static org.assertj.core.api.Assertions.contentOf;
6261
import static org.hamcrest.Matchers.containsString;
6362
import static org.hamcrest.Matchers.not;
6463
import static org.mockito.Mockito.mock;
@@ -89,15 +88,13 @@ public class LogbackLoggingSystemTests extends AbstractLoggingSystemTests {
8988

9089
private LoggingInitializationContext initializationContext;
9190

92-
private MockEnvironment environment;
93-
9491
@Before
9592
public void setup() {
9693
this.loggingSystem.cleanUp();
9794
this.logger = ((LoggerContext) StaticLoggerBinder.getSingleton()
9895
.getLoggerFactory()).getLogger(getClass());
99-
this.environment = new MockEnvironment();
100-
this.initializationContext = new LoggingInitializationContext(this.environment);
96+
MockEnvironment environment = new MockEnvironment();
97+
this.initializationContext = new LoggingInitializationContext(environment);
10198
}
10299

103100
@Override
@@ -120,7 +117,7 @@ public void noFile() {
120117
}
121118

122119
@Test
123-
public void withFile() throws Exception {
120+
public void withFile() {
124121
this.loggingSystem.beforeInitialize();
125122
this.logger.info("Hidden");
126123
this.loggingSystem.initialize(this.initializationContext, null,
@@ -340,7 +337,7 @@ public void testLevelPatternProperty() {
340337
}
341338

342339
@Test
343-
public void testFilePatternProperty() throws Exception {
340+
public void testFilePatternProperty() {
344341
MockEnvironment environment = new MockEnvironment();
345342
environment.setProperty("logging.pattern.file", "%logger %msg");
346343
LoggingInitializationContext loggingInitializationContext = new LoggingInitializationContext(
@@ -355,7 +352,7 @@ public void testFilePatternProperty() throws Exception {
355352
}
356353

357354
@Test
358-
public void testMaxFileSizeProperty() throws Exception {
355+
public void testMaxFileSizeProperty() {
359356
MockEnvironment environment = new MockEnvironment();
360357
environment.setProperty("logging.file.max-size", "100MB");
361358
LoggingInitializationContext loggingInitializationContext = new LoggingInitializationContext(
@@ -370,7 +367,7 @@ public void testMaxFileSizeProperty() throws Exception {
370367
}
371368

372369
@Test
373-
public void testMaxFileSizePropertyWithXmlConfiguration() throws Exception {
370+
public void testMaxFileSizePropertyWithXmlConfiguration() {
374371
MockEnvironment environment = new MockEnvironment();
375372
environment.setProperty("logging.file.max-size", "100MB");
376373
LoggingInitializationContext loggingInitializationContext = new LoggingInitializationContext(
@@ -386,7 +383,7 @@ public void testMaxFileSizePropertyWithXmlConfiguration() throws Exception {
386383
}
387384

388385
@Test
389-
public void testMaxHistoryProperty() throws Exception {
386+
public void testMaxHistoryProperty() {
390387
MockEnvironment environment = new MockEnvironment();
391388
environment.setProperty("logging.file.max-history", "30");
392389
LoggingInitializationContext loggingInitializationContext = new LoggingInitializationContext(
@@ -415,20 +412,19 @@ public void testMaxHistoryPropertyWithXmlConfiguration() throws Exception {
415412
}
416413

417414
@Test
418-
public void exceptionsIncludeClassPackaging() throws Exception {
415+
public void exceptionsIncludeClassPackaging() {
419416
this.loggingSystem.beforeInitialize();
420417
this.loggingSystem.initialize(this.initializationContext, null,
421418
getLogFile(null, tmpDir()));
422419
Matcher<String> expectedOutput = containsString("[junit-");
423420
this.output.expect(expectedOutput);
424421
this.logger.warn("Expected exception", new RuntimeException("Expected"));
425-
String fileContents = FileCopyUtils
426-
.copyToString(new FileReader(new File(tmpDir() + "/spring.log")));
422+
String fileContents = contentOf(new File(tmpDir() + "/spring.log"));
427423
assertThat(fileContents).is(Matched.by(expectedOutput));
428424
}
429425

430426
@Test
431-
public void customExceptionConversionWord() throws Exception {
427+
public void customExceptionConversionWord() {
432428
System.setProperty(LoggingSystemProperties.EXCEPTION_CONVERSION_WORD, "%ex");
433429
try {
434430
this.loggingSystem.beforeInitialize();
@@ -441,8 +437,7 @@ public void customExceptionConversionWord() throws Exception {
441437
this.output.expect(expectedOutput);
442438
this.logger.warn("Expected exception",
443439
new RuntimeException("Expected", new RuntimeException("Cause")));
444-
String fileContents = FileCopyUtils
445-
.copyToString(new FileReader(new File(tmpDir() + "/spring.log")));
440+
String fileContents = contentOf(new File(tmpDir() + "/spring.log"));
446441
assertThat(fileContents).is(Matched.by(expectedOutput));
447442
}
448443
finally {
@@ -512,9 +507,8 @@ private static SizeAndTimeBasedRollingPolicy<?> getRollingPolicy() {
512507
return (SizeAndTimeBasedRollingPolicy<?>) getFileAppender().getRollingPolicy();
513508
}
514509

515-
private String getLineWithText(File file, String outputSearch) throws Exception {
516-
return getLineWithText(FileCopyUtils.copyToString(new FileReader(file)),
517-
outputSearch);
510+
private String getLineWithText(File file, String outputSearch) {
511+
return getLineWithText(contentOf(file), outputSearch);
518512
}
519513

520514
private String getLineWithText(String output, String outputSearch) {

0 commit comments

Comments
 (0)