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