Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: enhance PluginFinder to support check plugin availability by version #6236

Merged
merged 1 commit into from
Jul 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@
public interface PluginFinder {

boolean available(String pluginName);

boolean available(String pluginName, String requiresVersion);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.pf4j.PluginManager;
import org.pf4j.PluginState;
import org.pf4j.PluginWrapper;
import run.halo.app.plugin.HaloPluginManager;
import org.springframework.util.Assert;
import run.halo.app.theme.finders.Finder;
import run.halo.app.theme.finders.PluginFinder;

Expand All @@ -17,17 +18,29 @@
@Finder("pluginFinder")
@AllArgsConstructor
public class PluginFinderImpl implements PluginFinder {
private final HaloPluginManager haloPluginManager;
private final PluginManager pluginManager;

@Override
public boolean available(String pluginName) {
if (StringUtils.isBlank(pluginName)) {
return false;
}
PluginWrapper pluginWrapper = haloPluginManager.getPlugin(pluginName);
PluginWrapper pluginWrapper = pluginManager.getPlugin(pluginName);
if (pluginWrapper == null) {
return false;
}
return PluginState.STARTED.equals(pluginWrapper.getPluginState());
}

@Override
public boolean available(String pluginName, String requiresVersion) {
Assert.notNull(requiresVersion, "Requires version must not be null.");
if (!this.available(pluginName)) {
return false;
}
var pluginWrapper = pluginManager.getPlugin(pluginName);
var pluginVersion = pluginWrapper.getDescriptor().getVersion();
return pluginManager.getVersionManager()
.checkVersionConstraint(pluginVersion, requiresVersion);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package run.halo.app.theme.finders.impl;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import org.junit.jupiter.api.Test;
Expand All @@ -10,6 +12,8 @@
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.jupiter.MockitoExtension;
import org.pf4j.DefaultVersionManager;
import org.pf4j.PluginDescriptor;
import org.pf4j.PluginState;
import org.pf4j.PluginWrapper;
import run.halo.app.plugin.HaloPluginManager;
Expand Down Expand Up @@ -47,4 +51,39 @@ void available() {
available = pluginFinder.available("fake-plugin");
assertThat(available).isTrue();
}

@Test
void availableWithVersionTest() {
when(haloPluginManager.getVersionManager()).thenReturn(new DefaultVersionManager());

assertThatThrownBy(() -> pluginFinder.available("fake-plugin", null))
.isInstanceOf(IllegalArgumentException.class);

boolean available = pluginFinder.available("fake-plugin", "1.0.0");
assertThat(available).isFalse();

PluginWrapper mockPluginWrapper = Mockito.mock(PluginWrapper.class);
when(haloPluginManager.getPlugin(eq("fake-plugin")))
.thenReturn(mockPluginWrapper);

when(mockPluginWrapper.getPluginState()).thenReturn(PluginState.STARTED);
var descriptor = mock(PluginDescriptor.class);
when(mockPluginWrapper.getDescriptor()).thenReturn(descriptor);
when(descriptor.getVersion()).thenReturn("1.0.0");

available = pluginFinder.available("fake-plugin", "1.0.0");
assertThat(available).isTrue();

available = pluginFinder.available("fake-plugin", ">=1.0.0");
assertThat(available).isTrue();

available = pluginFinder.available("fake-plugin", "<2.0.0");
assertThat(available).isTrue();

available = pluginFinder.available("fake-plugin", "2.0.0");
assertThat(available).isFalse();

available = pluginFinder.available("fake-plugin", "<1.0.0");
assertThat(available).isFalse();
}
}