2
* Copyright (C) 2010 The Guava Authors
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
8
* http://www.apache.org/licenses/LICENSE-2.0
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.
17
package com.google.common.testing;
19
import static com.google.common.truth.Truth.assertThat;
21
import com.google.common.annotations.GwtCompatible;
22
import junit.framework.TestCase;
23
import org.checkerframework.checker.nullness.qual.Nullable;
25
/** @author Luiz-Otavio "Z" Zorzella */
27
public class TearDownStackTest extends TestCase {
29
private TearDownStack tearDownStack = new TearDownStack();
31
public void testSingleTearDown() throws Exception {
32
final TearDownStack stack = buildTearDownStack();
34
final SimpleTearDown tearDown = new SimpleTearDown();
35
stack.addTearDown(tearDown);
37
assertEquals(false, tearDown.ran);
41
assertEquals("tearDown should have run", true, tearDown.ran);
44
public void testMultipleTearDownsHappenInOrder() throws Exception {
45
final TearDownStack stack = buildTearDownStack();
47
final SimpleTearDown tearDownOne = new SimpleTearDown();
48
stack.addTearDown(tearDownOne);
50
final Callback callback =
55
"tearDownTwo should have been run before tearDownOne", false, tearDownOne.ran);
59
final SimpleTearDown tearDownTwo = new SimpleTearDown(callback);
60
stack.addTearDown(tearDownTwo);
62
assertEquals(false, tearDownOne.ran);
63
assertEquals(false, tearDownTwo.ran);
67
assertEquals("tearDownOne should have run", true, tearDownOne.ran);
68
assertEquals("tearDownTwo should have run", true, tearDownTwo.ran);
71
public void testThrowingTearDown() throws Exception {
72
final TearDownStack stack = buildTearDownStack();
74
final ThrowingTearDown tearDownOne = new ThrowingTearDown("one");
75
stack.addTearDown(tearDownOne);
77
final ThrowingTearDown tearDownTwo = new ThrowingTearDown("two");
78
stack.addTearDown(tearDownTwo);
80
assertEquals(false, tearDownOne.ran);
81
assertEquals(false, tearDownTwo.ran);
85
fail("runTearDown should have thrown an exception");
86
} catch (ClusterException expected) {
87
assertThat(expected).hasCauseThat().hasMessageThat().isEqualTo("two");
88
} catch (RuntimeException e) {
89
throw new RuntimeException(
90
"A ClusterException should have been thrown, rather than a " + e.getClass().getName(), e);
93
assertEquals(true, tearDownOne.ran);
94
assertEquals(true, tearDownTwo.ran);
98
public final void runBare() throws Throwable {
108
protected void tearDown() {
109
tearDownStack.runTearDown();
112
/** Builds a {@link TearDownStack} that makes sure it's clear by the end of this test. */
113
private TearDownStack buildTearDownStack() {
114
final TearDownStack result = new TearDownStack();
115
tearDownStack.addTearDown(
119
public void tearDown() throws Exception {
120
synchronized (result.lock) {
122
"The test should have cleared the stack (say, by virtue of running runTearDown)",
124
result.stack.size());
131
private static final class ThrowingTearDown implements TearDown {
133
private final String id;
136
ThrowingTearDown(String id) {
141
public void tearDown() throws Exception {
143
throw new RuntimeException(id);
147
private static final class SimpleTearDown implements TearDown {
150
@Nullable Callback callback = null;
152
public SimpleTearDown() {}
154
public SimpleTearDown(Callback callback) {
155
this.callback = callback;
159
public void tearDown() throws Exception {
160
if (callback != null) {
167
private interface Callback {