// Copyright (C) 2015 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.gerrit.extensions.registration;

import static com.google.common.truth.Truth.assertThat;

import com.google.gerrit.extensions.registration.DynamicSet;
import com.google.inject.Key;
import com.google.inject.util.Providers;

import org.junit.Test;

public class DynamicSetTest {
  @Test
  public void testContainsWithEmpty() throws Exception {
    DynamicSet<Integer> ds = new DynamicSet<>();
    assertThat(ds).doesNotContain(2);
  }

  @Test
  public void testContainsTrueWithSingleElement() throws Exception {
    DynamicSet<Integer> ds = new DynamicSet<>();
    ds.add(2);

    assertThat(ds).contains(2);
  }

  @Test
  public void testContainsFalseWithSingleElement() throws Exception {
    DynamicSet<Integer> ds = new DynamicSet<>();
    ds.add(2);

    assertThat(ds).doesNotContain(3);
  }

  @Test
  public void testContainsTrueWithTwoElements() throws Exception {
    DynamicSet<Integer> ds = new DynamicSet<>();
    ds.add(2);
    ds.add(4);

    assertThat(ds).contains(4);
  }

  @Test
  public void testContainsFalseWithTwoElements() throws Exception {
    DynamicSet<Integer> ds = new DynamicSet<>();
    ds.add(2);
    ds.add(4);

    assertThat(ds).doesNotContain(3);
  }

  @Test
  public void testContainsDynamic() throws Exception {
    DynamicSet<Integer> ds = new DynamicSet<>();
    ds.add(2);

    Key<Integer> key = Key.get(Integer.class);
    ReloadableRegistrationHandle<Integer> handle = ds.add(key, Providers.of(4));

    ds.add(6);

    // At first, 4 is contained.
    assertThat(ds).contains(4);

    // Then we remove 4.
    handle.remove();

    // And now 4 should no longer be contained.
    assertThat(ds).doesNotContain(4);
  }
}

@@ -898,24 +898,585 @@
 tTest %7B%0A
+  // In tests for %7B@link DynamicSet#contains(Object)%7D, be sure to avoid%0A  // %7B@code assertThat(ds).contains(...) @%7D and%0A  // %7B@code assertThat(ds).DoesNotContains(...) @%7D as (since%0A  // %7B@link DynamicSet@%7D is not a %7B@link Collection@%7D) those boil down to%0A  // iterating over the %7B@link DynamicSet@%7D and checking equality instead%0A  // of calling %7B@link DynamicSet#contains(Object)%7D.%0A  // To test for %7B@link DynamicSet#contains(Object)%7D, use%0A  // %7B@code assertThat(ds.contains(...)).isTrue() @%7D and%0A  // %7B@code assertThat(ds.contains(...)).isFalse() @%7D instead.%0A%0A
   @Test%0A
   public
@@ -1459,32 +1459,32 @@
 stead.%0A%0A  @Test%0A
-
   public void te
@@ -1594,28 +1594,70 @@
 t(ds
-).doesNotC
+.contains(2)).isFalse(); //See above comment about ds.c
 ontain
-(2);
+s
 %0A  %7D
@@ -1809,33 +1809,32 @@
    assertThat(ds
-)
 .contains(2);%0A  
@@ -1829,17 +1829,65 @@
 tains(2)
-;
+).isTrue(); //See above comment about ds.contains
 %0A  %7D%0A%0A  
@@ -2044,36 +2044,78 @@
 tThat(ds
-).doesNotC
+.contains(3)).isFalse(); //See above comment about ds.c
 ontain
-(3);
+s
 %0A  %7D%0A%0A  
@@ -2276,33 +2276,32 @@
    assertThat(ds
-)
 .contains(4);%0A  
@@ -2296,17 +2296,65 @@
 tains(4)
-;
+).isTrue(); //See above comment about ds.contains
 %0A  %7D%0A%0A  
@@ -2528,28 +2528,70 @@
 t(ds
-).doesNotC
+.contains(3)).isFalse(); //See above comment about ds.c
 ontain
-(3);
+s
 %0A  %7D
@@ -2907,33 +2907,32 @@
    assertThat(ds
-)
 .contains(4);%0A%0A 
@@ -2927,17 +2927,65 @@
 tains(4)
-;
+).isTrue(); //See above comment about ds.contains
 %0A%0A    //
@@ -3094,28 +3094,70 @@
 t(ds
-).doesNotC
+.contains(4)).isFalse(); //See above comment about ds.c
 ontain
-(4);
+s
 %0A  %7D

Test 'contains' method in DynamicSet's containment tests