11/*
2- * Copyright 2012-2023 the original author or authors.
2+ * Copyright 2012-2024 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.
1717package org .springframework .boot ;
1818
1919import org .apache .commons .logging .Log ;
20+ import org .junit .jupiter .api .BeforeEach ;
2021import org .junit .jupiter .api .Test ;
2122
2223import org .springframework .boot .SpringApplication .Startup ;
23- import org .springframework .boot . system . ApplicationPid ;
24+ import org .springframework .mock . env . MockEnvironment ;
2425
2526import static org .assertj .core .api .Assertions .assertThat ;
2627import static org .mockito .ArgumentMatchers .assertArg ;
@@ -39,27 +40,60 @@ class StartupInfoLoggerTests {
3940
4041 private final Log log = mock (Log .class );
4142
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+
4252 @ Test
4353 void startingFormat () {
4454 given (this .log .isInfoEnabled ()).willReturn (true );
45- new StartupInfoLogger (getClass ()).logStarting (this .log );
55+ new StartupInfoLogger (getClass (), this . environment ).logStarting (this .log );
4656 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" ) + ")" )));
5185 }
5286
5387 @ Test
5488 void startingFormatInAotMode () {
5589 System .setProperty ("spring.aot.enabled" , "true" );
5690 try {
5791 given (this .log .isInfoEnabled ()).willReturn (true );
58- new StartupInfoLogger (getClass ()).logStarting (this .log );
92+ new StartupInfoLogger (getClass (), this . environment ).logStarting (this .log );
5993 then (this .log ).should ()
6094 .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 "
6397 + System .getProperty ("user.name" ) + " in " + System .getProperty ("user.dir" ) + ")" )));
6498
6599 }
@@ -71,7 +105,7 @@ void startingFormatInAotMode() {
71105 @ Test
72106 void startedFormat () {
73107 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" ));
75109 then (this .log ).should ()
76110 .info (assertArg ((message ) -> assertThat (message .toString ()).matches ("Started " + getClass ().getSimpleName ()
77111 + " in \\ d+\\ .\\ d{1,3} seconds \\ (process running for 1.345\\ )" )));
@@ -80,7 +114,7 @@ void startedFormat() {
80114 @ Test
81115 void startedWithoutUptimeFormat () {
82116 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" ));
84118 then (this .log ).should ()
85119 .info (assertArg ((message ) -> assertThat (message .toString ())
86120 .matches ("Started " + getClass ().getSimpleName () + " in \\ d+\\ .\\ d{1,3} seconds" )));
@@ -89,7 +123,7 @@ void startedWithoutUptimeFormat() {
89123 @ Test
90124 void restoredFormat () {
91125 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" ));
93127 then (this .log ).should ()
94128 .info (assertArg ((message ) -> assertThat (message .toString ())
95129 .matches ("Restored " + getClass ().getSimpleName () + " in \\ d+\\ .\\ d{1,3} seconds" )));
0 commit comments