Skip to content

Commit

Permalink
Fix an issue with mutually dependent arguments
Browse files Browse the repository at this point in the history
Fixed a bug in the argument parser's support for mutually dependent
arguments.  In a mutually dependent argument set, if any one of the
arguments is provided, then all of the other arguments in the set
must also be provided, but there was a problem with support for sets
containing three or more elements.  In such cases, the argument
parser would only enforce that at least two arguments from the set
were present.
  • Loading branch information
dirmgr committed Nov 28, 2022
1 parent 017fff2 commit 76bbe43
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 8 deletions.
10 changes: 10 additions & 0 deletions docs/release-notes.html
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,16 @@ <h3>Version 6.0.7</h3>
<br><br>
</li>

<li>
Fixed a bug in the argument parser's support for mutually dependent arguments.
In a mutually dependent argument set, if any one of the arguments is provided,
then all of the other arguments in the set must also be provided, but there was
a problem with support for sets containing three or more elements. In such
cases, the argument parser would only enforce that at least two arguments from
the set were present.
<br><br>
</li>

<li>
Added JSONObject methods for retrieving fields by name using case-insensitive
matching. By default, JSON field names are treated in a case-sensitive manner,
Expand Down
13 changes: 7 additions & 6 deletions src/com/unboundid/util/args/ArgumentParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.LinkedHashMap;
Expand Down Expand Up @@ -1488,12 +1487,14 @@ public void addMutuallyDependentArgumentSet(
" that is not registered with the argument parser.");
}

final Set<Argument> allArgsSet = new HashSet<>(arguments);
for (final Argument a : allArgsSet)
final List<Argument> allArgsList = new ArrayList<>(arguments);
for (int i=0; i < allArgsList.size(); i++)
{
final Set<Argument> dependentArgs = new HashSet<>(allArgsSet);
dependentArgs.remove(a);
addDependentArgumentSet(a, dependentArgs);
for (int j=(i+1); j < allArgsList.size(); j++)
{
addDependentArgumentSet(allArgsList.get(i), allArgsList.get(j));
addDependentArgumentSet(allArgsList.get(j), allArgsList.get(i));
}
}
}

Expand Down
104 changes: 102 additions & 2 deletions tests/unit/src/com/unboundid/util/args/ArgumentParserTestCase.java
Original file line number Diff line number Diff line change
Expand Up @@ -604,13 +604,15 @@ public void testAddMutuallyDependentArgumentSet()

assertNotNull(p.getDependentArgumentSets());
assertFalse(p.getDependentArgumentSets().isEmpty());
assertEquals(p.getDependentArgumentSets().size(), 2);
assertEquals(p.getDependentArgumentSets().size(), 2,
String.valueOf(p.getDependentArgumentSets()));

p.addMutuallyDependentArgumentSet(c, d, e);

assertNotNull(p.getDependentArgumentSets());
assertFalse(p.getDependentArgumentSets().isEmpty());
assertEquals(p.getDependentArgumentSets().size(), 5);
assertEquals(p.getDependentArgumentSets().size(), 8,
String.valueOf(p.getDependentArgumentSets()));

try
{
Expand Down Expand Up @@ -702,6 +704,104 @@ public void testAddMutuallyDependentArgumentSet()
{
// This was expected.
}


// Make sure that the parser properly rejects attempts to use mutually
// dependent arguments without each other.
try
{
p.reset();
p.parse(new String[] { "-a" });
fail("Expected an exception when providing a but not b");
}
catch (final ArgumentException ex)
{
// This was expected.
}

try
{
p.reset();
p.parse(new String[] { "-b" });
fail("Expected an exception when providing b but not a");
}
catch (final ArgumentException ex)
{
// This was expected.
}

p.reset();
p.parse(new String[] { "-a", "-b" });


try
{
p.reset();
p.parse(new String[] { "-c" });
fail("Expected an exception when providing c but not d or e");
}
catch (final ArgumentException ex)
{
// This was expected.
}

try
{
p.reset();
p.parse(new String[] { "-d" });
fail("Expected an exception when providing d but not c or e");
}
catch (final ArgumentException ex)
{
// This was expected.
}

try
{
p.reset();
p.parse(new String[] { "-e" });
fail("Expected an exception when providing e but not c or d");
}
catch (final ArgumentException ex)
{
// This was expected.
}

try
{
p.reset();
p.parse(new String[] { "-c", "-d" });
fail("Expected an exception when providing c and d but not e");
}
catch (final ArgumentException ex)
{
// This was expected.
}

try
{
p.reset();
p.parse(new String[] { "-c", "-e" });
fail("Expected an exception when providing c and e but not d");
}
catch (final ArgumentException ex)
{
// This was expected.
}

try
{
p.reset();
p.parse(new String[] { "-d", "-e" });
fail("Expected an exception when providing d and e but not c");
}
catch (final ArgumentException ex)
{
// This was expected.
}

p.reset();
p.parse(new String[] { "-c", "-d", "-e" });
}


Expand Down

0 comments on commit 76bbe43

Please sign in to comment.