Skip to content

Commit

Permalink
FORGE-2023: Created config-set and config-clear
Browse files Browse the repository at this point in the history
  • Loading branch information
gastaldi committed Sep 19, 2014
1 parent 84289a2 commit 97178c2
Show file tree
Hide file tree
Showing 4 changed files with 190 additions and 110 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
/**
* Copyright 2014 Red Hat, Inc. and/or its affiliates.
*
* Licensed under the Eclipse Public License version 1.0, available at
* http://www.eclipse.org/legal/epl-v10.html
*/

package org.jboss.forge.addon.configuration.ui;

import java.io.PrintStream;
import java.util.Iterator;

import javax.inject.Inject;

import org.jboss.forge.addon.configuration.Configuration;
import org.jboss.forge.addon.configuration.facets.ConfigurationFacet;
import org.jboss.forge.addon.projects.Project;
import org.jboss.forge.addon.projects.ProjectFactory;
import org.jboss.forge.addon.projects.Projects;
import org.jboss.forge.addon.ui.annotation.Command;
import org.jboss.forge.addon.ui.annotation.Option;
import org.jboss.forge.addon.ui.annotation.predicate.NonGUIEnabledPredicate;
import org.jboss.forge.addon.ui.context.UIContext;
import org.jboss.forge.addon.ui.output.UIOutput;
import org.jboss.forge.addon.ui.result.Result;
import org.jboss.forge.addon.ui.result.Results;

/**
* Configuration commands
*
* @author <a href="ggastald@redhat.com">George Gastaldi</a>
*/
public class ConfigCommand
{
@Inject
private Configuration userConfig;

@Inject
private ProjectFactory projectFactory;

@Command(value = "Config: Set", categories = { "Configuration" }, enabled = NonGUIEnabledPredicate.class)
public Result setConfigurationProperty(UIContext context,
@Option(value = "key", required = true) String key,
@Option(value = "value", required = true) String value,
@Option(value = "local") boolean local) throws Exception
{
if (local)
{
Project project = Projects.getSelectedProject(projectFactory, context);
if (project != null)
{
Configuration projectConfig = project.getFacet(ConfigurationFacet.class).getConfiguration();
projectConfig.setProperty(key, value);
}
else
{
return Results.fail("No project found in current context. Can't store in local configuration.");
}
}
else
{
userConfig.setProperty(key, value);
}
return Results.success();
}

@Command(value = "Config: Clear", categories = { "Configuration" }, enabled = NonGUIEnabledPredicate.class)
public Result clearProperty(UIContext context,
@Option(value = "key", required = true) String key,
@Option(value = "local") boolean local) throws Exception
{
if (local)
{
Project project = Projects.getSelectedProject(projectFactory, context);
if (project != null)
{
Configuration projectConfig = project.getFacet(ConfigurationFacet.class).getConfiguration();
projectConfig.clearProperty(key);
}
else
{
return Results.fail("No project found in current context. Can't clear from local configuration.");
}
}
else
{
userConfig.clearProperty(key);
}
return Results.success();
}

@Command(value = "Config: List", categories = { "Configuration" }, enabled = NonGUIEnabledPredicate.class)
public void listConfiguration(UIContext context, UIOutput output)
{
PrintStream out = output.out();

Project project = Projects.getSelectedProject(projectFactory, context);
Configuration projectConfig = null;

if (project != null)
{
projectConfig = project.getFacet(ConfigurationFacet.class).getConfiguration();
}

Iterator<?> userConfigKeys = userConfig.getKeys();

while (userConfigKeys.hasNext())
{
Object key = userConfigKeys.next();

if (key != null)
{
out.print(key.toString());
out.print("=");

out.print("user: [" + userConfig.getProperty(key.toString()) + "]");
if (projectConfig != null)
{
out.print(", project: ");
Object value = projectConfig.getProperty(key.toString());
if (value != null)
{
out.print("[" + value.toString() + "] ");
}
else
out.print("[]");
}
}
out.println();
}

if (projectConfig != null)
{
Iterator<?> projectConfigKeys = projectConfig.getKeys();

while (projectConfigKeys.hasNext())
{
String key = projectConfigKeys.next().toString();
if (!userConfig.containsKey(key))
{
out.print(key.toString());
out.print("=project: [");
out.print(projectConfig.getProperty(key.toString()).toString() + "]");
}
out.println();
}
}
}

}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.jboss.forge.addon.configuration.ui;

import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.not;

import java.util.concurrent.TimeUnit;

