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.Beta;
022import com.google.common.annotations.GwtCompatible;
023import com.google.common.collect.Lists;
024import com.google.errorprone.annotations.concurrent.GuardedBy;
025import java.util.ArrayList;
026import java.util.LinkedList;
027import java.util.List;
028import java.util.logging.Level;
029import java.util.logging.Logger;
030
031/**
032 * A {@code TearDownStack} contains a stack of {@link TearDown} instances.
033 *
034 * <p>This class is thread-safe.
035 *
036 * @author Kevin Bourrillion
037 * @since 10.0
038 */
039@Beta
040@GwtCompatible
041public class TearDownStack implements TearDownAccepter {
042  private static final Logger logger = Logger.getLogger(TearDownStack.class.getName());
043
044  @GuardedBy("stack")
045  final LinkedList<TearDown> stack = new LinkedList<>();
046
047  private final boolean suppressThrows;
048
049  public TearDownStack() {
050    this.suppressThrows = false;
051  }
052
053  public TearDownStack(boolean suppressThrows) {
054    this.suppressThrows = suppressThrows;
055  }
056
057  @Override
058  public final void addTearDown(TearDown tearDown) {
059    synchronized (stack) {
060      stack.addFirst(checkNotNull(tearDown));
061    }
062  }
063
064  /** Causes teardown to execute. */
065  public final void runTearDown() {
066    List<Throwable> exceptions = new ArrayList<>();
067    List<TearDown> stackCopy;
068    synchronized (stack) {
069      stackCopy = Lists.newArrayList(stack);
070      stack.clear();
071    }
072    for (TearDown tearDown : stackCopy) {
073      try {
074        tearDown.tearDown();
075      } catch (Throwable t) {
076        if (suppressThrows) {
077          logger.log(Level.INFO, "exception thrown during tearDown", t);
078        } else {
079          exceptions.add(t);
080        }
081      }
082    }
083    if ((!suppressThrows) && (exceptions.size() > 0)) {
084      throw ClusterException.create(exceptions);
085    }
086  }
087}