• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2012 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.io;
18 
19 import static org.mockito.Mockito.doThrow;
20 import static org.mockito.Mockito.mock;
21 import static org.mockito.Mockito.verify;
22 
23 import java.io.Flushable;
24 import java.io.IOException;
25 import junit.framework.TestCase;
26 
27 /**
28  * Unit tests for {@link Flushables}.
29  *
30  * <p>Checks proper flushing behavior, and ensures that IOExceptions on Flushable.flush() are not
31  * propagated out from the {@link Flushables#flush} method if {@code swallowException} is true.
32  *
33  * @author Michael Lancaster
34  */
35 public class FlushablesTest extends TestCase {
36   private Flushable mockFlushable;
37 
testFlush_clean()38   public void testFlush_clean() throws IOException {
39     // make sure that no exception is thrown regardless of value of
40     // 'swallowException' when the mock does not throw an exception.
41     setupFlushable(false);
42     doFlush(mockFlushable, false, false);
43 
44     setupFlushable(false);
45     doFlush(mockFlushable, true, false);
46   }
47 
testFlush_flushableWithEatenException()48   public void testFlush_flushableWithEatenException() throws IOException {
49     // make sure that no exception is thrown if 'swallowException' is true
50     // when the mock does throw an exception on flush.
51     setupFlushable(true);
52     doFlush(mockFlushable, true, false);
53   }
54 
testFlush_flushableWithThrownException()55   public void testFlush_flushableWithThrownException() throws IOException {
56     // make sure that the exception is thrown if 'swallowException' is false
57     // when the mock does throw an exception on flush.
58     setupFlushable(true);
59     doFlush(mockFlushable, false, true);
60   }
61 
testFlushQuietly_flushableWithEatenException()62   public void testFlushQuietly_flushableWithEatenException() throws IOException {
63     // make sure that no exception is thrown by flushQuietly when the mock does
64     // throw an exception on flush.
65     setupFlushable(true);
66     Flushables.flushQuietly(mockFlushable);
67   }
68 
69   // Set up a flushable to expect to be flushed, and optionally to
70   // throw an exception.
setupFlushable(boolean shouldThrowOnFlush)71   private void setupFlushable(boolean shouldThrowOnFlush) throws IOException {
72     mockFlushable = mock(Flushable.class);
73     if (shouldThrowOnFlush) {
74       doThrow(
75               new IOException(
76                   "This should only appear in the " + "logs. It should not be rethrown."))
77           .when(mockFlushable)
78           .flush();
79     }
80   }
81 
82   // Flush the flushable using the Flushables, passing in the swallowException
83   // parameter. expectThrown determines whether we expect an exception to
84   // be thrown by Flushables.flush;
doFlush(Flushable flushable, boolean swallowException, boolean expectThrown)85   private void doFlush(Flushable flushable, boolean swallowException, boolean expectThrown)
86       throws IOException {
87     try {
88       Flushables.flush(flushable, swallowException);
89       if (expectThrown) {
90         fail("Didn't throw exception.");
91       }
92     } catch (IOException e) {
93       if (!expectThrown) {
94         fail("Threw exception");
95       }
96     }
97     verify(flushable).flush();
98   }
99 }
100