Android Open Source - coursera-android-pos Synchronized Queue Test






From Project

Back to project page coursera-android-pos.

License

The source code is released under:

MIT License

If you think the Android project coursera-android-pos listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package edu.vuum.mooca;
import static org.junit.Assert.*;
/*  w ww  .  j a v  a  2 s .  co m*/
import java.util.concurrent.ArrayBlockingQueue;

import org.junit.Before;
import org.junit.Test;

import edu.vuum.mooca.SynchronizedQueue.*;

/**
 * @class SynchronizedQueueTest
 *
 * @brief This class tests queues for proper functionality by 
 *     using the testQueue method defined in SynchronizedQueue.java
 */
public class SynchronizedQueueTest {
    /**
     * Indicates how big the queue should be.
     */
    int mQueueSize;
  
    /**
     * Run the test for the queue parameter.
     *
     * @return result. If SynchronizedQueue test ran properly, returns
     * null. If not, returns error message.
     */
    static SynchronizedQueueResult runQueueTest(String qName,
                                                QueueAdapter<Integer> queue) {
      if (SynchronizedQueue.diagnosticsEnabled) {      
            System.out.println("Starting " 
                               + qName 
                               + " test...");
            if (qName == "BuggyBlockingQueue") 
                System.out.println("An exception may be thrown since "
                                   + qName
                                   + " is intentially BUGGY.");
      }    

      /**
         * We have to instantiate this object because Java doesn't
       * like things being abstract AND static, which makes
       * implementing the Template Pattern statically more painful
       * than it should be.
         */
        SynchronizedQueueResult result =
            new SynchronizedQueueImpl().testQueue(queue, qName);

        if (SynchronizedQueue.diagnosticsEnabled) {
            System.out.println("End " + qName + " test.\n");
            System.out.println("See JUnit view for results -- \n" 
                               + "Green check-marks denote program correctness. \n" 
                               + "Blue x-marks indicate a problem with your implementation. \n");
        }
        
        if (result != SynchronizedQueueResult.RAN_PROPERLY)
            return result;
        else
            return null;
    }
  
    /**
     * Runs before each test. Sets mQueueSize.
     * @throws Exception
     */
    @Before
        public void setUp() throws Exception {
        // Indicate how big the queue should be, which should be
        // smaller than the number of iterations to induce blocking
        // behavior.
        mQueueSize = SynchronizedQueue.mMaxIterations / 10;
    }

    /**
     * Tests the ArrayBlockingQueue, which should pass without error.
     */
    @Test
    public void arrayBlockingQueueTest() {
        // Make the appropriate QueueAdapter for the
        // ArrayBlockingQueue.
        QueueAdapter<Integer> queueAdapter =
            new QueueAdapter<Integer>(new ArrayBlockingQueue<Integer>(mQueueSize));

        // Run a test on the ArrayBlockingQueue.
        SynchronizedQueueResult errors =
            runQueueTest("ArrayBlockingQueue", queueAdapter);

        String errorMessage = "";
        
        if (errors != null) 
            errorMessage = errors.getString();

        assertNull("Error occurred: " + 
                   errorMessage,
                   errors);
    }
  
    /**
     * Tests the BuggyBlockingQueue, an intentionally flawed class.
     * The buggyBlockingQueueTest() will succeed if the testQueue
     * method fails, i.e., this test succeeds if our queue causes
     * errors (which is what we expect)!
     */
    @Test
    public void buggyBlockingQueueTest() {
        // Make the appropriate QueueAdapter for the
        // BuggyBlockingQueue.

        QueueAdapter<Integer> queueAdapter =
            new QueueAdapter<Integer>(new BuggyBlockingQueue<Integer>(mQueueSize));

        // Run a test on the BuggyBlockingQueue.
        SynchronizedQueueResult errors =
            runQueueTest("BuggyBlockingQueue", queueAdapter);
        
        assertNotNull("Test should not complete without errors. " +
                      "BuggyBlockingQueue is intended to function incorrectly.",
                      errors);
    }
}




Java Source Code List

