From d0682d7b6227a8d5363e1ff6d5347bc732324131 Mon Sep 17 00:00:00 2001 From: Sebastian Zarnekow Date: Mon, 8 Jan 2024 11:01:21 +0100 Subject: [PATCH] Keep the filter-chain consistent when using the LoggingTester Manipulating the next-pointer of the linked list is error-prone and corrupted the filter chain. It's safer to re-create the filter chain from scratch by calling addFilter again. Signed-off-by: Sebastian Zarnekow --- .../xtext/junit4/logging/LoggingTester.java | 22 ++++++++++--------- .../xtext/testing/logging/LoggingTester.java | 22 ++++++++++--------- 2 files changed, 24 insertions(+), 20 deletions(-) diff --git a/org.eclipse.xtext.junit4/src/org/eclipse/xtext/junit4/logging/LoggingTester.java b/org.eclipse.xtext.junit4/src/org/eclipse/xtext/junit4/logging/LoggingTester.java index eac882771bf..eed565167f4 100644 --- a/org.eclipse.xtext.junit4/src/org/eclipse/xtext/junit4/logging/LoggingTester.java +++ b/org.eclipse.xtext.junit4/src/org/eclipse/xtext/junit4/logging/LoggingTester.java @@ -1,5 +1,5 @@ /** - * Copyright (c) 2015, 2022 itemis AG (http://www.itemis.eu) and others. + * Copyright (c) 2015, 2024 itemis AG (http://www.itemis.eu) and others. * This program and the accompanying materials are made available under the * terms of the Eclipse Public License 2.0 which is available at * http://www.eclipse.org/legal/epl-2.0. @@ -333,16 +333,18 @@ private static List appenderHierarchy(Logger logger) { } private static void removeFilter(Appender appender, Filter filter) { - if (Objects.equal(appender.getFilter(), filter)) { - appender.clearFilters(); - appender.addFilter(filter.getNext()); - } else { - for (Filter current = appender.getFilter(); (current != null); current = current.getNext()) { - if (Objects.equal(current.getNext(), filter)) { - current.setNext(filter.getNext()); - return; - } + List filtersToKeep = new ArrayList<>(); + Filter present = appender.getFilter(); + while (present != null) { + if (!Objects.equal(present, filter)) { + filtersToKeep.add(present); } + present = present.getNext(); + } + appender.clearFilters(); + for(Filter addMe: filtersToKeep) { + addMe.setNext(null); + appender.addFilter(addMe); } } diff --git a/org.eclipse.xtext.testing/src/org/eclipse/xtext/testing/logging/LoggingTester.java b/org.eclipse.xtext.testing/src/org/eclipse/xtext/testing/logging/LoggingTester.java index ca0b24ea9dc..86f01e365b6 100644 --- a/org.eclipse.xtext.testing/src/org/eclipse/xtext/testing/logging/LoggingTester.java +++ b/org.eclipse.xtext.testing/src/org/eclipse/xtext/testing/logging/LoggingTester.java @@ -1,5 +1,5 @@ /** - * Copyright (c) 2015, 2020 itemis AG (http://www.itemis.eu) and others. + * Copyright (c) 2015, 2024 itemis AG (http://www.itemis.eu) and others. * This program and the accompanying materials are made available under the * terms of the Eclipse Public License 2.0 which is available at * http://www.eclipse.org/legal/epl-2.0. @@ -69,17 +69,19 @@ private static List appenderHierarchy(Logger logger) { } private static void removeFilter(Appender appender, Filter filter) { - if (Objects.equals(appender.getFilter(), filter)) { - appender.clearFilters(); - appender.addFilter(filter.getNext()); - } else { - for (Filter current = appender.getFilter(); current != null; current = current.getNext()) { - if (Objects.equals(current.getNext(), filter)) { - current.setNext(filter.getNext()); - return; - } + List filtersToKeep = new ArrayList<>(); + Filter present = appender.getFilter(); + while (present != null) { + if (!Objects.equals(present, filter)) { + filtersToKeep.add(present); } + present = present.getNext(); } + appender.clearFilters(); + filtersToKeep.forEach(addMe -> { + addMe.setNext(null); + appender.addFilter(addMe); + }); } public static class LogCapture {