001/*
002 * Copyright (C) 2008 The Guava Authors
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package com.google.common.testing;
018
019import static com.google.common.base.Preconditions.checkNotNull;
020
021import com.google.common.annotations.GwtCompatible;
022import com.google.common.collect.Lists;
023import com.google.errorprone.annotations.concurrent.GuardedBy;
024import java.util.ArrayList;
025import java.util.LinkedList;
026import java.util.List;
027import java.util.logging.Level;
028import java.util.logging.Logger;
029
030/**
031 * A {@code TearDownStack} contains a stack of {@link TearDown} instances.
032 *
033 * <p>This class is thread-safe.
034 *
035 * @author Kevin Bourrillion
036 * @since 10.0
037 */
038@GwtCompatible
039@ElementTypesAreNonnullByDefault
040public class TearDownStack implements TearDownAccepter {
041  private static final Logger logger = Logger.getLogger(TearDownStack.class.getName());
042
043  @GuardedBy("stack")
044  final LinkedList<TearDown> stack = new LinkedList<>();
045
046  private final boolean suppressThrows;
047
048  public TearDownStack() {
049    this.suppressThrows = false;
050  }
051
052  public TearDownStack(boolean suppressThrows) {
053    this.suppressThrows = suppressThrows;
054  }
055
056  @Override
057  public final void addTearDown(TearDown tearDown) {
058    synchronized (stack) {
059      stack.addFirst(checkNotNull(tearDown));
060    }
061  }
062
063  /** Causes teardown to execute. */
064  public final void runTearDown() {
065    List<Throwable> exceptions = new ArrayList<>();
066    List<TearDown> stackCopy;
067    synchronized (stack) {
068      stackCopy = Lists.newArrayList(stack);
069      stack.clear();
070    }
071    for (TearDown tearDown : stackCopy) {
072      try {
073        tearDown.tearDown();
074      } catch (Throwable t) {
075        if (suppressThrows) {
076          logger.log(Level.INFO, "exception thrown during tearDown", t);
077        } else {
078          exceptions.add(t);
079        }
080      }
081    }
082    if (!suppressThrows && (exceptions.size() > 0)) {
083      throw ClusterException.create(exceptions);
084    }
085  }
086}