Search in sources :

Example 6 with Support_UnmodifiableCollectionTest

use of tests.support.Support_UnmodifiableCollectionTest in project j2objc by google.

the class CollectionsTest method test_unmodifiableSetLjava_util_Set.

/**
	 * @tests java.util.Collections#unmodifiableSet(java.util.Set)
	 */
public void test_unmodifiableSetLjava_util_Set() {
    // Test for method java.util.Set
    // java.util.Collections.unmodifiableSet(java.util.Set)
    boolean exception = false;
    Set c = Collections.unmodifiableSet(s);
    assertTrue("Returned set is of incorrect size", c.size() == s.size());
    Iterator i = ll.iterator();
    while (i.hasNext()) assertTrue("Returned set missing elements", c.contains(i.next()));
    try {
        c.add(new Object());
    } catch (UnsupportedOperationException e) {
        exception = true;
    // Correct
    }
    if (!exception) {
        fail("Allowed modification of set");
    }
    try {
        c.remove(new Object());
        fail("Allowed modification of set");
    } catch (UnsupportedOperationException e) {
    // Correct
    }
    Set mySet = Collections.unmodifiableSet(new HashSet());
    assertTrue("Should not contain null", !mySet.contains(null));
    mySet = Collections.unmodifiableSet(Collections.singleton(null));
    assertTrue("Should contain null", mySet.contains(null));
    mySet = new TreeSet();
    for (int counter = 0; counter < 100; counter++) {
        mySet.add(objArray[counter]);
    }
    new Support_UnmodifiableCollectionTest("", Collections.unmodifiableSet(mySet)).runTest();
}
Also used : SortedSet(java.util.SortedSet) TreeSet(java.util.TreeSet) HashSet(java.util.HashSet) Set(java.util.Set) TreeSet(java.util.TreeSet) Support_UnmodifiableCollectionTest(tests.support.Support_UnmodifiableCollectionTest) ListIterator(java.util.ListIterator) Iterator(java.util.Iterator) HashSet(java.util.HashSet)

Example 7 with Support_UnmodifiableCollectionTest

use of tests.support.Support_UnmodifiableCollectionTest in project j2objc by google.

the class HashMapTest method test_values.

/**
     * java.util.HashMap#values()
     */
public void test_values() {
    // Test for method java.util.Collection java.util.HashMap.values()
    Collection c = hm.values();
    assertTrue("Returned collection of incorrect size()", c.size() == hm.size());
    for (int i = 0; i < objArray.length; i++) assertTrue("Returned collection does not contain all keys", c.contains(objArray[i]));
    HashMap myHashMap = new HashMap();
    for (int i = 0; i < 100; i++) myHashMap.put(objArray2[i], objArray[i]);
    Collection values = myHashMap.values();
    new Support_UnmodifiableCollectionTest("Test Returned Collection From HashMap.values()", values).runTest();
    values.remove(new Integer(0));
    assertTrue("Removing from the values collection should remove from the original map", !myHashMap.containsValue(new Integer(0)));
}
Also used : HashMap(java.util.HashMap) Support_UnmodifiableCollectionTest(tests.support.Support_UnmodifiableCollectionTest) Collection(java.util.Collection)

Example 8 with Support_UnmodifiableCollectionTest

use of tests.support.Support_UnmodifiableCollectionTest in project j2objc by google.

the class HashtableTest method test_values.

/**
     * java.util.Hashtable#values()
     */
public void test_values() {
    // Test for method java.util.Collection java.util.Hashtable.values()
    Collection c = ht10.values();
    Enumeration e = elmVector.elements();
    while (e.hasMoreElements()) assertTrue("Returned incorrect values", c.contains(e.nextElement()));
    // BEGIN android-removed
    // implementation dependent
    //        assertEquals("Not synchronized",
    //                "java.util.Collections$SynchronizedCollection", c.getClass().getName());
    // END android-removed
    Hashtable myHashtable = new Hashtable();
    for (int i = 0; i < 100; i++) myHashtable.put(new Integer(i), new Integer(i));
    Collection values = myHashtable.values();
    new Support_UnmodifiableCollectionTest("Test Returned Collection From Hashtable.values()", values).runTest();
    values.remove(new Integer(0));
    assertTrue("Removing from the values collection should remove from the original map", !myHashtable.containsValue(new Integer(0)));
}
Also used : Enumeration(java.util.Enumeration) Hashtable(java.util.Hashtable) Support_UnmodifiableCollectionTest(tests.support.Support_UnmodifiableCollectionTest) Collection(java.util.Collection)