Expand All @@ -13,6 +14,7 @@
import org.jboss.forge.addon.projects.Project;
import org.jboss.forge.addon.projects.ProjectFactory;
import org.jboss.forge.addon.shell.test.ShellTest;
import org.jboss.forge.addon.ui.result.Failed;
import org.jboss.forge.arquillian.AddonDependency;
import org.jboss.forge.arquillian.Dependencies;
import org.jboss.forge.arquillian.archive.ForgeArchive;
Expand All @@ -25,7 +27,7 @@
import org.junit.runner.RunWith;

@RunWith(Arquillian.class)
public class ConfigListCommandTest
public class ConfigCommandTest
{
@Deployment
@Dependencies({
Expand All @@ -50,7 +52,7 @@ public static ForgeArchive getDeployment()

return archive;
}

@Inject
private Configuration userConfig;

Expand All @@ -60,7 +62,6 @@ public static ForgeArchive getDeployment()
@Inject
private ProjectFactory projectFactory;


@Before
public void setUp() throws Exception
{
Expand All @@ -76,6 +77,14 @@ public void testConfigList() throws Exception
Assert.assertThat(test.getStdOut(), containsString("key2=user: [userValue2]"));
}

@Test
public void testConfigSetProperty() throws Exception
{
Assert.assertFalse(test.execute("config-set --key key1 --value userValue1", 5, TimeUnit.SECONDS) instanceof Failed);
Assert.assertFalse(test.execute("config-list", 5, TimeUnit.SECONDS) instanceof Failed);
Assert.assertThat(test.getStdOut(), containsString("key1=user: [userValue1]"));
}

@Test
public void testConfigListInProject() throws Exception
{
Expand All @@ -87,7 +96,31 @@ public void testConfigListInProject() throws Exception
Assert.assertThat(test.getStdOut(), containsString("key2=project: [projectValue2]"));
Assert.assertThat(test.getStdOut(), containsString("key3=project: [projectValue3]"));
}


@Test
public void testConfigClear() throws Exception
{
Assert.assertFalse(test.execute("config-set --key key1 --value userValue1", 5, TimeUnit.SECONDS) instanceof Failed);
Assert.assertFalse(test.execute("config-list", 5, TimeUnit.SECONDS) instanceof Failed);
Assert.assertThat(test.getStdOut(), containsString("key1=user: [userValue1]"));
test.clearScreen();
Assert.assertFalse(test.execute("config-clear --key key1", 5, TimeUnit.SECONDS) instanceof Failed);
Assert.assertFalse(test.execute("config-list", 5, TimeUnit.SECONDS) instanceof Failed);
Assert.assertThat(test.getStdOut(), not(containsString("key1=user: [userValue1]")));
}

@Test
public void testConfigSetPropertyListInProject() throws Exception
{
Project project = projectFactory.createTempProject();
test.getShell().setCurrentResource(project.getRoot());
test.execute("config-set --key key2 --value projectValue2 --local", 5, TimeUnit.MINUTES);
test.execute("config-set --key key3 --value projectValue3 --local", 5, TimeUnit.SECONDS);
Assert.assertFalse(test.execute("config-list", 5, TimeUnit.SECONDS) instanceof Failed);
Assert.assertThat(test.getStdOut(), containsString("key2=project: [projectValue2]"));
Assert.assertThat(test.getStdOut(), containsString("key3=project: [projectValue3]"));
}

@Test
public void testMergedConfigList() throws Exception
{
Expand All @@ -101,7 +134,7 @@ public void testMergedConfigList() throws Exception
Assert.assertThat(test.getStdOut(), containsString("key2=user: [userValue2], project: [projectValue2]"));
Assert.assertThat(test.getStdOut(), containsString("key3=project: [projectValue3]"));
}

private void addPropsToUserConfig()
{
userConfig.setProperty("key1", "userValue1");
Expand All @@ -113,7 +146,7 @@ private void addPropsToProjectConfig(Configuration projectConfig)
projectConfig.addProperty("key2", "projectValue2");
projectConfig.addProperty("key3", "projectValue3");
}

@After
public void tearDown() throws Exception
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ private ProcessedCommand generateCommand(final CommandController command, final
final Object defaultValue = InputComponents.getValueFor(input);
final boolean isMultiple = input instanceof ManyValued;
final boolean hasValue = (!InputType.CHECKBOX.equals(InputComponents.getInputType(input)) && !Boolean.class
.isAssignableFrom(input.getValueType()));
.isAssignableFrom(input.getValueType()) && !boolean.class.isAssignableFrom(input.getValueType()));
try
{
OptionBuilder optionBuilder = new OptionBuilder();
Expand Down

0 comments on commit 97178c2

Please sign in to comment.