Mercurial > hg > thermostat-tools-eclipse
changeset 106:4356c844cff9
Implement `option`, `options` and `group`
line wrap: on
line diff
--- a/com.redhat.thermostat.tools.eclipse.plugin.tests/src/com/redhat/thermostat/tools/eclipse/plugin/tests/model/CommandTest.java Wed Feb 19 16:39:36 2014 -0500 +++ b/com.redhat.thermostat.tools.eclipse.plugin.tests/src/com/redhat/thermostat/tools/eclipse/plugin/tests/model/CommandTest.java Wed Feb 19 18:32:13 2014 -0500 @@ -15,6 +15,7 @@ import com.redhat.thermostat.tools.eclipse.plugin.model.Command; import com.redhat.thermostat.tools.eclipse.plugin.model.Environments; import com.redhat.thermostat.tools.eclipse.plugin.model.Option; +import com.redhat.thermostat.tools.eclipse.plugin.model.Options; public class CommandTest { @@ -32,7 +33,7 @@ public void testConstruction() { Command command = new Command("name", "description", "usage", Collections.<String> emptyList(), - Collections.<Option> emptyList(), + new Options(), new Environments(), Collections.<Bundle> emptyList());
--- a/com.redhat.thermostat.tools.eclipse.plugin.tests/src/com/redhat/thermostat/tools/eclipse/plugin/tests/model/PluginModelReaderWriterTest.java Wed Feb 19 16:39:36 2014 -0500 +++ b/com.redhat.thermostat.tools.eclipse.plugin.tests/src/com/redhat/thermostat/tools/eclipse/plugin/tests/model/PluginModelReaderWriterTest.java Wed Feb 19 18:32:13 2014 -0500 @@ -5,20 +5,26 @@ import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; +import java.io.ByteArrayOutputStream; +import java.nio.charset.StandardCharsets; +import java.util.ArrayList; import java.util.List; import org.junit.Test; import com.redhat.thermostat.tools.eclipse.plugin.model.Bundle; import com.redhat.thermostat.tools.eclipse.plugin.model.Command; +import com.redhat.thermostat.tools.eclipse.plugin.model.Environments; import com.redhat.thermostat.tools.eclipse.plugin.model.Extension; +import com.redhat.thermostat.tools.eclipse.plugin.model.Option; +import com.redhat.thermostat.tools.eclipse.plugin.model.Options; import com.redhat.thermostat.tools.eclipse.plugin.model.Plugin; import com.redhat.thermostat.tools.eclipse.plugin.model.PluginModelReaderWriter; public class PluginModelReaderWriterTest { @Test - public void testParsingCommand() { + public void testReadingCommandFromXml() { String document = "<?xml version='1.0'?>\n" + "<plugin xmlns=\"http://icedtea.classpath.org/thermostat/plugins/v1.0\"\n" + " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n" @@ -33,6 +39,7 @@ + " </arguments>\n" + " <options>\n" + " <option>\n" + + " <short>a</short>\n" + " </option>\n" + " </options>\n" + " <environments>\n" @@ -56,6 +63,13 @@ assertEquals("foo bar", foo.getUsage()); assertEquals("foos the bar if possible", foo.getDescription()); + Options options = foo.getOptions(); + assertNotNull(options); + + List<Option> simpleOptions = options.getOptions(); + assertNotNull(simpleOptions); + assertEquals(1, simpleOptions.size()); + assertTrue(foo.getEnvironments().isCli()); assertFalse(foo.getEnvironments().isShell()); @@ -68,7 +82,7 @@ } @Test - public void testBinding() { + public void testReadingExtensionFromXml() { String document = "<?xml version='1.0'?>\n" + "<plugin xmlns=\"http://icedtea.classpath.org/thermostat/plugins/v1.0\"\n" + " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n" @@ -89,4 +103,85 @@ List<Extension> extensions = plugin.getExtensions(); assertEquals(1, extensions.size()); } + + @Test + public void testWritingCommandToXml() { + String emptyDocument = "<?xml version='1.0'?>\n" + + "<plugin xmlns=\"http://icedtea.classpath.org/thermostat/plugins/v1.0\"\n" + + " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n" + + " xsi:schemaLocation=\"http://icedtea.classpath.org/thermostat/plugins/v1.0 thermostat-plugin.xsd\">\n" + + "</plugin>"; + + String expectedDocument = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n" + + "<plugin xmlns=\"http://icedtea.classpath.org/thermostat/plugins/v1.0\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://icedtea.classpath.org/thermostat/plugins/v1.0 http://icedtea.classpath.org/thermostat/docs/1.0/thermostat-plugin.xsd\">\n" + + " <commands>\n" + + " <command>\n" + + " <name>foo</name>\n" + + " <description>foos the bar if possible</description>\n" + + " <usage>foo bar</usage>\n" + + " <arguments>\n" + + " <argument>bar</argument>\n" + + " </arguments>\n" + + " <options>\n" + + " <option>\n" + + " <short>a</short>\n" + + " <required>true</required>\n" + + " </option>\n" + + " </options>\n" + + " <environments>\n" + + " <environment>cli</environment>\n" + + " </environments>\n" + + " <bundles>\n" + + " <bundle>\n" + + " <symbolic-name>foo</symbolic-name>\n" + + " <version>1</version>\n" + + " </bundle>\n" + + " </bundles>\n" + + " </command>\n" + + " </commands>\n" + + "</plugin>\n"; + + PluginModelReaderWriter pluginModel = new PluginModelReaderWriter(null, "<stdin>"); + Plugin model = pluginModel.loadModel(emptyDocument); + + // just to verify that the initial model is empty before adding the actual command + assertEquals(0, model.getCommands().size()); + + Command command = new Command(); + command.setName("foo"); + command.setUsage("foo bar"); + command.setDescription("foos the bar if possible"); + + String argument = "bar"; + command.addArgument(argument); + + Options options = new Options(); + command.setOptions(options); + + List<Option> simpleOptions = new ArrayList<>(); + options.setOptions(simpleOptions); + + Option optionA = new Option(); + optionA.setShortName("a"); + optionA.setRequired(true); + simpleOptions.add(optionA); + + Environments environments = new Environments(); + environments.setCli(true); + environments.setShell(false); + command.setEnvironments(environments); + + Bundle bundle = new Bundle(); + bundle.setSymbolicName("foo"); + bundle.setVersion("1"); + command.addBundle(bundle); + + model.addCommand(command); + + ByteArrayOutputStream bytes = new ByteArrayOutputStream(); + pluginModel.saveModel(model, bytes); + String generatedDocument = new String(bytes.toByteArray(), StandardCharsets.UTF_8); + + assertEquals(expectedDocument, generatedDocument); + } }
--- a/com.redhat.thermostat.tools.eclipse.plugin/src/com/redhat/thermostat/tools/eclipse/plugin/model/Command.java Wed Feb 19 16:39:36 2014 -0500 +++ b/com.redhat.thermostat.tools.eclipse.plugin/src/com/redhat/thermostat/tools/eclipse/plugin/model/Command.java Wed Feb 19 18:32:13 2014 -0500 @@ -19,7 +19,7 @@ private String description; private String usage; private List<String> arguments = new ArrayList<>(); - private List<Option> options = new ArrayList<>(); + private Options options; private Environments environments = new Environments(); private List<Bundle> bundles = new ArrayList<>(); @@ -28,7 +28,7 @@ } public Command(String name, String description, String usage, - List<String> arguments, List<Option> options, + List<String> arguments, Options options, Environments environments, List<Bundle> bundles) { this.name = name; this.description = description; @@ -87,13 +87,12 @@ firePropertyChange("arguments", null, this.arguments); //$NON-NLS-1$ } - @XmlElementWrapper(name="options") - @XmlElement(name="option") - public List<Option> getOptions() { + @XmlElement(name="options") + public Options getOptions() { return options; } - public void setOptions(List<Option> options) { + public void setOptions(Options options) { firePropertyChange("options", this.options, this.options = options); //$NON-NLS-1$ }
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/com.redhat.thermostat.tools.eclipse.plugin/src/com/redhat/thermostat/tools/eclipse/plugin/model/Group.java Wed Feb 19 18:32:13 2014 -0500 @@ -0,0 +1,26 @@ +package com.redhat.thermostat.tools.eclipse.plugin.model; + +import java.util.ArrayList; +import java.util.List; + +public class Group { + + private boolean required; + private List<Option> options = new ArrayList<>(); + + public boolean getRequired() { + return required; + } + + public void setRequired(boolean required) { + this.required = required; + } + + public List<Option> getOptions() { + return this.options; + } + + public void setOptions(List<Option> options) { + this.options = options; + } +}
--- a/com.redhat.thermostat.tools.eclipse.plugin/src/com/redhat/thermostat/tools/eclipse/plugin/model/Option.java Wed Feb 19 16:39:36 2014 -0500 +++ b/com.redhat.thermostat.tools.eclipse.plugin/src/com/redhat/thermostat/tools/eclipse/plugin/model/Option.java Wed Feb 19 18:32:13 2014 -0500 @@ -1,25 +1,68 @@ package com.redhat.thermostat.tools.eclipse.plugin.model; +import javax.xml.bind.annotation.XmlAccessType; +import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlElement; +import javax.xml.bind.annotation.XmlType; +@XmlAccessorType(XmlAccessType.NONE) +@XmlType(propOrder = { + "longName", + "shortName", + "argument", + "required", + "description"}) public class Option { - private String name; + private String longName; + private String shortName; + private String argument; + private boolean required; + private String description; - public Option() { - this.name = null; + @XmlElement(name="long") + public String getLongName() { + return longName; + } + + public void setLongName(String name) { + this.longName = name; } - public Option(String name) { - this.name = name; + @XmlElement(name="short") + public String getShortName() { + return shortName; + } + + public void setShortName(String shortName) { + this.shortName = shortName; + } + + @XmlElement(name="argument") + public String getArgument() { + return argument; + } + + public void setArgument(String argument) { + this.argument = argument; } - @XmlElement(name="long") - public String getName() { - return name; + @XmlElement(name="required") + public boolean getRequired() { + return required; + } + + public void setRequired(boolean required) { + this.required = required; } - public void setName(String name) { - this.name = name; + @XmlElement(name="description") + public String getDescription() { + return description; } -} \ No newline at end of file + + public void setDescription(String description) { + this.description = description; + } + +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/com.redhat.thermostat.tools.eclipse.plugin/src/com/redhat/thermostat/tools/eclipse/plugin/model/Options.java Wed Feb 19 18:32:13 2014 -0500 @@ -0,0 +1,29 @@ +package com.redhat.thermostat.tools.eclipse.plugin.model; + +import java.util.ArrayList; +import java.util.List; + +import javax.xml.bind.annotation.XmlElement; + +public class Options { + + private Group group; + private List<Option> options = new ArrayList<>(); + + public Group getGroup() { + return group; + } + + public void setGroup(Group group) { + this.group = group; + } + + @XmlElement(name="option") + public List<Option> getOptions() { + return options; + } + + public void setOptions(List<Option> options) { + this.options = options; + } +}