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.annotations.VisibleForTesting;
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@GwtCompatible
040@ElementTypesAreNonnullByDefault
041public class TearDownStack implements TearDownAccepter {
042  private static final Logger logger = Logger.getLogger(TearDownStack.class.getName());
043
044  @VisibleForTesting final Object lock = new Object();
045
046  @GuardedBy("lock")
047  final LinkedList<TearDown> stack = new LinkedList<>();
048
049  private final boolean suppressThrows;
050
051  public TearDownStack() {
052    this.suppressThrows = false;
053  }
054
055  public TearDownStack(boolean suppressThrows) {
056    this.suppressThrows = suppressThrows;
057  }
058
059  @Override
060  public final void addTearDown(TearDown tearDown) {
061    synchronized (lock) {
062      stack.addFirst(checkNotNull(tearDown));
063    }
064  }
065
066  /** Causes teardown to execute. */
067  public final void runTearDown() {
068    List<Throwable> exceptions = new ArrayList<>();
069    List<TearDown> stackCopy;
070    synchronized (lock) {
071      stackCopy = Lists.newArrayList(stack);
072      stack.clear();
073    }
074    for (TearDown tearDown : stackCopy) {
075      try {
076        tearDown.tearDown();
077      } catch (Throwable t) {
078        if (suppressThrows) {
079          logger.log(Level.INFO, "exception thrown during tearDown", t);
080        } else {
081          exceptions.add(t);
082        }
083      }
084    }
085    if (!suppressThrows && (exceptions.size() > 0)) {
086      throw ClusterException.create(exceptions);
087    }
088  }
089}