-
-
Notifications
You must be signed in to change notification settings - Fork 554
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
Refactoring to make PluginDescriptor more usable #180
Conversation
From #175 (comment) you can see that I planed some modifications related to |
*/ | ||
public class DefaultPluginDescriptor implements PluginDescriptor { | ||
|
||
private String pluginId; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indentation
public class DefaultPluginDescriptor implements PluginDescriptor { | ||
|
||
private String pluginId; | ||
private String pluginDescription; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indentation
* @param provider | ||
* @param license | ||
*/ | ||
public DefaultPluginDescriptor( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I prefer a more compact format, maybe one line
@@ -32,7 +32,7 @@ | |||
* they are not found dependencies, they are dependencies with wrong version). | |||
* This class is very useful for if-else scenarios. | |||
* | |||
* Only some attributes (pluginId, dependencies and pluginVersion) from {@link PluginDescriptor} are used in | |||
* Only some attributes (pluginId, dependencies and pluginVersion) from {@link DefaultPluginDescriptor} are used in |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need to change. I feel that it's from the refactoring action
@@ -88,7 +88,7 @@ protected Path getManifestPath(Path pluginPath) throws PluginException { | |||
} | |||
|
|||
protected PluginDescriptor createPluginDescriptor(Manifest manifest) { | |||
PluginDescriptor pluginDescriptor = createPluginDescriptorInstance(); | |||
DefaultPluginDescriptor pluginDescriptor = createPluginDescriptorInstance(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need to change. I feel that it's from the refactoring action
@@ -38,7 +38,7 @@ | |||
*/ | |||
@Test | |||
public void testCreate() { | |||
PluginDescriptor pluginDescriptor = mock(PluginDescriptor.class); | |||
PluginDescriptor pluginDescriptor = mock(DefaultPluginDescriptor.class); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need to change. I feel that it's from the refactoring action
@@ -57,7 +57,7 @@ public void testCreate() { | |||
*/ | |||
@Test | |||
public void testCreateFail() { | |||
PluginDescriptor pluginDescriptor = mock(PluginDescriptor.class); | |||
PluginDescriptor pluginDescriptor = mock(DefaultPluginDescriptor.class); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need to change. I feel that it's from the refactoring action
@@ -75,7 +75,7 @@ public void testCreateFail() { | |||
*/ | |||
@Test | |||
public void testCreateFailNotFound() { | |||
PluginDescriptor pluginDescriptor = mock(PluginDescriptor.class); | |||
PluginDescriptor pluginDescriptor = mock(DefaultPluginDescriptor.class); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need to change. I feel that it's from the refactoring action
@@ -93,7 +93,7 @@ public void testCreateFailNotFound() { | |||
*/ | |||
@Test | |||
public void testCreateFailConstructor() { | |||
PluginDescriptor pluginDescriptor = mock(PluginDescriptor.class); | |||
PluginDescriptor pluginDescriptor = mock(DefaultPluginDescriptor.class); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need to change. I feel that it's from the refactoring action
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These are for testing the DefaultPluginDescriptor--are you sure you want to mock the interface and test the mock?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This test was not written by me but after I reviewed again the code I believe that original code is the right one. We need to test DefaultPluginFactory
class and from this reason the author mock the code for PluginDescriptor
. I don't know why don't use the real object DefaultPluginDescriptor
without mock but this is another story.
@@ -27,13 +27,13 @@ | |||
|
|||
public class DefaultPluginManagerTest { | |||
|
|||
private PluginDescriptor pd1 = null; | |||
private DefaultPluginDescriptor pd1 = null; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need to change. I feel that it's from the refactoring action
…e test coverage is improving
@@ -125,8 +125,8 @@ protected PluginDescriptor createPluginDescriptor(Manifest manifest) { | |||
return pluginDescriptor; | |||
} | |||
|
|||
protected PluginDescriptor createPluginDescriptorInstance() { | |||
return new PluginDescriptor(); | |||
protected DefaultPluginDescriptor createPluginDescriptorInstance() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My bad. On the returned object we call setXYZ.
* 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; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't forget about the copyright header
@@ -0,0 +1,29 @@ | |||
package org.pf4j.processor; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Copyright header
@@ -93,7 +93,7 @@ public void testCreateFailNotFound() { | |||
*/ | |||
@Test | |||
public void testCreateFailConstructor() { | |||
PluginDescriptor pluginDescriptor = mock(PluginDescriptor.class); | |||
PluginDescriptor pluginDescriptor = mock(DefaultPluginDescriptor.class); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This test was not written by me but after I reviewed again the code I believe that original code is the right one. We need to test DefaultPluginFactory
class and from this reason the author mock the code for PluginDescriptor
. I don't know why don't use the real object DefaultPluginDescriptor
without mock but this is another story.
Can you show us a snippet from your custom PluginDescriptor? |
Sure--I'm already using the changes I've made. With these changes, I can just use a public class YamlPluginDescriptorFinder implements PluginDescriptorFinder {
@Override
public boolean isApplicable(Path pluginPath) {
final String pathName = pluginPath.toString();
if(pathName.endsWith(".jar") || pathName.endsWith(".war") || pathName.endsWith(".zip")) {
try {
final JarFile jarFile = new JarFile(pluginPath.toFile());
final Manifest manifest = jarFile.getManifest();
if(manifest != null) {
final Attributes attributes = manifest.getMainAttributes();
if (attributes != null) {
if("true".equals(attributes.getValue("sunshower-plugin"))) {
return true;
}
}
}
} catch (IOException e) {
return false;
}
}
return false;
}
@Override
public PluginDescriptor find(Path pluginPath) throws PluginException {
try {
final ZipFile zipFile = new ZipFile(pluginPath.toFile());
final ZipEntry entry = zipFile.getEntry("plugin.yml");
if(entry != null) {
return readEntry(zipFile, entry);
}
return null;
} catch (IOException e) {
throw new PluginException(e);
}
}
private PluginDescriptor readEntry(ZipFile zipFile, ZipEntry entry) throws IOException {
final Yaml yaml = new Yaml();
Map load = (Map) yaml.load(zipFile.getInputStream(entry));
return new YamlPluginDescriptor(load);
}
} public class YamlPluginDescriptor extends DefaultPluginDescriptor {
public static final String GROUP_ID_NAME = "group";
public static final String ARTIFACT_ID_NAME = "artifact";
public static final String VERSION_NAME = "version";
public static final String DESCRIPTION_NAME = "version";
public static final String PLUGIN_CLASS_NAME = "plugin-class";
public YamlPluginDescriptor(Map load) {
super(
getRequired(GROUP_ID_NAME, load),
get(DESCRIPTION_NAME, load),
getRequired(PLUGIN_CLASS_NAME, load),
get(VERSION_NAME, load),
);
} |
Your snippet looks good. |
Thanks! |
Thank you! |
I need to be able to create a PluginDescriptor from YAML. This seems to me to make the system even more extensible.