2
* Copyright (C) 2008 The Guava Authors
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
* you may not use this file except in compliance with the License.
6
* You may obtain a copy of the License at
8
* http://www.apache.org/licenses/LICENSE-2.0
10
* Unless required by applicable law or agreed to in writing, software
11
* distributed under the License is distributed on an "AS IS" BASIS,
12
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
* See the License for the specific language governing permissions and
14
* limitations under the License.
17
package com.google.common.testing;
19
import static com.google.common.base.Preconditions.checkNotNull;
21
import com.google.common.annotations.GwtCompatible;
22
import com.google.common.annotations.VisibleForTesting;
23
import com.google.common.collect.Lists;
24
import com.google.errorprone.annotations.concurrent.GuardedBy;
25
import java.util.ArrayList;
26
import java.util.LinkedList;
28
import java.util.logging.Level;
29
import java.util.logging.Logger;
32
* A {@code TearDownStack} contains a stack of {@link TearDown} instances.
34
* <p>This class is thread-safe.
36
* @author Kevin Bourrillion
40
@ElementTypesAreNonnullByDefault
41
public class TearDownStack implements TearDownAccepter {
42
private static final Logger logger = Logger.getLogger(TearDownStack.class.getName());
44
@VisibleForTesting final Object lock = new Object();
47
final LinkedList<TearDown> stack = new LinkedList<>();
49
private final boolean suppressThrows;
51
public TearDownStack() {
52
this.suppressThrows = false;
55
public TearDownStack(boolean suppressThrows) {
56
this.suppressThrows = suppressThrows;
60
public final void addTearDown(TearDown tearDown) {
62
stack.addFirst(checkNotNull(tearDown));
66
/** Causes teardown to execute. */
67
public final void runTearDown() {
68
List<Throwable> exceptions = new ArrayList<>();
69
List<TearDown> stackCopy;
71
stackCopy = Lists.newArrayList(stack);
74
for (TearDown tearDown : stackCopy) {
77
} catch (Throwable t) {
79
logger.log(Level.INFO, "exception thrown during tearDown", t);
85
if (!suppressThrows && (exceptions.size() > 0)) {
86
throw ClusterException.create(exceptions);