diff options
author | Decebal Suiu <decebal.suiu@gmail.com> | 2024-11-29 21:40:19 +0200 |
---|---|---|
committer | Decebal Suiu <decebal.suiu@gmail.com> | 2024-11-29 21:40:19 +0200 |
commit | 9f596ae83c7c2466969636ac969ff0baed6c62e6 (patch) | |
tree | 48d7aac72377c1266b77207f89d33940d86e8c4c | |
parent | b0cee20307e75b1b00c3ef2c14135774606a7c05 (diff) | |
download | pf4j-9f596ae83c7c2466969636ac969ff0baed6c62e6.tar.gz pf4j-9f596ae83c7c2466969636ac969ff0baed6c62e6.zip |
Add tests for CompoundPluginLoader
-rw-r--r-- | pf4j/src/test/java/org/pf4j/CompoundPluginLoaderTest.java | 96 |
1 files changed, 96 insertions, 0 deletions
diff --git a/pf4j/src/test/java/org/pf4j/CompoundPluginLoaderTest.java b/pf4j/src/test/java/org/pf4j/CompoundPluginLoaderTest.java new file mode 100644 index 0000000..fcbf3a4 --- /dev/null +++ b/pf4j/src/test/java/org/pf4j/CompoundPluginLoaderTest.java @@ -0,0 +1,96 @@ +/* + * Copyright (C) 2012-present the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.pf4j; + +import org.junit.jupiter.api.Test; + +import java.nio.file.Path; + +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +class CompoundPluginLoaderTest { + + @Test + void addLoaderSuccessfully() { + CompoundPluginLoader compoundLoader = new CompoundPluginLoader(); + PluginLoader mockLoader = mock(PluginLoader.class); + compoundLoader.add(mockLoader); + assertTrue(compoundLoader.getLoaders().contains(mockLoader)); + } + + @Test + void addLoaderWithConditionTrue() { + CompoundPluginLoader compoundLoader = new CompoundPluginLoader(); + PluginLoader mockLoader = mock(PluginLoader.class); + compoundLoader.add(mockLoader, () -> true); + assertTrue(compoundLoader.getLoaders().contains(mockLoader)); + } + + @Test + void addLoaderWithConditionFalse() { + CompoundPluginLoader compoundLoader = new CompoundPluginLoader(); + PluginLoader mockLoader = mock(PluginLoader.class); + compoundLoader.add(mockLoader, () -> false); + assertFalse(compoundLoader.getLoaders().contains(mockLoader)); + } + + @Test + void applicableLoaderFound() { + CompoundPluginLoader compoundLoader = new CompoundPluginLoader(); + PluginLoader mockLoader = mock(PluginLoader.class); + Path mockPath = mock(Path.class); + when(mockLoader.isApplicable(mockPath)).thenReturn(true); + compoundLoader.add(mockLoader); + assertTrue(compoundLoader.isApplicable(mockPath)); + } + + @Test + void noApplicableLoaderFound() { + CompoundPluginLoader compoundLoader = new CompoundPluginLoader(); + PluginLoader mockLoader = mock(PluginLoader.class); + Path mockPath = mock(Path.class); + when(mockLoader.isApplicable(mockPath)).thenReturn(false); + compoundLoader.add(mockLoader); + assertFalse(compoundLoader.isApplicable(mockPath)); + } + + @Test + void loadPluginSuccessfully() { + CompoundPluginLoader compoundLoader = new CompoundPluginLoader(); + PluginLoader mockLoader = mock(PluginLoader.class); + Path mockPath = mock(Path.class); + PluginDescriptor mockDescriptor = mock(PluginDescriptor.class); + ClassLoader mockClassLoader = mock(ClassLoader.class); + when(mockLoader.isApplicable(mockPath)).thenReturn(true); + when(mockLoader.loadPlugin(mockPath, mockDescriptor)).thenReturn(mockClassLoader); + compoundLoader.add(mockLoader); + assertEquals(mockClassLoader, compoundLoader.loadPlugin(mockPath, mockDescriptor)); + } + + @Test + void loadPluginThrowsExceptionWhenNoLoaderApplicable() { + CompoundPluginLoader compoundLoader = new CompoundPluginLoader(); + PluginLoader mockLoader = mock(PluginLoader.class); + Path mockPath = mock(Path.class); + PluginDescriptor mockDescriptor = mock(PluginDescriptor.class); + when(mockLoader.isApplicable(mockPath)).thenReturn(false); + compoundLoader.add(mockLoader); + assertThrows(RuntimeException.class, () -> compoundLoader.loadPlugin(mockPath, mockDescriptor)); + } + +} |