Java

Форк
0
89 строк · 2.5 Кб
1
/*
2
 * Copyright (C) 2008 The Guava Authors
3
 *
4
 * Licensed under the Apache License, Version 2.0 (the "License");
5
 * you may not use this file except in compliance with the License.
6
 * You may obtain a copy of the License at
7
 *
8
 * http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 * Unless required by applicable law or agreed to in writing, software
11
 * distributed under the License is distributed on an "AS IS" BASIS,
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 * See the License for the specific language governing permissions and
14
 * limitations under the License.
15
 */
16

17
package com.google.common.testing;
18

19
import static com.google.common.base.Preconditions.checkNotNull;
20

21
import com.google.common.annotations.GwtCompatible;
22
import com.google.common.annotations.VisibleForTesting;
23
import com.google.common.collect.Lists;
24
import com.google.errorprone.annotations.concurrent.GuardedBy;
25
import java.util.ArrayList;
26
import java.util.LinkedList;
27
import java.util.List;
28
import java.util.logging.Level;
29
import java.util.logging.Logger;
30

31
/**
32
 * A {@code TearDownStack} contains a stack of {@link TearDown} instances.
33
 *
34
 * <p>This class is thread-safe.
35
 *
36
 * @author Kevin Bourrillion
37
 * @since 10.0
38
 */
39
@GwtCompatible
40
@ElementTypesAreNonnullByDefault
41
public class TearDownStack implements TearDownAccepter {
42
  private static final Logger logger = Logger.getLogger(TearDownStack.class.getName());
43

44
  @VisibleForTesting final Object lock = new Object();
45

46
  @GuardedBy("lock")
47
  final LinkedList<TearDown> stack = new LinkedList<>();
48

49
  private final boolean suppressThrows;
50

51
  public TearDownStack() {
52
    this.suppressThrows = false;
53
  }
54

55
  public TearDownStack(boolean suppressThrows) {
56
    this.suppressThrows = suppressThrows;
57
  }
58

59
  @Override
60
  public final void addTearDown(TearDown tearDown) {
61
    synchronized (lock) {
62
      stack.addFirst(checkNotNull(tearDown));
63
    }
64
  }
65

66
  /** Causes teardown to execute. */
67
  public final void runTearDown() {
68
    List<Throwable> exceptions = new ArrayList<>();
69
    List<TearDown> stackCopy;
70
    synchronized (lock) {
71
      stackCopy = Lists.newArrayList(stack);
72
      stack.clear();
73
    }
74
    for (TearDown tearDown : stackCopy) {
75
      try {
76
        tearDown.tearDown();
77
      } catch (Throwable t) {
78
        if (suppressThrows) {
79
          logger.log(Level.INFO, "exception thrown during tearDown", t);
80
        } else {
81
          exceptions.add(t);
82
        }
83
      }
84
    }
85
    if (!suppressThrows && (exceptions.size() > 0)) {
86
      throw ClusterException.create(exceptions);
87
    }
88
  }
89
}
90

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.