1 /* 2 * Copyright (C) 2010 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.truth.Truth.assertThat; 20 21 import com.google.common.annotations.GwtCompatible; 22 import junit.framework.TestCase; 23 24 /** @author Luiz-Otavio "Z" Zorzella */ 25 @GwtCompatible 26 public class TearDownStackTest extends TestCase { 27 28 private TearDownStack tearDownStack = new TearDownStack(); 29 testSingleTearDown()30 public void testSingleTearDown() throws Exception { 31 final TearDownStack stack = buildTearDownStack(); 32 33 final SimpleTearDown tearDown = new SimpleTearDown(); 34 stack.addTearDown(tearDown); 35 36 assertEquals(false, tearDown.ran); 37 38 stack.runTearDown(); 39 40 assertEquals("tearDown should have run", true, tearDown.ran); 41 } 42 testMultipleTearDownsHappenInOrder()43 public void testMultipleTearDownsHappenInOrder() throws Exception { 44 final TearDownStack stack = buildTearDownStack(); 45 46 final SimpleTearDown tearDownOne = new SimpleTearDown(); 47 stack.addTearDown(tearDownOne); 48 49 final Callback callback = 50 new Callback() { 51 @Override 52 public void run() { 53 assertEquals( 54 "tearDownTwo should have been run before tearDownOne", false, tearDownOne.ran); 55 } 56 }; 57 58 final SimpleTearDown tearDownTwo = new SimpleTearDown(callback); 59 stack.addTearDown(tearDownTwo); 60 61 assertEquals(false, tearDownOne.ran); 62 assertEquals(false, tearDownTwo.ran); 63 64 stack.runTearDown(); 65 66 assertEquals("tearDownOne should have run", true, tearDownOne.ran); 67 assertEquals("tearDownTwo should have run", true, tearDownTwo.ran); 68 } 69 testThrowingTearDown()70 public void testThrowingTearDown() throws Exception { 71 final TearDownStack stack = buildTearDownStack(); 72 73 final ThrowingTearDown tearDownOne = new ThrowingTearDown("one"); 74 stack.addTearDown(tearDownOne); 75 76 final ThrowingTearDown tearDownTwo = new ThrowingTearDown("two"); 77 stack.addTearDown(tearDownTwo); 78 79 assertEquals(false, tearDownOne.ran); 80 assertEquals(false, tearDownTwo.ran); 81 82 try { 83 stack.runTearDown(); 84 fail("runTearDown should have thrown an exception"); 85 } catch (ClusterException expected) { 86 assertThat(expected).hasCauseThat().hasMessageThat().isEqualTo("two"); 87 } catch (RuntimeException e) { 88 throw new RuntimeException( 89 "A ClusterException should have been thrown, rather than a " + e.getClass().getName(), e); 90 } 91 92 assertEquals(true, tearDownOne.ran); 93 assertEquals(true, tearDownTwo.ran); 94 } 95 96 @Override runBare()97 public final void runBare() throws Throwable { 98 try { 99 setUp(); 100 runTest(); 101 } finally { 102 tearDown(); 103 } 104 } 105 106 @Override tearDown()107 protected void tearDown() { 108 tearDownStack.runTearDown(); 109 } 110 111 /** Builds a {@link TearDownStack} that makes sure it's clear by the end of this test. */ buildTearDownStack()112 private TearDownStack buildTearDownStack() { 113 final TearDownStack result = new TearDownStack(); 114 tearDownStack.addTearDown( 115 new TearDown() { 116 117 @Override 118 public void tearDown() throws Exception { 119 synchronized (result.stack) { 120 assertEquals( 121 "The test should have cleared the stack (say, by virtue of running runTearDown)", 122 0, 123 result.stack.size()); 124 } 125 } 126 }); 127 return result; 128 } 129 130 private static final class ThrowingTearDown implements TearDown { 131 132 private final String id; 133 boolean ran = false; 134 ThrowingTearDown(String id)135 ThrowingTearDown(String id) { 136 this.id = id; 137 } 138 139 @Override tearDown()140 public void tearDown() throws Exception { 141 ran = true; 142 throw new RuntimeException(id); 143 } 144 } 145 146 private static final class SimpleTearDown implements TearDown { 147 148 boolean ran = false; 149 Callback callback = null; 150 SimpleTearDown()151 public SimpleTearDown() {} 152 SimpleTearDown(Callback callback)153 public SimpleTearDown(Callback callback) { 154 this.callback = callback; 155 } 156 157 @Override tearDown()158 public void tearDown() throws Exception { 159 if (callback != null) { 160 callback.run(); 161 } 162 ran = true; 163 } 164 } 165 166 private interface Callback { run()167 void run(); 168 } 169 } 170