Revision: 5943
          http://jnode.svn.sourceforge.net/jnode/?rev=5943&view=rev
Author:   galatnm
Date:     2012-12-27 15:08:56 +0000 (Thu, 27 Dec 2012)
Log Message:
-----------
CORE : Add unit tests for PluginDescriptorModel

Modified Paths:
--------------
    trunk/core/src/core/org/jnode/plugin/model/ConfigurationElementModel.java
    trunk/core/src/core/org/jnode/plugin/model/PluginDescriptorModel.java
    trunk/core/src/core/org/jnode/plugin/model/PluginPrerequisiteModel.java

Added Paths:
-----------
    trunk/core/src/test/org/jnode/plugin/
    trunk/core/src/test/org/jnode/plugin/model/
    trunk/core/src/test/org/jnode/plugin/model/PluginDescriptorModelTest.java

Modified: 
trunk/core/src/core/org/jnode/plugin/model/ConfigurationElementModel.java
===================================================================
--- trunk/core/src/core/org/jnode/plugin/model/ConfigurationElementModel.java   
2012-12-21 15:21:18 UTC (rev 5942)
+++ trunk/core/src/core/org/jnode/plugin/model/ConfigurationElementModel.java   
2012-12-27 15:08:56 UTC (rev 5943)
@@ -17,12 +17,13 @@
  * along with this library; If not, write to the Free Software Foundation, 
Inc., 
  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  */
- 
+
 package org.jnode.plugin.model;
 
 import java.util.ArrayList;
 import java.util.HashSet;
 import java.util.Set;
+
 import org.jnode.nanoxml.XMLElement;
 import org.jnode.plugin.ConfigurationElement;
 import org.jnode.plugin.PluginException;
@@ -32,72 +33,76 @@
  */
 final class ConfigurationElementModel extends PluginModelObject implements 
