Package sis.colck

Source Code of sis.colck.ClockTest

package sis.colck;

import static org.junit.Assert.*;

import java.util.*;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

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

import sis.clock.Clock;
import sis.clock.ClockListener;

/**
* @author Carl Adler(C.A.)
* */
public class ClockTest {
 
  private Clock clock;
  private Lock lock;
  private Condition receivedEnoughTics;
 
  @Before
  public void setUp() {
    lock = new ReentrantLock();
    receivedEnoughTics = lock.newCondition();
  }
 
  @Ignore
  public void testClock() throws Exception {
    final int seconds = 2;
        final List<Date> tics = new ArrayList<Date>();
        ClockListener listener = createClockListener(tics, seconds);
        clock = new Clock(listener);
        lock.lock();
        try {
           receivedEnoughTics.await();
        }
        finally {
           lock.unlock();
        }
        clock.stop();
        verify(tics, seconds);
  }
 
  private ClockListener createClockListener(
           final List<Date> tics, final int seconds) {
        return new ClockListener() {
           private int count = 0;
           public void update(Date date) {
              tics.add(date);
              if (++count == seconds) {
                 lock.lock();
                 try {
                    receivedEnoughTics.signalAll();
                 }
                 finally {
                    lock.unlock();
                 }
              }
           }
        };
     }
 
  private void verify(List<Date> tics, int seconds) {
        assertEquals(seconds, tics.size());
        for (int i = 1; i < seconds; i++)
           assertEquals(1, getSecondsFromLast(tics, i));
     }

     private long getSecondsFromLast(List<Date> tics, int i) {
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(tics.get(i));
        int now = calendar.get(Calendar.SECOND);
        calendar.setTime(tics.get(i - 1));
        int then = calendar.get(Calendar.SECOND);
        if (now == 0)
           now = 60;
        return now - then;
     }

}
TOP

Related Classes of sis.colck.ClockTest

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.