001/*
002 * Copyright (C) 2009 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 com.google.common.annotations.Beta;
020import com.google.common.annotations.GwtCompatible;
021import java.util.logging.Level;
022import java.util.logging.Logger;
023
024/**
025 * Simple utility for when you want to create a {@link TearDown} that may throw an exception but
026 * should not fail a test when it does. (The behavior of a {@code TearDown} that throws an exception
027 * varies; see its documentation for details.) Use it just like a {@code TearDown}, except override
028 * {@link #sloppyTearDown()} instead.
029 *
030 * @author Luiz-Otavio Zorzella
031 * @since 10.0
032 */
033@Beta
034@GwtCompatible
035public abstract class SloppyTearDown implements TearDown {
036  private static final Logger logger = Logger.getLogger(SloppyTearDown.class.getName());
037
038  @Override
039  public final void tearDown() {
040    try {
041      sloppyTearDown();
042    } catch (Throwable t) {
043      logger.log(Level.INFO, "exception thrown during tearDown: " + t.getMessage(), t);
044    }
045  }
046
047  public abstract void sloppyTearDown() throws Exception;
048}