.BuggyLongTest.java
.Main.java
.PingPongWrong.java
.SimpleAtomicLongTest.java
.SimpleAtomicLong.java
edu.vuum.mocca.AndroidPlatformStrategy.java
edu.vuum.mocca.AndroidPlatformStrategy.java
edu.vuum.mocca.AndroidPlatformStrategy.java
edu.vuum.mocca.BuggyLongTest.java
edu.vuum.mocca.BuggyLongTest.java
edu.vuum.mocca.ConsolePlatformStrategy.java
edu.vuum.mocca.ConsolePlatformStrategy.java
edu.vuum.mocca.ConsolePlatformStrategy.java
edu.vuum.mocca.DownloadActivity.java
edu.vuum.mocca.DownloadActivity.java
edu.vuum.mocca.DownloadBase.java
edu.vuum.mocca.DownloadIntentService.java
edu.vuum.mocca.DownloadService.java
edu.vuum.mocca.DownloadUtils.java
edu.vuum.mocca.Main.java
edu.vuum.mocca.Main.java
edu.vuum.mocca.Main.java
edu.vuum.mocca.Options.java
edu.vuum.mocca.Options.java
edu.vuum.mocca.Options.java
edu.vuum.mocca.PingPongActivity.java
edu.vuum.mocca.PingPongActivity.java
edu.vuum.mocca.PingPongRight.java
edu.vuum.mocca.PingPongRight.java
edu.vuum.mocca.PingPongRight.java
edu.vuum.mocca.PlatformStrategyFactory.java
edu.vuum.mocca.PlatformStrategyFactory.java
edu.vuum.mocca.PlatformStrategyFactory.java
edu.vuum.mocca.PlatformStrategy.java
edu.vuum.mocca.PlatformStrategy.java
edu.vuum.mocca.PlatformStrategy.java
edu.vuum.mocca.PlayPingPong.java
edu.vuum.mocca.PlayPingPong.java
edu.vuum.mocca.PlayPingPong.java
edu.vuum.mocca.SimpleAtomicLongMultithreadedTest.java
edu.vuum.mocca.SimpleAtomicLongMultithreadedTest.java
edu.vuum.mocca.SimpleAtomicLongMultithreadedTest.java
edu.vuum.mocca.SimpleAtomicLongSingleThreadedTest.java
edu.vuum.mocca.SimpleAtomicLongSingleThreadedTest.java
edu.vuum.mocca.SimpleAtomicLongSingleThreadedTest.java
edu.vuum.mocca.SimpleAtomicLongTests.java
edu.vuum.mocca.SimpleAtomicLongTests.java
edu.vuum.mocca.SimpleAtomicLongTests.java
edu.vuum.mocca.SimpleAtomicLong.java
edu.vuum.mocca.SimpleAtomicLong.java
edu.vuum.mocca.SimpleAtomicLong.java
edu.vuum.mocca.SimpleAtomicLong.java
edu.vuum.mocca.SimpleAtomicLong.java
edu.vuum.mocca.SimpleSemaphore.java
edu.vuum.mocca.SimpleSemaphore.java
edu.vuum.mocca.SimpleSemaphore.java
edu.vuum.mocca.SimpleSemaphore.java
edu.vuum.mocca.SimpleSemaphore.java
edu.vuum.mocca.SimpleSemaphore.java
edu.vuum.mocca.ThreadPoolDownloadService.java
edu.vuum.mocca.ThreadedDownloads.java
edu.vuum.mocca.provider.MoocSchema.java
edu.vuum.mocca.storage.MoocResolver.java
edu.vuum.mocca.storage.StorageUtilities.java
edu.vuum.mocca.storage.StoryCreator.java
edu.vuum.mocca.storage.StoryData.java
edu.vuum.mocca.ui.CreateStoryActivity.java
edu.vuum.mocca.ui.EditStoryActivity.java
edu.vuum.mocca.ui.ListStoryActivity.java
edu.vuum.mocca.ui.LoginActivity.java
edu.vuum.mocca.ui.SoundRecordActivity.java
edu.vuum.mocca.ui.StoryActivityBase.java
edu.vuum.mocca.ui.StoryDataArrayAdaptor.java
edu.vuum.mocca.ui.ViewStoryActivity.java
edu.vuum.mooca.BuggyBlockingQueue.java
edu.vuum.mooca.BuggyBlockingQueue.java
edu.vuum.mooca.BuggyBlockingQueue.java
edu.vuum.mooca.SynchronizedQueueImpl.java
edu.vuum.mooca.SynchronizedQueueImpl.java
edu.vuum.mooca.SynchronizedQueueTest.java
edu.vuum.mooca.SynchronizedQueueTest.java
edu.vuum.mooca.SynchronizedQueueTest.java
edu.vuum.mooca.SynchronizedQueueTest.java
edu.vuum.mooca.SynchronizedQueueTest.java
edu.vuum.mooca.SynchronizedQueue.java
edu.vuum.mooca.SynchronizedQueue.java
edu.vuum.mooca.SynchronizedQueue.java
edu.vuum.mooca.SynchronizedQueue.java
edu.vuum.mooca.SynchronizedQueue.java.java