Example 9 with Support_UnmodifiableCollectionTest

use of tests.support.Support_UnmodifiableCollectionTest in project j2objc by google.

the class CollectionsTest method test_unmodifiableCollectionLjava_util_Collection.

/**
	 * @throws InterruptedException
	 * @tests java.util.Collections#synchronizedCollection(java.util.Collection)
	 */
/* TODO(tball): enable when threading is supported.
	public void test_synchronizedCollectionLjava_util_Collection() throws InterruptedException {
		// Test for method java.util.Collection
		// java.util.Collections.synchronizedCollection(java.util.Collection)

		LinkedList smallList = new LinkedList();
		for (int i = 0; i < 50; i++) {
			smallList.add(objArray[i]);
		}

		final int numberOfLoops = 200;
		Collection synchCol = Collections.synchronizedCollection(smallList);
		// Replacing the previous line with the line below *should* cause the
		// test to fail--the collecion below isn't synchronized
		// Collection synchCol = smallList;

		SynchCollectionChecker normalSynchChecker = new SynchCollectionChecker(
				synchCol, false, numberOfLoops);
		SynchCollectionChecker offsetSynchChecker = new SynchCollectionChecker(
				synchCol, true, numberOfLoops);
		Thread normalThread = new Thread(normalSynchChecker);
		Thread offsetThread = new Thread(offsetSynchChecker);
		normalThread.start();
		offsetThread.start();
		while ((normalSynchChecker.getNumberOfChecks() < numberOfLoops)
				|| (offsetSynchChecker.getNumberOfChecks() < numberOfLoops)) {
	    
                       try {
                               Thread.sleep(10);
                       } catch (InterruptedException e) {
                       }

		}
		assertTrue("Returned collection corrupted by multiple thread access",
				normalSynchChecker.getResult()
						&& offsetSynchChecker.getResult());
		try {
			normalThread.join(5000);
			offsetThread.join(5000);
		} catch (InterruptedException e) {
			fail("join() interrupted");
		}

		synchCol.add(null);
		assertTrue("Trying to use nulls in collection failed", synchCol
				.contains(null));

		smallList = new LinkedList();
		for (int i = 0; i < 100; i++) {
			smallList.add(objArray[i]);
		}
		new Support_CollectionTest("", Collections
				.synchronizedCollection(smallList)).runTest();

        //Test self reference
        synchCol = Collections.synchronizedCollection(smallList);
        synchCol.add(smallList);
        assertTrue("should contain self ref", synchCol.toString().indexOf("(this") > -1);
	}
	*/
/**
	 * @tests java.util.Collections#synchronizedList(java.util.List)
	 */
/* TODO(tball): enable when threading is supported.
	public void test_synchronizedListLjava_util_List() {
		try {
			Collections.synchronizedList(null);
			fail("Expected NullPointerException for null list parameter");
		} catch (NullPointerException e) {
            //Expected
		}

		// test with a Sequential Access List
		List smallList = new LinkedList();
		testSynchronizedList(smallList, "Sequential Access");

		smallList = new LinkedList();
		List myList;
		for (int i = 0; i < 100; i++) {
			smallList.add(objArray[i]);
		}
		myList = Collections.synchronizedList(smallList);

		new Support_ListTest("", myList).runTest();
        
		// test with a Random Access List
		smallList = new ArrayList();
		testSynchronizedList(smallList, "Random Access");

		smallList = new ArrayList();
		for (int i = 0; i < 100; i++) {
			smallList.add(objArray[i]);
		}
		myList = Collections.synchronizedList(smallList);
		new Support_ListTest("", myList).runTest();
        
		//Test self reference
        myList = Collections.synchronizedList(smallList);
        myList.add(smallList);
        assertTrue("should contain self ref", myList.toString().indexOf("(this") > -1);
	}
	*/
