@ -17,21 +17,19 @@
package org.springframework.boot.actuate.logging ;
import java.io.File ;
import java.io.IOException ;
import org.junit.Before ;
import org.junit.Rule ;
import org.junit.ClassRule ;
import org.junit.Test ;
import org.junit.rules.TemporaryFolder ;
import org.junit.runner.RunWith ;
import org.springframework.boot.actuate.endpoint.web.test.WebEndpointRunners ;
import org.springframework.boot. test.util.TestPropertyValues ;
import org.springframework.boot. logging.LogFile ;
import org.springframework.context.ConfigurableApplicationContext ;
import org.springframework.context.annotation.Bean ;
import org.springframework.context.annotation.Configuration ;
import org.springframework.core.env.Environment ;
import org.springframework.http.MediaType ;
import org.springframework.mock.env.MockEnvironment ;
import org.springframework.test.web.reactive.server.WebTestClient ;
import org.springframework.util.FileCopyUtils ;
@ -48,32 +46,19 @@ public class LogFileWebEndpointWebIntegrationTests {
private static WebTestClient client ;
@ Rule
public final TemporaryFolder temp = new TemporaryFolder ( ) ;
@ Class Rule
public static final TemporaryFolder temp = new TemporaryFolder ( ) ;
private File logFile ;
@Before
public void setUp ( ) throws IOException {
this . logFile = this . temp . newFile ( ) ;
FileCopyUtils . copy ( "--TEST--" . getBytes ( ) , this . logFile ) ;
}
@Test
public void getRequestProduces404ResponseWhenLogFileNotFound ( ) {
client . get ( ) . uri ( "/actuator/logfile" ) . exchange ( ) . expectStatus ( ) . isNotFound ( ) ;
}
private static File logFile ;
@Test
public void getRequestProducesResponseWithLogFile ( ) {
TestPropertyValues . of ( "logging.file:" + this . logFile . getAbsolutePath ( ) ) . applyTo ( context ) ;
client . get ( ) . uri ( "/actuator/logfile" ) . exchange ( ) . expectStatus ( ) . isOk ( ) . expectHeader ( )
. contentType ( "text/plain; charset=UTF-8" ) . expectBody ( String . class ) . isEqualTo ( "--TEST--" ) ;
}
@Test
public void getRequestThatAcceptsTextPlainProducesResponseWithLogFile ( ) {
TestPropertyValues . of ( "logging.file:" + this . logFile . getAbsolutePath ( ) ) . applyTo ( context ) ;
client . get ( ) . uri ( "/actuator/logfile" ) . accept ( MediaType . TEXT_PLAIN ) . exchange ( ) . expectStatus ( ) . isOk ( )
. expectHeader ( ) . contentType ( "text/plain; charset=UTF-8" ) . expectBody ( String . class ) . isEqualTo ( "--TEST--" ) ;
}
@ -82,8 +67,12 @@ public class LogFileWebEndpointWebIntegrationTests {
static class TestConfiguration {
@Bean
public LogFileWebEndpoint logFileEndpoint ( Environment environment ) {
return new LogFileWebEndpoint ( environment ) ;
public LogFileWebEndpoint logFileEndpoint ( ) throws Exception {
logFile = temp . newFile ( ) ;
FileCopyUtils . copy ( "--TEST--" . getBytes ( ) , logFile ) ;
MockEnvironment environment = new MockEnvironment ( ) ;
environment . setProperty ( "logging.file" , logFile . getAbsolutePath ( ) ) ;
return new LogFileWebEndpoint ( LogFile . get ( environment ) , null ) ;
}
}