Package org.constretto.internal.store

Source Code of org.constretto.internal.store.PropertiesStoreInputStreamCloseTest

package org.constretto.internal.store;

import org.junit.Assert;
import org.constretto.exception.ConstrettoException;
import org.constretto.model.Resource;
import org.junit.Test;
import org.mockito.internal.stubbing.answers.ThrowsException;

import java.io.IOException;
import java.io.InputStream;

import static org.mockito.Mockito.*;

/**
*
* @author <a href="mailto:asbjorn@aarrestad.com>Asbj&oslash;rn Aarrestad</a>
*/
public class PropertiesStoreInputStreamCloseTest {

    @Test
    public void verifyInputStreamIsClosed() throws IOException {
        Resource resource = mock(Resource.class);
        InputStream is = mock(InputStream.class);
        when(resource.exists()).thenReturn(true);
        when(resource.getInputStream()).thenReturn(is);
        PropertiesStore ps = new PropertiesStore();
        ps.addResource(resource);
        verify(is).close();
    }

    @Test
    public void verifyInputStreamIsClosedOnException() throws IOException {
        InputStream is = null;
        try {
            Resource resource = mock(Resource.class);
            is = mock(InputStream.class, new ThrowsException(new IOException()));
            when(resource.exists()).thenReturn(true);
            when(resource.getInputStream()).thenReturn(is);
            PropertiesStore ps = new PropertiesStore();
            ps.addResource(resource);
            Assert.fail();;
        } catch (ConstrettoException ce) {
            verify(is).close();
        }

    }
}
TOP

Related Classes of org.constretto.internal.store.PropertiesStoreInputStreamCloseTest

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.