@Test
  public void testEquals() throws Exception {
    KeyBundle kb1 = KeyBundle.withRandomKeys();
    KeyBundle kb2 = KeyBundle.withRandomKeys();
    CollectionKeys a = CollectionKeys.generateCollectionKeys();
    CollectionKeys b = CollectionKeys.generateCollectionKeys();

    // Random keys are different.
    assertFalse(a.equals(b));
    assertFalse(b.equals(a));

    // keys with unset default key bundles are different.
    b.setDefaultKeyBundle(null);
    assertFalse(a.equals(b));

    // keys with equal default key bundles and no other collections are the same.
    b.setDefaultKeyBundle(a.defaultKeyBundle());
    assertTrue(a.equals(b));

    // keys with equal defaults and equal collections are the same.
    a.setKeyBundleForCollection("1", kb1);
    b.setKeyBundleForCollection("1", kb1);
    assertTrue(a.equals(b));

    // keys with equal defaults but some collection missing are different.
    a.setKeyBundleForCollection("2", kb2);
    assertFalse(a.equals(b));
    assertFalse(b.equals(a));

    // keys with equal defaults and some collection set to the default are the same.
    a.setKeyBundleForCollection("2", a.defaultKeyBundle());
    b.setKeyBundleForCollection("3", b.defaultKeyBundle());
    assertTrue(a.equals(b));
    assertTrue(b.equals(a));
  }
  @Test
  public void testKeyForCollection() throws CryptoException, NoCollectionKeysSetException {
    CollectionKeys ck = new CollectionKeys();
    try {
      ck.keyBundleForCollection("test");
      fail("keyForCollection should throw.");
    } catch (NoCollectionKeysSetException ex) {
      // Good.
    }
    KeyBundle testKeys = KeyBundle.withRandomKeys();
    KeyBundle otherKeys = KeyBundle.withRandomKeys();

    ck.setDefaultKeyBundle(testKeys);
    assertEquals(testKeys, ck.defaultKeyBundle());
    assertEquals(testKeys, ck.keyBundleForCollection("test")); // Returns default.

    ck.setKeyBundleForCollection("test", otherKeys);
    assertEquals(otherKeys, ck.keyBundleForCollection("test")); // Returns default.
  }
  @Test
  public void testDifferences() throws Exception {
    KeyBundle kb1 = KeyBundle.withRandomKeys();
    KeyBundle kb2 = KeyBundle.withRandomKeys();
    KeyBundle kb3 = KeyBundle.withRandomKeys();
    CollectionKeys a = CollectionKeys.generateCollectionKeys();
    CollectionKeys b = CollectionKeys.generateCollectionKeys();
    Set<String> diffs;

    a.setKeyBundleForCollection("1", kb1);
    b.setKeyBundleForCollection("1", kb1);
    diffs = CollectionKeys.differences(a, b);
    assertTrue(diffs.isEmpty());

    a.setKeyBundleForCollection("2", kb2);
    diffs = CollectionKeys.differences(a, b);
    assertArrayEquals(new String[] {"2"}, diffs.toArray(new String[diffs.size()]));

    b.setKeyBundleForCollection("3", kb3);
    diffs = CollectionKeys.differences(a, b);
    assertEquals(2, diffs.size());
    assertTrue(diffs.contains("2"));
    assertTrue(diffs.contains("3"));

    b.setKeyBundleForCollection("1", KeyBundle.withRandomKeys());
    diffs = CollectionKeys.differences(a, b);
    assertEquals(3, diffs.size());

    // This tests that explicitly setting a default key works.
    a = CollectionKeys.generateCollectionKeys();
    b = CollectionKeys.generateCollectionKeys();
    b.setDefaultKeyBundle(a.defaultKeyBundle());
    a.setKeyBundleForCollection("a", a.defaultKeyBundle());
    b.setKeyBundleForCollection("b", b.defaultKeyBundle());
    assertTrue(CollectionKeys.differences(a, b).isEmpty());
    assertTrue(CollectionKeys.differences(b, a).isEmpty());
  }