Search in sources :

Example 1 with ByteStreams

use of com.google.common.io.ByteStreams in project checkstyle by checkstyle.

the class PropertyCacheFileTest method testNonExistentResource.

@Test
public void testNonExistentResource() throws IOException {
    final Configuration config = new DefaultConfiguration("myName");
    final String filePath = File.createTempFile("junit", null, temporaryFolder).getPath();
    final PropertyCacheFile cache = new PropertyCacheFile(config, filePath);
    // create cache with one file
    cache.load();
    final String myFile = "myFile";
    cache.put(myFile, 1);
    final String hash = cache.get(PropertyCacheFile.CONFIG_HASH_KEY);
    assertWithMessage("Config hash key should not be null").that(hash).isNotNull();
    try (MockedStatic<ByteStreams> byteStream = mockStatic(ByteStreams.class)) {
        byteStream.when(() -> ByteStreams.toByteArray(any(BufferedInputStream.class))).thenThrow(IOException.class);
        // apply new external resource to clear cache
        final Set<String> resources = new HashSet<>();
        final String resource = getPath("InputPropertyCacheFile.header");
        resources.add(resource);
        cache.putExternalResources(resources);
        assertWithMessage("Should return false in file is not in cache").that(cache.isInCache(myFile, 1)).isFalse();
        assertWithMessage("Should return false in file is not in cache").that(cache.isInCache(resource, 1)).isFalse();
    }
}
Also used : Configuration(com.puppycrawl.tools.checkstyle.api.Configuration) BufferedInputStream(java.io.BufferedInputStream) ByteStreams(com.google.common.io.ByteStreams) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Aggregations

ByteStreams (com.google.common.io.ByteStreams)1 Configuration (com.puppycrawl.tools.checkstyle.api.Configuration)1 BufferedInputStream (java.io.BufferedInputStream)1 HashSet (java.util.HashSet)1 Test (org.junit.jupiter.api.Test)1 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)1