Example usage for org.apache.commons.collections Buffer addAll

List of usage examples for org.apache.commons.collections Buffer addAll

Introduction

In this page you can find the example usage for org.apache.commons.collections Buffer addAll.

Prototype

boolean addAll(Collection<? extends E> c);

Source Link

Document

Adds all of the elements in the specified collection to this collection (optional operation).

Usage

From source file:edu.illinois.enforcemop.examples.apache.collections.TestBoundedBuffer.java

@Test
//  @Schedule(name = "default", sequence = "[beforeAdd:afterAdd]@main->beforeRemove@removeThread")
public void testAddAllToFullBufferRemoveViaIterator() {
    final Buffer bounded = BoundedBuffer.decorate(new UnboundedFifoBuffer(), 2, 500);
    bounded.add("Hello");
    bounded.add("World");
    new NonDelayedIteratorRemove(bounded, 100, 2, "removeThread").start();
    /* @Event("beforeAdd")*/
    bounded.addAll(Arrays.asList(new String[] { "Foo", "Bar" }));
    /* @Event("afterAdd")*/
    assertEquals(2, bounded.size());/*from ww w  .j  av  a  2s .  c  om*/
    assertEquals("Foo", bounded.remove());
    assertEquals("Bar", bounded.remove());
}

From source file:edu.illinois.imunit.examples.apache.collections.TestBlockingBuffer.java

/**
 * Tests {@link BlockingBuffer#get()} in combination with {@link BlockingBuffer#addAll(java.util.Collection)} using
 * multiple read threads./*w  w  w  . j av a  2  s . com*/
 * <p/>
 * Two read threads should block on an empty buffer until a singleton is added then both threads should complete.
 */
@Test
@Schedules({
        @Schedule(name = "BlockedGetWithAddAll", value = "[beforeGet:afterGet]@readThread1->beforeAddAll@main,"
                + "[beforeGet:afterGet]@readThread2->beforeAddAll@main,"
                + "afterGet@readThread1->afterAddAll@main," + "afterGet@readThread2->afterAddAll@main") })
public void testBlockedGetWithAddAll() throws InterruptedException {
    Buffer blockingBuffer = BlockingBuffer.decorate(new MyBuffer());
    Object obj = new Object();

    // run methods will get and compare -- must wait for addAll
    Thread thread1 = new ReadThread(blockingBuffer, obj, "BlockedGetWithAddAll", "readThread1");
    Thread thread2 = new ReadThread(blockingBuffer, obj, "BlockedGetWithAddAll", "readThread2");
    thread1.start();
    thread2.start();

    // give hungry read threads ample time to hang
    try {
        // Thread.sleep(100);
    } catch (Exception e) {
        e.printStackTrace();
    }

    // notifyAll should allow both read threads to complete
    fireEvent("beforeAddAll");
    blockingBuffer.addAll(Collections.singleton(obj));

    // allow notified threads to complete 
    // Thread.sleep(100);
    fireEvent("afterAddAll");

    // There should not be any threads waiting.
    //assertFalse("BlockedGetWithAddAll", thread1.isAlive() || thread2.isAlive());
    //    if( thread1.isAlive() || thread2.isAlive() ) {
    //      fail( "Live thread(s) when both should be dead." );
    //    }
}

From source file:edu.illinois.imunit.examples.apache.collections.TestBlockingBuffer.java

/**@TO-DO Can't specify thread is ended, and or relationship between schedules*/
@Test//from  ww w  .  ja  va2s .co m
@Schedules({
        @Schedule(name = "BlockedRemoveWithAddAll1", value = "[beforeRemove: afterRemove]@readThread1->beforeFirstAddAll@main,"
                + "[beforeRemove:afterRemove]@readThread2->beforeFirstAddAll@main") })
public void testBlockedRemoveWithAddAll1() throws InterruptedException {
    Buffer blockingBuffer = BlockingBuffer.decorate(new MyBuffer());
    Object obj = new Object();

    // run methods will remove and compare -- must wait for addAll
    Thread thread1 = new ReadThread(blockingBuffer, obj, null, "remove", "BlockedRemoveWithAddAll1",
            "readThread1");
    Thread thread2 = new ReadThread(blockingBuffer, obj, null, "remove", "BlockedRemoveWithAddAll1",
            "readThread2");
    thread1.start();
    thread2.start();

    // give hungry read threads ample time to hang
    try {
        // Thread.sleep(100);
    } catch (Exception e) {
        e.printStackTrace();
    }

    fireEvent("beforeFirstAddAll");
    blockingBuffer.addAll(Collections.singleton(obj));

    // allow notified threads to complete 
    try {
        // Thread.sleep(100);
    } catch (Exception e) {
        e.printStackTrace();
    }
    // There should be one thread waiting.
    //assertTrue( "BlockedRemoveWithAddAll1", thread1.isAlive() ^ thread2.isAlive() );
    fireEvent("beforeSecondAddAll");
    blockingBuffer.addAll(Collections.singleton(obj));

    // allow notified thread to complete 
    try {
        // Thread.sleep(100);
    } catch (Exception e) {
        e.printStackTrace();
    }
    // There should not be any threads waiting.
    thread1.join();
    thread2.join();
    //assertFalse( "BlockedRemoveWithAddAll1", thread1.isAlive() || thread2.isAlive() );
    //if( thread1.isAlive() || thread2.isAlive() ) {
    //fail( "Live thread(s) when both should be dead." );
    //}
}

