3333import ch .qos .logback .core .testUtil .CoreTestConstants ;
3434import ch .qos .logback .core .testUtil .FileTestUtil ;
3535import ch .qos .logback .core .testUtil .RandomUtil ;
36- import ch .qos .logback .core .util .StatusPrinter ;
36+ import ch .qos .logback .core .util .Loader ;
3737import ch .qos .logback .core .util .StatusPrinter2 ;
3838import org .junit .jupiter .api .*;
3939
4040import java .io .*;
41+ import java .net .URL ;
4142import java .util .List ;
4243import java .util .concurrent .CountDownLatch ;
4344import java .util .concurrent .ExecutionException ;
@@ -70,6 +71,8 @@ public class ReconfigureOnChangeTaskTest extends ReconfigureTaskTestSupport {
7071
7172 private static final String SCAN_PERIOD_DEFAULT_FILE_AS_STR = JORAN_INPUT_PREFIX + "roct/scan_period_default.xml" ;
7273
74+ private static final String TOP_FILE_WITH_INCLUSION = "asResource/topWithFileInclusion.xml" ;
75+
7376 Logger logger = loggerContext .getLogger (this .getClass ());
7477 StatusChecker statusChecker = new StatusChecker (loggerContext );
7578 StatusPrinter2 statusPrinter2 = new StatusPrinter2 ();
@@ -91,6 +94,14 @@ void configure(File file) throws JoranException {
9194 jc .doConfigure (file );
9295 }
9396
97+ void configureAsResource (String filename ) throws JoranException {
98+ URL url = Loader .getResource (filename , this .getClass ().getClassLoader ());
99+ assertNotNull (url );
100+ JoranConfigurator jc = new JoranConfigurator ();
101+ jc .setContext (loggerContext );
102+ jc .doConfigure (url );
103+ }
104+
94105 @ Test
95106 @ Timeout (value = TIMEOUT , unit = TimeUnit .SECONDS )
96107 public void checkBasicLifecyle () throws JoranException , IOException , InterruptedException {
@@ -130,6 +141,7 @@ public void scanWithFileInclusion() throws JoranException, IOException, Interrup
130141 checkThatTaskCanBeStopped ();
131142 }
132143
144+
133145 @ Test
134146 @ Timeout (value = TIMEOUT , unit = TimeUnit .SECONDS )
135147 public void scanWithResourceInclusion () throws JoranException , IOException , InterruptedException {
@@ -256,7 +268,7 @@ public void fallbackToSafe_FollowedByRecovery() throws IOException, JoranExcepti
256268
257269 private void addResetResistantOnConsoleStatusListener () {
258270 // enable when debugging
259- if (1 = =1 )
271+ if (1 ! =1 )
260272 return ;
261273 OnConsoleStatusListener ocs = new OnConsoleStatusListener ();
262274 ocs .setContext (loggerContext );
@@ -265,6 +277,43 @@ private void addResetResistantOnConsoleStatusListener() {
265277 loggerContext .getStatusManager ().add (ocs );
266278 }
267279
280+ @ Test
281+ @ Timeout (value = 2 , unit = TimeUnit .SECONDS )
282+ public void XXXscanWithIncludedFileCreatedLater () throws IOException , JoranException , InterruptedException {
283+ ReconfigurationTaskRegisteredConfigEventListener roctRegisteredListener = new ReconfigurationTaskRegisteredConfigEventListener ();
284+ loggerContext .addConfigurationEventListener (roctRegisteredListener );
285+ addResetResistantOnConsoleStatusListener ();
286+ String innerFileAsStr = CoreTestConstants .OUTPUT_DIR_PREFIX + "scanWithIncludedFileCreatedLater-" + diff + ".xml" ;
287+ System .setProperty ("fileCreatedLater" , innerFileAsStr );
288+ configureAsResource (TOP_FILE_WITH_INCLUSION );
289+
290+ File innerFile = new File (innerFileAsStr );
291+
292+ List <File > fileList = getConfigurationWatchList (loggerContext );
293+ assertThatListContainsFile (fileList , innerFile );
294+
295+ // capture reference to ReconfigureOnChangeTask
296+ //ReconfigureOnChangeTask roct = roctRegisteredListener.reconfigureOnChangeTask;
297+ //assertNotNull(roct);
298+
299+ CountDownLatch changeDetectedLatch = registerChangeDetectedListener ();
300+ CountDownLatch configurationDoneLatch = registerNewReconfigurationDoneSuccessfullyListener ();
301+
302+
303+ writeToFile (innerFile , "<included><root level=\" ERROR\" /></included> " );
304+ changeDetectedLatch .await ();
305+ configurationDoneLatch .await ();
306+
307+ //Thread.sleep(1000);
308+ //waitForReconfigureOnChangeTaskToRun();
309+
310+ //statusPrinter2.print(loggerContext);
311+ Logger root = loggerContext .getLogger (Logger .ROOT_LOGGER_NAME );
312+ assertEquals (Level .ERROR , root .getLevel ());
313+
314+ }
315+
316+
268317 @ Test
269318 @ Timeout (value = TIMEOUT_LONG , unit = TimeUnit .SECONDS )
270319 public void fallbackToSafeWithIncludedFile_FollowedByRecovery () throws IOException , JoranException , InterruptedException , ExecutionException {
@@ -278,16 +327,11 @@ public void fallbackToSafeWithIncludedFile_FollowedByRecovery() throws IOExcepti
278327 writeToFile (innerFile , "<included><root level=\" ERROR\" /></included> " );
279328 addResetResistantOnConsoleStatusListener ();
280329
281- ReconfigurationTaskRegisteredConfigEventListener roctRegisteredListener = new ReconfigurationTaskRegisteredConfigEventListener ();
282- loggerContext .addConfigurationEventListener (roctRegisteredListener );
283-
284330 configure (topLevelFile );
285331
286- ReconfigureOnChangeTask roct = roctRegisteredListener .reconfigureOnChangeTask ;
287-
288332
289333 CountDownLatch changeDetectedLatch = registerChangeDetectedListener ();
290- CountDownLatch configurationDoneLatch = registerNewReconfigurationDoneSuccessfullyListener (roct );
334+ CountDownLatch configurationDoneLatch = registerNewReconfigurationDoneSuccessfullyListener ();
291335
292336 writeToFile (innerFile , "<included>\n <root>\n </included>" );
293337 changeDetectedLatch .await ();
@@ -298,7 +342,7 @@ public void fallbackToSafeWithIncludedFile_FollowedByRecovery() throws IOExcepti
298342 statusChecker .assertContainsMatch (Status .WARN , FALLING_BACK_TO_SAFE_CONFIGURATION );
299343 statusChecker .assertContainsMatch (Status .INFO , RE_REGISTERING_PREVIOUS_SAFE_CONFIGURATION );
300344
301- // statusPrinter2.print(loggerContext);
345+ statusPrinter2 .print (loggerContext );
302346
303347 loggerContext .getStatusManager ().clear ();
304348
@@ -312,17 +356,13 @@ public void fallbackToSafeWithIncludedFile_FollowedByRecovery() throws IOExcepti
312356 }
313357
314358 CountDownLatch registerNewReconfigurationDoneSuccessfullyListener () {
315- return registerNewReconfigurationDoneSuccessfullyListener (null );
316- }
317-
318- CountDownLatch registerNewReconfigurationDoneSuccessfullyListener (ReconfigureOnChangeTask roct ) {
319359 CountDownLatch latch = new CountDownLatch (1 );
320- ReconfigurationDoneListener reconfigurationDoneListener = new ReconfigurationDoneListener (latch , roct );
360+ ReconfigurationDoneListener reconfigurationDoneListener = new ReconfigurationDoneListener (latch );
321361 loggerContext .addConfigurationEventListener (reconfigurationDoneListener );
322362 return latch ;
323363 }
324364
325- class RunMethodInvokedListener implements ConfigurationEventListener {
365+ static class RunMethodInvokedListener implements ConfigurationEventListener {
326366 CountDownLatch countDownLatch ;
327367 ReconfigureOnChangeTask reconfigureOnChangeTask ;
328368
@@ -332,15 +372,12 @@ class RunMethodInvokedListener implements ConfigurationEventListener {
332372
333373 @ Override
334374 public void listen (ConfigurationEvent configurationEvent ) {
335- switch (configurationEvent .getEventType ()) {
336- case CHANGE_DETECTOR_RUNNING :
375+ if (configurationEvent .getEventType () == ConfigurationEvent .EventType .CHANGE_DETECTOR_RUNNING ) {
337376 countDownLatch .countDown ();
338377 Object data = configurationEvent .getData ();
339378 if (data instanceof ReconfigureOnChangeTask ) {
340379 reconfigureOnChangeTask = (ReconfigureOnChangeTask ) data ;
341380 }
342- break ;
343- default :
344381 }
345382 }
346383 }
0 commit comments