1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
|
/*
* 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.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Properties;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class PropertiesPluginDescriptorFinderTest {
private VersionManager versionManager;
@TempDir
Path pluginsPath;
@BeforeEach
public void setUp() throws IOException {
Path pluginPath = Files.createDirectories(pluginsPath.resolve("test-plugin-1"));
storePropertiesToPath(getPlugin1Properties(), pluginPath.resolve("plugin.properties"));
pluginPath = Files.createDirectories(pluginsPath.resolve("test-plugin-2"));
storePropertiesToPath(getPlugin2Properties(), pluginPath.resolve("plugin.properties"));
// empty plugin
Files.createDirectories(pluginsPath.resolve("test-plugin-3"));
// no plugin class
pluginPath = Files.createDirectories(pluginsPath.resolve("test-plugin-4"));
storePropertiesToPath(getPlugin4Properties(), pluginPath.resolve("plugin.properties"));
// no plugin version
pluginPath = Files.createDirectories(pluginsPath.resolve("test-plugin-5"));
storePropertiesToPath(getPlugin5Properties(), pluginPath.resolve("plugin.properties"));
// no plugin id
pluginPath = Files.createDirectories(pluginsPath.resolve("test-plugin-6"));
storePropertiesToPath(getPlugin6Properties(), pluginPath.resolve("plugin.properties"));
versionManager = new DefaultVersionManager();
}
@Test
public void testFind() throws Exception {
PluginDescriptorFinder descriptorFinder = new PropertiesPluginDescriptorFinder();
PluginDescriptor plugin1 = descriptorFinder.find(pluginsPath.resolve("test-plugin-1"));
PluginDescriptor plugin2 = descriptorFinder.find(pluginsPath.resolve("test-plugin-2"));
assertEquals("test-plugin-1", plugin1.getPluginId());
assertEquals("Test Plugin 1", plugin1.getPluginDescription());
assertEquals("org.pf4j.plugin.TestPlugin", plugin1.getPluginClass());
assertEquals("0.0.1", plugin1.getVersion());
assertEquals("Decebal Suiu", plugin1.getProvider());
assertEquals(2, plugin1.getDependencies().size());
assertEquals("test-plugin-2", plugin1.getDependencies().get(0).getPluginId());
assertEquals("test-plugin-3", plugin1.getDependencies().get(1).getPluginId());
assertEquals("~1.0", plugin1.getDependencies().get(1).getPluginVersionSupport());
assertEquals("Apache-2.0", plugin1.getLicense());
assertEquals(">=1", plugin1.getRequires());
assertTrue(versionManager.checkVersionConstraint("1.0.0", plugin1.getRequires()));
assertFalse(versionManager.checkVersionConstraint("0.1.0", plugin1.getRequires()));
assertEquals("test-plugin-2", plugin2.getPluginId());
assertEquals("", plugin2.getPluginDescription());
assertEquals("org.pf4j.plugin.TestPlugin", plugin2.getPluginClass());
assertEquals("0.0.1", plugin2.getVersion());
assertEquals("Decebal Suiu", plugin2.getProvider());
assertEquals(0, plugin2.getDependencies().size());
assertEquals("*", plugin2.getRequires()); // Default is *
assertTrue(versionManager.checkVersionConstraint("1.0.0", plugin2.getRequires()));
}
@Test
public void testNotFound() {
PluginDescriptorFinder descriptorFinder = new PropertiesPluginDescriptorFinder();
assertThrows(PluginException.class, () -> descriptorFinder.find(pluginsPath.resolve("test-plugin-3")));
}
private Properties getPlugin1Properties() {
Properties properties = new Properties();
properties.setProperty("plugin.id", "test-plugin-1");
properties.setProperty("plugin.version", "0.0.1");
properties.setProperty("plugin.description", "Test Plugin 1");
properties.setProperty("plugin.provider", "Decebal Suiu");
properties.setProperty("plugin.class", "org.pf4j.plugin.TestPlugin");
properties.setProperty("plugin.dependencies", "test-plugin-2,test-plugin-3@~1.0");
properties.setProperty("plugin.requires", ">=1");
properties.setProperty("plugin.license", "Apache-2.0");
return properties;
}
private Properties getPlugin2Properties() {
Properties properties = new Properties();
properties.setProperty("plugin.id", "test-plugin-2");
properties.setProperty("plugin.version", "0.0.1");
properties.setProperty("plugin.provider", "Decebal Suiu");
properties.setProperty("plugin.class", "org.pf4j.plugin.TestPlugin");
properties.setProperty("plugin.dependencies", "");
return properties;
}
private Properties getPlugin4Properties() {
Properties properties = new Properties();
properties.setProperty("plugin.id", "test-plugin-2");
properties.setProperty("plugin.version", "0.0.1");
properties.setProperty("plugin.provider", "Decebal Suiu");
properties.setProperty("plugin.dependencies", "");
properties.setProperty("plugin.requires", "*");
return properties;
}
private Properties getPlugin5Properties() {
Properties properties = new Properties();
properties.setProperty("plugin.id", "test-plugin-2");
properties.setProperty("plugin.provider", "Decebal Suiu");
properties.setProperty("plugin.class", "org.pf4j.plugin.TestPlugin");
properties.setProperty("plugin.dependencies", "");
properties.setProperty("plugin.requires", "*");
return properties;
}
private Properties getPlugin6Properties() {
Properties properties = new Properties();
properties.setProperty("plugin.version", "0.0.1");
properties.setProperty("plugin.provider", "Decebal Suiu");
properties.setProperty("plugin.class", "org.pf4j.plugin.TestPlugin");
properties.setProperty("plugin.dependencies", "");
properties.setProperty("plugin.requires", "*");
return properties;
}
private void storePropertiesToPath(Properties properties, Path path) throws IOException {
try (Writer writer = new OutputStreamWriter(new FileOutputStream(path.toFile()), StandardCharsets.UTF_8)) {
properties.store(writer, "");
}
}
}
|