From source file:edu.illinois.enforcemop.examples.apache.collections.TestBlockingBuffer.java

/**
 * Tests {@link BlockingBuffer#get()} in combination with {@link BlockingBuffer#addAll(java.util.Collection)} using
 * multiple read threads./*from   www . j a  v a  2 s. c  o  m*/
 * <p/>
 * Two read threads should block on an empty buffer until a singleton is added then both threads should complete.
 */
@Test
// @Schedules({
//   @Schedule(name = "BlockedGetWithAddAll", sequence = "[beforeGet:afterGet]@readThread1->beforeAddAll@main," +
//       "[beforeGet:afterGet]@readThread2->beforeAddAll@main," + 
//       "afterGet@readThread1->afterAddAll@main," +"afterGet@readThread2->afterAddAll@main") })
public void testBlockedGetWithAddAll() throws InterruptedException {
    Buffer blockingBuffer = BlockingBuffer.decorate(new MyBuffer());
    Object obj = new Object();

    // run methods will get and compare -- must wait for addAll
    Thread thread1 = new ReadThread(blockingBuffer, obj, "BlockedGetWithAddAll", "readThread1");
    Thread thread2 = new ReadThread(blockingBuffer, obj, "BlockedGetWithAddAll", "readThread2");
    thread1.start();
    thread2.start();

    // give hungry read threads ample time to hang
    try {
        Thread.sleep(100);
    } catch (Exception e) {
        e.printStackTrace();
    }

    // notifyAll should allow both read threads to complete
    /* @Event("beforeAddAll")*/
    blockingBuffer.addAll(Collections.singleton(obj));

    // allow notified threads to complete 
    Thread.sleep(100);
    /* @Event("afterAddAll")*/

    // There should not be any threads waiting.
    //assertFalse("BlockedGetWithAddAll", thread1.isAlive() || thread2.isAlive());
    //    if( thread1.isAlive() || thread2.isAlive() ) {
    //      fail( "Live thread(s) when both should be dead." );
    //    }
}

From source file:edu.illinois.enforcemop.examples.apache.collections.TestBlockingBuffer.java

/**@TO-DO Can't specify thread is ended, and or relationship between schedules*/
@Test//  w w  w .  j  av a2  s.c  o  m
// @Schedules({
//   @Schedule(name = "BlockedRemoveWithAddAll1", sequence = "[beforeRemove: afterRemove]@readThread1->beforeFirstAddAll@main," + 
//       "[beforeRemove:afterRemove]@readThread2->beforeFirstAddAll@main") })
public void testBlockedRemoveWithAddAll1() throws InterruptedException {
    Buffer blockingBuffer = BlockingBuffer.decorate(new MyBuffer());
    Object obj = new Object();

    // run methods will remove and compare -- must wait for addAll
    Thread thread1 = new ReadThread(blockingBuffer, obj, null, "remove", "BlockedRemoveWithAddAll1",
            "readThread1");
    Thread thread2 = new ReadThread(blockingBuffer, obj, null, "remove", "BlockedRemoveWithAddAll1",
            "readThread2");
    thread1.start();
    thread2.start();

    // give hungry read threads ample time to hang
    try {
        Thread.sleep(100);
    } catch (Exception e) {
        e.printStackTrace();
    }

    /* @Event("beforeFirstAddAll")*/
    blockingBuffer.addAll(Collections.singleton(obj));

    // allow notified threads to complete 
    try {
        Thread.sleep(100);
    } catch (Exception e) {
        e.printStackTrace();
    }
    // There should be one thread waiting.
    //assertTrue( "BlockedRemoveWithAddAll1", thread1.isAlive() ^ thread2.isAlive() );
    /* @Event("beforeSecondAddAll")*/
    blockingBuffer.addAll(Collections.singleton(obj));

    // allow notified thread to complete 
    try {
        Thread.sleep(100);
    } catch (Exception e) {
        e.printStackTrace();
    }
    // There should not be any threads waiting.
    thread1.join();
    thread2.join();
    //assertFalse( "BlockedRemoveWithAddAll1", thread1.isAlive() || thread2.isAlive() );
    //if( thread1.isAlive() || thread2.isAlive() ) {
    //fail( "Live thread(s) when both should be dead." );
    //}
}

From source file:edu.illinois.imunit.examples.apache.collections.TestBlockingBuffer.java

