[java-oidc-common] branch main updated: Add file loading strategy test to test Exception during reading

Phil Smart philip.smart at jisc.ac.uk
Wed Feb 23 11:24:55 UTC 2022


This is an automated email from the git hooks/post-receive script.

philsmart pushed a commit to branch main
in repository java-oidc-common.

View the commit online:
http://git.shibboleth.net/view/?p=java-oidc-common.git;a=commit;h=00bb5e5d5be79a636622306d82aa9c6ae44e6f45

The following commit(s) were added to refs/heads/main by this push:
     new 00bb5e5  Add file loading strategy test to test Exception during reading
00bb5e5 is described below

commit 00bb5e5d5be79a636622306d82aa9c6ae44e6f45
Author: Phil Smart <philip.smart at jisc.ac.uk>
AuthorDate: Wed Feb 23 11:24:50 2022 +0000

    Add file loading strategy test to test Exception during reading
---
 .../cache/impl/DefaultFileLoadingStrategy.java     |  3 +-
 .../impl/DefaultFileLoadingStrategyTest.java       | 39 ++++++++++++++++++----
 2 files changed, 34 insertions(+), 8 deletions(-)

diff --git a/oidc-common-metadata-impl/src/main/java/net/shibboleth/oidc/metadata/cache/impl/DefaultFileLoadingStrategy.java b/oidc-common-metadata-impl/src/main/java/net/shibboleth/oidc/metadata/cache/impl/DefaultFileLoadingStrategy.java
index 64c1668..87e1307 100644
--- a/oidc-common-metadata-impl/src/main/java/net/shibboleth/oidc/metadata/cache/impl/DefaultFileLoadingStrategy.java
+++ b/oidc-common-metadata-impl/src/main/java/net/shibboleth/oidc/metadata/cache/impl/DefaultFileLoadingStrategy.java
@@ -36,7 +36,6 @@ import net.shibboleth.oidc.metadata.cache.CacheLoadingException;
 import net.shibboleth.oidc.metadata.cache.LoadingStrategy;
 import net.shibboleth.oidc.metadata.impl.ResolverHelper;
 import net.shibboleth.utilities.java.support.annotation.constraint.NotEmpty;
-import net.shibboleth.utilities.java.support.resolver.ResolverException;
 
 /** Default strategy for loading information from a file.*/
 @ThreadSafe
@@ -92,7 +91,7 @@ public class DefaultFileLoadingStrategy implements LoadingStrategy {
                 return ResolverHelper.inputstreamToByteArray(new FileInputStream(metadataFile));
             }
             return null;
