Search in sources :

Example 81 with NoSuchElementException

use of java.util.NoSuchElementException in project robovm by robovm.

the class EnumMapTest method test_entrySet.

/**
     * java.util.EnumMap#entrySet()
     */
@AndroidOnly("Map.Entry is indirectly modified on RI when Iterator.next() is invoked")
@SuppressWarnings({ "unchecked", "boxing" })
public void test_entrySet() {
    EnumMap enumSizeMap = new EnumMap(Size.class);
    enumSizeMap.put(Size.Middle, 1);
    enumSizeMap.put(Size.Big, null);
    MockEntry mockEntry = new MockEntry(Size.Middle, 1);
    Set set = enumSizeMap.entrySet();
    Set set1 = enumSizeMap.entrySet();
    assertSame("Should be same", set1, set);
    try {
        set.add(mockEntry);
        fail("Should throw UnsupportedOperationException");
    } catch (UnsupportedOperationException e) {
    // Expected
    }
    assertTrue("Returned false for contained object", set.contains(mockEntry));
    mockEntry = new MockEntry(Size.Middle, null);
    assertFalse("Returned true for uncontained object", set.contains(mockEntry));
    assertFalse("Returned true for uncontained object", set.contains(Size.Small));
    mockEntry = new MockEntry(new Integer(1), 1);
    assertFalse("Returned true for uncontained object", set.contains(mockEntry));
    assertFalse("Returned true for uncontained object", set.contains(new Integer(1)));
    mockEntry = new MockEntry(Size.Big, null);
    assertTrue("Returned false for contained object", set.contains(mockEntry));
    assertTrue("Returned false when the object can be removed", set.remove(mockEntry));
    assertFalse("Returned true for uncontained object", set.contains(mockEntry));
    assertFalse("Returned true when the object can not be removed", set.remove(mockEntry));
    mockEntry = new MockEntry(new Integer(1), 1);
    assertFalse("Returned true when the object can not be removed", set.remove(mockEntry));
    assertFalse("Returned true when the object can not be removed", set.remove(new Integer(1)));
    // The set is backed by the map so changes to one are reflected by the
    // other.
    enumSizeMap.put(Size.Big, 3);
    mockEntry = new MockEntry(Size.Big, 3);
    assertTrue("Returned false for contained object", set.contains(mockEntry));
    enumSizeMap.remove(Size.Big);
    assertFalse("Returned true for uncontained object", set.contains(mockEntry));
    assertEquals("Wrong size", 1, set.size());
    set.clear();
    assertEquals("Wrong size", 0, set.size());
    enumSizeMap = new EnumMap(Size.class);
    enumSizeMap.put(Size.Middle, 1);
    enumSizeMap.put(Size.Big, null);
    set = enumSizeMap.entrySet();
    Collection c = new ArrayList();
    c.add(new MockEntry(Size.Middle, 1));
    assertTrue("Return wrong value", set.containsAll(c));
    assertTrue("Remove does not success", set.removeAll(c));
    enumSizeMap.put(Size.Middle, 1);
    c.add(new MockEntry(Size.Big, 3));
    assertTrue("Remove does not success", set.removeAll(c));
    assertFalse("Should return false", set.removeAll(c));
    assertEquals("Wrong size", 1, set.size());
    enumSizeMap = new EnumMap(Size.class);
    enumSizeMap.put(Size.Middle, 1);
    enumSizeMap.put(Size.Big, null);
    set = enumSizeMap.entrySet();
    c = new ArrayList();
    c.add(new MockEntry(Size.Middle, 1));
    c.add(new MockEntry(Size.Big, 3));
    assertTrue("Retain does not success", set.retainAll(c));
    assertEquals("Wrong size", 1, set.size());
    assertFalse("Should return false", set.retainAll(c));
    enumSizeMap = new EnumMap(Size.class);
    enumSizeMap.put(Size.Middle, 1);
    enumSizeMap.put(Size.Big, null);
    set = enumSizeMap.entrySet();
    Object[] array = set.toArray();
    assertEquals("Wrong length", 2, array.length);
    Map.Entry entry = (Map.Entry) array[0];
    assertEquals("Wrong key", Size.Middle, entry.getKey());
    assertEquals("Wrong value", 1, entry.getValue());
    Object[] array1 = new Object[10];
    array1 = set.toArray();
    assertEquals("Wrong length", 2, array1.length);
    entry = (Map.Entry) array[0];
    assertEquals("Wrong key", Size.Middle, entry.getKey());
    assertEquals("Wrong value", 1, entry.getValue());
    array1 = new Object[10];
    array1 = set.toArray(array1);
    assertEquals("Wrong length", 10, array1.length);
    entry = (Map.Entry) array[1];
    assertEquals("Wrong key", Size.Big, entry.getKey());
    assertNull("Should be null", array1[2]);
    set = enumSizeMap.entrySet();
    Integer integer = new Integer("1");
    assertFalse("Returned true when the object can not be removed", set.remove(integer));
    assertTrue("Returned false when the object can be removed", set.remove(entry));
    enumSizeMap = new EnumMap(Size.class);
    enumSizeMap.put(Size.Middle, 1);
    enumSizeMap.put(Size.Big, null);
    set = enumSizeMap.entrySet();
    Iterator iter = set.iterator();
    entry = (Map.Entry) iter.next();
    assertTrue("Returned false for contained object", set.contains(entry));
    mockEntry = new MockEntry(Size.Middle, 2);
    assertFalse("Returned true for uncontained object", set.contains(mockEntry));
    mockEntry = new MockEntry(new Integer(2), 2);
    assertFalse("Returned true for uncontained object", set.contains(mockEntry));
    entry = (Map.Entry) iter.next();
    assertTrue("Returned false for contained object", set.contains(entry));
    enumSizeMap.put(Size.Middle, 1);
    enumSizeMap.remove(Size.Big);
    mockEntry = new MockEntry(Size.Big, null);
    assertEquals("Wrong size", 1, set.size());
    assertFalse("Returned true for uncontained object", set.contains(mockEntry));
    enumSizeMap.put(Size.Big, 2);
    mockEntry = new MockEntry(Size.Big, 2);
    assertTrue("Returned false for contained object", set.contains(mockEntry));
    iter.remove();
    try {
        iter.remove();
        fail("Should throw IllegalStateException");
    } catch (IllegalStateException e) {
    // Expected
    }
    try {
        entry.setValue(2);
        fail("Should throw IllegalStateException");
    } catch (IllegalStateException e) {
    // Expected
    }
    try {
        set.contains(entry);
        fail("Should throw IllegalStateException");
    } catch (IllegalStateException e) {
    // Expected
    }
    enumSizeMap = new EnumMap(Size.class);
    enumSizeMap.put(Size.Middle, 1);
    enumSizeMap.put(Size.Big, null);
    set = enumSizeMap.entrySet();
    iter = set.iterator();
    entry = (Map.Entry) iter.next();
    assertEquals("Wrong key", Size.Middle, entry.getKey());
    assertTrue("Returned false for contained object", set.contains(entry));
    enumSizeMap.put(Size.Middle, 3);
    assertTrue("Returned false for contained object", set.contains(entry));
    entry.setValue(2);
    assertTrue("Returned false for contained object", set.contains(entry));
    assertFalse("Returned true for uncontained object", set.remove(new Integer(1)));
    iter.next();
    assertEquals("Wrong key", Size.Middle, entry.getKey());
    set.clear();
    assertEquals("Wrong size", 0, set.size());
    enumSizeMap = new EnumMap(Size.class);
    enumSizeMap.put(Size.Middle, 1);
    enumSizeMap.put(Size.Big, null);
    set = enumSizeMap.entrySet();
    iter = set.iterator();
    mockEntry = new MockEntry(Size.Middle, 1);
    assertFalse("Wrong result", entry.equals(mockEntry));
    try {
        iter.remove();
        fail("Should throw IllegalStateException");
    } catch (IllegalStateException e) {
    // Expected
    }
    entry = (Map.Entry) iter.next();
    assertEquals("Wrong key", Size.Middle, entry.getKey());
    assertTrue("Should return true", entry.equals(mockEntry));
    assertEquals("Should be equal", mockEntry.hashCode(), entry.hashCode());
    mockEntry = new MockEntry(Size.Big, 1);
    assertFalse("Wrong result", entry.equals(mockEntry));
    entry = (Map.Entry) iter.next();
    assertFalse("Wrong result", entry.equals(mockEntry));
    assertEquals("Wrong key", Size.Big, entry.getKey());
    iter.remove();
    assertFalse("Wrong result", entry.equals(mockEntry));
    assertEquals("Wrong size", 1, set.size());
    try {
        iter.remove();
        fail("Should throw IllegalStateException");
    } catch (IllegalStateException e) {
    // Expected
    }
    try {
        iter.next();
        fail("Should throw NoSuchElementException");
    } catch (NoSuchElementException e) {
    // Expected
    }
}
Also used : Set(java.util.Set) ArrayList(java.util.ArrayList) Iterator(java.util.Iterator) Collection(java.util.Collection) EnumMap(java.util.EnumMap) EnumMap(java.util.EnumMap) Map(java.util.Map) HashMap(java.util.HashMap) NoSuchElementException(java.util.NoSuchElementException) AndroidOnly(dalvik.annotation.AndroidOnly)

