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; 020import static com.google.common.base.Throwables.throwIfUnchecked; 021 022import com.google.common.annotations.GwtCompatible; 023import com.google.common.annotations.VisibleForTesting; 024import com.google.errorprone.annotations.concurrent.GuardedBy; 025import java.util.ArrayDeque; 026import java.util.ArrayList; 027import java.util.Deque; 028import java.util.List; 029import java.util.logging.Level; 030import java.util.logging.Logger; 031import org.jspecify.annotations.NullMarked; 032 033/** 034 * A {@code TearDownStack} contains a stack of {@link TearDown} instances. 035 * 036 * <p>This class is thread-safe. 037 * 038 * @author Kevin Bourrillion 039 * @since 10.0 040 */ 041@GwtCompatible 042@NullMarked 043public class TearDownStack implements TearDownAccepter { 044 private static final Logger logger = Logger.getLogger(TearDownStack.class.getName()); 045 046 @VisibleForTesting final Object lock = new Object(); 047 048 @GuardedBy("lock") 049 final Deque<TearDown> stack = new ArrayDeque<>(); 050 051 /* 052 * TODO(cpovirk): Rework the "suppress" terminology in this class for Java's "new" concept of 053 * "suppressed exceptions." At the moment, we use addSuppressed only if suppressThrows is *false*! 054 */ 055 056 private final boolean suppressThrows; 057 058 /** 059 * Constructs a new {@code TearDownStack} that does not suppress exceptions throw during teardown. 060 */ 061 public TearDownStack() { 062 this.suppressThrows = false; 063 } 064 065 /** 066 * Constructs a new {@code TearDownStack}, specifying whether to suppress exceptions thrown during 067 * teardown. 068 */ 069 public TearDownStack(boolean suppressThrows) { 070 this.suppressThrows = suppressThrows; 071 } 072 073 @Override 074 public final void addTearDown(TearDown tearDown) { 075 synchronized (lock) { 076 stack.addFirst(checkNotNull(tearDown)); 077 } 078 } 079 080 /** Causes teardown to execute. */ 081 public final void runTearDown() { 082 Throwable exception = null; 083 List<TearDown> stackCopy; 084 synchronized (lock) { 085 stackCopy = new ArrayList<>(stack); 086 stack.clear(); 087 } 088 for (TearDown tearDown : stackCopy) { 089 try { 090 tearDown.tearDown(); 091 } catch (Throwable t) { 092 if (suppressThrows) { 093 logger.log(Level.INFO, "exception thrown during tearDown", t); 094 } else { 095 if (exception == null) { 096 exception = t; 097 } else { 098 exception.addSuppressed(t); 099 } 100 } 101 } 102 } 103 if (exception != null) { 104 throwIfUnchecked(exception); 105 throw new RuntimeException("failure during tearDown", exception); 106 } 107 } 108}