changeset 63:fb4fcb5bb4ac

Make Command a JavaBean
author Omair Majid <omajid@redhat.com>
date Thu, 02 Jan 2014 11:35:03 -0500
parents 52d63c65f7bb
children c9c6c6c327ea
files src/com/redhat/thermostat/plugin/eclipse/editor/CommandEditPage.java src/com/redhat/thermostat/plugin/eclipse/model/Command.java src/com/redhat/thermostat/plugin/eclipse/model/PluginModel.java test/com/redhat/thermostat/plugin/eclipse/model/CommandTest.java test/com/redhat/thermostat/plugin/eclipse/model/PluginModelTest.java
diffstat 5 files changed, 246 insertions(+), 33 deletions(-) [+]
line wrap: on
line diff
--- a/src/com/redhat/thermostat/plugin/eclipse/editor/CommandEditPage.java	Thu Jan 02 10:52:35 2014 -0500
+++ b/src/com/redhat/thermostat/plugin/eclipse/editor/CommandEditPage.java	Thu Jan 02 11:35:03 2014 -0500
@@ -81,18 +81,18 @@
 
         // TODO change this is-content-different into is-content-dirty
         boolean dirty = false;
-        if (cli.getSelection() ^ commandModel.environments.contains("cli")) {
+        if (cli.getSelection() ^ commandModel.getEnvironments().contains("cli")) {
             dirty = true;
         }
-        if (shell.getSelection() ^ commandModel.environments.contains("shell")) {
+        if (shell.getSelection() ^ commandModel.getEnvironments().contains("shell")) {
             dirty = true;
         }
-        if (!commandModel.bundles.equals(BundleInformationHelpers.getBundles((List<String>) bundleViewer.getInput()))) {
+        if (!commandModel.getBundles().equals(BundleInformationHelpers.getBundles((List<String>) bundleViewer.getInput()))) {
             dirty = true;
         }
-        dirty = dirty || !(commandName.getText().equals(commandModel.name)
-                && commandDescription.getText().equals(commandModel.description)
-                && commandUsage.getText().equals(commandModel.usage));
+        dirty = dirty || !(commandName.getText().equals(commandModel.getName())
+                && commandDescription.getText().equals(commandModel.getDescription())
+                && commandUsage.getText().equals(commandModel.getUsage()));
         System.out.println("[" + command + "] isDirty : " + dirty);
         return dirty;
     }
@@ -255,12 +255,12 @@
             return;
         }
 
-        commandName.setText(commandModel.name);
-        commandDescription.setText(commandModel.description);
-        commandUsage.setText(commandModel.usage);
+        commandName.setText(commandModel.getName());
+        commandDescription.setText(commandModel.getDescription());
+        commandUsage.setText(commandModel.getUsage());
         shell.setSelection(false);
         cli.setSelection(false);
-        for (String env : commandModel.environments) {
+        for (String env : commandModel.getEnvironments()) {
             if (env.equals("shell")) {
                 shell.setSelection(true);
             }
@@ -269,7 +269,8 @@
                 cli.setSelection(true);
             }
         }
-        bundleViewer.setInput(BundleInformationHelpers.getStrings(commandModel.bundles));
+
+        bundleViewer.setInput(BundleInformationHelpers.getStrings(commandModel.getBundles()));
     }
 
     private void updateModel() {
--- a/src/com/redhat/thermostat/plugin/eclipse/model/Command.java	Thu Jan 02 10:52:35 2014 -0500
+++ b/src/com/redhat/thermostat/plugin/eclipse/model/Command.java	Thu Jan 02 11:35:03 2014 -0500
@@ -2,15 +2,19 @@
 
 import java.util.List;
 
-public class Command {
+public class Command extends ModelObject {
 
-    public final String name;
-    public final String description;
-    public final String usage;
-    public final List<String> arguments;
-    public final List<Option> options;
-    public final List<String> environments;
-    public final List<BundleInformation> bundles;
+    private String name;
+    private String description;
+    private String usage;
+    private List<String> arguments;
+    private List<Option> options;
+    private List<String> environments;
+    private List<BundleInformation> bundles;
+
+    public Command() {
+        // no-arg java bean constructor
+    }
 
     public Command(String name, String description, String usage,
             List<String> arguments, List<Option> options,
@@ -24,4 +28,90 @@
         this.environments = environments;
         this.bundles = bundles;
     }
-}
\ No newline at end of file
+
+    public String getName() {
+        return name;
+    }
+
+    public void setName(String name) {
+        firePropertyChange("name", this.name, this.name = name);
+    }
+
+    public String getDescription() {
+        return description;
+    }
+
+    public void setDescription(String description) {
+        firePropertyChange("description", this.description, this.description = description);
+    }
+
+    public String getUsage() {
+        return usage;
+    }
+
+    public void setUsage(String usage) {
+        firePropertyChange("usage", this.usage, this.usage = usage);
+    }
+
+    public List<String> getArguments() {
+        return arguments;
+    }
+
+    public void setArguments(List<String> arguments) {
+        firePropertyChange("arguments", this.arguments, this.arguments = arguments);
+    }
+
+    public void addArgument(String argument) {
+        this.arguments.add(argument);
+        firePropertyChange("arguments", null, this.arguments);
+    }
+
+    public void removeArgument(String argument) {
+        this.arguments.remove(argument);
+        firePropertyChange("arguments", null, this.arguments);
+    }
+
+    public List<Option> getOptions() {
+        return options;
+    }
+
+    public void setOptions(List<Option> options) {
+        firePropertyChange("options", this.options, this.options = options);
+    }
+
+    public List<String> getEnvironments() {
+        return environments;
+    }
+
+    public void setEnvironments(List<String> environments) {
+        firePropertyChange("environments", this.environments, this.environments = environments);
+    }
+
+    public void addEnvironment(String environment) {
+        this.environments.add(environment);
+        firePropertyChange("environments", null, this.environments);
+    }
+
+    public void removeEnvironment(String environment) {
+        this.environments.remove(environment);
+        firePropertyChange("environments", null, this.environments);
+    }
+
+    public List<BundleInformation> getBundles() {
+        return bundles;
+    }
+
+    public void setBundles(List<BundleInformation> bundles) {
+        this.bundles = bundles;
+    }
+
+    public void addBundle(BundleInformation bundle) {
+        this.bundles.add(bundle);
+        firePropertyChange("bundles", null, this.bundles);
+    }
+
+    public void removeBundle(BundleInformation bundle) {
+        this.bundles.remove(bundle);
+        firePropertyChange("bundles", null, this.bundles);
+    }
+}
--- a/src/com/redhat/thermostat/plugin/eclipse/model/PluginModel.java	Thu Jan 02 10:52:35 2014 -0500
+++ b/src/com/redhat/thermostat/plugin/eclipse/model/PluginModel.java	Thu Jan 02 11:35:03 2014 -0500
@@ -218,7 +218,7 @@
     }
 
     public void updateCommand(Command command) {
-        checkName(command.name);
+        checkName(command.getName());
 
         XPath xpath = xPathfactory.newXPath();
         String expressionString;
@@ -227,7 +227,7 @@
 
         try {
             // find a command with a child 'name' having the matching text
-            expressionString = "/plugin/commands/command[name/text() = '" + command.name + "']";
+            expressionString = "/plugin/commands/command[name/text() = '" + command.getName() + "']";
             expr = xpath.compile(expressionString);
             node = (Node) expr.evaluate(doc, XPathConstants.NODE);
             Node commandNode = node;
@@ -269,19 +269,19 @@
                 commandNode.appendChild(descriptionNode);
             }
 
-            descriptionNode.setTextContent(command.description);
+            descriptionNode.setTextContent(command.getDescription());
 
             if (usageNode == null) {
                 usageNode = doc.createElement("usage");
                 commandNode.appendChild(usageNode);
             }
 
-            usageNode.setTextContent(command.usage);
+            usageNode.setTextContent(command.getUsage());
 
-            updateArguments(command.arguments, commandNode, argumentsNode);
-            updateOptions(command.options, commandNode, optionsNode);
-            updateEnvironments(command.environments, commandNode, environmentsNode);
-            updateBundles(command.bundles, commandNode, bundlesNode);
+            updateArguments(command.getArguments(), commandNode, argumentsNode);
+            updateOptions(command.getOptions(), commandNode, optionsNode);
+            updateEnvironments(command.getEnvironments(), commandNode, environmentsNode);
+            updateBundles(command.getBundles(), commandNode, bundlesNode);
 
             fireModelChanged();
         } catch (XPathExpressionException e) {
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/com/redhat/thermostat/plugin/eclipse/model/CommandTest.java	Thu Jan 02 11:35:03 2014 -0500
@@ -0,0 +1,122 @@
+package com.redhat.thermostat.plugin.eclipse.model;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+
+import java.beans.PropertyChangeListener;
+import java.util.Arrays;
+import java.util.Collections;
+
+import org.junit.Before;
+import org.junit.Test;
+
+public class CommandTest {
+
+    private CustomListener listener;
+    private Command command;
+
+    @Before
+    public void setUp() {
+        command = new Command();
+
+        listener = new CustomListener();
+    }
+
+    @Test
+    public void testConstruction() {
+        Command command = new Command("name", "description", "usage",
+                Collections.<String> emptyList(),
+                Collections.<Option> emptyList(),
+                Collections.<String> emptyList(),
+                Collections.<BundleInformation> emptyList());
+
+        assertNotNull(command);
+
+        assertEquals("name", command.getName());
+        assertEquals("description", command.getDescription());
+        assertEquals("usage", command.getUsage());
+    }
+
+    @Test
+    public void testNameChange() {
+        command.setName("old");
+        command.addPropertyChangeListener("name", listener);
+
+        command.setName("new");
+
+        assertPropertyChanged(listener, "name", "old", "new");
+    }
+
+    @Test
+    public void testDescriptionChange() {
+        command.setDescription("old");
+        command.addPropertyChangeListener("description", listener);
+
+        command.setDescription("new");
+
+        assertPropertyChanged(listener, "description", "old", "new");
+    }
+
+    @Test
+    public void testUsageChange() {
+        command.setUsage("old");
+        command.addPropertyChangeListener("usage", listener);
+
+        command.setUsage("new");
+
+        assertPropertyChanged(listener, "usage", "old", "new");
+    }
+
+    @Test
+    public void testArgumentsChange() {
+        command.setArguments(Arrays.asList(new String[] { "old" }));
+        command.addPropertyChangeListener("arguments", listener);
+
+        command.setArguments(Arrays.asList(new String[] { "new" }));
+
+        assertPropertyChanged(listener, "arguments",
+                Arrays.asList(new String[] { "old" }), Arrays.asList(new String[] { "new" }));
+    }
+
+    private static void assertPropertyChanged(CustomListener listener,
+            String propertyName, Object oldValue, Object newValue) {
+
+        assertTrue(listener.getInvoked());
+        assertEquals(propertyName, listener.getPropertyName());
+        assertEquals(oldValue, listener.getOldValue());
+        assertEquals(newValue, listener.getNewValue());
+    }
+
+    static class CustomListener implements PropertyChangeListener {
+
+        private boolean invoked = false;
+        private String propertyName;
+        private Object oldValue;
+        private Object newValue;
+
+        public void propertyChange(java.beans.PropertyChangeEvent evt) {
+            this.invoked = true;
+            this.propertyName = evt.getPropertyName();
+            this.oldValue = evt.getOldValue();
+            this.newValue = evt.getNewValue();
+        };
+
+        public boolean getInvoked() {
+            return invoked;
+        }
+
+        public String getPropertyName() {
+            return propertyName;
+        }
+
+        public Object getOldValue() {
+            return oldValue;
+        }
+
+        public Object getNewValue() {
+            return newValue;
+        }
+
+    }
+}
--- a/test/com/redhat/thermostat/plugin/eclipse/model/PluginModelTest.java	Thu Jan 02 10:52:35 2014 -0500
+++ b/test/com/redhat/thermostat/plugin/eclipse/model/PluginModelTest.java	Thu Jan 02 11:35:03 2014 -0500
@@ -46,13 +46,13 @@
 
         assertNotNull(foo);
 
-        assertEquals("foo", foo.name);
-        assertEquals("foo bar", foo.usage);
-        assertEquals("foos the bar if possible", foo.description);
+        assertEquals("foo", foo.getName());
+        assertEquals("foo bar", foo.getUsage());
+        assertEquals("foos the bar if possible", foo.getDescription());
 
-        assertArrayEquals(new String[] {"cli"}, foo.environments.toArray(new String[0]));
+        assertArrayEquals(new String[] {"cli"}, foo.getEnvironments().toArray(new String[0]));
 
-        List<BundleInformation> bundles = foo.bundles;
+        List<BundleInformation> bundles = foo.getBundles();
         assertEquals(1, bundles.size());
 
         BundleInformation bundle = bundles.get(0);