diff options
author | Decebal Suiu <decebal.suiu@gmail.com> | 2024-12-27 22:28:39 +0200 |
---|---|---|
committer | Decebal Suiu <decebal.suiu@gmail.com> | 2024-12-27 22:28:39 +0200 |
commit | 83ac693a3773e77ad10d7a087fc25152b7384184 (patch) | |
tree | 6fd1c59fec8a776252a52da862cb176a07da5a1b | |
parent | d763024aac175c2a5c3bedadc2986dd2111db65b (diff) | |
download | pf4j-83ac693a3773e77ad10d7a087fc25152b7384184.tar.gz pf4j-83ac693a3773e77ad10d7a087fc25152b7384184.zip |
8 files changed, 173 insertions, 64 deletions
diff --git a/pf4j/src/test/java/org/pf4j/CompoundPluginDescriptorFinderTest.java b/pf4j/src/test/java/org/pf4j/CompoundPluginDescriptorFinderTest.java index f80c19e..9823882 100644 --- a/pf4j/src/test/java/org/pf4j/CompoundPluginDescriptorFinderTest.java +++ b/pf4j/src/test/java/org/pf4j/CompoundPluginDescriptorFinderTest.java @@ -18,8 +18,8 @@ package org.pf4j; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.io.TempDir; import org.pf4j.test.PluginJar; -import org.pf4j.test.PluginZip; import org.pf4j.test.TestPlugin; +import org.pf4j.test.PropertiesUtils; import java.io.FileOutputStream; import java.io.IOException; @@ -113,7 +113,7 @@ public class CompoundPluginDescriptorFinderTest { map.put(PropertiesPluginDescriptorFinder.PLUGIN_REQUIRES, ">=1"); map.put(PropertiesPluginDescriptorFinder.PLUGIN_LICENSE, "Apache-2.0"); - return PluginZip.createProperties(map); + return PropertiesUtils.createProperties(map); } private void storePropertiesToPath(Properties properties, Path pluginPath) throws IOException { diff --git a/pf4j/src/test/java/org/pf4j/ManifestPluginDescriptorFinderTest.java b/pf4j/src/test/java/org/pf4j/ManifestPluginDescriptorFinderTest.java index 079d8e2..d34a77d 100644 --- a/pf4j/src/test/java/org/pf4j/ManifestPluginDescriptorFinderTest.java +++ b/pf4j/src/test/java/org/pf4j/ManifestPluginDescriptorFinderTest.java @@ -18,7 +18,7 @@ package org.pf4j; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.io.TempDir; -import org.pf4j.test.PluginJar; +import org.pf4j.test.ManifestUtils; import java.io.FileOutputStream; import java.io.IOException; @@ -121,7 +121,7 @@ public class ManifestPluginDescriptorFinderTest { map.put(ManifestPluginDescriptorFinder.PLUGIN_REQUIRES, "*"); map.put(ManifestPluginDescriptorFinder.PLUGIN_LICENSE, "Apache-2.0"); - return PluginJar.createManifest(map); + return ManifestUtils.createManifest(map); } private Manifest getPlugin2Manifest() { @@ -132,7 +132,7 @@ public class ManifestPluginDescriptorFinderTest { map.put(ManifestPluginDescriptorFinder.PLUGIN_PROVIDER, "Decebal Suiu"); map.put(ManifestPluginDescriptorFinder.PLUGIN_DEPENDENCIES, ""); - return PluginJar.createManifest(map); + return ManifestUtils.createManifest(map); } private Manifest getPlugin4Manifest() { @@ -141,7 +141,7 @@ public class ManifestPluginDescriptorFinderTest { map.put(ManifestPluginDescriptorFinder.PLUGIN_VERSION, "0.0.1"); map.put(ManifestPluginDescriptorFinder.PLUGIN_PROVIDER, "Decebal Suiu"); - return PluginJar.createManifest(map); + return ManifestUtils.createManifest(map); } private Manifest getPlugin5Manifest() { @@ -150,7 +150,7 @@ public class ManifestPluginDescriptorFinderTest { map.put(ManifestPluginDescriptorFinder.PLUGIN_CLASS, "org.pf4j.plugin.TestPlugin"); map.put(ManifestPluginDescriptorFinder.PLUGIN_PROVIDER, "Decebal Suiu"); - return PluginJar.createManifest(map); + return ManifestUtils.createManifest(map); } private Manifest getPlugin6Manifest() { @@ -158,7 +158,7 @@ public class ManifestPluginDescriptorFinderTest { map.put(ManifestPluginDescriptorFinder.PLUGIN_CLASS, "org.pf4j.plugin.TestPlugin"); map.put(ManifestPluginDescriptorFinder.PLUGIN_PROVIDER, "Decebal Suiu"); - return PluginJar.createManifest(map); + return ManifestUtils.createManifest(map); } private void storeManifestToPath(Manifest manifest, Path pluginPath) throws IOException { diff --git a/pf4j/src/test/java/org/pf4j/PluginClassLoaderTest.java b/pf4j/src/test/java/org/pf4j/PluginClassLoaderTest.java index 1a24770..b43d6b9 100644 --- a/pf4j/src/test/java/org/pf4j/PluginClassLoaderTest.java +++ b/pf4j/src/test/java/org/pf4j/PluginClassLoaderTest.java @@ -133,7 +133,7 @@ class PluginClassLoaderTest { .addFile(whaszzupGreetingClassPath, JavaFileObjectUtils.getAllBytes(generatedClasses.get(JavaSources.WHAZZUP_GREETING_CLASS_NAME))) .build(); - FileUtils.expandIfZip(pluginDependencyZip.path()); + pluginDependencyZip.unzip(); PluginClasspath pluginDependencyClasspath = new DefaultPluginClasspath(); @@ -175,7 +175,7 @@ class PluginClassLoaderTest { .addFile(classesPath.resolve(LegacyExtensionStorage.EXTENSIONS_RESOURCE), "plugin") .build(); - FileUtils.expandIfZip(pluginZip.path()); + pluginZip.unzip(); PluginClasspath pluginClasspath = new DefaultPluginClasspath(); diff --git a/pf4j/src/test/java/org/pf4j/PropertiesPluginDescriptorFinderTest.java b/pf4j/src/test/java/org/pf4j/PropertiesPluginDescriptorFinderTest.java index 4d73a4f..987ee13 100644 --- a/pf4j/src/test/java/org/pf4j/PropertiesPluginDescriptorFinderTest.java +++ b/pf4j/src/test/java/org/pf4j/PropertiesPluginDescriptorFinderTest.java @@ -18,8 +18,8 @@ package org.pf4j; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.io.TempDir; -import org.pf4j.test.PluginZip; import org.pf4j.test.TestPlugin; +import org.pf4j.test.PropertiesUtils; import java.io.FileOutputStream; import java.io.IOException; @@ -118,7 +118,7 @@ public class PropertiesPluginDescriptorFinderTest { map.put(PropertiesPluginDescriptorFinder.PLUGIN_REQUIRES, ">=1"); map.put(PropertiesPluginDescriptorFinder.PLUGIN_LICENSE, "Apache-2.0"); - return PluginZip.createProperties(map); + return PropertiesUtils.createProperties(map); } private Properties getPlugin2Properties() { @@ -129,7 +129,7 @@ public class PropertiesPluginDescriptorFinderTest { map.put(PropertiesPluginDescriptorFinder.PLUGIN_PROVIDER, "Decebal Suiu"); map.put(PropertiesPluginDescriptorFinder.PLUGIN_DEPENDENCIES, ""); - return PluginZip.createProperties(map); + return PropertiesUtils.createProperties(map); } private Properties getPlugin4Properties() { @@ -140,7 +140,7 @@ public class PropertiesPluginDescriptorFinderTest { map.put(PropertiesPluginDescriptorFinder.PLUGIN_DEPENDENCIES, ""); map.put(PropertiesPluginDescriptorFinder.PLUGIN_REQUIRES, "*"); - return PluginZip.createProperties(map); + return PropertiesUtils.createProperties(map); } private Properties getPlugin5Properties() { @@ -151,7 +151,7 @@ public class PropertiesPluginDescriptorFinderTest { map.put(PropertiesPluginDescriptorFinder.PLUGIN_DEPENDENCIES, ""); map.put(PropertiesPluginDescriptorFinder.PLUGIN_REQUIRES, "*"); - return PluginZip.createProperties(map); + return PropertiesUtils.createProperties(map); } private Properties getPlugin6Properties() { @@ -162,7 +162,7 @@ public class PropertiesPluginDescriptorFinderTest { map.put(PropertiesPluginDescriptorFinder.PLUGIN_DEPENDENCIES, ""); map.put(PropertiesPluginDescriptorFinder.PLUGIN_REQUIRES, "*"); - return PluginZip.createProperties(map); + return PropertiesUtils.createProperties(map); } private void storePropertiesToPath(Properties properties, Path pluginPath) throws IOException { diff --git a/pf4j/src/test/java/org/pf4j/test/ManifestUtils.java b/pf4j/src/test/java/org/pf4j/test/ManifestUtils.java new file mode 100644 index 0000000..e4dd33f --- /dev/null +++ b/pf4j/src/test/java/org/pf4j/test/ManifestUtils.java @@ -0,0 +1,40 @@ +/* + * 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.test; + +import java.util.Map; +import java.util.jar.Attributes; +import java.util.jar.Manifest; + +public class ManifestUtils { + + private ManifestUtils() {} + + /** + * Creates a {@link Manifest} object from the given map. + */ + public static Manifest createManifest(Map<String, String> map) { + Manifest manifest = new Manifest(); + Attributes attributes = manifest.getMainAttributes(); + attributes.put(Attributes.Name.MANIFEST_VERSION, "1.0.0"); + for (Map.Entry<String, String> entry : map.entrySet()) { + attributes.put(new Attributes.Name(entry.getKey()), entry.getValue()); + } + + return manifest; + } + +} diff --git a/pf4j/src/test/java/org/pf4j/test/PluginJar.java b/pf4j/src/test/java/org/pf4j/test/PluginJar.java index 034b10f..1ad4d21 100644 --- a/pf4j/src/test/java/org/pf4j/test/PluginJar.java +++ b/pf4j/src/test/java/org/pf4j/test/PluginJar.java @@ -16,6 +16,7 @@ package org.pf4j.test; import org.pf4j.ManifestPluginDescriptorFinder; +import org.pf4j.processor.LegacyExtensionStorage; import java.io.ByteArrayOutputStream; import java.io.File; @@ -28,7 +29,6 @@ import java.util.LinkedHashMap; import java.util.LinkedHashSet; import java.util.Map; import java.util.Set; -import java.util.jar.Attributes; import java.util.jar.JarEntry; import java.util.jar.JarOutputStream; import java.util.jar.Manifest; @@ -53,46 +53,50 @@ public class PluginJar { this.pluginVersion = builder.pluginVersion; } + /** + * Returns the {@code jar} file path. + */ public Path path() { return path; } + /** + * Returns the {@code jar} file. + */ public File file() { return path.toFile(); } + /** + * Returns the plugin class. + */ public String pluginClass() { return pluginClass; } + /** + * Returns the plugin id. + */ public String pluginId() { return pluginId; } + /** + * Returns the plugin version. + */ public String pluginVersion() { return pluginVersion; } - public static Manifest createManifest(Map<String, String> map) { - Manifest manifest = new Manifest(); - Attributes attributes = manifest.getMainAttributes(); - attributes.put(Attributes.Name.MANIFEST_VERSION, "1.0.0"); - for (Map.Entry<String, String> entry : map.entrySet()) { - attributes.put(new Attributes.Name(entry.getKey()), entry.getValue()); - } - - return manifest; - } - public static class Builder { private final Path path; private final String pluginId; + private final Map<String, String> manifestAttributes = new LinkedHashMap<>(); + private final Set<String> extensions = new LinkedHashSet<>(); private String pluginClass; private String pluginVersion; - private Map<String, String> manifestAttributes = new LinkedHashMap<>(); - private Set<String> extensions = new LinkedHashSet<>(); private ClassDataProvider classDataProvider = new DefaultClassDataProvider(); public Builder(Path path, String pluginId) { @@ -144,13 +148,15 @@ public class PluginJar { return this; } + /** + * Builds the {@link PluginJar} instance. + */ public PluginJar build() throws IOException { - Manifest manifest = createManifest(); try (OutputStream outputStream = new FileOutputStream(path.toFile()); - JarOutputStream jarOutputStream = new JarOutputStream(outputStream, manifest)) { + JarOutputStream jarOutputStream = new JarOutputStream(outputStream, createManifest())) { if (!extensions.isEmpty()) { // add extensions.idx - JarEntry jarEntry = new JarEntry("META-INF/extensions.idx"); + JarEntry jarEntry = new JarEntry(LegacyExtensionStorage.EXTENSIONS_RESOURCE); jarOutputStream.putNextEntry(jarEntry); jarOutputStream.write(extensionsAsByteArray()); jarOutputStream.closeEntry(); @@ -175,11 +181,9 @@ public class PluginJar { if (pluginClass != null) { map.put(ManifestPluginDescriptorFinder.PLUGIN_CLASS, pluginClass); } - if (manifestAttributes != null) { - map.putAll(manifestAttributes); - } + map.putAll(manifestAttributes); - return PluginJar.createManifest(map); + return ManifestUtils.createManifest(map); } private byte[] extensionsAsByteArray() throws IOException { diff --git a/pf4j/src/test/java/org/pf4j/test/PluginZip.java b/pf4j/src/test/java/org/pf4j/test/PluginZip.java index 4b9a0c9..45fb3b2 100644 --- a/pf4j/src/test/java/org/pf4j/test/PluginZip.java +++ b/pf4j/src/test/java/org/pf4j/test/PluginZip.java @@ -16,6 +16,7 @@ package org.pf4j.test; import org.pf4j.PropertiesPluginDescriptorFinder; +import org.pf4j.util.FileUtils; import java.io.File; import java.io.FileOutputStream; @@ -49,52 +50,79 @@ public class PluginZip { this.pluginDependencies = builder.pluginDependencies; } + /** + * Returns the path of the {@code zip} file. + */ public Path path() { return path; } + /** + * Returns the {@code zip} file. + */ public File file() { return path.toFile(); } + /** + * Returns the plugin id. + */ public String pluginId() { return pluginId; } + /** + * Returns the plugin class. + */ public String pluginClass() { return pluginClass; } + /** + * Returns the plugin version. + */ public String pluginVersion() { return pluginVersion; } + /** + * Returns the plugin dependencies. + */ public String pluginDependencies() { return pluginDependencies; } + /** + * Returns the path where the {@code zip} file will be unzipped. + */ public Path unzippedPath() { - Path path = path(); - String fileName = path.getFileName().toString(); + Path zipPath = path(); + String fileName = zipPath.getFileName().toString(); - return path.getParent().resolve(fileName.substring(0, fileName.length() - 4)); // without ".zip" suffix + return zipPath.getParent().resolve(fileName.substring(0, fileName.length() - 4)); // without ".zip" suffix } - public static Properties createProperties(Map<String, String> map) { - Properties properties = new Properties(); - properties.putAll(map); - - return properties; + /** + * Unzips the {@code zip} file. + */ + public Path unzip() throws IOException { + return FileUtils.expandIfZip(path()); } + /** + * Builder for {@link PluginZip}. + * The {@code plugin.properties} file is created on the fly from the information supplied in this builder. + * The {@code plugin.properties} file is created in the root of the {@code zip} file. + * The {@code zip} file can contain extra files. + */ public static class Builder { private final Path path; private final String pluginId; + private final Map<String, String> properties = new LinkedHashMap<>(); + private final Map<Path, byte[]> files = new LinkedHashMap<>(); private String pluginClass; private String pluginVersion; private String pluginDependencies; - private Map<String, String> properties = new LinkedHashMap<>(); - private Map<Path, byte[]> files = new LinkedHashMap<>(); public Builder(Path path, String pluginId) { this.path = path; @@ -163,13 +191,28 @@ public class PluginZip { return this; } + /** + * Builds the {@link PluginZip} instance. + */ public PluginZip build() throws IOException { - createPropertiesFile(); + try (ZipOutputStream outputStream = new ZipOutputStream(new FileOutputStream(path.toFile()))) { + ZipEntry propertiesFile = new ZipEntry(PropertiesPluginDescriptorFinder.DEFAULT_PROPERTIES_FILE_NAME); + outputStream.putNextEntry(propertiesFile); + createProperties().store(outputStream, ""); + outputStream.closeEntry(); + + for (Map.Entry<Path, byte[]> fileEntry : files.entrySet()) { + ZipEntry file = new ZipEntry(fileEntry.getKey().toString()); + outputStream.putNextEntry(file); + outputStream.write(fileEntry.getValue()); + outputStream.closeEntry(); + } + } return new PluginZip(this); } - protected void createPropertiesFile() throws IOException { + private Properties createProperties() { Map<String, String> map = new LinkedHashMap<>(); map.put(PropertiesPluginDescriptorFinder.PLUGIN_ID, pluginId); map.put(PropertiesPluginDescriptorFinder.PLUGIN_VERSION, pluginVersion); @@ -179,23 +222,10 @@ public class PluginZip { if (pluginClass != null) { map.put(PropertiesPluginDescriptorFinder.PLUGIN_CLASS, pluginClass); } - if (properties != null) { - map.putAll(properties); - } - try (ZipOutputStream outputStream = new ZipOutputStream(new FileOutputStream(path.toFile()))) { - ZipEntry propertiesFile = new ZipEntry(PropertiesPluginDescriptorFinder.DEFAULT_PROPERTIES_FILE_NAME); - outputStream.putNextEntry(propertiesFile); - createProperties(map).store(outputStream, ""); - outputStream.closeEntry(); + map.putAll(properties); - for (Map.Entry<Path, byte[]> fileEntry : files.entrySet()) { - ZipEntry file = new ZipEntry(fileEntry.getKey().toString()); - outputStream.putNextEntry(file); - outputStream.write(fileEntry.getValue()); - outputStream.closeEntry(); - } - } + return PropertiesUtils.createProperties(map); } } diff --git a/pf4j/src/test/java/org/pf4j/test/PropertiesUtils.java b/pf4j/src/test/java/org/pf4j/test/PropertiesUtils.java new file mode 100644 index 0000000..72b3751 --- /dev/null +++ b/pf4j/src/test/java/org/pf4j/test/PropertiesUtils.java @@ -0,0 +1,35 @@ +/* + * 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.test; + +import java.util.Map; +import java.util.Properties; + +public class PropertiesUtils { + + private PropertiesUtils() {} + + /** + * Creates a {@link Properties} object from the given map. + */ + public static Properties createProperties(Map<String, String> map) { + Properties properties = new Properties(); + properties.putAll(map); + + return properties; + } + +} |