Example 82 with NoSuchElementException

use of java.util.NoSuchElementException in project robovm by robovm.

the class EnumMapTest method test_keySet.

/**
     * java.util.EnumMap#keySet()
     */
@SuppressWarnings({ "unchecked", "boxing" })
public void test_keySet() {
    EnumMap enumSizeMap = new EnumMap(Size.class);
    enumSizeMap.put(Size.Middle, 2);
    enumSizeMap.put(Size.Big, null);
    Set set = enumSizeMap.keySet();
    Set set1 = enumSizeMap.keySet();
    assertSame("Should be same", set1, set);
    try {
        set.add(Size.Big);
        fail("Should throw UnsupportedOperationException");
    } catch (UnsupportedOperationException e) {
    // Expected
    }
    assertTrue("Returned false for contained object", set.contains(Size.Middle));
    assertTrue("Returned false for contained object", set.contains(Size.Big));
    assertFalse("Returned true for uncontained object", set.contains(Size.Small));
    assertFalse("Returned true for uncontained object", set.contains(new Integer(1)));
    assertTrue("Returned false when the object can be removed", set.remove(Size.Big));
    assertFalse("Returned true for uncontained object", set.contains(Size.Big));
    assertFalse("Returned true when the object can not be removed", set.remove(Size.Big));
    assertFalse("Returned true when the object can not be removed", set.remove(new Integer(1)));
    // The set is backed by the map so changes to one are reflected by the
    // other.
    enumSizeMap.put(Size.Big, 3);
    assertTrue("Returned false for contained object", set.contains(Size.Big));
    enumSizeMap.remove(Size.Big);
    assertFalse("Returned true for uncontained object", set.contains(Size.Big));
    assertEquals("Wrong size", 1, set.size());
    set.clear();
    assertEquals("Wrong size", 0, set.size());
    enumSizeMap = new EnumMap(Size.class);
    enumSizeMap.put(Size.Middle, 1);
    enumSizeMap.put(Size.Big, null);
    set = enumSizeMap.keySet();
    Collection c = new ArrayList();
    c.add(Size.Big);
    assertTrue("Should return true", set.containsAll(c));
    c.add(Size.Small);
    assertFalse("Should return false", set.containsAll(c));
    assertTrue("Should return true", set.removeAll(c));
    assertEquals("Wrong size", 1, set.size());
    assertFalse("Should return false", set.removeAll(c));
    assertEquals("Wrong size", 1, set.size());
    try {
        set.addAll(c);
        fail("Should throw UnsupportedOperationException");
    } catch (UnsupportedOperationException e) {
    // Expected
    }
    enumSizeMap.put(Size.Big, null);
    assertEquals("Wrong size", 2, set.size());
    assertTrue("Should return true", set.retainAll(c));
    assertEquals("Wrong size", 1, set.size());
    assertFalse("Should return false", set.retainAll(c));
    assertEquals(1, set.size());
    Object[] array = set.toArray();
    assertEquals("Wrong length", 1, array.length);
    assertEquals("Wrong key", Size.Big, array[0]);
    enumSizeMap = new EnumMap(Size.class);
    enumSizeMap.put(Size.Middle, 1);
    enumSizeMap.put(Size.Big, null);
    set = enumSizeMap.keySet();
    c = new ArrayList();
    c.add(Color.Blue);
    assertFalse("Should return false", set.remove(c));
    assertEquals("Wrong size", 2, set.size());
    assertTrue("Should return true", set.retainAll(c));
    assertEquals("Wrong size", 0, set.size());
    enumSizeMap = new EnumMap(Size.class);
    enumSizeMap.put(Size.Middle, 1);
    enumSizeMap.put(Size.Big, null);
    set = enumSizeMap.keySet();
    Iterator iter = set.iterator();
    Enum enumKey = (Enum) iter.next();
    assertTrue("Returned false for contained object", set.contains(enumKey));
    enumKey = (Enum) iter.next();
    assertTrue("Returned false for contained object", set.contains(enumKey));
    enumSizeMap.remove(Size.Big);
    assertFalse("Returned true for uncontained object", set.contains(enumKey));
    iter.remove();
    try {
        iter.remove();
        fail("Should throw IllegalStateException");
    } catch (IllegalStateException e) {
    // Expected
    }
    assertFalse("Returned true for uncontained object", set.contains(enumKey));
    iter = set.iterator();
    enumKey = (Enum) iter.next();
    assertTrue("Returned false for contained object", set.contains(enumKey));
    enumSizeMap.put(Size.Middle, 3);
    assertTrue("Returned false for contained object", set.contains(enumKey));
    enumSizeMap = new EnumMap(Size.class);
    enumSizeMap.put(Size.Middle, 1);
    enumSizeMap.put(Size.Big, null);
    set = enumSizeMap.keySet();
    iter = set.iterator();
    try {
        iter.remove();
        fail("Should throw IllegalStateException");
    } catch (IllegalStateException e) {
    // Expected
    }
    enumKey = (Enum) iter.next();
    assertEquals("Wrong key", Size.Middle, enumKey);
    assertSame("Wrong key", Size.Middle, enumKey);
    assertFalse("Returned true for unequal object", iter.equals(enumKey));
    iter.remove();
    assertFalse("Returned true for uncontained object", set.contains(enumKey));
    try {
        iter.remove();
        fail("Should throw IllegalStateException");
    } catch (IllegalStateException e) {
    // Expected
    }
    assertEquals("Wrong size", 1, set.size());
    enumKey = (Enum) iter.next();
    assertEquals("Wrong key", Size.Big, enumKey);
    iter.remove();
    try {
        iter.next();
        fail("Should throw NoSuchElementException");
    } catch (NoSuchElementException e) {
    // Expected
    }
}
Also used : Set(java.util.Set) ArrayList(java.util.ArrayList) Iterator(java.util.Iterator) Collection(java.util.Collection) EnumMap(java.util.EnumMap) NoSuchElementException(java.util.NoSuchElementException)

