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ø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();
}
}
}