1
1
/*
2
- * Copyright 2012-2023 the original author or authors.
2
+ * Copyright 2012-2024 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
17
17
package org .springframework .boot ;
18
18
19
19
import org .apache .commons .logging .Log ;
20
+ import org .junit .jupiter .api .BeforeEach ;
20
21
import org .junit .jupiter .api .Test ;
21
22
22
23
import org .springframework .boot .SpringApplication .Startup ;
23
- import org .springframework .boot . system . ApplicationPid ;
24
+ import org .springframework .mock . env . MockEnvironment ;
24
25
25
26
import static org .assertj .core .api .Assertions .assertThat ;
26
27
import static org .mockito .ArgumentMatchers .assertArg ;
@@ -39,27 +40,60 @@ class StartupInfoLoggerTests {
39
40
40
41
private final Log log = mock (Log .class );
41
42
43
+ private MockEnvironment environment ;
44
+
45
+ @ BeforeEach
46
+ void setUp () {
47
+ this .environment = new MockEnvironment ();
48
+ this .environment .setProperty ("spring.application.version" , "1.2.3" );
49
+ this .environment .setProperty ("spring.application.pid" , "42" );
50
+ }
51
+
42
52
@ Test
43
53
void startingFormat () {
44
54
given (this .log .isInfoEnabled ()).willReturn (true );
45
- new StartupInfoLogger (getClass ()).logStarting (this .log );
55
+ new StartupInfoLogger (getClass (), this . environment ).logStarting (this .log );
46
56
then (this .log ).should ()
47
- .info (assertArg ((message ) -> assertThat (message .toString ())
48
- .contains ("Starting " + getClass ().getSimpleName () + " using Java " + System .getProperty ("java.version" )
49
- + " with PID " + new ApplicationPid () + " (started by " + System .getProperty ("user.name" )
50
- + " in " + System .getProperty ("user.dir" ) + ")" )));
57
+ .info (assertArg (
58
+ (message ) -> assertThat (message .toString ()).contains ("Starting " + getClass ().getSimpleName ()
59
+ + " v1.2.3 using Java " + System .getProperty ("java.version" ) + " with PID 42 (started by "
60
+ + System .getProperty ("user.name" ) + " in " + System .getProperty ("user.dir" ) + ")" )));
61
+ }
62
+
63
+ @ Test
64
+ void startingFormatWhenVersionIsNotAvailable () {
65
+ this .environment .setProperty ("spring.application.version" , "" );
66
+ given (this .log .isInfoEnabled ()).willReturn (true );
67
+ new StartupInfoLogger (getClass (), this .environment ).logStarting (this .log );
68
+ then (this .log ).should ()
69
+ .info (assertArg (
70
+ (message ) -> assertThat (message .toString ()).contains ("Starting " + getClass ().getSimpleName ()
71
+ + " using Java " + System .getProperty ("java.version" ) + " with PID 42 (started by "
72
+ + System .getProperty ("user.name" ) + " in " + System .getProperty ("user.dir" ) + ")" )));
73
+ }
74
+
75
+ @ Test
76
+ void startingFormatWhenPidIsNotAvailable () {
77
+ this .environment .setProperty ("spring.application.pid" , "" );
78
+ given (this .log .isInfoEnabled ()).willReturn (true );
79
+ new StartupInfoLogger (getClass (), this .environment ).logStarting (this .log );
80
+ then (this .log ).should ()
81
+ .info (assertArg (
82
+ (message ) -> assertThat (message .toString ()).contains ("Starting " + getClass ().getSimpleName ()
83
+ + " v1.2.3 using Java " + System .getProperty ("java.version" ) + " (started by "
84
+ + System .getProperty ("user.name" ) + " in " + System .getProperty ("user.dir" ) + ")" )));
51
85
}
52
86
53
87
@ Test
54
88
void startingFormatInAotMode () {
55
89
System .setProperty ("spring.aot.enabled" , "true" );
56
90
try {
57
91
given (this .log .isInfoEnabled ()).willReturn (true );
58
- new StartupInfoLogger (getClass ()).logStarting (this .log );
92
+ new StartupInfoLogger (getClass (), this . environment ).logStarting (this .log );
59
93
then (this .log ).should ()
60
94
.info (assertArg ((message ) -> assertThat (message .toString ())
61
- .contains ("Starting AOT-processed " + getClass ().getSimpleName () + " using Java "
62
- + System .getProperty ("java.version" ) + " with PID " + new ApplicationPid () + " (started by "
95
+ .contains ("Starting AOT-processed " + getClass ().getSimpleName () + " v1.2.3 using Java "
96
+ + System .getProperty ("java.version" ) + " with PID 42 (started by "
63
97
+ System .getProperty ("user.name" ) + " in " + System .getProperty ("user.dir" ) + ")" )));
64
98
65
99
}
@@ -71,7 +105,7 @@ void startingFormatInAotMode() {
71
105
@ Test
72
106
void startedFormat () {
73
107
given (this .log .isInfoEnabled ()).willReturn (true );
74
- new StartupInfoLogger (getClass ()).logStarted (this .log , new TestStartup (1345L , "Started" ));
108
+ new StartupInfoLogger (getClass (), this . environment ).logStarted (this .log , new TestStartup (1345L , "Started" ));
75
109
then (this .log ).should ()
76
110
.info (assertArg ((message ) -> assertThat (message .toString ()).matches ("Started " + getClass ().getSimpleName ()
77
111
+ " in \\ d+\\ .\\ d{1,3} seconds \\ (process running for 1.345\\ )" )));
@@ -80,7 +114,7 @@ void startedFormat() {
80
114
@ Test
81
115
void startedWithoutUptimeFormat () {
82
116
given (this .log .isInfoEnabled ()).willReturn (true );
83
- new StartupInfoLogger (getClass ()).logStarted (this .log , new TestStartup (null , "Started" ));
117
+ new StartupInfoLogger (getClass (), this . environment ).logStarted (this .log , new TestStartup (null , "Started" ));
84
118
then (this .log ).should ()
85
119
.info (assertArg ((message ) -> assertThat (message .toString ())
86
120
.matches ("Started " + getClass ().getSimpleName () + " in \\ d+\\ .\\ d{1,3} seconds" )));
@@ -89,7 +123,7 @@ void startedWithoutUptimeFormat() {
89
123
@ Test
90
124
void restoredFormat () {
91
125
given (this .log .isInfoEnabled ()).willReturn (true );
92
- new StartupInfoLogger (getClass ()).logStarted (this .log , new TestStartup (null , "Restored" ));
126
+ new StartupInfoLogger (getClass (), this . environment ).logStarted (this .log , new TestStartup (null , "Restored" ));
93
127
then (this .log ).should ()
94
128
.info (assertArg ((message ) -> assertThat (message .toString ())
95
129
.matches ("Restored " + getClass ().getSimpleName () + " in \\ d+\\ .\\ d{1,3} seconds" )));
0 commit comments