This is an automated email from the ASF dual-hosted git repository. klund pushed a commit to branch support/1.13 in repository https://gitbox.apache.org/repos/asf/geode.git
commit 52b2435cd4f65360e327e2ac4dabe43db1610a27 Author: Kirk Lund <kl...@apache.org> AuthorDate: Thu May 14 14:29:20 2020 -0700 GEODE-8125: Extract AccessibleErrorCollector from SharedErrorCollector (#5112) (cherry picked from commit f057b5c4dc8fa484e57987720605101a835acdaf) --- .../test/dunit/rules/SharedErrorCollector.java | 44 ++------------- .../rules/accessible/AccessibleErrorCollector.java | 64 ++++++++++++++++++++++ 2 files changed, 68 insertions(+), 40 deletions(-) diff --git a/geode-dunit/src/main/java/org/apache/geode/test/dunit/rules/SharedErrorCollector.java b/geode-dunit/src/main/java/org/apache/geode/test/dunit/rules/SharedErrorCollector.java index 464885a..525a892 100644 --- a/geode-dunit/src/main/java/org/apache/geode/test/dunit/rules/SharedErrorCollector.java +++ b/geode-dunit/src/main/java/org/apache/geode/test/dunit/rules/SharedErrorCollector.java @@ -16,9 +16,7 @@ package org.apache.geode.test.dunit.rules; import static org.apache.geode.test.dunit.VM.getAllVMs; -import java.lang.reflect.Field; import java.util.ArrayList; -import java.util.Collection; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -28,6 +26,7 @@ import org.hamcrest.Matcher; import org.junit.rules.ErrorCollector; import org.apache.geode.test.dunit.VM; +import org.apache.geode.test.junit.rules.accessible.AccessibleErrorCollector; /** * JUnit Rule that provides a shared ErrorCollector in all DistributedTest VMs. In particular, this @@ -58,7 +57,7 @@ import org.apache.geode.test.dunit.VM; */ public class SharedErrorCollector extends AbstractDistributedRule { - private static volatile VisibleErrorCollector errorCollector; + private static volatile AccessibleErrorCollector errorCollector; private final Map<Integer, List<Throwable>> beforeBounceErrors = new HashMap<>(); @@ -73,7 +72,7 @@ public class SharedErrorCollector extends AbstractDistributedRule { @Override protected void after() throws Throwable { - VisibleErrorCollector allErrors = errorCollector; + AccessibleErrorCollector allErrors = errorCollector; try { for (VM vm : getAllVMs()) { List<Throwable> remoteFailures = new ArrayList<>(vm.invoke(() -> errorCollector.errors())); @@ -135,42 +134,7 @@ public class SharedErrorCollector extends AbstractDistributedRule { } private void invokeBefore() { - errorCollector = new VisibleErrorCollector(); + errorCollector = new AccessibleErrorCollector(); } - /** - * Increases visibility of {@link #verify()} to public and uses reflection to acquire access to - * the {@code List} of {@code Throwable}s in {@link ErrorCollector}. - */ - private static class VisibleErrorCollector extends ErrorCollector { - - private final List<Throwable> visibleErrors; - - private VisibleErrorCollector() { - visibleErrors = getErrorsReference(); - } - - @Override - public void verify() throws Throwable { - super.verify(); - } - - List<Throwable> errors() { - return visibleErrors; - } - - void addErrors(Collection<Throwable> errors) { - visibleErrors.addAll(errors); - } - - private List<Throwable> getErrorsReference() { - try { - Field superErrors = ErrorCollector.class.getDeclaredField("errors"); - superErrors.setAccessible(true); - return (List<Throwable>) superErrors.get(this); - } catch (IllegalAccessException | NoSuchFieldException e) { - throw new RuntimeException(e); - } - } - } } diff --git a/geode-junit/src/main/java/org/apache/geode/test/junit/rules/accessible/AccessibleErrorCollector.java b/geode-junit/src/main/java/org/apache/geode/test/junit/rules/accessible/AccessibleErrorCollector.java new file mode 100644 index 0000000..b7ebefa --- /dev/null +++ b/geode-junit/src/main/java/org/apache/geode/test/junit/rules/accessible/AccessibleErrorCollector.java @@ -0,0 +1,64 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more contributor license + * agreements. See the NOTICE file distributed with this work for additional information regarding + * copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the License. You may obtain a + * copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing permissions and limitations under + * the License. + */ +package org.apache.geode.test.junit.rules.accessible; + +import static java.util.Objects.requireNonNull; + +import java.lang.reflect.Field; +import java.util.Collection; +import java.util.List; + +import org.junit.rules.ErrorCollector; + +/** + * Increases visibility of {@link #verify()} to public and uses reflection to acquire access to + * the {@code List} of {@code Throwable}s in {@link ErrorCollector}. + */ +public class AccessibleErrorCollector extends ErrorCollector { + + private final List<Throwable> visibleErrors; + + public AccessibleErrorCollector() { + visibleErrors = getErrorsReference(); + } + + @Override + public void verify() throws Throwable { + super.verify(); + } + + @Override + public void addError(Throwable error) { + super.addError(requireNonNull(error)); + } + + public List<Throwable> errors() { + return visibleErrors; + } + + public void addErrors(Collection<Throwable> errors) { + visibleErrors.addAll(errors); + } + + private List<Throwable> getErrorsReference() { + try { + Field superErrors = ErrorCollector.class.getDeclaredField("errors"); + superErrors.setAccessible(true); + return (List<Throwable>) superErrors.get(this); + } catch (IllegalAccessException | NoSuchFieldException e) { + throw new RuntimeException(e); + } + } +}