/* TODO(tball): enable when threading is supported.
	private void testSynchronizedList(List smallList, String type) {
		for (int i = 0; i < 50; i++) {
			smallList.add(objArray[i]);
		}
		final int numberOfLoops = 200;
		List synchList = Collections.synchronizedList(smallList);
		if (type.equals("Random Access"))
			assertTrue(
					"Returned synchronized list should implement the Random Access interface",
					synchList instanceof RandomAccess);
		else
			assertTrue(
					"Returned synchronized list should not implement the Random Access interface",
					!(synchList instanceof RandomAccess));

		// Replacing the previous line with the line below *should* cause the
		// test to fail--the list below isn't synchronized
		// List synchList = smallList;
		SynchCollectionChecker normalSynchChecker = new SynchCollectionChecker(
				synchList, false, numberOfLoops);
		SynchCollectionChecker offsetSynchChecker = new SynchCollectionChecker(
				synchList, true, numberOfLoops);
		Thread normalThread = new Thread(normalSynchChecker);
		Thread offsetThread = new Thread(offsetSynchChecker);
		normalThread.start();
		offsetThread.start();
		while ((normalSynchChecker.getNumberOfChecks() < numberOfLoops)
				|| (offsetSynchChecker.getNumberOfChecks() < numberOfLoops)) {
			try {
				Thread.sleep(10);
			} catch (InterruptedException e) {
                //Expected
			}
		}
		assertTrue(
                type
                        + " list tests: Returned list corrupted by multiple thread access",
                normalSynchChecker.getResult()
                        && offsetSynchChecker.getResult());
		try {
			normalThread.join(5000);
			offsetThread.join(5000);
		} catch (InterruptedException e) {
			fail(type + " list tests: join() interrupted");
		}
		synchList.set(25, null);
		assertNull(type + " list tests: Trying to use nulls in list failed",
				synchList.get(25));
	}
	*/
/**
	 * @tests java.util.Collections#synchronizedMap(java.util.Map)
	 */
/* TODO(tball): enable when threading is supported.
	public void test_synchronizedMapLjava_util_Map() {
		// Test for method java.util.Map
		// java.util.Collections.synchronizedMap(java.util.Map)
		HashMap smallMap = new HashMap();
		for (int i = 0; i < 50; i++) {
			smallMap.put(objArray[i], objArray[i]);
		}

		final int numberOfLoops = 200;
		Map synchMap = Collections.synchronizedMap(smallMap);
		// Replacing the previous line with the line below should cause the test
		// to fail--the list below isn't synchronized
		// Map synchMap = smallMap;

		SynchMapChecker normalSynchChecker = new SynchMapChecker(synchMap,
				false, numberOfLoops);
		SynchMapChecker offsetSynchChecker = new SynchMapChecker(synchMap,
				true, numberOfLoops);
		Thread normalThread = new Thread(normalSynchChecker);
		Thread offsetThread = new Thread(offsetSynchChecker);
		normalThread.start();
		offsetThread.start();
		while ((normalSynchChecker.getNumberOfChecks() < numberOfLoops)
				|| (offsetSynchChecker.getNumberOfChecks() < numberOfLoops)) {
			try {
				Thread.sleep(10);
			} catch (InterruptedException e) {
                //Expected
			}
		}
		assertTrue("Returned map corrupted by multiple thread access",
				normalSynchChecker.getResult()
						&& offsetSynchChecker.getResult());
		try {
			normalThread.join(5000);
			offsetThread.join(5000);
		} catch (InterruptedException e) {
			fail("join() interrupted");
		}

		// synchronized map does not have to permit null keys or values
		synchMap.put(new Long(25), null);
		synchMap.put(null, new Long(30));
		assertNull("Trying to use a null value in map failed", synchMap
				.get(new Long(25)));
		assertTrue("Trying to use a null key in map failed", synchMap.get(null)
				.equals(new Long(30)));

		smallMap = new HashMap();
		for (int i = 0; i < 100; i++) {
			smallMap.put(objArray[i].toString(), objArray[i]);
		}
		synchMap = Collections.synchronizedMap(smallMap);
		new Support_UnmodifiableMapTest("", synchMap).runTest();
		synchMap.keySet().remove(objArray[50].toString());
		assertNull(
				"Removing a key from the keySet of the synchronized map did not remove it from the synchronized map: ",
				synchMap.get(objArray[50].toString()));
		assertNull(
				"Removing a key from the keySet of the synchronized map did not remove it from the original map",
				smallMap.get(objArray[50].toString()));
	}
	*/
