Skip to content

Commit

Permalink
Add an endpoint for getting and clearing used properties
Browse files Browse the repository at this point in the history
  • Loading branch information
akang31 committed Jan 17, 2024
1 parent 6e4de99 commit 90be839
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,16 @@ public class ConcurrentCompositeConfiguration extends ConcurrentMapConfiguration
private final Set<String> usedProperties = ConcurrentHashMap.newKeySet();

public Set<String> getUsedProperties() {
return usedProperties;
return Collections.unmodifiableSet(new HashSet<>(usedProperties));
}

public Set<String> getAndClearUsedProperties() {
synchronized (usedProperties) {
Set<String> ret = getUsedProperties();
usedProperties.clear();
return ret;
}
}

private List<AbstractConfiguration> configList = new CopyOnWriteArrayList<AbstractConfiguration>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import static org.junit.Assert.*;

import java.util.List;
import java.util.Set;

import org.apache.commons.configuration.AbstractConfiguration;
import org.apache.commons.configuration.BaseConfiguration;
Expand Down Expand Up @@ -80,11 +81,24 @@ public void testInstrumentation() {
config.addProperty("prop1", "val1");
config.addProperty("prop2", "val2");
assertEquals(config.getProperty("prop1"), "val1");
assertEquals(config.getUsedProperties().size(), 1);
assertEquals(config.getUsedProperties().iterator().next(), "prop1");

// Confirm that the usage is captured
Set<String> usedProperties = config.getUsedProperties();
assertEquals(usedProperties.size(), 1);
assertEquals(usedProperties.iterator().next(), "prop1");

// Confirm that an uninstrumented call is ignored
assertEquals(config.getPropertyUninstrumented("prop2"), "val2");
assertEquals(config.getUsedProperties().size(), 1);
assertEquals(config.getUsedProperties().iterator().next(), "prop1");
usedProperties = config.getAndClearUsedProperties();
assertEquals(usedProperties.size(), 1);
assertEquals(usedProperties.iterator().next(), "prop1");

// Confirm that both usedProperties endpoints respect when the properties have been cleared
usedProperties = config.getUsedProperties();
assertTrue(usedProperties.isEmpty());

usedProperties = config.getAndClearUsedProperties();
assertTrue(usedProperties.isEmpty());

System.clearProperty(ConcurrentCompositeConfiguration.ENABLE_INSTRUMENTATION);
}
Expand Down

0 comments on commit 90be839

Please sign in to comment.