/**
 * Tests {@link BlockingBuffer#remove()} in combination with {@link BlockingBuffer#addAll(java.util.Collection)}
 * using multiple read threads./*from  w  w w . j  a va 2  s.c  om*/
 * <p/>
 * Two read threads should block on an empty buffer until a collection with two distinct objects is added then both
 * threads should complete. Each thread should have read a different object.
 * @throws InterruptedException 
 */
@Test
@Schedules({
        @Schedule(name = "BlockedRemoveWithAddAll2", value = "[beforeNullRemove: afterNullRemove]@readThread1->beforeAddAll@main,"
                + "[beforeNullRemove: afterNullRemove]@readThread2->beforeAddAll@main,"
                + "afterNullRemove@readThread1->afterAddAll@main,"
                + "afterNullRemove@readThread2->afterAddAll@main") })
public void testBlockedRemoveWithAddAll2() throws InterruptedException {
    Buffer blockingBuffer = BlockingBuffer.decorate(new MyBuffer());
    Object obj1 = new Object();
    Object obj2 = new Object();
    Set objs = Collections.synchronizedSet(new HashSet());
    objs.add(obj1);
    objs.add(obj2);

    // run methods will remove and compare -- must wait for addAll
    Thread thread1 = new ReadThread(blockingBuffer, objs, "remove", "BlockedRemoveWithAddAll2", "readThread1");
    Thread thread2 = new ReadThread(blockingBuffer, objs, "remove", "BlockedRemoveWithAddAll2", "readThread2");
    thread1.start();
    thread2.start();

    // give hungry read threads ample time to hang
    try {
        // Thread.sleep(100);
    } catch (Exception e) {
        e.printStackTrace();
    }

    fireEvent("beforeAddAll");
    blockingBuffer.addAll(objs);
    // allow notified threads to complete 
    try {
        // Thread.sleep(100);
    } catch (Exception e) {
        e.printStackTrace();
    }
    fireEvent("afterAddAll");
    assertEquals("BlockedRemoveWithAddAll2", 0, objs.size());

    // There should not be any threads waiting.
    thread1.join();
    thread2.join();
    //assertFalse( "BlockedRemoveWithAddAll1", thread1.isAlive() || thread2.isAlive() );
    //if( thread1.isAlive() || thread2.isAlive() ) {
    //fail( "Live thread(s) when both should be dead." );
    //}
}

From source file:edu.illinois.enforcemop.examples.apache.collections.TestBlockingBuffer.java

/**
 * Tests {@link BlockingBuffer#remove()} in combination with {@link BlockingBuffer#addAll(java.util.Collection)}
 * using multiple read threads.//from w  ww  .j ava  2s . c om
 * <p/>
 * Two read threads should block on an empty buffer until a collection with two distinct objects is added then both
 * threads should complete. Each thread should have read a different object.
 * @throws InterruptedException 
 */
@Test
// @Schedules({
//   @Schedule(name = "BlockedRemoveWithAddAll2", sequence = "[beforeNullRemove: afterNullRemove]@readThread1->beforeAddAll@main," + 
//       "[beforeNullRemove: afterNullRemove]@readThread2->beforeAddAll@main," + 
//       "afterNullRemove@readThread1->afterAddAll@main," + "afterNullRemove@readThread2->afterAddAll@main") })
public void testBlockedRemoveWithAddAll2() throws InterruptedException {
    Buffer blockingBuffer = BlockingBuffer.decorate(new MyBuffer());
    Object obj1 = new Object();
    Object obj2 = new Object();
    Set objs = Collections.synchronizedSet(new HashSet());
    objs.add(obj1);
    objs.add(obj2);

    // run methods will remove and compare -- must wait for addAll
    Thread thread1 = new ReadThread(blockingBuffer, objs, "remove", "BlockedRemoveWithAddAll2", "readThread1");
    Thread thread2 = new ReadThread(blockingBuffer, objs, "remove", "BlockedRemoveWithAddAll2", "readThread2");
    thread1.start();
    thread2.start();

    // give hungry read threads ample time to hang
    try {
        Thread.sleep(100);
    } catch (Exception e) {
        e.printStackTrace();
    }

    /* @Event("beforeAddAll")*/
    blockingBuffer.addAll(objs);
    // allow notified threads to complete 
    try {
        Thread.sleep(100);
    } catch (Exception e) {
        e.printStackTrace();
    }
    /* @Event("afterAddAll")*/
    assertEquals("BlockedRemoveWithAddAll2", 0, objs.size());

    // There should not be any threads waiting.
    thread1.join();
    thread2.join();
    //assertFalse( "BlockedRemoveWithAddAll1", thread1.isAlive() || thread2.isAlive() );
    //if( thread1.isAlive() || thread2.isAlive() ) {
    //fail( "Live thread(s) when both should be dead." );
    //}
}