/**
	 * @tests java.util.Collections#synchronizedSet(java.util.Set)
	 */
/* TODO(tball): enable when threading is supported.
	public void test_synchronizedSetLjava_util_Set() {
		// Test for method java.util.Set
		// java.util.Collections.synchronizedSet(java.util.Set)
		HashSet smallSet = new HashSet();
		for (int i = 0; i < 50; i++) {
			smallSet.add(objArray[i]);
		}

		final int numberOfLoops = 200;
		Set synchSet = Collections.synchronizedSet(smallSet);
		// Replacing the previous line with the line below should cause the test
		// to fail--the set below isn't synchronized
		// Set synchSet = smallSet;

		SynchCollectionChecker normalSynchChecker = new SynchCollectionChecker(
				synchSet, false, numberOfLoops);
		SynchCollectionChecker offsetSynchChecker = new SynchCollectionChecker(
				synchSet, true, numberOfLoops);
		Thread normalThread = new Thread(normalSynchChecker);
		Thread offsetThread = new Thread(offsetSynchChecker);
		normalThread.start();
		offsetThread.start();
		while ((normalSynchChecker.getNumberOfChecks() < numberOfLoops)
				|| (offsetSynchChecker.getNumberOfChecks() < numberOfLoops)) {
			try {
				Thread.sleep(10);
			} catch (InterruptedException e) {
                //Expected
			}
		}
		assertTrue("Returned set corrupted by multiple thread access",
				normalSynchChecker.getResult()
						&& offsetSynchChecker.getResult());
		try {
			normalThread.join(5000);
			offsetThread.join(5000);
		} catch (InterruptedException e) {
			fail("join() interrupted");
		}

		Set mySet = Collections.synchronizedSet(smallSet);
		mySet.add(null);
		assertTrue("Trying to use nulls in list failed", mySet.contains(null));

		smallSet = new HashSet();
		for (int i = 0; i < 100; i++) {
			smallSet.add(objArray[i]);
		}
		new Support_SetTest("", Collections.synchronizedSet(smallSet))
				.runTest();
        
		//Test self reference
        mySet = Collections.synchronizedSet(smallSet);
        mySet.add(smallSet);
        assertTrue("should contain self ref", mySet.toString().indexOf("(this") > -1);
	}
	*/
/**
	 * @tests java.util.Collections#synchronizedSortedMap(java.util.SortedMap)
	 */