-        } catch (final IOException | ResolverException e) {
+        } catch (final Exception e) {
             final String errMsg = "Unable to read metadata file " + metadataFile.getAbsolutePath();
             log.error(errMsg, e.getMessage());
             throw new CacheLoadingException(errMsg, e);
diff --git a/oidc-common-metadata-impl/src/test/java/net/shibboleth/oidc/metadata/impl/DefaultFileLoadingStrategyTest.java b/oidc-common-metadata-impl/src/test/java/net/shibboleth/oidc/metadata/impl/DefaultFileLoadingStrategyTest.java
index 4c2e080..6afc68b 100644
--- a/oidc-common-metadata-impl/src/test/java/net/shibboleth/oidc/metadata/impl/DefaultFileLoadingStrategyTest.java
+++ b/oidc-common-metadata-impl/src/test/java/net/shibboleth/oidc/metadata/impl/DefaultFileLoadingStrategyTest.java
@@ -1,32 +1,35 @@
 package net.shibboleth.oidc.metadata.impl;
 
 import static org.junit.Assert.assertNull;
+import static org.mockito.Mockito.when;
 import static org.testng.Assert.assertEquals;
 
+import java.io.File;
+import java.io.FileInputStream;
 import java.io.FileNotFoundException;
 import java.io.IOException;
 import java.nio.charset.StandardCharsets;
 import java.time.Instant;
 
+import org.mockito.Mockito;
 import org.springframework.core.io.ClassPathResource;
 import org.springframework.core.io.Resource;
 import org.testng.annotations.Test;
 
 import net.shibboleth.oidc.metadata.cache.CacheLoadingContext;
+import net.shibboleth.oidc.metadata.cache.CacheLoadingException;
 import net.shibboleth.oidc.metadata.cache.impl.DefaultFileLoadingStrategy;
 
 /** Tests for the DefaultFileLoadingStrategy.*/
 public class DefaultFileLoadingStrategyTest {
     
-    private DefaultFileLoadingStrategy strategy;
-    
     private final static String EXAMPLE_FILE_CONTENT = "test";
     
     @Test
     public void testFileLoads() throws Exception {
         final Resource resource = 
                 new ClassPathResource("/net/shibboleth/oidc/metadata/impl/file-loading-strategy-test.txt");
-        strategy = new DefaultFileLoadingStrategy(resource);
+        final DefaultFileLoadingStrategy strategy = new DefaultFileLoadingStrategy(resource);
         final byte[] loaded = strategy.load(new CacheLoadingContext(null, null));
         assertEquals(loaded, EXAMPLE_FILE_CONTENT.getBytes(StandardCharsets.UTF_8));
     }
@@ -35,12 +38,12 @@ public class DefaultFileLoadingStrategyTest {
     public void testFileDoesNotExist() throws IOException {
         final Resource resource = 
                 new ClassPathResource("/net/shibboleth/oidc/metadata/impl/no-file.txt");
-        strategy = new DefaultFileLoadingStrategy(resource);
+        final DefaultFileLoadingStrategy strategy = new DefaultFileLoadingStrategy(resource);
     }
     
     @Test
     public void testNullFile_NullResponse() throws Exception {
-        strategy = new DefaultFileLoadingStrategy(null);
+        final DefaultFileLoadingStrategy strategy = new DefaultFileLoadingStrategy(null);
         final byte[] loaded = strategy.load(new CacheLoadingContext(null, null));
         assertNull(loaded);
     }
@@ -49,10 +52,34 @@ public class DefaultFileLoadingStrategyTest {
     public void testNullResponse_FileNotUpdatedSinceLastLoad() throws Exception {
         final Resource resource = 
                 new ClassPathResource("/net/shibboleth/oidc/metadata/impl/file-loading-strategy-test.txt");
-        strategy = new DefaultFileLoadingStrategy(resource);
+        final DefaultFileLoadingStrategy strategy = new DefaultFileLoadingStrategy(resource);
         final byte[] loaded = strategy.load(new CacheLoadingContext(Instant.MAX, Instant.MAX));
         assertNull(loaded);
     }
     
+    /**
+     * Test an IO error on reading. Because we can not mock the internal structure
+     * of the File class, this actually dies with an {@link NullPointerException} inside
+     * the {@link FileInputStream} class. It does however trigger the correct branch of
+     * the logic to exercise the error handling.
+     * 
+     * @throws Exception on error.
+     */
+    @Test(expectedExceptions = CacheLoadingException.class)
+    public void testIOErrorOnRead() throws Exception {
+        
+        final Resource mockResource = Mockito.mock(Resource.class);
+        when(mockResource.getDescription()).thenReturn(new String("mock source"));
+        final File mockFile = Mockito.mock(File.class);
+        when(mockResource.getFile()).thenReturn(mockFile);
+        when(mockFile.exists()).thenReturn(true);
+        when(mockFile.isFile()).thenReturn(true);
+        when(mockFile.canRead()).thenReturn(true);
+        when(mockFile.getPath()).thenReturn("no path");
+
+        final DefaultFileLoadingStrategy strategy = new DefaultFileLoadingStrategy(mockResource);
+        final byte[] loaded = strategy.load(new CacheLoadingContext(null, null));
+    }
+    
 
 }

-- 
To stop receiving notification emails like this one, please contact
the administrator of this repository.


More information about the commits mailing list