@Test
public void testOptionalVariables() {

    // JENA-1770
    // Setup a situation where the second binding in a spill file binds more
    // variables than the first binding.
    BindingMap binding1 = BindingFactory.create();
    binding1.add(Var.alloc("1"), NodeFactory.createLiteral("A"));
    BindingMap binding2 = BindingFactory.create();
    binding2.add(Var.alloc("1"), NodeFactory.createLiteral("A"));
    binding2.add(Var.alloc("2"), NodeFactory.createLiteral("B"));
    List<Binding> undistinct = Arrays.asList(binding1, binding2, binding1);
    List<Binding> control = Iter.toList(Iter.distinct(undistinct.iterator()));
    List<Binding> distinct = new ArrayList<>();
    DistinctDataBag<Binding> db = new DistinctDataBag<>(new ThresholdPolicyCount<Binding>(2),
                                                        SerializationFactoryFinder.bindingSerializationFactory(),
                                                        new BindingComparator(new ArrayList<SortCondition>()));
    try {
        db.addAll(undistinct);
        Iterator<Binding> iter = db.iterator();
        while (iter.hasNext()) {
            distinct.add(iter.next());
        }
        Iter.close(iter);
    }
    finally {
        db.close();
    }
    assertEquals(control.size(), distinct.size());
    assertTrue(ResultSetCompare.equalsByTest(control, distinct, NodeUtils.sameNode));
}