package fr.umlv.set;

import java.util.AbstractMap;
import java.util.AbstractMap.SimpleImmutableEntry;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import org.junit.Test;

public class SetsTest {
  @Test(expected=UnsupportedOperationException.class)
  public void testReadOnlyAdd() {
    HashSet<String> set = new HashSet<>();
    Set<String> set2 = Sets.asReadOnlySet(set);
    set2.add("foo");
  }
  
  @Test(expected=UnsupportedOperationException.class)
  public void testReadOnlyRemove() {
    HashSet<Integer> set = new HashSet<>();
    set.add(345);
    Set<Integer> set2 = Sets.asReadOnlySet(set);
    set2.remove(345);
  }
  
  @Test(expected=UnsupportedOperationException.class)
  public void testReadOnlyIteratorRemove() {
    HashSet<String> set = new HashSet<>();
    set.add("foo");
    Set<String> set2 = Sets.asReadOnlySet(set);
    Iterator<String> iterator = set2.iterator();
    iterator.next();
    iterator.remove();
  }
  
  @Test(expected=UnsupportedOperationException.class)
  public void testReadOnlyEntrySetPut() {
    HashMap<String, String> map = new HashMap<>();
    Set<Entry<String, String>> set = Sets.asReadOnlyEntrySet(map);
    set.add(new SimpleImmutableEntry<>("foo", "bar"));
  }
  
  @Test(expected=UnsupportedOperationException.class)
  public void testReadOnlyEntryRemove() {
    HashMap<String, Object> map = new HashMap<>();
    map.put("foo", 3);
    Set<Entry<String, Object>> set = Sets.asReadOnlyEntrySet(map);
    set.remove(new SimpleImmutableEntry<>("foo", 3));
  }
  
  @Test(expected=UnsupportedOperationException.class)
  public void testReadOnlyEntryIteratorRemove() {
    HashMap<String, Object> map = new HashMap<>();
    map.put("foo", 3);
    Set<Entry<String, Object>> set = Sets.asReadOnlyEntrySet(map);
    Iterator<Entry<String, Object>> iterator = set.iterator();
    iterator.next();
    iterator.remove();
  }
  
  @Test(expected=UnsupportedOperationException.class)
  public void testReadOnlyEntryIteratorEntrySetValue() {
    HashMap<String, Object> map = new HashMap<>();
    map.put("foo", 3);
    Set<Entry<String, Object>> set = Sets.asReadOnlyEntrySet(map);
    Iterator<Entry<String, Object>> iterator = set.iterator();
    Entry<String, Object> entry = iterator.next();
    entry.setValue(7);
  }
}