Example 83 with NoSuchElementException

use of java.util.NoSuchElementException in project robovm by robovm.

the class CollectionsTest method test_minLjava_util_Collection.

/**
     * java.util.Collections#min(java.util.Collection)
     */
public void test_minLjava_util_Collection() {
    // Test for method java.lang.Object
    // java.util.Collections.min(java.util.Collection)
    // assumes s, objArray are sorted
    assertTrue("Returned incorrect min element", Collections.min(s) == objArray[0]);
    ArrayList al = new ArrayList();
    try {
        Collections.min(al);
        fail("NoSuchElementException expected");
    } catch (NoSuchElementException e) {
    //expected
    }
    al.add("String");
    al.add(new Integer(1));
    al.add(new Double(3.14));
    try {
        Collections.min(al);
        fail("ClassCastException expected");
    } catch (ClassCastException e) {
    //expected
    }
}
Also used : ArrayList(java.util.ArrayList) NoSuchElementException(java.util.NoSuchElementException)

Example 84 with NoSuchElementException

use of java.util.NoSuchElementException in project robovm by robovm.

the class CollectionsTest method test_maxLjava_util_CollectionLjava_util_Comparator.

/**
     * java.util.Collections#max(java.util.Collection,
     *        java.util.Comparator)
     */
public void test_maxLjava_util_CollectionLjava_util_Comparator() {
    // Test for method java.lang.Object
    // java.util.Collections.max(java.util.Collection, java.util.Comparator)
    // assumes s, objArray are sorted
    // With this custom (backwards) comparator the 'max' element should be
    // the smallest in the list
    ReversedMyIntComparator rmic = new ReversedMyIntComparator();
    assertTrue("Returned incorrect max element using custom comparator", Collections.max(mys, rmic) == myobjArray[0]);
    ArrayList al = new ArrayList();
    try {
        Collections.max(al, rmic);
        fail("NoSuchElementException expected");
    } catch (NoSuchElementException e) {
    //expected
    }
    al.add("String");
    al.add(new Integer(1));
    al.add(new Double(3.14));
    try {
        Collections.max(al, rmic);
        fail("ClassCastException expected");
    } catch (ClassCastException e) {
    //expected
    }
}
Also used : ArrayList(java.util.ArrayList) NoSuchElementException(java.util.NoSuchElementException)

