Skip to content

Commit

Permalink
[FORGE-2241] Fixed: Configuration.clearProperty() does not work for m…
Browse files Browse the repository at this point in the history
…ultilevel subsets
  • Loading branch information
danielsoro committed Feb 26, 2015
1 parent 24b69d3 commit e4c5045
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 39 deletions.
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
/*
* 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
*/
* 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;

import javax.enterprise.inject.Vetoed;

import org.apache.commons.configuration.HierarchicalConfiguration;

import javax.enterprise.inject.Vetoed;
import java.util.Collections;
import java.util.Iterator;

/**
* @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
* @author <a href="mailto:danielsoro@gmail.com">Daniel Cunha (soro)</a>
*/
@Vetoed
public class ConfigurationAdapterSubset extends ConfigurationAdapter
Expand All @@ -34,17 +37,50 @@ public ConfigurationAdapterSubset(HierarchicalConfiguration delegate, String pre
}

@Override
public void setProperty(final String key, final Object value)
public Iterator<?> getKeys()
{
synchronized (parent)
{
try
{
return parent.configurationAt(prefix).getKeys();
}
catch (IllegalArgumentException e)
{
return Collections.emptyIterator();
}
}
}

@Override
public void clearProperty(String key)
{
synchronized (parent)
{
if (!parent.containsKey(prefix))
try
{
parent.configurationAt(prefix).clearProperty(key);
}
catch (IllegalArgumentException e)
{
parent.setProperty(prefix, "");
setDelegate(parent.configurationAt(prefix));
//do nothing;
}
getDelegate().setProperty(key, value);
}
}

}
@Override
public void setProperty(final String key, final Object value)
{
synchronized (parent)
{
try
{
parent.configurationAt(prefix).setProperty(key, value);
}
catch (IllegalArgumentException e)
{
parent.setProperty(prefix + "." + key, value);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
/*
* Copyright 2015 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 static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.not;

import java.util.concurrent.TimeUnit;

import javax.inject.Inject;

import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.forge.addon.configuration.Configuration;
import org.jboss.forge.addon.configuration.Subset;
import org.jboss.forge.addon.configuration.facets.ConfigurationFacet;
import org.jboss.forge.addon.projects.Project;
import org.jboss.forge.addon.projects.ProjectFactory;
Expand All @@ -26,6 +26,15 @@
import org.junit.Test;
import org.junit.runner.RunWith;

import javax.inject.Inject;
import java.util.concurrent.TimeUnit;

import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.not;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;

@RunWith(Arquillian.class)
public class ConfigCommandTest
{
Expand Down Expand Up @@ -62,6 +71,10 @@ public static ForgeArchive getDeployment()
@Inject
private ProjectFactory projectFactory;

@Inject
@Subset("subset.subset")
private Configuration subSubsetConfiguration;

@Before
public void setUp() throws Exception
{
Expand All @@ -73,16 +86,16 @@ public void testConfigList() throws Exception
{
addPropsToUserConfig();
test.execute("config-list", 5, TimeUnit.SECONDS);
Assert.assertThat(test.getStdOut(), containsString("key1=user: [userValue1]"));
Assert.assertThat(test.getStdOut(), containsString("key2=user: [userValue2]"));
assertThat(test.getStdOut(), containsString("key1=user: [userValue1]"));
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]"));
assertFalse(test.execute("config-set --key key1 --value userValue1", 5, TimeUnit.SECONDS) instanceof Failed);
assertFalse(test.execute("config-list", 5, TimeUnit.SECONDS) instanceof Failed);
assertThat(test.getStdOut(), containsString("key1=user: [userValue1]"));
}

@Test
Expand All @@ -93,20 +106,20 @@ public void testConfigListInProject() throws Exception
addPropsToProjectConfig(projectConfig);
test.getShell().setCurrentResource(project.getRoot());
test.execute("config-list", 5, TimeUnit.SECONDS);
Assert.assertThat(test.getStdOut(), containsString("key2=project: [projectValue2]"));
Assert.assertThat(test.getStdOut(), containsString("key3=project: [projectValue3]"));
assertThat(test.getStdOut(), containsString("key2=project: [projectValue2]"));
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]"));
assertFalse(test.execute("config-set --key key1 --value userValue1", 5, TimeUnit.SECONDS) instanceof Failed);
assertFalse(test.execute("config-list", 5, TimeUnit.SECONDS) instanceof Failed);
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]")));
assertFalse(test.execute("config-clear --key key1", 5, TimeUnit.SECONDS) instanceof Failed);
assertFalse(test.execute("config-list", 5, TimeUnit.SECONDS) instanceof Failed);
assertThat(test.getStdOut(), not(containsString("key1=user: [userValue1]")));
}

@Test
Expand All @@ -116,9 +129,9 @@ public void testConfigSetPropertyListInProject() throws Exception
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]"));
assertFalse(test.execute("config-list", 5, TimeUnit.SECONDS) instanceof Failed);
assertThat(test.getStdOut(), containsString("key2=project: [projectValue2]"));
assertThat(test.getStdOut(), containsString("key3=project: [projectValue3]"));
}

@Test
Expand All @@ -130,9 +143,20 @@ public void testMergedConfigList() throws Exception
addPropsToProjectConfig(projectConfig);
test.getShell().setCurrentResource(project.getRoot());
test.execute("config-list", 5, TimeUnit.SECONDS);
Assert.assertThat(test.getStdOut(), containsString("key1=user: [userValue1]"));
Assert.assertThat(test.getStdOut(), containsString("key2=user: [userValue2], project: [projectValue2]"));
Assert.assertThat(test.getStdOut(), containsString("key3=project: [projectValue3]"));
assertThat(test.getStdOut(), containsString("key1=user: [userValue1]"));
assertThat(test.getStdOut(), containsString("key2=user: [userValue2], project: [projectValue2]"));
assertThat(test.getStdOut(), containsString("key3=project: [projectValue3]"));
}

@Test
public void testSubSubsetConfigurationClearProperty() throws Exception
{
userConfig.clear();
userConfig.setProperty("subset.subset.A", "Value");
assertTrue(subSubsetConfiguration.getKeys().hasNext());
subSubsetConfiguration.clearProperty("A");
assertFalse(subSubsetConfiguration.getKeys().hasNext());
assertFalse(userConfig.getKeys().hasNext());
}

private void addPropsToUserConfig()
Expand Down

0 comments on commit e4c5045

Please sign in to comment.