/* TODO(tball): enable when threading is supported.
	public void test_synchronizedSortedMapLjava_util_SortedMap() {
		// Test for method java.util.SortedMap
		// java.util.Collections.synchronizedSortedMap(java.util.SortedMap)
		TreeMap smallMap = new TreeMap();
		for (int i = 0; i < 50; i++) {
			smallMap.put(objArray[i], objArray[i]);
		}

		final int numberOfLoops = 200;
		Map synchMap = Collections.synchronizedMap(smallMap);
		// Replacing the previous line with the line below should cause the test
		// to fail--the list below isn't synchronized
		// Map synchMap = smallMap;

		SynchMapChecker normalSynchChecker = new SynchMapChecker(synchMap,
				false, numberOfLoops);
		SynchMapChecker offsetSynchChecker = new SynchMapChecker(synchMap,
				true, numberOfLoops);
		Thread normalThread = new Thread(normalSynchChecker);
		Thread offsetThread = new Thread(offsetSynchChecker);
		normalThread.start();
		offsetThread.start();
		while ((normalSynchChecker.getNumberOfChecks() < numberOfLoops)
				|| (offsetSynchChecker.getNumberOfChecks() < numberOfLoops)) {
			try {
				Thread.sleep(10);
			} catch (InterruptedException e) {
                //Expected
			}
		}
		assertTrue("Returned map corrupted by multiple thread access",
				normalSynchChecker.getResult()
						&& offsetSynchChecker.getResult());
		try {
			normalThread.join(5000);
			offsetThread.join(5000);
		} catch (InterruptedException e) {
			fail("join() interrupted");
		}

		smallMap = new TreeMap();
		for (int i = 0; i < 100; i++) {
			smallMap.put(objArray[i].toString(), objArray[i]);
		}
		synchMap = Collections.synchronizedSortedMap(smallMap);
		new Support_UnmodifiableMapTest("", synchMap).runTest();
		synchMap.keySet().remove(objArray[50].toString());
		assertNull(
				"Removing a key from the keySet of the synchronized map did not remove it from the synchronized map",
				synchMap.get(objArray[50].toString()));
		assertNull(
				"Removing a key from the keySet of the synchronized map did not remove it from the original map",
				smallMap.get(objArray[50].toString()));
	}
	*/
/**
	 * @tests java.util.Collections#synchronizedSortedSet(java.util.SortedSet)
	 */
/* TODO(tball): enable when threading is supported.
	public void test_synchronizedSortedSetLjava_util_SortedSet() {
		// Test for method java.util.SortedSet
		// java.util.Collections.synchronizedSortedSet(java.util.SortedSet)
		TreeSet smallSet = new TreeSet();
		for (int i = 0; i < 50; i++) {
			smallSet.add(objArray[i]);
		}

		final int numberOfLoops = 200;
		Set synchSet = Collections.synchronizedSet(smallSet);
		// Replacing the previous line with the line below should cause the test
		// to fail--the list below isn't synchronized
		// Set synchSet = smallSet;

		SynchCollectionChecker normalSynchChecker = new SynchCollectionChecker(
				synchSet, false, numberOfLoops);
		SynchCollectionChecker offsetSynchChecker = new SynchCollectionChecker(
				synchSet, true, numberOfLoops);
		Thread normalThread = new Thread(normalSynchChecker);
		Thread offsetThread = new Thread(offsetSynchChecker);
		normalThread.start();
		offsetThread.start();
		while ((normalSynchChecker.getNumberOfChecks() < numberOfLoops)
				|| (offsetSynchChecker.getNumberOfChecks() < numberOfLoops)) {
			try {
				Thread.sleep(10);
			} catch (InterruptedException e) {
                //Expected
			}
		}
		assertTrue("Returned set corrupted by multiple thread access",
				normalSynchChecker.getResult()
						&& offsetSynchChecker.getResult());
		try {
			normalThread.join(5000);
			offsetThread.join(5000);
		} catch (InterruptedException e) {
			fail("join() interrupted");
		}
	}
	*/
/**
	 * @tests java.util.Collections#unmodifiableCollection(java.util.Collection)
	 */
public void test_unmodifiableCollectionLjava_util_Collection() {
    // Test for method java.util.Collection
    // java.util.Collections.unmodifiableCollection(java.util.Collection)
    boolean exception = false;
    Collection c = Collections.unmodifiableCollection(ll);
    assertTrue("Returned collection is of incorrect size", c.size() == ll.size());
    Iterator i = ll.iterator();
    while (i.hasNext()) assertTrue("Returned list missing elements", c.contains(i.next()));
    try {
        c.add(new Object());
    } catch (UnsupportedOperationException e) {
        exception = true;
    // Correct
    }
    if (!exception) {
        fail("Allowed modification of collection");
    }
    try {
        c.remove(new Object());
        fail("Allowed modification of collection");
    } catch (UnsupportedOperationException e) {
    // Correct
    }
    Collection myCollection = new ArrayList();
    myCollection.add(new Integer(20));
    myCollection.add(null);
    c = Collections.unmodifiableCollection(myCollection);
    assertTrue("Collection should contain null", c.contains(null));
    assertTrue("Collection should contain Integer(20)", c.contains(new Integer(20)));
    myCollection = new ArrayList();
    for (int counter = 0; counter < 100; counter++) {
        myCollection.add(objArray[counter]);
    }
    new Support_UnmodifiableCollectionTest("", Collections.unmodifiableCollection(myCollection)).runTest();
}
Also used : Support_UnmodifiableCollectionTest(tests.support.Support_UnmodifiableCollectionTest) ListIterator(java.util.ListIterator) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) Collection(java.util.Collection)