Example 85 with NoSuchElementException

use of java.util.NoSuchElementException in project robovm by robovm.

the class CollectionsTest method test_maxLjava_util_Collection.

/**
     * java.util.Collections#max(java.util.Collection)
     */
public void test_maxLjava_util_Collection() {
    // Test for method java.lang.Object
    // java.util.Collections.max(java.util.Collection)
    // assumes s, objArray are sorted
    assertTrue("Returned incorrect max element", Collections.max(s) == objArray[objArray.length - 1]);
    ArrayList al = new ArrayList();
    try {
        Collections.max(al);
        fail("NoSuchElementException expected");
    } catch (NoSuchElementException e) {
    //expected
    }
    al.add("String");
    al.add(new Integer(1));
    al.add(new Double(3.14));
    try {
        Collections.max(al);
        fail("ClassCastException expected");
    } catch (ClassCastException e) {
    //expected
    }
}
Also used : ArrayList(java.util.ArrayList) NoSuchElementException(java.util.NoSuchElementException)

Aggregations

NoSuchElementException (java.util.NoSuchElementException)1629 Iterator (java.util.Iterator)234 ArrayList (java.util.ArrayList)138 IOException (java.io.IOException)137 Test (org.junit.Test)95 StringTokenizer (java.util.StringTokenizer)88 Scanner (java.util.Scanner)86 Map (java.util.Map)56 List (java.util.List)54 File (java.io.File)51 HashMap (java.util.HashMap)47 InputMismatchException (java.util.InputMismatchException)47 LinkedList (java.util.LinkedList)32 HashSet (java.util.HashSet)31 Set (java.util.Set)29 CorruptDataException (com.ibm.j9ddr.CorruptDataException)28 Locale (java.util.Locale)27 Collection (java.util.Collection)26 BufferedReader (java.io.BufferedReader)24 Enumeration (java.util.Enumeration)24