congrats Icon
New! Announcing Tabnine Chat Beta
Learn More
Tabnine Logo
AbstractTestCollection
Code IndexAdd Tabnine to your IDE (free)

How to use
AbstractTestCollection
in
org.apache.commons.collections.collection

Best Java code snippets using org.apache.commons.collections.collection.AbstractTestCollection (Showing top 20 results out of 315)

origin: commons-collections/commons-collections

/**
 *  Tests {@link Collection#size()}.
 */
public void testCollectionSize() {
  resetEmpty();
  assertEquals("Size of new Collection is 0.", 0, collection.size());
  resetFull();
  assertTrue("Size of full collection should be greater than zero", 
        collection.size() > 0);
}
origin: commons-collections/commons-collections

resetEmpty();
elements = getFullElements();
for(int i = 0; i < elements.length; i++) {
  assertTrue("Empty collection shouldn't contain element[" + i + "]",
        !collection.contains(elements[i]));
verify();
elements = getOtherElements();
for(int i = 0; i < elements.length; i++) {
  assertTrue("Empty collection shouldn't contain element[" + i + "]",
        !collection.contains(elements[i]));
verify();
resetFull();
elements = getFullElements();
for(int i = 0; i < elements.length; i++) {
  assertTrue("Full collection should contain element[" + i + "]", 
        collection.contains(elements[i]));
verify();
resetFull();
elements = getOtherElements();
for(int i = 0; i < elements.length; i++) {
  assertTrue("Full collection shouldn't contain element", 
        !collection.contains(elements[i]));
origin: commons-collections/commons-collections

/**
 *  Returns an array of elements that are <I>not</I> contained in a
 *  full collection.  Every element in the returned array must 
 *  not exist in a collection returned by {@link #makeFullCollection()}.
 *  The default implementation returns a heterogenous array of elements
 *  without null.  Note that some of the tests add these elements
 *  to an empty or full collection, so if your collection restricts
 *  certain kinds of elements, you should override this method.
 */
public Object[] getOtherElements() {
  return getOtherNonNullElements();
}

origin: commons-collections/commons-collections

/**
 *  Tests <code>toString</code> on a collection.
 */
public void testCollectionToString() {
  resetEmpty();
  assertTrue("toString shouldn't return null", 
        collection.toString() != null);
  resetFull();
  assertTrue("toString shouldn't return null", 
        collection.toString() != null);
}
origin: commons-collections/commons-collections

/**
 *  Returns a full collection to be used for testing.  The collection
 *  returned by this method should contain every element returned by
 *  {@link #getFullElements()}.  The default implementation, in fact,
 *  simply invokes <code>addAll</code> on an empty collection with
 *  the results of {@link #getFullElements()}.  Override this default
 *  if your collection doesn't support addAll.
 */
public Collection makeFullCollection() {
  Collection c = makeCollection();
  c.addAll(Arrays.asList(getFullElements()));
  return c;
}
origin: commons-collections/commons-collections

if (!isRemoveSupported()) return;
resetEmpty();
try {
  collection.iterator().remove();
  fail("New iterator.remove should raise IllegalState");
} catch (IllegalStateException e) {
verify();
  iter.hasNext();
  iter.remove();
  fail("New iterator.remove should raise IllegalState " +
     "even after hasNext");
} catch (IllegalStateException e) {
verify();
resetFull();
int size = collection.size();
Iterator iter = collection.iterator();
    o = cloneMapEntry((Map.Entry) o);
  if(!areEqualElementsDistinguishable()) {
    confirmed.remove(o);
    verify();
  assertEquals("Collection should shrink by one after " +
origin: commons-collections/commons-collections

if (!isAddSupported()) return;
resetEmpty();
Object[] elements = getFullElements();
boolean r = collection.addAll(Arrays.asList(elements));
confirmed.addAll(Arrays.asList(elements));
verify();
assertTrue("Empty collection should change after addAll", r);
for (int i = 0; i < elements.length; i++) {
  assertTrue("Collection should contain added element",
        collection.contains(elements[i]));
resetFull();
int size = collection.size();
elements = getOtherElements();
r = collection.addAll(Arrays.asList(elements));
confirmed.addAll(Arrays.asList(elements));
verify();
assertTrue("Full collection should change after addAll", r);
for (int i = 0; i < elements.length; i++) {
  assertTrue("Full collection should contain added element",
        collection.contains(elements[i]));
assertEquals("Size should increase after addAll", 
       size + elements.length, collection.size());
resetFull();
size = collection.size();
r = collection.addAll(Arrays.asList(getFullElements()));
origin: commons-collections/commons-collections

if (!isFailFastSupported()) return;
if (isAddSupported()) {
  resetFull();
  try {
    Iterator iter = collection.iterator();
    Object o = getOtherElements()[0];
    collection.add(o);
    confirmed.add(o);
    iter.next();
    fail("next after add should raise ConcurrentModification");
  } catch (ConcurrentModificationException e) {
  verify();
  resetFull();
  try {
    Iterator iter = collection.iterator();
    collection.addAll(Arrays.asList(getOtherElements()));
    confirmed.addAll(Arrays.asList(getOtherElements()));
    iter.next();
    fail("next after addAll should raise ConcurrentModification");
  } catch (ConcurrentModificationException e) {
  verify();
if (!isRemoveSupported()) return;
resetFull();
origin: commons-collections/commons-collections

if (isAddSupported()) return;
resetEmpty();
try {
  collection.add(new Object());
  fail("Emtpy collection should not support add.");
} catch (UnsupportedOperationException e) {
verify();
  collection.addAll(Arrays.asList(getFullElements()));
  fail("Emtpy collection should not support addAll.");
} catch (UnsupportedOperationException e) {
verify();
resetFull();
try {
  collection.add(new Object());
  fail("Full collection should not support add.");
} catch (UnsupportedOperationException e) {
verify();
  collection.addAll(Arrays.asList(getOtherElements()));
  fail("Full collection should not support addAll.");
} catch (UnsupportedOperationException e) {
verify();
origin: commons-collections/commons-collections

/**
 *  Tests {@link Collection#add(Object)}.
 */
public void testCollectionAdd() {
  if (!isAddSupported()) return;
  
  Object[] elements = getFullElements();
  for (int i = 0; i < elements.length; i++) {
    resetEmpty();
    boolean r = collection.add(elements[i]);
    confirmed.add(elements[i]);
    verify();
    assertTrue("Empty collection changed after add", r);
    assertEquals("Collection size is 1 after first add", 1, collection.size());
  }
  
  resetEmpty();
  int size = 0;
  for (int i = 0; i < elements.length; i++) {
    boolean r = collection.add(elements[i]);
    confirmed.add(elements[i]);
    verify();
    if (r) size++;
    assertEquals("Collection size should grow after add", 
           size, collection.size());
    assertTrue("Collection should contain added element",
          collection.contains(elements[i]));
  }
}

origin: commons-collections/commons-collections

if (isRemoveSupported()) return;
resetEmpty();
try {
  collection.clear();
  fail("clear should raise UnsupportedOperationException");
} catch (UnsupportedOperationException e) {
verify();
  fail("remove should raise UnsupportedOperationException");
} catch (UnsupportedOperationException e) {
verify();
  fail("removeAll should raise UnsupportedOperationException");
} catch (UnsupportedOperationException e) {
verify();
  fail("removeAll should raise UnsupportedOperationException");
} catch (UnsupportedOperationException e) {
verify();
resetFull();
try {
  Iterator iterator = collection.iterator();
  iterator.next();
origin: commons-collections/commons-collections

public void testSerializeDeserializeThenCompare() throws Exception {
  Object obj = makeCollection();
  if (obj instanceof Serializable && isTestSerialization()) {
    ByteArrayOutputStream buffer = new ByteArrayOutputStream();
    ObjectOutputStream out = new ObjectOutputStream(buffer);
    out.writeObject(obj);
    out.close();
    ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(buffer.toByteArray()));
    Object dest = in.readObject();
    in.close();
    if (isEqualsCheckable()) {
      assertEquals("obj != deserialize(serialize(obj)) - EMPTY Collection", obj, dest);
    }
  }
  obj = makeFullCollection();
  if (obj instanceof Serializable && isTestSerialization()) {
    ByteArrayOutputStream buffer = new ByteArrayOutputStream();
    ObjectOutputStream out = new ObjectOutputStream(buffer);
    out.writeObject(obj);
    out.close();
    ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(buffer.toByteArray()));
    Object dest = in.readObject();
    in.close();
    if (isEqualsCheckable()) {
      assertEquals("obj != deserialize(serialize(obj)) - FULL Collection", obj, dest);
    }
  }
}

origin: commons-collections/commons-collections

/**
 *  Returns an array of objects that are contained in a collection
 *  produced by {@link #makeFullCollection()}.  Every element in the
 *  returned array <I>must</I> be an element in a full collection.<P>
 *  The default implementation returns a heterogenous array of 
 *  objects with some duplicates. null is added if allowed.
 *  Override if you require specific testing elements.  Note that if you
 *  override {@link #makeFullCollection()}, you <I>must</I> override
 *  this method to reflect the contents of a full collection.
 */
public Object[] getFullElements() {
  if (isNullSupported()) {
    ArrayList list = new ArrayList();
    list.addAll(Arrays.asList(getFullNonNullElements()));
    list.add(4, null);
    return list.toArray();
  } else {
    return (Object[]) getFullNonNullElements().clone();
  }
}
origin: commons-collections/commons-collections

assertEquals("Collection size should match confirmed collection's",
       confirmedSize, collection.size());
assertEquals("Collection isEmpty() result should match confirmed " +
       " collection's", 
       confirmed.isEmpty(), collection.isEmpty());
    fail("Collection should not contain a value that the " +
       "confirmed collection does not have: " + o +
       "\nTest: " + collection + "\nReal: " + confirmed);
  if(!matched[i]) {
    fail("Collection should contain all values that are in the confirmed collection" +
       "\nTest: " + collection + "\nReal: " + confirmed);
origin: commons-collections/commons-collections

if (!isRemoveSupported()) return;
resetEmpty();
Object[] elements = getFullElements();
for (int i = 0; i < elements.length; i++) {
  assertTrue("Shouldn't remove nonexistent element", 
        !collection.remove(elements[i]));
  verify();
Object[] other = getOtherElements();
resetFull();
for (int i = 0; i < other.length; i++) {
  assertTrue("Shouldn't remove nonexistent other element", 
        !collection.remove(other[i]));
  verify();
  resetFull();
  assertTrue("Collection should remove extant element: " + elements[i],
        collection.remove(elements[i]));
  if(!areEqualElementsDistinguishable()) {
    confirmed.remove(elements[i]);
    verify();
  assertEquals("Collection should shrink after remove", 
         size - 1, collection.size());
origin: commons-collections/commons-collections

/**
 *  Tests {@link Collection#isEmpty()}.
 */
public void testCollectionIsEmpty() {
  resetEmpty();
  assertEquals("New Collection should be empty.", 
         true, collection.isEmpty());
  // make sure calls to "isEmpty() don't change anything
  verify();
  resetFull();
  assertEquals("Full collection shouldn't be empty", 
         false, collection.isEmpty());
  // make sure calls to "isEmpty() don't change anything
  verify();
}
origin: commons-collections/commons-collections

resetEmpty();
Collection col = new HashSet();
assertTrue("Every Collection should contain all elements of an " +
      "empty Collection.", collection.containsAll(col));
col.addAll(Arrays.asList(getOtherElements()));
assertTrue("Empty Collection shouldn't contain all elements of " +
      "a non-empty Collection.", !collection.containsAll(col));
verify();
resetFull();
assertTrue("Full collection shouldn't contain other elements", 
      !collection.containsAll(col));
col.addAll(Arrays.asList(getFullElements()));
assertTrue("Full collection should containAll full elements",
      collection.containsAll(col));
verify();
int min = (getFullElements().length < 2 ? 0 : 2);
int max = (getFullElements().length == 1 ? 1 : 
      (getFullElements().length <= 5 ? getFullElements().length - 1 : 5));
col = Arrays.asList(getFullElements()).subList(min, max);
assertTrue("Full collection should containAll partial full " +
      "elements", collection.containsAll(col));
assertTrue("Full collection should containAll itself", 
      collection.containsAll(collection));
origin: commons-collections/commons-collections

resetEmpty();
assertEquals("Empty Collection should return empty array for toArray",
       0, collection.toArray().length);
resetFull();
Object[] array = collection.toArray();
assertEquals("Full collection toArray should be same size as " +
       "collection", array.length, collection.size());
Object[] confirmedArray = confirmed.toArray();
assertEquals("length of array from confirmed collection should " +
       "match the length of the collection's array", 
       confirmedArray.length, array.length);
  assertTrue("Collection should contain element in toArray",
        collection.contains(array[i]));
    fail("element " + i + " in returned array should be found " +
       "in the confirmed collection's array");
  assertEquals("Collection should return all its elements in " +
         "toArray", true, matched[i]);
origin: commons-collections/commons-collections

if (!isRemoveSupported()) return;
resetEmpty();
assertTrue("Emtpy collection removeAll should return false for " +
      "empty input", 
      !collection.removeAll(Collections.EMPTY_SET));
verify();
assertTrue("Emtpy collection removeAll should return false for " +
      "nonempty input", 
      !collection.removeAll(new ArrayList(collection)));
verify();
resetFull();
assertTrue("Full collection removeAll should return false for " + 
      "empty input", 
      !collection.removeAll(Collections.EMPTY_SET));
verify();
assertTrue("Full collection removeAll should return false for other elements", 
      !collection.removeAll(Arrays.asList(getOtherElements())));
verify();
assertTrue("Full collection removeAll should return true for full elements", 
      collection.removeAll(new HashSet(collection)));
confirmed.removeAll(new HashSet(confirmed));
verify();
resetFull();
origin: commons-collections/commons-collections

resetEmpty();
Iterator it1 = collection.iterator();
assertEquals("Iterator for empty Collection shouldn't have next.",
       false, it1.hasNext());
try {
  it1.next();
  fail("Iterator at end of Collection should throw " +
     "NoSuchElementException when next is called.");
} catch(NoSuchElementException e) {
verify();
resetFull();
it1 = collection.iterator();
for (int i = 0; i < collection.size(); i++) {
  assertTrue("Iterator for full collection should haveNext", 
        it1.hasNext());
  it1.next();
assertTrue("Iterator should be finished", !it1.hasNext());
for (int i = 0; i < collection.size(); i++) {
  Object next = it1.next();
  assertTrue("Collection should contain element returned by " +
        "its iterator", collection.contains(next));
  list.add(next);
  fail("iterator.next() should raise NoSuchElementException " +
     "after it finishes");
org.apache.commons.collections.collectionAbstractTestCollection

Javadoc

Abstract test class for java.util.Collection methods and contracts.

You should create a concrete subclass of this class to test any custom Collection implementation. At minimum, you'll have to implement the #makeCollection() method. You might want to override some of the additional public methods as well:

Element Population Methods

Override these if your collection restricts what kind of elements are allowed (for instance, if null is not permitted):

  • #getFullElements()
  • #getOtherElements()

Supported Operation Methods

Override these if your collection doesn't support certain operations:

  • #isAddSupported()
  • #isRemoveSupported()
  • #areEqualElementsDistinguishable()
  • #isNullSupported()
  • #isFailFastSupported()

Fixture Methods

Fixtures are used to verify that the the operation results in correct state for the collection. Basically, the operation is performed against your collection implementation, and an identical operation is performed against a confirmed collection implementation. A confirmed collection implementation is something like java.util.ArrayList, which is known to conform exactly to its collection interface's contract. After the operation takes place on both your collection implementation and the confirmed collection implementation, the two collections are compared to see if their state is identical. The comparison is usually much more involved than a simple equals test. This verification is used to ensure proper modifications are made along with ensuring that the collection does not change when read-only modifications are made.

The #collection field holds an instance of your collection implementation; the #confirmed field holds an instance of the confirmed collection implementation. The #resetEmpty() and #resetFull() methods set these fields to empty or full collections, so that tests can proceed from a known state.

After a modification operation to both #collection and #confirmed, the #verify() method is invoked to compare the results. You may want to override #verify() to perform additional verifications. For instance, when testing the collection views of a map, AbstractTestMap would override #verify() to make sure the map is changed after the collection view is changed.

If you're extending this class directly, you will have to provide implementations for the following:

  • #makeConfirmedCollection()
  • #makeConfirmedFullCollection()

Those methods should provide a confirmed collection implementation that's compatible with your collection implementation.

If you're extending AbstractTestList, AbstractTestSet, or AbstractTestBag, you probably don't have to worry about the above methods, because those three classes already override the methods to provide standard JDK confirmed collections.

Other notes

If your Collection fails one of these tests by design, you may still use this base set of cases. Simply override the test case (method) your Collection fails.

Most used methods

  • areEqualElementsDistinguishable
    Specifies whether equal elements in the collection are, in fact, distinguishable with information no
  • assertEquals
  • assertTrue
  • cloneMapEntry
    Creates a new Map Entry that is independent of the first and the map.
  • fail
  • getFullElements
    Returns an array of objects that are contained in a collection produced by #makeFullCollection(). Ev
  • getFullNonNullElements
    Returns a list of elements suitable for return by #getFullElements(). The array returned by this met
  • getOtherElements
    Returns an array of elements that are not contained in a full collection. Every element in the retur
  • getOtherNonNullElements
    Returns the default list of objects returned by #getOtherElements(). Includes many objects of differ
  • isAddSupported
    Returns true if the collections produced by #makeCollection() and #makeFullCollection()support the a
  • isEqualsCheckable
    Returns true to indicate that the collection supports equals() comparisons. This implementation retu
  • isFailFastSupported
    Returns true to indicate that the collection supports fail fast iterators. The default implementatio
  • isEqualsCheckable,
  • isFailFastSupported,
  • isNullSupported,
  • isRemoveSupported,
  • isTestSerialization,
  • makeCollection,
  • makeConfirmedCollection,
  • makeConfirmedFullCollection,
  • makeFullCollection,
  • resetEmpty

Popular in Java

  • Reactive rest calls using spring rest template
  • getSystemService (Context)
  • setContentView (Activity)
  • onRequestPermissionsResult (Fragment)
  • Table (com.google.common.collect)
    A collection that associates an ordered pair of keys, called a row key and a column key, with a sing
  • Connection (java.sql)
    A connection represents a link from a Java application to a database. All SQL statements and results
  • StringTokenizer (java.util)
    Breaks a string into tokens; new code should probably use String#split.> // Legacy code: StringTo
  • JarFile (java.util.jar)
    JarFile is used to read jar entries and their associated data from jar files.
  • ZipFile (java.util.zip)
    This class provides random read access to a zip file. You pay more to read the zip file's central di
  • Location (org.springframework.beans.factory.parsing)
    Class that models an arbitrary location in a Resource.Typically used to track the location of proble
  • Top Vim plugins
Tabnine Logo
  • Products

    Search for Java codeSearch for JavaScript code
  • IDE Plugins

    IntelliJ IDEAWebStormVisual StudioAndroid StudioEclipseVisual Studio CodePyCharmSublime TextPhpStormVimGoLandRubyMineEmacsJupyter NotebookJupyter LabRiderDataGripAppCode
  • Company

    About UsContact UsCareers
  • Resources

    FAQBlogTabnine AcademyTerms of usePrivacy policyJava Code IndexJavascript Code Index
Get Tabnine for your IDE now