Example 10 with Support_UnmodifiableCollectionTest

use of tests.support.Support_UnmodifiableCollectionTest in project j2objc by google.

the class CollectionsTest method test_unmodifiableListLjava_util_List.

/**
	 * @tests java.util.Collections#unmodifiableList(java.util.List)
	 */
public void test_unmodifiableListLjava_util_List() {
    // Test for method java.util.List
    // java.util.Collections.unmodifiableList(java.util.List)
    // test with a Sequential Access List
    boolean exception = false;
    List c = Collections.unmodifiableList(ll);
    // Ensure a NPE is thrown if the list is NULL
    try {
        Collections.unmodifiableList(null);
        fail("Expected NullPointerException for null list parameter");
    } catch (NullPointerException e) {
    }
    assertTrue("Returned list is of incorrect size", c.size() == ll.size());
    assertTrue("Returned List should not implement Random Access interface", !(c instanceof RandomAccess));
    Iterator i = ll.iterator();
    while (i.hasNext()) assertTrue("Returned list missing elements", c.contains(i.next()));
    try {
        c.add(new Object());
    } catch (UnsupportedOperationException e) {
        exception = true;
    // Correct
    }
    if (!exception) {
        fail("Allowed modification of list");
    }
    try {
        c.remove(new Object());
        fail("Allowed modification of list");
    } catch (UnsupportedOperationException e) {
    // Correct
    }
    // test with a Random Access List
    List smallList = new ArrayList();
    smallList.add(null);
    smallList.add("yoink");
    c = Collections.unmodifiableList(smallList);
    assertNull("First element should be null", c.get(0));
    assertTrue("List should contain null", c.contains(null));
    assertTrue("T1. Returned List should implement Random Access interface", c instanceof RandomAccess);
    smallList = new ArrayList();
    for (int counter = 0; counter < 100; counter++) {
        smallList.add(objArray[counter]);
    }
    List myList = Collections.unmodifiableList(smallList);
    assertTrue("List should not contain null", !myList.contains(null));
    assertTrue("T2. Returned List should implement Random Access interface", myList instanceof RandomAccess);
    assertTrue("get failed on unmodifiable list", myList.get(50).equals(new Integer(50)));
    ListIterator listIterator = myList.listIterator();
    for (int counter = 0; listIterator.hasNext(); counter++) {
        assertTrue("List has wrong elements", ((Integer) listIterator.next()).intValue() == counter);
    }
    new Support_UnmodifiableCollectionTest("", smallList).runTest();
}
Also used : Support_UnmodifiableCollectionTest(tests.support.Support_UnmodifiableCollectionTest) ListIterator(java.util.ListIterator) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) RandomAccess(java.util.RandomAccess) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) ListIterator(java.util.ListIterator)

Aggregations

Support_UnmodifiableCollectionTest (tests.support.Support_UnmodifiableCollectionTest)15 Collection (java.util.Collection)8 Iterator (java.util.Iterator)6 ListIterator (java.util.ListIterator)6 ArrayList (java.util.ArrayList)4 LinkedList (java.util.LinkedList)4 List (java.util.List)4 Enumeration (java.util.Enumeration)2 HashSet (java.util.HashSet)2 Hashtable (java.util.Hashtable)2 LinkedHashMap (java.util.LinkedHashMap)2 RandomAccess (java.util.RandomAccess)2 Set (java.util.Set)2 SortedSet (java.util.SortedSet)2 TreeSet (java.util.TreeSet)2 HashMap (java.util.HashMap)1 TreeMap (java.util.TreeMap)1