ConfigurationElement {
 
+    /** Name of the current configuration element. */
     private final String name;
     private final AttributeModel[] attributes;
+    /** Child elements of the current configuration element. */
     private final ConfigurationElement[] elements;
 
     /**
-     * Create a new instance
-     *
-     * @param e
+     * Create new instance of configuration element model.
+     * 
+     * @param plugin The model for plugin descriptor.
+     * @param element An XML element.
+     * @throws PluginException
      */
-    public ConfigurationElementModel(PluginDescriptorModel plugin, XMLElement 
e)
-        throws PluginException {
+    public ConfigurationElementModel(PluginDescriptorModel plugin, XMLElement 
element)
+            throws PluginException {
         super(plugin);
-        name = e.getName();
+        name = element.getName();
 
-        final Set<String> aI = e.attributeNames();
-        if (!aI.isEmpty()) {
+        final Set<String> attributeNames = element.attributeNames();
+        if (!attributeNames.isEmpty()) {
             final ArrayList<AttributeModel> list = new 
ArrayList<AttributeModel>();
-            for (String name : aI) {
-                final String value = e.getStringAttribute(name);
+            for (String name : attributeNames) {
+                final String value = element.getStringAttribute(name);
                 list.add(new AttributeModel(name, value));
                 if (value == null) {
                     throw new PluginException("Cannot find attribute value for 
attribute " + name);
                 }
-                //System.out.println("name[" + name + "] value[" + value + 
"]");
             }
-            attributes = (AttributeModel[]) list.toArray(new 
AttributeModel[list.size()]);
+            attributes = list.toArray(new AttributeModel[list.size()]);
         } else {
             attributes = null;
         }
 
-        final ArrayList<ConfigurationElementModel> list = new 
ArrayList<ConfigurationElementModel>();
-        for (final XMLElement ce : e.getChildren()) {
+        final ArrayList<ConfigurationElementModel> list =
+                new ArrayList<ConfigurationElementModel>();
+        for (final XMLElement ce : element.getChildren()) {
             list.add(new ConfigurationElementModel(plugin, ce));
         }
-        elements = (ConfigurationElement[]) list.toArray(new 
ConfigurationElement[list.size()]);
+        elements = list.toArray(new ConfigurationElement[list.size()]);
     }
 
     /**
      * Gets the value of an attribute with a given name
-     *
-     * @param name
-     * @return The attribute value, or null if not found
+     * 
+     * @param name Name of the element.
+     * @return The attribute value, or null if not found.
      */
+    @Override
     public String getAttribute(String name) {
         if (attributes != null) {
-            final int max = attributes.length;
-            for (int i = 0; i < max; i++) {
-                if (attributes[i].getName().equals(name)) {
-                    return attributes[i].getValue();
+            for (AttributeModel attribute : attributes)
+                if (attribute.equals(name)) {
+                    return attribute.getValue();
                 }
-            }
         }
         return null;
     }
 
-
     /**
      * Gets the names of all attributes in this element.
-     *
-     * @return
+     * 
+     * @return A set of attribute names. This set can be empty if no attributes
+     *         found.
      */
+    @Override
     public Set<String> attributeNames() {
         final HashSet<String> set = new HashSet<String>();
         if (attributes != null) {
-            for (AttributeModel attr : attributes) {
-                set.add(attr.getName());
+            for (AttributeModel attribute : attributes) {
+                set.add(attribute.getName());
             }
         }
         return set;
@@ -106,6 +111,7 @@
     /**
      * Gets all child elements
      */
+    @Override
     public ConfigurationElement[] getElements() {
         return elements;
     }
@@ -113,14 +119,15 @@
     /**
      * Gets the name of this element
      */
+    @Override
     public String getName() {
         return name;
     }
 
-
     /**
      * Resolve all references to (elements of) other plugin descriptors
      */
+    @Override
     protected void resolve(PluginRegistryModel registry) {
         // Do nothing
     }
@@ -128,14 +135,17 @@
     /**
      * Remove all references to (elements of) other plugin descriptors
      */
+    @Override
     protected void unresolve(PluginRegistryModel registry) {
         // Do nothing
     }
 
+    @Override
     public String toString() {
         StringBuilder tmp = new StringBuilder(name);
         for (AttributeModel attr : attributes) {
-            tmp.append(' 
').append(attr.getName()).append("=\"").append(attr.getValue()).append('\"');
+            tmp.append(' 
').append(attr.getName()).append("=\"").append(attr.getValue())
+                    .append('\"');
         }
         return tmp.toString();
     }

Modified: trunk/core/src/core/org/jnode/plugin/model/PluginDescriptorModel.java
===================================================================
--- trunk/core/src/core/org/jnode/plugin/model/PluginDescriptorModel.java       
2012-12-21 15:21:18 UTC (rev 5942)
+++ trunk/core/src/core/org/jnode/plugin/model/PluginDescriptorModel.java       
2012-12-27 15:08:56 UTC (rev 5943)
@@ -111,25 +111,25 @@
     /**
      * Create a new instance
      *
-     * @param e the root XMLElement for the XML plugin descriptor
+     * @param rootElement the root XMLElement for the XML plugin descriptor
      * @param jarFile the PluginJar object to associate with the descriptor.
      */
-    PluginDescriptorModel(PluginJar jarFile, XMLElement e)
+    PluginDescriptorModel(PluginJar jarFile, XMLElement rootElement)
         throws PluginException {
         this.jarFile = jarFile;
         this.fragments = new BootableArrayList<FragmentDescriptorModel>();
-        id = getAttribute(e, "id", true);
-        name = getAttribute(e, "name", true);
-        providerName = getAttribute(e, "provider-name", false);
-        providerUrl = getAttribute(e, "provider-url", false);
-        licenseName = getAttribute(e, "license-name", true);
-        licenseUrl = getAttribute(e, "license-url", false);
-        version = getAttribute(e, "version", true);
-        className = getAttribute(e, "class", false);
-        system = getBooleanAttribute(e, "system", false);
-        autoStart = getBooleanAttribute(e, "auto-start", false);
+        id = getAttribute(rootElement, "id", true);
+        name = getAttribute(rootElement, "name", true);
+        providerName = getAttribute(rootElement, "provider-name", false);
+        providerUrl = getAttribute(rootElement, "provider-url", false);
+        licenseName = getAttribute(rootElement, "license-name", true);
+        licenseUrl = getAttribute(rootElement, "license-url", false);
+        version = getAttribute(rootElement, "version", true);
+        className = getAttribute(rootElement, "class", false);
+        system = getBooleanAttribute(rootElement, "system", false);
+        autoStart = getBooleanAttribute(rootElement, "auto-start", false);
         priority = Math.min(MAX_PRIORITY, Math.max(MIN_PRIORITY,
-            getIntAttribute(e, "priority", DEFAULT_PRIORITY)));
+            getIntAttribute(rootElement, "priority", DEFAULT_PRIORITY)));
 
         // if (registry != null) {
         // registry.registerPlugin(this);
@@ -140,9 +140,9 @@
         final ArrayList<PluginPrerequisiteModel> reqList = new 
ArrayList<PluginPrerequisiteModel>();
         RuntimeModel runtime = null;
 
-        initializeRequiresList(reqList, e);
+        initializeRequiresList(reqList, rootElement);
 
-        for (final XMLElement childE : e.getChildren()) {
+        for (final XMLElement childE : rootElement.getChildren()) {
             final String tag = childE.getName();
             if (tag.equals("extension-point")) {
                 final ExtensionPointModel ep = new ExtensionPointModel(this,

Modified: 
trunk/core/src/core/org/jnode/plugin/model/PluginPrerequisiteModel.java
===================================================================
--- trunk/core/src/core/org/jnode/plugin/model/PluginPrerequisiteModel.java     
2012-12-21 15:21:18 UTC (rev 5942)
+++ trunk/core/src/core/org/jnode/plugin/model/PluginPrerequisiteModel.java     
2012-12-27 15:08:56 UTC (rev 5943)
@@ -29,7 +29,7 @@
  */
 final class PluginPrerequisiteModel extends PluginModelObject implements 
PluginPrerequisite {
 
-    private final String plugin;
+    private final String pluginIdentifier;
     private final String version;
 
     /**
@@ -42,7 +42,7 @@
     public PluginPrerequisiteModel(PluginDescriptorModel plugin, XMLElement e)
         throws PluginException {
         super(plugin);
-        this.plugin = getAttribute(e, "plugin", true);
+        this.pluginIdentifier = getAttribute(e, "plugin", true);
         final String version = getAttribute(e, "version", false);
         if (version != null) {
             this.version = version;
@@ -55,19 +55,19 @@
      * Initialize this instance.
      *
      * @param plugin
-     * @param pluginId
+     * @param pluginIdentifier
      * @param pluginVersion
      */
     public PluginPrerequisiteModel(PluginDescriptorModel plugin,
-                                   String pluginId, String pluginVersion) {
+                                   String pluginIdentifier, String 
pluginVersion) {
         super(plugin);
-        if (pluginId == null) {
+        if (pluginIdentifier == null) {
             throw new IllegalArgumentException("pluginId is null");
         }
         if (pluginVersion == null) {
             throw new IllegalArgumentException("pluginVersion is null");
         }
-        this.plugin = pluginId;
+        this.pluginIdentifier = pluginIdentifier;
         this.version = pluginVersion;
     }
 
@@ -75,7 +75,7 @@
      * Gets the identifier of the plugin that is required
      */
     public String getPluginId() {
-        return plugin;
+        return pluginIdentifier;
     }
 
 
@@ -97,9 +97,9 @@
      */
     protected void resolve(PluginRegistryModel registry)
         throws PluginException {
-        if (registry.getPluginDescriptor(plugin) == null) {
+        if (registry.getPluginDescriptor(pluginIdentifier) == null) {
             throw new PluginException(
-                "Unknown plugin " + plugin + " in import of " + 
getDeclaringPluginDescriptor().getId());
+                "Unknown plugin " + pluginIdentifier + " in import of " + 
getDeclaringPluginDescriptor().getId());
         }
     }
 

Added: trunk/core/src/test/org/jnode/plugin/model/PluginDescriptorModelTest.java
===================================================================
--- trunk/core/src/test/org/jnode/plugin/model/PluginDescriptorModelTest.java   
                        (rev 0)
+++ trunk/core/src/test/org/jnode/plugin/model/PluginDescriptorModelTest.java   
2012-12-27 15:08:56 UTC (rev 5943)
@@ -0,0 +1,110 @@
+package org.jnode.plugin.model;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
+
+import org.jnode.nanoxml.XMLElement;
+import org.jnode.plugin.Extension;
+import org.jnode.plugin.PluginException;
+import org.jnode.plugin.PluginPrerequisite;
+import org.jnode.plugin.PluginReference;
+import org.junit.Before;
+import org.junit.Test;
+
+public class PluginDescriptorModelTest {
+
+    private PluginDescriptorModel model;
+
+    @Before
+    public void setUp() throws PluginException {
+        XMLElement element = new XMLElement();
+        element.parseString("<?xml version=\"1.0\" 
encoding=\"UTF-8\"?><!DOCTYPE plugin SYSTEM \"jnode.dtd\"><plugin id=\"model1\" 
name=\"model1 name\" version=\"version\" provider-name=\"provider\" 
license-name=\"lgpl\"><requires><import plugin=\"plug1\"/><import 
plugin=\"plug2\"/></requires><runtime><library name=\"plugin.jar\"><export 
name=\"content.*\"/></library></runtime><extension point=\"extension\"><alias 
name=\"alias\" class=\"class\"/></extension></plugin>");
+        model = new PluginDescriptorModel(element);
+    }
+
+    @Test
+    public void testGetId() {
+        assertEquals("model1", model.getId());
+    }
+
+    @Test
+    public void testGetName() {
+        assertEquals("model1 name", model.getName());
+    }
+
+    @Test
+    public void testGetVersion() {
+        assertEquals("version", model.getVersion());
+    }
+
+    @Test
+    public void testGetProvider() {
+        assertEquals("provider", model.getProviderName());
+        assertNull(model.getProviderUrl());
+    }
+
+    @Test
+    public void testGetLicence() {
+        assertEquals("lgpl", model.getLicenseName());
+        assertNull(model.getLicenseUrl());
+    }
+
+    @Test
+    public void testGetPrerequisites() {
+        PluginPrerequisite[] prerequisites = model.getPrerequisites();
+        assertEquals(2, prerequisites.length);
+
+    }
+
+    @Test
+    public void testDependencyFound() {
+        assertTrue(model.depends("plug1"));
+    }
+
+    @Test
+    public void testDependencyNotFound() {
+        assertFalse(model.depends("plug3"));
+    }
+
+    @Test
+    public void testGetExtension() {
+        Extension[] extensions = model.getExtensions();
+        assertEquals(1, extensions.length);
+    }
+
+    @Test
+    public void testGetRuntime() {
+        org.jnode.plugin.Runtime runtime = model.getRuntime();
+        assertEquals(1, runtime.getLibraries().length);
+    }
+
+    @Test
+    public void testHasCustomPluginClass() {
+        assertFalse(model.hasCustomPluginClass());
+    }
+
+    @Test
+    public void testIsAutoStart() {
+        assertFalse(model.isAutoStart());
+    }
+
+    @Test
+    public void testNoPriorityDefined() {
+        assertEquals(5, model.getPriority());
+    }
+
+    @Test
+    public void testToString() {
+        assertEquals("model1", model.toString());
+        model.getPluginReference();
+    }
+
+    @Test
+    public void testGetPluginReference() {
+        PluginReference reference = model.getPluginReference();
+        assertEquals("model1", reference.getId());
+        assertEquals("version", reference.getVersion());
+    }
+}

This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.


------------------------------------------------------------------------------
Master Visual Studio, SharePoint, SQL, ASP.NET, C# 2012, HTML5, CSS,
MVC, Windows 8 Apps, JavaScript and much more. Keep your skills current
with LearnDevNow - 3,200 step-by-step video tutorials by Microsoft
MVPs and experts. ON SALE this month only -- learn more at:
http://p.sf.net/sfu/learnmore_122712
_______________________________________________
Jnode-svn-commits mailing list
Jnode-svn-commits@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/jnode-svn-commits

Reply via email to