2929import org .junit .rules .TemporaryFolder ;
3030import org .springframework .boot .OutputCapture ;
3131import org .springframework .boot .SpringApplication ;
32+ import org .springframework .boot .TestUtils ;
3233import org .springframework .boot .logging .LogLevel ;
3334import org .springframework .boot .logging .java .JavaLoggingSystem ;
3435import org .springframework .context .support .GenericApplicationContext ;
35- import org .springframework .core .env .PropertySource ;
3636
3737import static org .hamcrest .Matchers .containsString ;
3838import static org .hamcrest .Matchers .not ;
@@ -90,16 +90,8 @@ public void testDefaultConfigLocation() {
9090
9191 @ Test
9292 public void testOverrideConfigLocation () {
93- this .context .getEnvironment ().getPropertySources ()
94- .addFirst (new PropertySource <String >("manual" ) {
95- @ Override
96- public Object getProperty (String name ) {
97- if ("logging.config" .equals (name )) {
98- return "classpath:logback-nondefault.xml" ;
99- }
100- return null ;
101- }
102- });
93+ TestUtils .addEnviroment (this .context ,
94+ "logging.config: classpath:logback-nondefault.xml" );
10395 this .initializer .initialize (this .context );
10496 this .logger .info ("Hello world" );
10597 String output = this .outputCapture .toString ().trim ();
@@ -110,35 +102,16 @@ public Object getProperty(String name) {
110102
111103 @ Test
112104 public void testOverrideConfigDoesNotExist () throws Exception {
113- this .context .getEnvironment ().getPropertySources ()
114- .addFirst (new PropertySource <String >("manual" ) {
115- @ Override
116- public Object getProperty (String name ) {
117- if ("logging.config" .equals (name )) {
118- return "doesnotexist.xml" ;
119- }
120- return null ;
121- }
122- });
105+ TestUtils .addEnviroment (this .context , "logging.config: doesnotexist.xml" );
123106 this .initializer .initialize (this .context );
124107 // Should not throw
125108 }
126109
127110 @ Test
128111 public void testAddLogFileProperty () {
129- this .context .getEnvironment ().getPropertySources ()
130- .addFirst (new PropertySource <String >("manual" ) {
131- @ Override
132- public Object getProperty (String name ) {
133- if ("logging.config" .equals (name )) {
134- return "classpath:logback-nondefault.xml" ;
135- }
136- if ("logging.file" .equals (name )) {
137- return "foo.log" ;
138- }
139- return null ;
140- }
141- });
112+ TestUtils .addEnviroment (this .context ,
113+ "logging.config: classpath:logback-nondefault.xml" ,
114+ "logging.file: foo.log" );
142115 this .initializer .initialize (this .context );
143116 Log logger = LogFactory .getLog (LoggingApplicationContextInitializerTests .class );
144117 logger .info ("Hello world" );
@@ -148,19 +121,8 @@ public Object getProperty(String name) {
148121
149122 @ Test
150123 public void testAddLogPathProperty () {
151- this .context .getEnvironment ().getPropertySources ()
152- .addFirst (new PropertySource <String >("manual" ) {
153- @ Override
154- public Object getProperty (String name ) {
155- if ("logging.config" .equals (name )) {
156- return "classpath:logback-nondefault.xml" ;
157- }
158- if ("logging.path" .equals (name )) {
159- return "foo/" ;
160- }
161- return null ;
162- }
163- });
124+ TestUtils .addEnviroment (this .context ,
125+ "logging.config: classpath:logback-nondefault.xml" , "logging.path: foo/" );
164126 this .initializer .initialize (this .context );
165127 Log logger = LogFactory .getLog (LoggingApplicationContextInitializerTests .class );
166128 logger .info ("Hello world" );
0 commit comments